From 40d0b5c2df4e4e6edf6f4e655b201d38a370b962 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 5 Jun 2024 14:36:27 -0400 Subject: [PATCH] Distribute the CLI over NPM Signed-off-by: Juan Cruz Viotti --- .github/workflows/package.yml | 6 ++ CMakeLists.txt | 4 +- README.markdown | 6 ++ npm-deploy.sh | 101 ++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 2 deletions(-) create mode 100755 npm-deploy.sh diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index 394b8a0d..45289f8b 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -125,3 +125,9 @@ jobs: if: github.ref_type == 'tag' env: GH_TOKEN: ${{ github.token }} + + - name: Publish to NPM + run: ./npm-deploy.sh ${{ github.ref_name }} + if: github.ref_type == 'tag' + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/CMakeLists.txt b/CMakeLists.txt index bfb76e48..6eef8d65 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,7 @@ noa_target_clang_format(SOURCES noa_target_clang_tidy(SOURCES src/*.h src/*.cc) noa_target_shellcheck(SOURCES - test/*.sh install) + test/*.sh install *.sh) # Testing if(JSONSCHEMA_TESTS) @@ -32,7 +32,7 @@ string(FIND "${ACTION_YML}" "${PROJECT_VERSION}" ACTION_YML_HAS_VERSION) if(${ACTION_YML_HAS_VERSION} EQUAL -1) message(FATAL_ERROR "The GitHub Action definition must set the correct version: ${PROJECT_VERSION}") -endif () +endif() # Packaging find_program(GIT_BIN NAMES git) diff --git a/README.markdown b/README.markdown index 9b232c4a..7dcfdaf1 100644 --- a/README.markdown +++ b/README.markdown @@ -52,6 +52,12 @@ Where `X.Y.Z` is replaced with the desired version. For example: - run: jsonschema fmt path/to/schemas --check ``` +### From NPM + +```sh +npm install --global @intelligence-ai/jsonschema +``` + ### From GitHub Releases We publish precompiled binaries for every supported platforms to [GitHub diff --git a/npm-deploy.sh b/npm-deploy.sh new file mode 100755 index 00000000..a6e4e936 --- /dev/null +++ b/npm-deploy.sh @@ -0,0 +1,101 @@ +#!/bin/sh + +set -o errexit +set -o nounset + +if [ $# -lt 1 ] +then + echo "Usage: $0 " 1>&2 + exit 1 +fi + +OUTPUT="$(pwd)/build" +VERSION="$(echo "$1" | tr -d 'v')" + +# (1) Download artifacts +mkdir -p "$OUTPUT/npm/artifacts" +echo "Preparing $VERSION" 1>&2 +PACKAGE_BASE_URL="https://github.com/intelligence-ai/jsonschema/releases/download/v$VERSION" +curl --retry 5 --location --output "$OUTPUT/npm/artifacts/darwin-arm64.zip" \ + "$PACKAGE_BASE_URL/jsonschema-$VERSION-darwin-arm64.zip" +curl --retry 5 --location --output "$OUTPUT/npm/artifacts/linux-x86_64.zip" \ + "$PACKAGE_BASE_URL/jsonschema-$VERSION-linux-x86_64.zip" +curl --retry 5 --location --output "$OUTPUT/npm/artifacts/windows-x86_64.zip" \ + "$PACKAGE_BASE_URL/jsonschema-$VERSION-windows-x86_64.zip" +unzip -o "$OUTPUT/npm/artifacts/darwin-arm64.zip" -d "$OUTPUT/npm/artifacts" +unzip -o "$OUTPUT/npm/artifacts/linux-x86_64.zip" -d "$OUTPUT/npm/artifacts" +unzip -o "$OUTPUT/npm/artifacts/windows-x86_64.zip" -d "$OUTPUT/npm/artifacts" +ls -l "$OUTPUT/npm/artifacts" + +# (2) Stage package contents +rm -rf "$OUTPUT/npm/staging" +mkdir -p "$OUTPUT/npm/staging" + +install -m 0755 "$OUTPUT/npm/artifacts/jsonschema-$VERSION-darwin-arm64/bin/jsonschema" \ + "$OUTPUT/npm/staging/jsonschema-darwin-arm64" +install -m 0755 "$OUTPUT/npm/artifacts/jsonschema-$VERSION-linux-x86_64/bin/jsonschema" \ + "$OUTPUT/npm/staging/jsonschema-linux-x86_64" +install -m 0755 "$OUTPUT/npm/artifacts/jsonschema-$VERSION-windows-x86_64/bin/jsonschema.exe" \ + "$OUTPUT/npm/staging/jsonschema-windows-x86_64.exe" + +cat << EOF > "$OUTPUT/npm/staging/package.json" +{ + "name": "@intelligence-ai/jsonschema", + "version": "$VERSION", + "description": "The CLI for working with JSON Schema. Covers formatting, linting, testing, and much more for both local development and CI/CD pipelines", + "main": "cli.js", + "bin": { + "jsonschema": "cli.js" + }, + "license": "AGPL-3.0", + "homepage": "https://github.com/intelligence-ai/jsonschema", + "author": "Juan Cruz Viotti ", + "keywords": [ "jsonschema", "json" ], + "bugs": { + "url": "https://github.com/intelligence-ai/jsonschema/issues" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/intelligence-ai/jsonschema.git" + }, + "publishConfig": { + "access": "public" + } +} +EOF + +cat << 'EOF' > "$OUTPUT/npm/staging/cli.js" +#!/usr/bin/env node +const os = require('os'); +const path = require('path'); +const fs = require('fs'); +const child_process = require('child_process'); + +const PLATFORM = os.platform() === 'win32' ? 'windows' : os.platform(); +const ARCH = os.arch() === 'x64' ? 'x86_64' : os.arch(); +const EXECUTABLE = PLATFORM === 'windows' + ? path.join(__dirname, `jsonschema-${PLATFORM}-${ARCH}.exe`) + : path.join(__dirname, `jsonschema-${PLATFORM}-${ARCH}`); + +if (!fs.existsSync(EXECUTABLE)) { + console.error(`The JSON Schema CLI NPM package does not support ${os.platform()} for ${ARCH} yet`); + console.error('Please open a GitHub issue at https://github.com/Intelligence-AI/jsonschema'); + process.exit(1); +} + +if (PLATFORM === 'darwin') { + child_process.spawnSync('/usr/bin/xattr', [ '-c', EXECUTABLE ], { stdio: 'inherit' }); +} + +const result = child_process.spawnSync(EXECUTABLE, + process.argv.slice(2), { stdio: 'inherit' }); +process.exit(result.status); +EOF + +# (3) Try packaging +cd "$OUTPUT/npm" +npm pack ./staging +cd - + +# (4) Deploy to NPM +npm publish "$OUTPUT/npm/staging"