{"id":22903449,"url":"https://github.com/sawyerbutton/my-first-typescript-library","last_synced_at":"2025-04-01T07:23:24.677Z","repository":{"id":120750509,"uuid":"152513026","full_name":"sawyerbutton/My-first-TypeScript-Library","owner":"sawyerbutton","description":"My first TypeScript library","archived":false,"fork":false,"pushed_at":"2018-10-11T03:15:11.000Z","size":7101,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-07T04:40:28.068Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sawyerbutton.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2018-10-11T01:33:34.000Z","updated_at":"2018-10-11T03:15:12.000Z","dependencies_parsed_at":"2023-07-11T17:17:36.163Z","dependency_job_id":null,"html_url":"https://github.com/sawyerbutton/My-first-TypeScript-Library","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/sawyerbutton%2FMy-first-TypeScript-Library","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sawyerbutton%2FMy-first-TypeScript-Library/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sawyerbutton%2FMy-first-TypeScript-Library/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sawyerbutton%2FMy-first-TypeScript-Library/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sawyerbutton","download_url":"https://codeload.github.com/sawyerbutton/My-first-TypeScript-Library/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246598897,"owners_count":20803108,"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-14T02:36:45.502Z","updated_at":"2025-04-01T07:23:24.671Z","avatar_url":"https://github.com/sawyerbutton.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# My-first-TypeScript-Library\nMy first TypeScript library\n\n\n## first Step\n\n\u003e initiate the package.json file\n\n```bash\nnpm init\n```\n\n\u003e Remember to declare where to find the type declarations `\"types\": \"dist/index.d.ts\",`\n\n\u003e Remember to declare where to find the common entry js file `\"main\": \"dist/index.js\",`\n\n```json\n{\n  \"name\": \"@sawyerbutton/my-first-typescript-library\",\n  \"version\": \"1.0.0\",\n  \"description\": \"log \\\"hello\\\" and \\\"bye\\\" to the console!\",\n  \"main\": \"dist/index.js\",\n  \"types\": \"dist/index.d.ts\",\n  \"scripts\": {\n    \"test\": \"echo \\\"Error: no test specified\\\" \u0026\u0026 exit 1\"\n  },\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"git+https://github.com/sawyerbutton/My-first-TypeScript-Library.git\"\n  },\n  \"keywords\": [\n    \"TypeScript\"\n  ],\n  \"author\": \"sawyerbutton\",\n  \"license\": \"MIT\",\n  \"bugs\": {\n    \"url\": \"https://github.com/sawyerbutton/My-first-TypeScript-Library/issues\"\n  },\n  \"homepage\": \"https://github.com/sawyerbutton/My-first-TypeScript-Library#readme\"\n}\n```\n\n## second step\n\n\u003e setup tsconfig.json\n\n```json\n{\n  \"compilerOptions\": {\n    \"module\": \"commonjs\",\n    \"target\": \"es5\",\n    \"sourceMap\": true,\n    \"declaration\": true,\n    \"outDir\": \"./dist\"\n  },\n  \"include\": [\n    \"src/**/*\"\n  ],\n  \"exclude\": [\n    \"node_modules\"\n  ]\n}\n```\n\n\u003e remember to add `\"declaration\": true,` flat in order to generate the typing declaration required for the consumers to use this library\n\n## implement library\n\n\u003e setup xx.ts file under src folder\n\n## create index.ts file\n\n\u003e Add an index.ts file to src folder. The purpose of it is to export all the parts of the library available for consumers.\n\n\u003e index.ts file will be converted to index.d.ts file later\n\n## configure .npmignore\n\n\u003e commonly, packages downloaded from npm does not have src folder\n\n\u003e the compiled files along with the typings `(.d.ts files)` live in the dist folder.\n\n\u003e create a `.npmigonore` file to ignore these file\n\n```text\ntsconfig.json\nsrc\n```\n\n## test library\n\n\u003e Install packages for unit testing\n\n```bash\nyarn add mocha @types/mocha chai @types/chai ts-node typescript --dev\n```\n\n\u003e there is a unit\n\n```typescript\nexport function add(x: number, y: number) {\n  return x + y;\n}\n```\n\n\u003e corresponding unit test\n\n```typescript\nimport { add } from './hello-world';\n\nimport * as mocha from 'mocha';\nimport * as chai from 'chai';\n\nconst expect  = chai.expect;\n\ndescribe('My add function', () =\u003e {\n\n    it('should be able to add things correctly' , () =\u003e {\n        expect(add(3,4)).to.equal(7);\n    });\n\n});\n```\n\n\u003e run the test command \n\n```bash\nmocha --reporter spec --compilers ts:ts-node/register src/**/*.spec.ts\n```\n\n\u003e console should output\n\n```bash\n My add function\n    ✓ should be able to add things correctly\n\n\n  1 passing (7ms)\n```\n\n\u003e It will be more convenient to put this long command into package.json into \"scripts: { \"test\": \"...\"}\" and then run the tests with `npm test`.\n\n\n## compile file then publish\n\n```bash\ntsc\nnpm publish --access=public\n```\n\n\u003e remember to use `npm publish --access=public` command rather than `npm publush` if you don't have private package access in npm.\n\n## Just in case\n\n\u003e Just in case of making a library available as a system command\n\n1. On top of executable files` (the main files) like dist/index.js`, add the following line which makes sure the system understands how to execute the compiled javascript file, by instructing it to interpret it with node\n\n```javascript\n#!/usr/bin/env node\n```\n\n2. modify the `package.json` file by adding `\"bin\":{\"myTypeFunc\": \"dist/index.js\"}`\n\n```json\n{\n  \"name\": \"@sawyerbutton/my-first-typescript-library\",\n  \"version\": \"1.2.0\",\n  \"description\": \"log \\\"hello\\\" and \\\"bye\\\" to the console!\",\n  \"main\": \"dist/index.js\",\n  \"types\": \"dist/index.d.ts\",\n   \"bin\": {\n        \"myTypeFunc\": \"dist/index.js\"\n    },\n  \"scripts\": {\n    \"test\": \"mocha --reporter spec --compilers ts:ts-node/register src/**/*.spec.ts\"\n  },\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"git+https://github.com/sawyerbutton/My-first-TypeScript-Library.git\"\n  },\n  \"keywords\": [\n    \"TypeScript\"\n  ],\n  \"author\": \"sawyerbutton\",\n  \"license\": \"MIT\",\n  \"bugs\": {\n    \"url\": \"https://github.com/sawyerbutton/My-first-TypeScript-Library/issues\"\n  },\n  \"homepage\": \"https://github.com/sawyerbutton/My-first-TypeScript-Library#readme\",\n  \"devDependencies\": {\n    \"@types/chai\": \"^4.1.6\",\n    \"@types/mocha\": \"^5.2.5\",\n    \"chai\": \"^4.2.0\",\n    \"mocha\": \"^5.2.0\",\n    \"ts-node\": \"^7.0.1\",\n    \"typescript\": \"^3.1.2\"\n  }\n}\n```\n\n3. Once publish latest package, consumers will be able to install package globally on a machine using:\n\n```bash\nsudo npm install -g @sawyerbutton/my-first-typescript-library\n```\n\n\u003e then using command in terminal\n\n```bash\nmyTypeFunc\n```\n4. Yeah, current package is pretty useless.. but umm, it is just a small example to demonstrate how to do it","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsawyerbutton%2Fmy-first-typescript-library","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsawyerbutton%2Fmy-first-typescript-library","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsawyerbutton%2Fmy-first-typescript-library/lists"}