generated from YieldRay/nodejs-purejs-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dec24e2
Showing
9 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# https://prettier.io/docs/en/configuration.html | ||
semi: false | ||
printWidth: 80 | ||
tabWidth: 4 | ||
singleQuote: true |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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! |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* @template {number} T | ||
* @param {T} a | ||
* @param {T} b | ||
*/ | ||
export function add(a, b) { | ||
return a + b | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './add.mjs' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |