Skip to content

Commit

Permalink
Distribute the CLI over NPM
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
  • Loading branch information
jviotti committed Jun 5, 2024
1 parent 65f9149 commit 89adeb1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
85 changes: 85 additions & 0 deletions npm-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/sh

set -o errexit
set -o nounset

if [ $# -lt 1 ]
then
echo "Usage: $0 <version>" 1>&2
exit 1
fi

OUTPUT="$(pwd)/build"
VERSION="$1"

# (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 <juan@intelligence.ai>",
"keywords": [ "jsonschema", "json" ],
"bugs": {
"url": "https://github.com/intelligence-ai/jsonschema/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/intelligence-ai/jsonschema.git"
}
}
EOF

cat << 'EOF' > "$OUTPUT/npm/staging/cli.js"
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);
}
const result = child_process.spawnSync(EXECUTABLE,
process.argv.slice(2), { stdio: 'inherit' });
process.exit(result.status);
EOF

0 comments on commit 89adeb1

Please sign in to comment.