diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..e2e892f --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +[*] +charset = utf-8 +end_of_line = lf +indent_style = tab +insert_final_newline = true +trim_trailing_whitespace = true + +[*.{yml,yaml}] +indent_size = 2 +indent_style = space diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..8a5b4ae --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,26 @@ +# Please see the documentation for all configuration options: +# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + - package-ecosystem: "npm" + directory: "/" + open-pull-requests-limit: 30 + schedule: + interval: "weekly" + day: "saturday" + time: "02:42" # UTC + commit-message: + prefix: "build(npm):" + ignore: + - dependency-name: "@types/node" + update-types: ["version-update:semver-major"] + + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + day: "saturday" + time: "02:42" # UTC + commit-message: + prefix: "ci(actions):" diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..b12eb4d --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,45 @@ +name: Node.js + +on: + push: + pull_request: + schedule: + # Check if it works with current dependencies + - cron: '42 2 * * 6' # weekly on Saturday 2:42 UTC + +jobs: + test: + name: Node.js + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/setup-node@v4 + with: + node-version: 'lts/*' + - uses: actions/checkout@v4 + - run: npm install + - run: npm test + - run: npm pack + + publish: + name: Publish on NPM + if: startsWith(github.ref, 'refs/tags/v') + runs-on: ubuntu-latest + needs: test + permissions: + contents: write + id-token: write + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 'lts/*' + registry-url: 'https://registry.npmjs.org' + - run: npm install + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + NPM_CONFIG_ACCESS: public + NPM_CONFIG_PROVENANCE: true + - name: Create GitHub release + uses: softprops/action-gh-release@v1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3148a6d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/*-*.*.*.tgz +coverage +dist +node_modules diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0388e65 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) EdJoPaTo (https://edjopato.de) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ea15a0d --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# typescript-node-module-boilerplate + +[![NPM Version](https://img.shields.io/npm/v/typescript-node-module-boilerplate.svg)](https://www.npmjs.com/package/typescript-node-module-boilerplate) +[![node](https://img.shields.io/node/v/typescript-node-module-boilerplate.svg)](https://www.npmjs.com/package/typescript-node-module-boilerplate) + +> + +## Install + +```bash +npm install typescript-node-module-boilerplate +``` + +## Usage + +```ts +import {myFunction} from 'typescript-node-module-boilerplate'; + +myFunction('unicorns'); +//=> 'unicorns & rainbows' +``` diff --git a/copy-template-into-existing-project.sh b/copy-template-into-existing-project.sh new file mode 100755 index 0000000..b335895 --- /dev/null +++ b/copy-template-into-existing-project.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +set -eu + +# Usage +# Go to the project you want to improve via this template +# cd ~/git/my-project +# Run this script from the working directory of that project +# ~/git/typescript-node-module-boilerplate/copy-template-into-existing-project.sh + +name=$(basename "$PWD") +templatedir="$(dirname "$0")" + +cp -r \ + "$templatedir/"{package.json,tsconfig.json,.editorconfig,.gitattributes,.github,.gitignore,.npmrc} \ + . + +echo "everything copied" + +# Replace template name with folder name +# macOS: add '' after -i like this: sed -i '' "s/… +sed -i "s/typescript-node-module-boilerplate/$name/g" package.json .github/**/*.yml + +git --no-pager status --short diff --git a/package.json b/package.json new file mode 100644 index 0000000..306025b --- /dev/null +++ b/package.json @@ -0,0 +1,59 @@ +{ + "name": "typescript-node-module-boilerplate", + "version": "0.0.0", + "description": "", + "keywords": [ + "" + ], + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/EdJoPaTo/typescript-node-module-boilerplate.git" + }, + "author": { + "name": "EdJoPaTo", + "email": "typescript-node-module-boilerplate-npm-package@edjopato.de", + "url": "https://edjopato.de" + }, + "scripts": { + "build": "del-cli dist && tsc", + "prepack": "npm run build", + "test": "tsc --sourceMap && xo && c8 --all node --test --enable-source-maps" + }, + "type": "module", + "engines": { + "node": ">=14" + }, + "dependencies": {}, + "devDependencies": { + "@sindresorhus/tsconfig": "^5.0.0", + "@types/node": "^18.18.13", + "c8": "^8.0.1", + "del-cli": "^5.0.0", + "typescript": "^5.0.2", + "xo": "^0.56.0" + }, + "files": [ + "dist", + "!*.test.*", + "!test.*" + ], + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "xo": { + "rules": { + "@typescript-eslint/prefer-readonly-parameter-types": "warn" + }, + "overrides": [ + { + "files": [ + "**/*.test.*", + "**/test.*" + ], + "rules": { + "@typescript-eslint/prefer-readonly-parameter-types": "off" + } + } + ] + } +} diff --git a/source/index.ts b/source/index.ts new file mode 100644 index 0000000..da09d1c --- /dev/null +++ b/source/index.ts @@ -0,0 +1,8 @@ +export function moduleName(input: string | undefined): string { + if (typeof input !== 'string' && input !== undefined) { + throw new TypeError(`Expected a string, got ${typeof input}`); + } + + const sanetized = input?.trim() ?? 'Glitter'; + return sanetized + ' & rainbows'; +} diff --git a/source/test.ts b/source/test.ts new file mode 100644 index 0000000..09cbb59 --- /dev/null +++ b/source/test.ts @@ -0,0 +1,14 @@ +import {strictEqual, throws} from 'node:assert'; +import {test} from 'node:test'; +import {moduleName} from './index.js'; + +await test('simple string input', () => { + strictEqual(moduleName('unicorns'), 'unicorns & rainbows'); +}); + +await test('javascript users without typings get error on number', () => { + // @ts-expect-error moduleName expects a string not a number + throws(() => moduleName(123), { + message: 'Expected a string, got number', + }); +}); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..7de4d95 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "@sindresorhus/tsconfig/tsconfig.json", + "include": [ + "source" + ], + "compilerOptions": { + "lib": ["ES2020"], + "target": "ES2020", // Node.js 14 + "outDir": "dist" + } +}