{"id":17029549,"url":"https://github.com/kohheepeace/hello-ts-cli","last_synced_at":"2026-04-17T01:31:32.354Z","repository":{"id":57125323,"uuid":"372643835","full_name":"kohheepeace/hello-ts-cli","owner":"kohheepeace","description":null,"archived":false,"fork":false,"pushed_at":"2021-06-02T20:26:07.000Z","size":18,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-22T20:45:32.507Z","etag":null,"topics":["chalk","node-cli","typescript","yarn"],"latest_commit_sha":null,"homepage":"","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/kohheepeace.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":"2021-05-31T22:37:41.000Z","updated_at":"2021-06-02T20:26:32.000Z","dependencies_parsed_at":"2022-08-31T12:10:34.302Z","dependency_job_id":null,"html_url":"https://github.com/kohheepeace/hello-ts-cli","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/kohheepeace/hello-ts-cli","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kohheepeace%2Fhello-ts-cli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kohheepeace%2Fhello-ts-cli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kohheepeace%2Fhello-ts-cli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kohheepeace%2Fhello-ts-cli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kohheepeace","download_url":"https://codeload.github.com/kohheepeace/hello-ts-cli/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kohheepeace%2Fhello-ts-cli/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31911438,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T18:22:33.417Z","status":"ssl_error","status_checked_at":"2026-04-16T18:21:47.142Z","response_time":69,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["chalk","node-cli","typescript","yarn"],"created_at":"2024-10-14T08:00:41.104Z","updated_at":"2026-04-17T01:31:32.333Z","avatar_url":"https://github.com/kohheepeace.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hello-ts-cli tutorial\nThis github repository explains how to create a node cli tool with typescript and publish it as a npm (yarn) package.\n\n\n## 📌Create yarn package\n\n```sh\nmkdir hello-ts-cli\ncd hello-ts-cli\nyarn init -y\n```\n\n## 📌Install Typescript in your project\nTo use typescript, let's install `typescript`.\n\n```sh\nyarn add typescript --dev\n```\nhttps://www.typescriptlang.org/download\n\n\n## 📌Make first ts file\nMake `src` folder and `cli.ts` inside it.\n\n`src/cli.ts`\n```ts\n#!/usr/bin/env node\n\nconsole.log(\"Hello World\");\n```\n\n- [`#!/usr/bin/env node` is needed for making node.js cli tool.](https://stackoverflow.com/questions/33509816/what-exactly-does-usr-bin-env-node-do-at-the-beginning-of-node-files)\n- [filename convention: `cli.ts`](https://docs.npmjs.com/cli/v7/configuring-npm/package-json#bin)\n\n## 📌Typescript compile command\nLet's check basic typescript command.\n\nhttps://www.typescriptlang.org/docs/handbook/compiler-options.html\n### ✅`tsc`\n\n`tsc` is the command to compile the Typescript code.\n\n```sh\nyarn tsc src/cli.ts\n```\nThis command creates `src/cli.js`.\n### ✅`tsc --watch`\nAutomatically compile `ts` files when you make a change.\n```sh\ntsc src/index.ts --watch\n```\n\n## 📌`tsconfig.json`\nhttps://www.typescriptlang.org/docs/handbook/tsconfig-json.html\n### ✅What is `tsconfig.json` ?\n- Specify compile options\n- Indicates that the directory is the root of a TypeScript project\n- You can specify which files to compile and which files to exclude (**you don't need to specify each files to compile**).\n\n### ✅Make `tsconfig.json`\n```\nyarn tsc --init\n```\nto create the `tsconfig.json` file.\n### ✅Use Recommended `tsconfig.json` settings\n📝In the beginning, you may not know what settings to use, so let's extend the recommended settings.\n\nThis time, we will use the recommended settings for `node14`.\n\nhttps://www.typescriptlang.org/docs/handbook/tsconfig-json.html#tsconfig-bases\n\n`terminal`\n```sh\nyarn add -D @tsconfig/node14\n```\n\nThen,\n\n`tsconfig.json`\n```json\n{\n  \"extends\": \"@tsconfig/node14/tsconfig.json\",\n  \"include\": [\"src/**/*\"],\n  \"exclude\": [\"node_modules\"]\n}\n```\n\n### ✅ Add `rootDir`\nSet the `rootDir` to `src` since `ts` files are only stored under the `src` directory.\n\n`tsconfig.json`\n```json\n{\n  \"extends\": \"@tsconfig/node14/tsconfig.json\",\n  \"compilerOptions\": {\n    \"rootDir\": \"src\",\n  },\n  \"include\": [\"src/**/*\"],\n  \"exclude\": [\"node_modules\"],\n}\n```\n\nRef: https://www.typescriptlang.org/tsconfig#rootDir\n\n\n### ✅ Change output dir\n- Change the output destination of the compiled `.ts` file to `lib`.\n- Also, add exclude `lib` in `tsconfig.json`.\n```json\n{\n  \"extends\": \"@tsconfig/node14/tsconfig.json\",\n  \"compilerOptions\": {\n    \"rootDir\": \"src\",\n    \"outDir\": \"lib\",\n  },\n  \"include\": [\"src/**/*\"],\n  \"exclude\": [\"node_modules\", \"lib\"],\n}\n```\n\n[Ref: Naming Conventions of `lib`](https://stackoverflow.com/questions/39553079/difference-between-lib-and-dist-folders-when-packaging-library-using-webpack#:~:text=3%20Answers\u0026text=Usually%20the%20dist%20folder%20is,npm%20will%20consume%20that%20directly)\n\n## 📌Add `@types/node`\nIf you compile the file in this state, you will get the following error.\n\n`terminal`\n```sh\n$ yarn tsc # thanks to `tsconfig.json`, you don't need to specify target ts files now.\nCannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.\n\n1 console.log(\"Hello World\");\n  ~~~~~~~\n```\n\nThis is caused by the lack of `node` types. So, you can add\n```sh\nyarn add -D @types/node\n```\n\nRef: https://github.com/microsoft/TypeScript/issues/9545\n\n\n## 📌Add `main` and `bin` to `package.json`\n\n`package.json`\n```json\n{\n\t\"name\": \"hello-ts-cli\",\n\t\"version\": \"1.0.0\",\n\t\"main\": \"lib/cli.js\",\n\t\"bin\": {\n\t\t\"hello-ts-cli\": \"lib/cli.js\"\n\t},\n\t\"license\": \"MIT\",\n\t\"devDependencies\": {\n\t\t\"@tsconfig/node14\": \"^1.0.0\",\n\t\t\"@types/node\": \"^15.6.1\",\n\t\t\"typescript\": \"^4.3.2\"\n\t},\n\t\"dependencies\": {\n\t\t\"chalk\": \"^4.1.1\",\n\t\t\"commander\": \"^7.2.0\"\n\t}\n}\n```\n`main`: https://docs.npmjs.com/cli/v7/configuring-npm/package-json#main\n\n`bin`: https://docs.npmjs.com/cli/v7/configuring-npm/package-json#bin\n\n## 📌Run cli scripts\nNow we are ready to run the cli command. Let's install the current folder into `npm` global.\n\n`terminal`\n```sh\nnpm i -g .\n```\n\nNow you can use the `hello-ts-cli` command specified in `package.json`.\n\n`terminal`\n```sh\n$ hello-ts-cli\nHello World\n```\n\n❗Don't forget to uninstall after finishing this tutorial.\n```\nnpm uninstall -g .\n```\n\n## 📌Terminal Text Coloring\nhttps://github.com/chalk/chalk\n\n```sh\nyarn add chalk\n```\n\n`src/cli.ts`\n```ts\n#!/usr/bin/env node\nimport chalk from \"chalk\";\nconsole.log(chalk.blue(\"Hello World\"));\n```\n\n```sh\nyarn tsc\nhello-ts-cli\n```\n\n![2021-06-01_08h25_51](https://user-images.githubusercontent.com/29557494/120248657-114b1580-c2b3-11eb-93c8-4741b29aee5f.png)\n\n## 📌Accept Args\nhttps://github.com/tj/commander.js/\n\n```\nyarn add commander\n```\n\nI will use this example\n\nhttps://github.com/tj/commander.js/#action-handler\n\n```ts\n#!/usr/bin/env node\nimport { Command } from \"commander\";\nimport chalk from \"chalk\";\n\nconst program = new Command();\n\nprogram\n\t.arguments(\"\u003cname\u003e\")\n\t.option(\"-t, --title \u003chonorific\u003e\", \"title to use before name\")\n\t.option(\"-d, --debug\", \"display some debugging\")\n\t.action((name, options, command) =\u003e {\n\t\tif (options.debug) {\n\t\t\tconsole.error(\"Called %s with options %o\", command.name(), options);\n\t\t}\n\t\tconst title = options.title ? `${options.title} ` : \"\";\n\t\tconsole.log(chalk.blue(`Hello ${title}${name}`));\n\t});\n\nprogram.parse();\n```\n\nThen compile and run command with options\n\n```sh\n$ yarn tsc\n$ hello-ts-cli World --title=\"Super\"\nHello Super World\n```\n\n## 📌Publish Yarn Package\n1. [Create an npm account](https://www.npmjs.com/signup) \n\nThen,\n\n```sh\n# git tagging before publish\n$ git tag v1.0.0\n$ git tag -l\nv1.0.0\n$ git push origin v1.0.0\n\n# publish package\n$ yarn publish\n```\n\n### ✅If package name is already taken\n\nChange name in `package.json`\n```json\n{\n\t\"name\": \"@username/hello-ts-cli\",\n  ...\n}\n```\n\nThen\n\n```sh\nyarn publish --access public\n```\n\nRef: https://docs.npmjs.com/creating-and-publishing-scoped-public-packages\n\n### ✅Files included in package by `yarn publish`\nYou can check what files will be included in your package, by running `npx npm-packlist`.\n\n```sh\n$npx npm-packlist\nlib/cli.js\npackage.json\ntsconfig.json\nREADME.md\nsrc/cli.ts\n```\n\nRef: https://docs.npmjs.com/cli/v7/commands/npm-publish#files-included-in-package\n\n### ✅Prevent the possibility of forgetting to build before publish.\n\nIf you put the following in `package.json`, it will automatically build the typescript file when you type `yarn publish`.\n\nIn `package.json`\n```\n...\n\"scripts\": {\n\t\"prepare\": \"tsc\"\n},\n...\n```\n\nRef: https://docs.npmjs.com/cli/v7/using-npm/scripts#life-cycle-scripts\n\n## 📌Update and publish yarn package\n```sh\n$ yarn version\n...\n# Type new version here\nquestion New version: 1.0.1\n```\n`yarn version` automatically creates new git tag.\n\n```sh\n$ git tag -l\nv1.0.0\nv1.0.1\n\n# So, just push new version to remote\ngit push origin v1.0.1\n\n# publish updated version\nyarn publish\n```\n\n## Refs\n- https://developer.okta.com/blog/2019/06/18/command-line-app-with-nodejs\n- https://medium.com/netscape/a-guide-to-create-a-nodejs-command-line-package-c2166ad0452e\n- https://www.digitalocean.com/community/tutorials/typescript-new-project\n- https://itnext.io/how-to-create-your-own-typescript-cli-with-node-js-1faf7095ef89\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkohheepeace%2Fhello-ts-cli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkohheepeace%2Fhello-ts-cli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkohheepeace%2Fhello-ts-cli/lists"}