Add spec for nodejs packages.

pull/11/head
Joshua Potter 2023-12-01 15:52:24 -07:00
parent 72a5d2e607
commit 98dc0ce743
13 changed files with 338 additions and 0 deletions

73
specs/nodejs/runner Executable file
View File

@ -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>/$NAME/g" "$BUILD/flake.nix"
sed -i "s/<NAME>/$NAME/g" "$BUILD/package.json"
sed -i "s/<NAME>/$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,<SHA_256>,\$(prefetch-npm-deps package-lock.json),g\" flake.nix"
# ============================================================
# EPILOGUE
# ============================================================
# Success! Copy contents to target directory.
cp -r "$BUILD"/!(node_modules|validate.cjs) "$OUT"

6
specs/nodejs/spec.json Normal file
View File

@ -0,0 +1,6 @@
{
"name": {
"type": "line",
"prompt": "Name> "
}
}

View File

@ -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

View File

@ -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

11
specs/nodejs/template/.gitignore vendored Normal file
View File

@ -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/

View File

@ -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.

View File

@ -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
}

View File

@ -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 = "<SHA_256>";
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
];
};
});
}

29
specs/nodejs/template/package-lock.json generated Normal file
View File

@ -0,0 +1,29 @@
{
"name": "<NAME>",
"version": "0.1.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "<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"
}
}
}
}

View File

@ -0,0 +1,17 @@
{
"name": "<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"
}
}

View File

@ -0,0 +1,3 @@
console.log("Hello, world!");
export {};

View File

@ -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"
}

12
specs/nodejs/validate.cjs Normal file
View File

@ -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);
}