Skip to content

Commit

Permalink
Add NPM distribution (#563)
Browse files Browse the repository at this point in the history
* Add missing tool in contribution guidelines
* First cut of NPM distribution
* Include 386 arch builds. Switch to a specific file list instead of just copying anything that matches the platform.
* docs and changelog
* Moved package.json into the top level so it's easier to include the readme and license
* Some defensive techniques, the bin reference is now always there and simply replaced by postinstall
* Switched from postinstall to JS wrapper around the binary
  • Loading branch information
TheSpyder authored Oct 27, 2023
1 parent f8b2ab0 commit 0b09647
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/unreleased/Added-20231025-214101.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: Added
body: 'Changie is now available as an NPM package: https://www.npmjs.com/package/changie'
time: 2023-10-25T21:41:01.142107+11:00
custom:
Issue: "561"
3 changes: 3 additions & 0 deletions .changie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ replacements:
- path: 'docs/static/version.json'
find: ' "latest": ".*"'
replace: ' "latest": "{{.Version}}"'
- path: npm/package.json
find: ' "version": ".*",'
replace: ' "version": "{{.VersionNoPrefix}}",'
13 changes: 13 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ jobs:
with:
go-version: 1.21

- name: Setup NPM
uses: actions/setup-node@v3
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'

- name: Login to GitHub Container Registry
uses: docker/login-action@v3.0.0
with:
Expand All @@ -37,6 +43,13 @@ jobs:
args: --clean --release-notes=${{ env.RELEASE_NOTES_PATH }} --skip=validate --verbose
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Publish to NPM
run: |
node npm/prepare-release.mjs
npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Setup Hugo
uses: peaceiris/actions-hugo@v2.6.0
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ docs/content/config/_index.md

# goreleaser builds
dist/

# NPM publishing
npm/dist/
changie-*.tgz
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ If you do decide to contribute code or documentation you will need the following
* Newer versions should also work
* [Hugo](https://gohugo.io/): for documentation, be sure to install hugo-extended
* [Golang CI](https://golangci-lint.run/): for linting
* [goimports](https://pkg.go.dev/golang.org/x/tools/cmd/goimports): for linting

### Fork and create a branch
If there is an issue you want to propose changes for start by [creating a fork](https://help.github.com/articles/fork-a-repo).
Expand Down
7 changes: 7 additions & 0 deletions docs/content/guide/installation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ docker run \
ghcr.io/miniscruff/changie \
new
```

## NodeJS

* To add as a dependency of your project: `npm i -D changie`
* To install globally: `npm i -g changie`
* To run without adding a dependency: `npx changie`

## Manual
* Download from [here](https://github.com/miniscruff/changie/releases).
* Add executable somewhere in your path depending on your platform.
Expand Down
39 changes: 39 additions & 0 deletions npm/changie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env node
const fs = require('node:fs');
const path = require('node:path');
const { spawn } = require('node:child_process')

// Platform distributed through NPM, this should mirror the package.json file list
const executables = {
'darwin-arm64': true,
'darwin-x64': true,
'linux-arm64': true,
'linux-x64': true,
'win32-ia32': true,
'win32-x64': true
};

const DIST = path.join(__dirname, 'dist');
const target = `${process.platform}-${process.arch}`

const runChangie = (filename) => {
const ext = process.platform === 'win32' ? '.exe' : '';
const executable = path.join(DIST, filename + ext);
const stat = fs.statSync(executable)
if (stat.isFile()) {
const child = spawn(executable, process.argv.slice(2));
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
child.on('close', (code) => {
process.exit(code);
});
} else {
throw new Error(`Unable to find changie ${executable} in NPM package`)
}
}

if (executables[target]) {
runChangie(target);
} else {
throw new Error(`Unsupported platform for Changie: ${target}`);
}
48 changes: 48 additions & 0 deletions npm/prepare-release.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env node
import * as fs from 'node:fs/promises';
import * as path from 'node:path';

import { execSync } from 'node:child_process';

// Mapping from to goarch to Node's `process.arch`
var ARCH_MAPPING = {
'386': 'ia32',
'amd64': 'x64',
'arm64': 'arm64',
};

// Mapping between goos and Node's `process.platform`
var PLATFORM_MAPPING = {
'darwin': 'darwin',
'linux': 'linux',
'windows': 'win32'
};

const NPM = 'npm';
const NPM_DIST = path.join(NPM, 'dist');
const RELEASES = path.join('dist', 'artifacts.json');

// read the goreleaser JSON and filter down to just the binaries
const json = JSON.parse(await fs.readFile(RELEASES));
const binaries = json.filter(r => r.type === 'Binary');

// clean up any previous runs
const output = execSync(`git clean -fdX ${NPM}`);
console.log(output.toString())

// make the dist folder
await fs.mkdir(NPM_DIST, { recursive: true })

// copy each binary into the place the NPM distribution expects it to be
await binaries.forEach(async (release) => {
const os = PLATFORM_MAPPING[release.goos];
const arch = ARCH_MAPPING[release.goarch];

// use NodeJS constants for the filename, e.g. win32-x64.exe
const distfile = `${os}-${arch}${release.extra.Ext}`;

// copy files even if we don't use them, `package.json` uses a filtered list
const target = path.join(NPM_DIST, distfile);
await fs.copyFile(release.path, target);
console.log('copied ', release.path, 'to', target);
});
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "changie",
"version": "1.14.0",
"author": "miniscruff",
"description": "Automated changelog tool for preparing releases with lots of customization options",
"bin": {
"changie": "npm/changie.js"
},
"scripts": {
"prepack": "ls `node -e 'console.log((require(\"./package.json\").files||[]).join(\" \"))'`>/dev/null"
},
"repository": {
"type": "git",
"url": "git+https://github.com/miniscruff/changie.git"
},
"keywords": [
"changelog"
],
"files": [
"npm/dist/darwin-arm64",
"npm/dist/darwin-x64",
"npm/dist/linux-arm64",
"npm/dist/linux-x64",
"npm/dist/win32-ia32.exe",
"npm/dist/win32-x64.exe",
"npm/changie.js",
"README.md",
"LICENSE"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/miniscruff/changie/issues"
},
"homepage": "https://github.com/miniscruff/changie#readme"
}

0 comments on commit 0b09647

Please sign in to comment.