diff --git a/specs/nodejs/runner b/specs/nodejs/runner new file mode 100755 index 0000000..379c83f --- /dev/null +++ b/specs/nodejs/runner @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + +# Exit immediately if the script encounters a non-zero status. +set -e + +# If set, Bash includes filenames beginning with a `.` in the results of +# filename expansion. The filenames `.` and `..` must always be matched +# explicitly, even if dotglob is set. +shopt -s dotglob extglob + +# ============================================================ +# PROLOGUE +# ============================================================ + +# Create a new top-level directory as fallback in case $BUILD (defined below) +# is ever empty. +mkdir -p "/tmp/bs.nodejs" + +# Create an intermediate build directory. The final step of this script will +# copy the content from this directory to $OUT. +BUILD=$(mktemp -d -p "/tmp/bs.nodejs") + +if [ -z "$BUILD" ]; then + >&2 echo "Failed to create temp directory." + exit 1 +fi + +# Deletes the intermediate build directory on exit. We use a concatenation of +# the intermediate directory with the basename of the generated temp directory +# to ensure we never evaluate to root (i.e. `/`). That should never actually +# happen but a good habit to establish nonetheless. +function cleanup { + rm -r "/tmp/bs.nodejs/$(basename "$BUILD")" +} + +trap cleanup EXIT + +# ============================================================ +# BUILD +# ============================================================ + +# Copy template contents over to the intermediate build directory. +cp -r template/* "$BUILD" + +# Copy over the CommonJS module responsible for validating the given NAME. +cp validate.cjs "$BUILD" + +# Explicitly set permissions on all copied files. +find "$BUILD" -type f -execdir chmod 644 {} + +find "$BUILD" -type d -execdir chmod 755 {} + + +# Validate the provided name is usable. +nix develop "$BUILD" --command bash -c \ + "cd $BUILD && + npm install validate-npm-package-name && + node validate.cjs" + +# Replace the placeholder name found in the template files. +sed -i "s//$NAME/g" "$BUILD/flake.nix" +sed -i "s//$NAME/g" "$BUILD/package.json" +sed -i "s//$NAME/g" "$BUILD/package-lock.json" + +# Precompute the build hash with the new package name in place. +nix develop "$BUILD" --command bash -c \ + "cd $BUILD && + sed -i \"s,,\$(prefetch-npm-deps package-lock.json),g\" flake.nix" + +# ============================================================ +# EPILOGUE +# ============================================================ + +# Success! Copy contents to target directory. +cp -r "$BUILD"/!(node_modules|validate.cjs) "$OUT" diff --git a/specs/nodejs/spec.json b/specs/nodejs/spec.json new file mode 100644 index 0000000..4be1e50 --- /dev/null +++ b/specs/nodejs/spec.json @@ -0,0 +1,6 @@ +{ + "name": { + "type": "line", + "prompt": "Name> " + } +} diff --git a/specs/nodejs/template/.envrc b/specs/nodejs/template/.envrc new file mode 100644 index 0000000..817939c --- /dev/null +++ b/specs/nodejs/template/.envrc @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +if command -v git > /dev/null && on_git_branch; then + git config --local core.hooksPath .githooks/ +fi + +use flake diff --git a/specs/nodejs/template/.githooks/pre-commit b/specs/nodejs/template/.githooks/pre-commit new file mode 100755 index 0000000..4b2c4ca --- /dev/null +++ b/specs/nodejs/template/.githooks/pre-commit @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -e + +filesToFormat=$( + git --no-pager diff --name-status --no-color --cached | \ + awk '$1 != "D" && $2 ~ /\.jsx?$|\.tsx?$/ {print $NF}' +) + +for path in $filesToFormat +do + prettier --write "$path" + git add "$path" +done diff --git a/specs/nodejs/template/.gitignore b/specs/nodejs/template/.gitignore new file mode 100644 index 0000000..2ae6a44 --- /dev/null +++ b/specs/nodejs/template/.gitignore @@ -0,0 +1,11 @@ +# Directory used by `direnv` to hold `use flake`-generated profiles. +/.direnv/ + +# The directory containing all build outputs. +/dist/ + +# A symlink produced by default when running `nix build`. +/result + +# The NodeJS dependency directory. +/node_modules/ diff --git a/specs/nodejs/template/README.md b/specs/nodejs/template/README.md new file mode 100644 index 0000000..3bc0677 --- /dev/null +++ b/specs/nodejs/template/README.md @@ -0,0 +1,27 @@ +# NodeJS Flake Template + +This is a template for constructing a working environment for +[Node.js](https://nodejs.org/en) development (version v18.18.2) with the [npm](https://www.npmjs.com/) +(version 9.8.1) packaging tool. [direnv](https://direnv.net/) can be used to +launch a dev shell upon entering this directory (refer to `.envrc`). Otherwise +run via: +```bash +$ nix develop +``` + +## Language Server + +The [typescript-language-server](https://github.com/typescript-language-server/typescript-language-server) +(version 4.1.2) is included in this flake. + +## Formatting + +Formatting depends on [prettier](https://prettier.io/) (version 3.1.0). A +`pre-commit` hook is included in `.githooks` that can be used to format all +`*.jsx?` and `*.tsx?` files prior to commit. Install via: +```bash +$ git config --local core.hooksPath .githooks/ +``` +If running [direnv](https://direnv.net/), this hook is installed automatically +when entering the directory. + diff --git a/specs/nodejs/template/flake.lock b/specs/nodejs/template/flake.lock new file mode 100644 index 0000000..fdd5e26 --- /dev/null +++ b/specs/nodejs/template/flake.lock @@ -0,0 +1,76 @@ +{ + "nodes": { + "flake-compat": { + "locked": { + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "revCount": 57, + "type": "tarball", + "url": "https://api.flakehub.com/f/pinned/edolstra/flake-compat/1.0.1/018afb31-abd1-7bff-a5e4-cff7e18efb7a/source.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1701253981, + "narHash": "sha256-ztaDIyZ7HrTAfEEUt9AtTDNoCYxUdSd6NrRHaYOIxtk=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "e92039b55bcd58469325ded85d4f58dd5a4eaf58", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/specs/nodejs/template/flake.nix b/specs/nodejs/template/flake.nix new file mode 100644 index 0000000..07e6aba --- /dev/null +++ b/specs/nodejs/template/flake.nix @@ -0,0 +1,50 @@ +{ + description = '' + An opinionated nodejs flake. + + To generate a copy of this template elsewhere, install + [bootstrap](https://github.com/jrpotter/bootstrap) and run: + ```bash + $ bootstrap nodejs + ``` + ''; + + inputs = { + flake-compat.url = "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz"; + flake-utils.url = "github:numtide/flake-utils"; + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + }; + + outputs = { self, nixpkgs, flake-utils, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + { + packages = { + lib = pkgs.buildNpmPackage { + pname = "ohaef"; + version = "0.1.0"; + src = ./.; + npmDepsHash = ""; + + installPhase = '' + mkdir $out + cp dist/index.js $out + ''; + }; + + default = self.packages.${system}.lib; + }; + + devShells.default = pkgs.mkShell { + packages = with pkgs; [ + nodePackages.prettier + nodePackages.typescript-language-server + nodejs + prefetch-npm-deps + typescript + ]; + }; + }); +} diff --git a/specs/nodejs/template/package-lock.json b/specs/nodejs/template/package-lock.json new file mode 100644 index 0000000..0bd0272 --- /dev/null +++ b/specs/nodejs/template/package-lock.json @@ -0,0 +1,29 @@ +{ + "name": "", + "version": "0.1.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "", + "version": "0.1.0", + "license": "ISC", + "devDependencies": { + "typescript": "^5.3.2" + } + }, + "node_modules/typescript": { + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.2.tgz", + "integrity": "sha512-6l+RyNy7oAHDfxC4FzSJcz9vnjTKxrLpDG5M2Vu4SHRVNg6xzqZp6LYSR9zjqQTu8DU/f5xwxUdADOkbrIX2gQ==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + } + } +} diff --git a/specs/nodejs/template/package.json b/specs/nodejs/template/package.json new file mode 100644 index 0000000..dcf0a60 --- /dev/null +++ b/specs/nodejs/template/package.json @@ -0,0 +1,17 @@ +{ + "name": "", + "version": "0.1.0", + "description": "", + "main": "index.js", + "scripts": { + "build": "tsc", + "format": "prettier --write \"src/**/*.{js,jsx,ts,tsx}\"", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "typescript": "^5.3.2" + } +} diff --git a/specs/nodejs/template/src/index.ts b/specs/nodejs/template/src/index.ts new file mode 100644 index 0000000..5ac26dc --- /dev/null +++ b/specs/nodejs/template/src/index.ts @@ -0,0 +1,3 @@ +console.log("Hello, world!"); + +export {}; diff --git a/specs/nodejs/template/tsconfig.json b/specs/nodejs/template/tsconfig.json new file mode 100644 index 0000000..56f4f6a --- /dev/null +++ b/specs/nodejs/template/tsconfig.json @@ -0,0 +1,14 @@ +{ + // Visit https://aka.ms/tsconfig to read more about this file. + "compilerOptions": { + "target": "es2015", + "module": "commonjs", + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + "outDir": "dist" + }, + "include": ["src"], + "$schema": "https://json.schemastore.org/tsconfig" +} diff --git a/specs/nodejs/validate.cjs b/specs/nodejs/validate.cjs new file mode 100644 index 0000000..829baeb --- /dev/null +++ b/specs/nodejs/validate.cjs @@ -0,0 +1,12 @@ +'use strict' + +const validate = require('validate-npm-package-name'); + +const result = validate(process.env.NAME); +if (result.errors && result.errors.length > 0) { + console.log(''); + for (const e of result.errors) { + console.log('\x1b[31m%s\x1b[0m: %s', 'ERROR', e); + } + process.exit(1); +}