Created
September 15, 2025 18:17
-
-
Save jpolete/c8521ed5f807eaac05245856ca6838ed to your computer and use it in GitHub Desktop.
NPM Library with TypeScript, Jest, and JSDoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /bin/bash | |
| read -p "Project name. Lowercase, no spaces. [Default myproj]: " projname | |
| projname=${projname:-myproj} | |
| # Create a project directory | |
| mkdir "$projname" | |
| # Move into the dir | |
| cd "$projname" | |
| # Initialize git repo | |
| git init | |
| # Gitignore modules and dist folder | |
| cat > .gitignore << EOF | |
| node_modules | |
| dist | |
| docs | |
| EOF | |
| # Create a read me | |
| cat > README.md << EOF | |
| # $projname | |
| ## Commands | |
| \`\`\`sh | |
| # Build project | |
| \$ pnpm run build | |
| # Test project | |
| \$ pnpm run test | |
| # Generate docs | |
| \$ pnpm run docs | |
| # Publish to package repo | |
| # Setup repo and access token in .npmrc | |
| \$ pnpm publish | |
| \`\`\` | |
| ## References | |
| - [TypeScript](https://www.typescriptlang.org/) | |
| - [Jest](https://jestjs.io/) | |
| - [JS Doc](https://jsdoc.app/) | |
| EOF | |
| # Initialize npm project | |
| cat > package.json << EOF | |
| { | |
| "name": "$projname", | |
| "version": "1.0.0", | |
| "main": "dist/index.js", | |
| "type": "module", | |
| "scripts": { | |
| "test": "jest", | |
| "docs": "tsc && ./node_modules/.bin/jsdoc -c ./.jsdoc.conf.json", | |
| "build": "tsc" | |
| }, | |
| "author": "", | |
| "license": "ISC", | |
| "description": "", | |
| "devDependencies": { | |
| } | |
| } | |
| EOF | |
| # Install dev packages for typescript and testing, and jsdocs | |
| pnpm install -D typescript ts-node jest ts-jest @types/jest @jest/globals jsdoc | |
| # Create typescript config | |
| cat > tsconfig.json << EOF | |
| { | |
| // Visit https://aka.ms/tsconfig to read more about this file | |
| "compilerOptions": { | |
| "target": "esnext", | |
| "module": "NodeNext", | |
| "moduleResolution": "NodeNext", | |
| "outDir": "dist", | |
| "esModuleInterop": true, | |
| "strict": true, | |
| "skipLibCheck": true, | |
| "sourceMap": true, | |
| "declaration": true, | |
| "declarationMap": true, | |
| }, | |
| "exclude": ["node_modules", "./*.ts", "./src/*.test.ts", "dist"] | |
| } | |
| EOF | |
| # Create Jest config | |
| cat > jest.config.ts << EOF | |
| // https://dev.to/mangadev/set-up-a-backend-nodejs-typescript-jest-using-es-modules-1530 | |
| import type { JestConfigWithTsJest } from "ts-jest"; | |
| const config: JestConfigWithTsJest = { | |
| verbose: true, | |
| transform: { | |
| "^.+\\\\.ts?$": [ | |
| "ts-jest", | |
| { | |
| useESM: true, | |
| }, | |
| ], | |
| }, | |
| extensionsToTreatAsEsm: [".ts"], | |
| moduleNameMapper: { | |
| "^(\\\\.{1,2}/.*)\\\\.js$": "\$1", | |
| }, | |
| }; | |
| export default config; | |
| EOF | |
| # Create JSDoc config | |
| cat > .jsdoc.conf.json << EOF | |
| { | |
| "plugins": ["plugins/markdown"], | |
| "recurseDepth": 10, | |
| "opts": { | |
| "recurse": true, | |
| "destination": "./docs/" | |
| }, | |
| "source": { | |
| "include": ["dist"], | |
| "includePattern": ".+\\\\.js(doc|x)?\$", | |
| "excludePattern": "node_modules" | |
| }, | |
| "sourceType": "module", | |
| "tags": { | |
| "allowUnknownTags": true, | |
| "dictionaries": ["jsdoc", "closure"] | |
| }, | |
| "templates": { | |
| "cleverLinks": false, | |
| "monospaceLinks": false | |
| } | |
| } | |
| EOF | |
| # Make sample TypeScript code | |
| mkdir src | |
| cat > src/sum.ts << EOF | |
| /** | |
| * Adds two numbers together. | |
| * | |
| * @param {number} - x | |
| * @param {number} - y | |
| * | |
| * @returns {number} The sum of x and y | |
| */ | |
| export function sum(x: number, y: number): number { | |
| return x + y; | |
| } | |
| EOF | |
| cat > src/sum.test.ts << EOF | |
| import { sum } from "./sum.js"; | |
| test("Adds two numbers", () => { | |
| expect(sum(1, 2)).toEqual(3); | |
| }); | |
| EOF | |
| cat > src/index.ts << EOF | |
| export * from "./sum.js"; | |
| EOF | |
| cat > .npmrc << EOF | |
| # Set GITHUB_ACCESS_TOKEN in dev machine environment vars | |
| # | |
| # Mac: Add export statement to .zshrc | |
| # export GITHUB_ACCESS_TOKEN=xxxxxxxxxxxxxxxxxx | |
| # | |
| # Windows: run sysdm.cpl and add via GUI | |
| # https://docs.npmjs.com/using-private-packages-in-a-ci-cd-workflow#create-and-check-in-a-project-specific-npmrc-file | |
| //npm.pkg.github.com/:_authToken=${GITHUB_ACCESS_TOKEN} | |
| # https://docs.npmjs.com/cli/v11/configuring-npm/npmrc | |
| @metrocollege:registry=https://npm.pkg.github.com | |
| EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment