{"id":17771742,"url":"https://github.com/jonnyburger/xns","last_synced_at":"2025-04-21T23:31:49.658Z","repository":{"id":57401994,"uuid":"156590550","full_name":"JonnyBurger/xns","owner":"JonnyBurger","description":"Make the default export of a file executable in in Node.js!","archived":false,"fork":false,"pushed_at":"2020-01-07T20:04:27.000Z","size":9,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-26T22:24:52.936Z","etag":null,"topics":["cli","helper","node"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/xns","language":"TypeScript","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/JonnyBurger.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}},"created_at":"2018-11-07T18:26:47.000Z","updated_at":"2021-09-18T16:34:19.000Z","dependencies_parsed_at":"2022-09-15T18:42:11.798Z","dependency_job_id":null,"html_url":"https://github.com/JonnyBurger/xns","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonnyBurger%2Fxns","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonnyBurger%2Fxns/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonnyBurger%2Fxns/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonnyBurger%2Fxns/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JonnyBurger","download_url":"https://codeload.github.com/JonnyBurger/xns/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223881649,"owners_count":17219268,"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":["cli","helper","node"],"created_at":"2024-10-26T21:36:43.572Z","updated_at":"2024-11-09T21:02:53.527Z","avatar_url":"https://github.com/JonnyBurger.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# xns\n\n\u003e Turn any function into a script that can be invoked from the command line.\n\nxns stands for e**X**ecute **N**ode **S**cript!  \n\n## Problem\n\nLet's say you have written a cool JavaScript (or TypeScript) function!\n\n`get-bitcoin-price.ts`:\n```js\nimport got from 'got';\n\nexport default async (): Promise\u003cstring\u003e =\u003e {\n  const res = await got('https://api.coinbase.com/v2/prices/spot');\n  const {currency, amount} = JSON.parse(res.body).data;\n  return `1 Bitcoin is worth ${currency} ${amount}`;\n};\n```\n\nNow you would like to execute this function in your terminal.\nCurrent options are not ideal:\n- You could make the function self-invoking, but then it also executes by itself as soon as it's imported somewhere else.\n- You could make another file, import the function and execute it, then run that file, but it's cumbersome.\n\n## Solution\n\nWrap your function in `xns()`:\n\n`get-bitcoin-price.ts`:\n```js\nimport xns from 'xns';\nimport got from 'got';\n\nexport default xns(async (): Promise\u003cstring\u003e =\u003e {\n  const res = await got('https://api.coinbase.com/v2/prices/spot');\n  const {currency, amount} = JSON.parse(res.body).data;\n  return `1 Bitcoin is worth ${currency} ${amount}`;\n});\n```\n\nNow you can execute the function from your terminal:\n\n```sh\nts-node get-bitcoin-price\n1 Bitcoin is worth USD 7767.655\n```\n\nYou can also still import the function from somewhere else as usual and it will not execute itself 👌\n\n## Why `xns`?\n- Works with `node` and transpiled languages: `ts-node`, `babel-node`...\n- Can still be imported as normal\n- Prints out the return value\n- Fully typed, keep Typescript typings\n- Wrapping function in xns does not change behavior of your app\n- Exit code 1 on error, exit code 0 on success, perfect for CRON jobs\n- No `Unhandled promise rejection` errors, proper catch handler built in\n- Works with synchronous and asynchronous functions\n- Works with ES Modules and CommonJS import style\n    - Use named export for CommonJS: `const {xns} = require('xns')`\n\n## Old version of `xns`\nThis is version 2 of `xns`. Version 1 worked completely different, it compiled a file using babel and called the default export. It's still supported if you have Babel. See [here](https://github.com/JonnyBurger/xns/tree/e91cd60e16990e43bfb37aa76c6e382f64b4e9da) for the old API.\n\n## License\nMIT\n\n## Author\n[Jonny Burger](jonny.io)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonnyburger%2Fxns","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonnyburger%2Fxns","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonnyburger%2Fxns/lists"}