Skip to content

Commit

Permalink
Initial version of npm init neon --lib
Browse files Browse the repository at this point in the history
- generates necessary boilerplate to be a portable Node library implemented in Rust
- uses TS by default (can be disabled)
- generates GitHub Actions boilerplate by default (can be disabled) to make publishing push-button
  • Loading branch information
dherman committed Feb 26, 2024
1 parent 53c8410 commit 0a27050
Show file tree
Hide file tree
Showing 27 changed files with 4,341 additions and 449 deletions.
3,898 changes: 3,778 additions & 120 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions pkgs/create-neon/data/templates/.gitignore.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ index.node
**/node_modules
**/.DS_Store
npm-debug.log*
{{#if package.lang.isTypeScript}}
{{#eq package.library.lang compare="ts"}}
lib
{{/if}}
cargo.log
{{/eq}}cargo.log
cross.log
2 changes: 1 addition & 1 deletion pkgs/create-neon/data/templates/.npmignore.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{#if package.isLibrary}}
{{#if packageSpec.library}}
platforms
{{/if}}
17 changes: 9 additions & 8 deletions pkgs/create-neon/data/templates/README.md.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ $ node

In the project directory, you can run:

{{#unless package.isLibrary}}
{{#unless packageSpec.library}}
### `npm install`

Installs the project, including running `npm run build`.
Expand Down Expand Up @@ -69,13 +69,13 @@ The directory structure of this project is:
{{package.name}}/
├── Cargo.toml
├── README.md
{{#if package.isLibrary}}
{{#if packageSpec.library}}
├── lib/
{{#if package.lang.isTypeScript}}
{{#eq packageSpec.library.lang compare="ts"}}
├── ts/
| ├── index.mts
| └── index.cts
{{/if}}
{{/eq}}
├── platforms/
{{else}}
├── index.node
Expand All @@ -94,10 +94,10 @@ The Cargo [manifest file](https://doc.rust-lang.org/cargo/reference/manifest.htm

This file.

{{#if package.isLibrary}}
{{#if packageSpec}}
### lib/

{{#if package.lang.isTypeScript}}
{{#eq packageSpec.library.lang compare="ts"}}
The directory containing the generated output from [tsc](https://typescriptlang.org).

### ts/
Expand All @@ -111,18 +111,19 @@ Entry point for when this library is loaded via [ESM `import`](https://nodejs.or
### ts/index.cts

Entry point for when this library is loaded via [CJS `require`](https://nodejs.org/api/modules.html#requireid).

{{else}}
The directory containing the JavaScript source files.

{{/if}}
{{/eq}}
### platforms/

The directory containing distributions of the binary addon backend for each platform supported by this library.

{{else}}
### index.node

The Node addon—i.e., a binary Node module—generated by building the project. This is the main module for this package, as dictated by the `"main"` key in `package.json`.
The binary module (aka Node addon) generated by building the project. This is the main module for this package, as dictated by the `"main"` key in `package.json`.

Under the hood, a [Node addon](https://nodejs.org/api/addons.html) is a [dynamically-linked shared object](https://en.wikipedia.org/wiki/Library_(computing)#Shared_libraries). The `"build"` script produces this file by copying it from within the `target/` directory, which is where the Rust build produces the shared object.

Expand Down
4 changes: 4 additions & 0 deletions pkgs/create-neon/data/templates/ci/github/.env.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NODE_VERSION=18.x
NPM_REGISTRY=https://registry.npmjs.org
ACTIONS_USER=github-actions
ACTIONS_EMAIL=github-actions@github.com
124 changes: 124 additions & 0 deletions pkgs/create-neon/data/templates/ci/github/build.yml.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
name: Build

on:
workflow_call:
inputs:
branch:
description: 'Branch to check out'
required: false
type: string
default: {{#$}} github.event.repository.default_branch {{/$}}
update-version:
description: 'Update version before building?'
required: false
type: boolean
default: false
version:
description: 'Version update (ignored if update-version if false)'
required: false
type: string
default: 'patch'
github-release:
description: 'Publish GitHub release?'
required: false
type: boolean
default: false

jobs:
matrix:
name: Matrix
runs-on: ubuntu-latest
outputs:
matrix: {{#$}} steps.matrix.outputs.result {{/$}}
steps:
- name: Checkout Code
uses: actions/checkout@{{versions.actions.checkout}}
with:
token: {{#$}} secrets.TAG_TOKEN || github.token {{/$}}
ref: {{#$}} inputs.branch {{/$}}
- name: Set Environment Variables
uses: falti/dotenv-action@{{versions.actions.dotenv}}
with:
path: ./.github/workflows/.env
export-variables: true
keys-case: bypass
- name: Install Node
uses: actions/setup-node@{{versions.actions.setupNode}}
with:
node-version: {{#$}} env.NODE_VERSION {{/$}}
registry-url: {{#$}} env.NPM_REGISTRY {{/$}}
cache: npm
- name: Install Dependencies
shell: bash
run: npm ci
- name: Install cargo-messages
shell: bash
run: npm ci
working-directory: ./pkgs/cargo-messages
- name: Look Up Matrix Data
id: matrixData
shell: bash
run: |
echo "json=$(node ../../dist/cli ci github | jq -rc)" >> "$GITHUB_OUTPUT"
working-directory: ./pkgs/cargo-messages
- name: Compute Matrix
id: matrix
uses: actions/github-script@{{versions.actions.githubScript}}
with:
script: |
const platforms = {{#$}} steps.matrixData.outputs.json {{/$}};
const macOS = platforms.macOS.map(platform => {
return { os: "macos-latest", platform, cross: false };
});
const windows = platforms.Windows.map(platform => {
return { os: "windows-latest", platform, cross: false };
});
const linux = platforms.Linux.map(platform => {
return { os: "ubuntu-latest", platform, cross: true };
});
return [...macOS, ...windows, ...linux];

binaries:
name: Binaries
needs: [matrix]
strategy:
matrix:
cfg: {{#$}} fromJSON(needs.matrix.outputs.matrix) {{/$}}
runs-on: {{#$}} matrix.cfg.os {{/$}}
permissions:
contents: write
steps:
- name: Checkout Code
uses: actions/checkout@{{versions.actions.checkout}}
with:
fetch-depth: 0
- name: Set Environment Variables
uses: falti/dotenv-action@{{versions.actions.dotenv}}
with:
path: ./.github/workflows/.env
export-variables: true
keys-case: bypass
- name: Diagnostics
shell: bash
run: git log -10
- name: Install Dependencies
shell: bash
run: npm ci
- name: Update Version
if: {{#$}} inputs.update-version {{/$}}
shell: bash
run: |
git config --global user.name $ACTIONS_USER
git config --global user.email $ACTIONS_EMAIL
npm version {{#$}} inputs.version {{/$}} -m "v%s"
- name: Check Versions
shell: bash
run: ./test/lint/check-versions.sh
- name: Build
uses: neon-actions/build@{{versions.actions.neonBuild}}
with:
working-directory: ./pkgs/cargo-messages
node-version: {{#$}} env.NODE_VERSION {{/$}}
use-cross: {{#$}} !!matrix.cfg.cross {{/$}}
platform: {{#$}} matrix.cfg.platform {{/$}}
github-release: {{#$}} inputs.github-release {{/$}}
40 changes: 40 additions & 0 deletions pkgs/create-neon/data/templates/ci/github/comments.yml.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Comments

run-name: |
{{#$}} (github.event.issue.pull_request
&& contains(github.event.comment.body, '/build-all')
&& format('Full build (PR #{0})', github.event.issue.number))
|| 'Issue comment (no action)' {{/$}}

on:
issue_comment:
types: [created]

jobs:
setup:
name: Setup
runs-on: ubuntu-latest
if: {{#$}} github.event.issue.pull_request && contains(github.event.comment.body, '/build-all') {{/$}}
outputs:
branch: {{#$}} steps.pr-ref.outputs.branch || github.event.repository.default_branch {{/$}}
steps:
- name: PR Branch
if: {{#$}} github.event.issue.pull_request {{/$}}
id: pr-ref
shell: bash
run: echo "branch=$(gh pr view $PR_NO --repo $REPO --json headRefName --jq '.headRefName')" >> "$GITHUB_OUTPUT"
env:
REPO: {{#$}} github.repository {{/$}}
PR_NO: {{#$}} github.event.issue.number {{/$}}
GITHUB_TOKEN: {{#$}} secrets.GITHUB_TOKEN {{/$}}

build-all:
name: Build
needs: [setup]
permissions:
contents: write
uses: ./.github/workflows/build.yml
with:
branch: {{#$}} needs.setup.outputs.branch {{/$}}
update-version: true
github-release: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tag": "npm version -m 'v%s'",
"posttag": "git push --follow-tags",
"dryrun": "gh workflow run publish.yml -f dryrun=true"
}
Loading

0 comments on commit 0a27050

Please sign in to comment.