diff --git a/.github/workflows/publish-npm.yml b/.github/workflows/publish-npm.yml new file mode 100644 index 000000000..9ea234082 --- /dev/null +++ b/.github/workflows/publish-npm.yml @@ -0,0 +1,26 @@ +name: Publish npm package + +on: + release: + types: [published] + +permissions: + contents: read + id-token: write + +jobs: + publish: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: 22 + registry-url: https://registry.npmjs.org + + - name: Publish to npm + run: npm publish --provenance \ No newline at end of file diff --git a/install.js b/install.js new file mode 100644 index 000000000..eea8523e3 --- /dev/null +++ b/install.js @@ -0,0 +1,61 @@ +#!/usr/bin/env node + +// For NPM publishing purposes + +const fs = require("fs"); +const path = require("path"); + +const version = require("./package.json").version; + +const platform = process.platform; +const arch = process.arch; + +function getAssetName() { + if (platform === "linux" && arch === "x64") { + return `noir-v${version}-linux-x86_64`; + } + + if (platform === "linux" && arch === "arm64") { + return `noir-v${version}-linux-arm64`; + } + + if (platform === "darwin" && arch === "arm64") { + return `noir-v${version}-osx-arm64`; + } + + if (platform === "darwin" && arch === "x64") { + return `noir-v${version}-osx-x86_64`; + } + + throw new Error(`Unsupported platform: ${platform} ${arch}`); +} + +(async () => { + const asset = getAssetName(); + + const downloadUrl = + `https://github.com/owasp-noir/noir/releases/download/v${version}/${asset}`; + + const binDir = path.join(__dirname, "bin"); + const output = path.join(binDir, "noir"); + + fs.mkdirSync(binDir, { recursive: true }); + + console.log(`Downloading ${asset}...`); + + const response = await fetch(downloadUrl); + + if (!response.ok) { + throw new Error(`Download failed (${response.status})`); + } + + const buffer = Buffer.from(await response.arrayBuffer()); + + fs.writeFileSync(output, buffer); + fs.chmodSync(output, 0o755); + + console.log("Binary installed."); +})().catch((err) => { + console.error(err.message); + process.exit(1); +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 000000000..28d04fce8 --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "@owasp-noir/noir", + "version": "1.1.0", + "description": "OWASP Noir CLI", + "license": "MIT", + "bin": { + "noir": "./bin/noir" + }, + "scripts": { + "postinstall": "node install.js" + }, + "files": [ + "install.js" + ], + "engines": { + "node": ">=18" + } +} \ No newline at end of file