Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
YieldRay authored Mar 11, 2024
0 parents commit dec24e2
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
5 changes: 5 additions & 0 deletions .prettierrc.yaml
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
4 changes: 4 additions & 0 deletions README.md
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!
8 changes: 8 additions & 0 deletions add.mjs
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
}
8 changes: 8 additions & 0 deletions add.test.mjs
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)
})
60 changes: 60 additions & 0 deletions cli.mjs
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)
}
1 change: 1 addition & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './add.mjs'
25 changes: 25 additions & 0 deletions package.json
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"
}
}
24 changes: 24 additions & 0 deletions tsconfig.json
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
}
}

0 comments on commit dec24e2

Please sign in to comment.