diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/.prettierrc.yaml b/.prettierrc.yaml new file mode 100644 index 0000000..95d290b --- /dev/null +++ b/.prettierrc.yaml @@ -0,0 +1,5 @@ +# https://prettier.io/docs/en/configuration.html +semi: false +printWidth: 80 +tabWidth: 4 +singleQuote: true diff --git a/README.md b/README.md new file mode 100644 index 0000000..57a645f --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# nodejs-purejs-template + +Build a nodejs package in pure javascript, +but use [JSDoc](https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html) to enable typescript check! diff --git a/add.mjs b/add.mjs new file mode 100644 index 0000000..02fcb90 --- /dev/null +++ b/add.mjs @@ -0,0 +1,8 @@ +/** + * @template {number} T + * @param {T} a + * @param {T} b + */ +export function add(a, b) { + return a + b +} diff --git a/add.test.mjs b/add.test.mjs new file mode 100644 index 0000000..97fe994 --- /dev/null +++ b/add.test.mjs @@ -0,0 +1,8 @@ +import { test } from 'node:test' +import { add } from './add.mjs' +import * as assert from 'node:assert' + +test('test add()', async () => { + assert.equal(add(1, 1), 2) + assert.equal(add(1, -1), 0) +}) diff --git a/cli.mjs b/cli.mjs new file mode 100644 index 0000000..8036f99 --- /dev/null +++ b/cli.mjs @@ -0,0 +1,60 @@ +import { parseArgs } from 'node:util' +import process from 'node:process' +import pkg from './package.json' assert { type: 'json' } + +// https://nodejs.org/api/util.html#utilparseargsconfig +const { values, positionals } = parseArgs({ + options: { + help: { + type: 'boolean', + multiple: false, + short: 'h', + default: false, + }, + lhs: { + type: 'string', + short: 'a', + default: '0', + }, + rhs: { + type: 'string', + short: 'b', + default: '0', + }, + }, + strict: true, + allowPositionals: true, +}) + +if (positionals.length < 1 || values.help) { + help() +} + +const command = positionals[0] + +switch (command) { + case 'help': + help() + break + case 'add': + const { lhs, rhs } = values + const sum = Number.parseFloat(lhs) + Number.parseFloat(rhs) + console.log('%s + %s = %s', lhs, rhs, sum) + break + default: + process.stderr.write(`Unknown command ${command}\n`) + process.exit(1) +} + +function help() { + process.stdout.write(`\x1b[32m${pkg.name}\x1b[0m v${pkg.version} + + A nodejs template in pure javascript + + \x1b[1mUsage:\x1b[0m + + cli-name add --lhs [number] --rhs [number] Get sum of lhs and rhs + cli-name help Show this help +`) + process.exit(1) +} diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..bc37dd2 --- /dev/null +++ b/index.mjs @@ -0,0 +1 @@ +export * from './add.mjs' diff --git a/package.json b/package.json new file mode 100644 index 0000000..bd9170c --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "nodejs-purejs-template", + "private": true, + "version": "0.1.0", + "type": "module", + "description": "", + "main": "index.mjs", + "scripts": { + "test": "node --test", + "watch": "tsc --watch" + }, + "keywords": [], + "author": "", + "license": "", + "engines": { + "node": ">16" + }, + "bin": { + "cli-name": "./cli.mjs" + }, + "devDependencies": { + "@types/node": "^20.10.6", + "typescript": "^5.3.3" + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..44956c9 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,24 @@ +{ + // https://www.typescriptlang.org/tsconfig#compilerOptions + "compilerOptions": { + // Modules + "rootDir": "./", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "resolveJsonModule": true, + + // Others + "target": "ESNext", + "noEmit": true, + "skipLibCheck": true, + + // JavaScript Support + "allowJs": true, + "checkJs": true, + + // Type Checking + "strict": false, // (without TS it's hard to express some type) + "noFallthroughCasesInSwitch": true, + "noPropertyAccessFromIndexSignature": true + } +}