{"_id":"@allnulled/commandir","name":"@allnulled/commandir","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@allnulled/commandir","version":"1.0.0","description":"Commandir pattern allows to scale command pattern with directories or URLs. Nodejs or browser.","main":"commandir.js","scripts":{"up":"bash up.sh","test-browser":"npx http-server -c-1 . -o test/test.html","test":"node test/test.js"},"keywords":["command","pattern","polyfill","cross-env","browser","node.js","directories","urls","module","system"],"author":{"name":"allnulled"},"license":"WTFPL","gitHead":"5ecda922fc40b18bb3e5223079dabd28e81e90ff","_id":"@allnulled/commandir@1.0.0","_nodeVersion":"18.17.1","_npmVersion":"9.6.7","dist":{"integrity":"sha512-Ig+PREZFDoUTcF1tYHJXHNly6RvG97zrQS6MkQoyowdIW1k7M/w9VmytXHgGSCtMifi7ZsqORghBUNxtH1B5aQ==","shasum":"072b64e46b429ca83fd187c8be46d8cb15f64e39","tarball":"https://registry.npmjs.org/@allnulled/commandir/-/commandir-1.0.0.tgz","fileCount":7,"unpackedSize":11073,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCq8jW5dtOzoX68vakJ6z3vzdKB8HDCeEAcGNBoaDGCwQIgWO15u9fbGLOqXQTrNTsL6vFs0p2cxNmH3w8Tm9koTdY="}]},"_npmUser":{"name":"allnulled","email":"todosanulados@gmail.com"},"directories":{},"maintainers":[{"name":"allnulled","email":"todosanulados@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/commandir_1.0.0_1735152923843_0.15860974314186715"},"_hasShrinkwrap":false}},"time":{"created":"2024-12-25T18:55:23.764Z","1.0.0":"2024-12-25T18:55:24.026Z","modified":"2024-12-25T18:55:24.263Z"},"maintainers":[{"name":"allnulled","email":"todosanulados@gmail.com"}],"description":"Commandir pattern allows to scale command pattern with directories or URLs. Nodejs or browser.","keywords":["command","pattern","polyfill","cross-env","browser","node.js","directories","urls","module","system"],"author":{"name":"allnulled"},"license":"WTFPL","readme":"# commandir\n\nCommandir pattern allows to scale command pattern with directories or URLs. Nodejs or browser.\n\n## Installation\n\n```sh\nnpm install -s @allnulled/commandir\n```\n\n## Importation\n\nIn node.js:\n\n```js\nrequire(\"@allnulled/commandir\");\n```\n\nIn html:\n\n```html\n<script src=\"node_modules/@allnulled/commandir/commandir.js\"></script>\n```\n\n## Usage\n\nIn node.js:\n\n```js\nconst commandir = Commandir.nodejs(__dirname + \"/commands\");\n```\n\nIn browser:\n\n```js\nconst commandir = Commandir.browser(\"https://github.com/allnulled/path/to/file/raw\");\n```\n\nThe `browser` mode, which in fact means AJAX mode, is allowed in node.js too as it only uses `fetch`.\n\nThen, in any:\n\n```js\ncommandir.execute(\"path/to/command.js\", {\n    parameter1: 1,\n    parameter2: 2,\n    parameter3: 3\n});\n```\n\nYou can, then, `list()`, `register(name, callback)` and `unregister(name)`:\n\n```js\nconsole.log(commandir.list());\ncommandir.register(\"salute\", () => console.log(\"hi!\"));\ncommandir.execute(\"salute\");\ncommandir.unregister(\"salute\");\n```\n\n## Details\n\n1. The objects `module.exports` and `__dirname` exist in both environments.\n\nThe only difference is that `__dirname` in browsers points to the URL of the script. But `module.exports` and `return` can be used in `browser` mode to return a module from the file.\n\n2. The method `register` receive different parameters\n\nIn browser, `register(name, url)`. In node.js, `register(name, callback)`.\n\n3. The methods `register` and `unregister` work differently\n\nIn browser, they just associate a label with a URL. In node.js, they write a file in the filesystem with the callback provided as second parameter, and remove it on `unregister`.\n\n4. The method `list` cannot work in browser\n\nBecause directories in the web do not list by default, the method `list` is still not provided with a polyfill.\n\n5. Names are fixed and sanitized\n   \nNames have a process of validation and transformation before they get registered. They rewrite the `.js` at the end, and forbid `..` just in case.\n\nFor now, I think it is all.\n\n## Test\n\nThis is the example that the test uses. It is cross-environment but because we discriminated. \n\n```js\nlet basedir = \".\";\n\nconst isNodejs = typeof global !== \"undefined\";\n\nconst main = async function () {\n  if (isNodejs) {\n    require(__dirname + \"/../commandir.js\");\n    basedir = __dirname;\n  }\n\n  if (isNodejs) {\n    const commandir = Commandir.nodejs(basedir + \"/commands\");\n    commandir.execute(\"/hello\", {\n      dest: \"world\"\n    });\n    commandir.register(\"/goodbye\", function (parameters) {\n      console.log(`Goodbye, ${parameters.dest}!`);\n    });\n    commandir.execute(\"/goodbye\", { dest: \"world\" });\n    commandir.unregister(\"/goodbye\");\n    console.log(commandir.list(\"\"));\n  } else {\n    const commandir = Commandir.browser(\"/test/commands\");\n    await commandir.register(\"hello\", \"./commands/hello.js\");\n    await commandir.execute(\"hello\", { dest: \"world\" });\n  }\n};\n\nmain();\n```\n\n","readmeFilename":"README.md"}