Skip to content

Commit 2d251be

Browse files
committed
Take NPM packaging logic out of npm-deploy.sh
Signed-off-by: Juan Cruz Viotti <[email protected]>
1 parent 251fa7b commit 2d251be

File tree

11 files changed

+1298
-168
lines changed

11 files changed

+1298
-168
lines changed

.github/workflows/package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ jobs:
191191
node-version: '22.x'
192192
registry-url: 'https://registry.npmjs.org'
193193
- name: Publish to NPM
194-
run: sudo npm install --global npm@latest && ./npm-deploy.sh ${{ github.ref_name }}
194+
run: sudo npm install --global npm@latest && ./npm-pack.sh ./build/npm/dist && npm publish ./build/npm/dist
195195

196196
publish-pypi:
197197
needs: publish

.github/workflows/test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ jobs:
115115
- run: ./test/docker/help.sh
116116
- run: ./test/docker/bundle_fmt.sh
117117

118+
npm:
119+
runs-on: ubuntu-latest
120+
steps:
121+
- uses: actions/checkout@v4
122+
- run: ./npm-pack.sh ./build/npm/dist
123+
118124
snap:
119125
strategy:
120126
fail-fast: false

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ _deps
1212
Brewfile.lock.json
1313
.DS_Store
1414
*.snap
15+
/node_modules

.npmignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*
2+
!LICENSE
3+
!README.markdown
4+
!package.json
5+
!package-lock.json
6+
!*.js
7+
!*.mjs
8+
!build/github-releases/*

cli.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env node
2+
const os = require('os');
3+
const path = require('path');
4+
const fs = require('fs');
5+
const child_process = require('child_process');
6+
7+
const PLATFORM = os.platform() === 'win32' ? 'windows' : os.platform();
8+
const ARCH = os.arch() === 'x64' ? 'x86_64' : os.arch();
9+
const EXECUTABLE = PLATFORM === 'windows'
10+
? path.join(__dirname, 'build', 'github-releases', `jsonschema-${PLATFORM}-${ARCH}.exe`)
11+
: path.join(__dirname, 'build', 'github-releases', `jsonschema-${PLATFORM}-${ARCH}`);
12+
13+
if (!fs.existsSync(EXECUTABLE)) {
14+
console.error(`The JSON Schema CLI NPM package does not support ${os.platform()} for ${ARCH} yet`);
15+
console.error('Please open a GitHub issue at https://github.com/sourcemeta/jsonschema');
16+
process.exit(1);
17+
}
18+
19+
if (PLATFORM === 'darwin') {
20+
child_process.spawnSync('/usr/bin/xattr', [ '-c', EXECUTABLE ], { stdio: 'inherit' });
21+
}
22+
23+
const result = child_process.spawnSync(EXECUTABLE, process.argv.slice(2), {
24+
stdio: 'inherit',
25+
// Do not open a command prompt on spawning
26+
windowsHide: true
27+
});
28+
29+
process.exit(result.status);

eslint.config.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defineConfig } from "eslint/config";
2+
import globals from "globals";
3+
import js from "@eslint/js";
4+
5+
export default defineConfig([
6+
{ files: ["**/*.{js,mjs,cjs}"] },
7+
{ files: ["**/*.js"], languageOptions: { sourceType: "commonjs" } },
8+
{ files: ["**/*.{js,mjs,cjs}"], languageOptions: { globals: globals.node } },
9+
{ files: ["**/*.{js,mjs,cjs}"], plugins: { js }, extends: ["js/recommended"] },
10+
]);

npm-deploy.sh

Lines changed: 0 additions & 166 deletions
This file was deleted.

npm-pack.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
if [ $# -lt 1 ]
7+
then
8+
echo "Usage: $0 <directory>" 1>&2
9+
exit 1
10+
fi
11+
12+
VERSION="$(jq '.version' --raw-output package.json | tr -d 'v')"
13+
DIRECTORY="$1"
14+
OUTPUT="$(pwd)/build"
15+
16+
# (1) Download artifacts
17+
mkdir -p "$OUTPUT/npm/artifacts"
18+
echo "Preparing $VERSION" 1>&2
19+
PACKAGE_BASE_URL="https://github.com/sourcemeta/jsonschema/releases/download/v$VERSION"
20+
curl --retry 5 --location --output "$OUTPUT/npm/artifacts/darwin-arm64.zip" \
21+
"$PACKAGE_BASE_URL/jsonschema-$VERSION-darwin-arm64.zip"
22+
curl --retry 5 --location --output "$OUTPUT/npm/artifacts/darwin-x86_64.zip" \
23+
"$PACKAGE_BASE_URL/jsonschema-$VERSION-darwin-x86_64.zip"
24+
curl --retry 5 --location --output "$OUTPUT/npm/artifacts/linux-x86_64.zip" \
25+
"$PACKAGE_BASE_URL/jsonschema-$VERSION-linux-x86_64.zip"
26+
curl --retry 5 --location --output "$OUTPUT/npm/artifacts/linux-arm64.zip" \
27+
"$PACKAGE_BASE_URL/jsonschema-$VERSION-linux-arm64.zip"
28+
curl --retry 5 --location --output "$OUTPUT/npm/artifacts/windows-x86_64.zip" \
29+
"$PACKAGE_BASE_URL/jsonschema-$VERSION-windows-x86_64.zip"
30+
unzip -o "$OUTPUT/npm/artifacts/darwin-arm64.zip" -d "$OUTPUT/npm/artifacts"
31+
unzip -o "$OUTPUT/npm/artifacts/darwin-x86_64.zip" -d "$OUTPUT/npm/artifacts"
32+
unzip -o "$OUTPUT/npm/artifacts/linux-x86_64.zip" -d "$OUTPUT/npm/artifacts"
33+
unzip -o "$OUTPUT/npm/artifacts/linux-arm64.zip" -d "$OUTPUT/npm/artifacts"
34+
unzip -o "$OUTPUT/npm/artifacts/windows-x86_64.zip" -d "$OUTPUT/npm/artifacts"
35+
ls -l "$OUTPUT/npm/artifacts"
36+
37+
# (2) Stage package contents
38+
rm -rf "$OUTPUT/github-releases"
39+
mkdir -p "$OUTPUT/github-releases"
40+
41+
install -m 0755 "$OUTPUT/npm/artifacts/jsonschema-$VERSION-darwin-arm64/bin/jsonschema" \
42+
"$OUTPUT/github-releases/jsonschema-darwin-arm64"
43+
install -m 0755 "$OUTPUT/npm/artifacts/jsonschema-$VERSION-darwin-x86_64/bin/jsonschema" \
44+
"$OUTPUT/github-releases/jsonschema-darwin-x86_64"
45+
install -m 0755 "$OUTPUT/npm/artifacts/jsonschema-$VERSION-linux-x86_64/bin/jsonschema" \
46+
"$OUTPUT/github-releases/jsonschema-linux-x86_64"
47+
install -m 0755 "$OUTPUT/npm/artifacts/jsonschema-$VERSION-linux-arm64/bin/jsonschema" \
48+
"$OUTPUT/github-releases/jsonschema-linux-arm64"
49+
install -m 0755 "$OUTPUT/npm/artifacts/jsonschema-$VERSION-windows-x86_64/bin/jsonschema.exe" \
50+
"$OUTPUT/github-releases/jsonschema-windows-x86_64.exe"
51+
52+
# (3) Run checks
53+
npm ci
54+
npm test
55+
node cli.js
56+
57+
# (4) Package
58+
mkdir -p "$DIRECTORY"
59+
npm pack --pack-destination "$DIRECTORY"

0 commit comments

Comments
 (0)