{"id":23644428,"url":"https://github.com/allnulled/commandir","last_synced_at":"2025-07-10T18:34:39.091Z","repository":{"id":269741827,"uuid":"908329175","full_name":"allnulled/commandir","owner":"allnulled","description":"Commandir pattern allows to scale command pattern with directories or URLs. Nodejs or browser.","archived":false,"fork":false,"pushed_at":"2024-12-25T18:55:04.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-19T16:14:52.667Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npmjs.com/@allnulled/commandir","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/allnulled.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-25T18:51:29.000Z","updated_at":"2024-12-25T18:58:23.000Z","dependencies_parsed_at":"2024-12-25T22:17:15.707Z","dependency_job_id":null,"html_url":"https://github.com/allnulled/commandir","commit_stats":null,"previous_names":["allnulled/commandir"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/allnulled/commandir","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allnulled%2Fcommandir","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allnulled%2Fcommandir/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allnulled%2Fcommandir/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allnulled%2Fcommandir/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/allnulled","download_url":"https://codeload.github.com/allnulled/commandir/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allnulled%2Fcommandir/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264631213,"owners_count":23640941,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-28T12:20:06.923Z","updated_at":"2025-07-10T18:34:39.075Z","avatar_url":"https://github.com/allnulled.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"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\u003cscript src=\"node_modules/@allnulled/commandir/commandir.js\"\u003e\u003c/script\u003e\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\", () =\u003e 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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallnulled%2Fcommandir","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fallnulled%2Fcommandir","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallnulled%2Fcommandir/lists"}