From f484b18b0c75ecb17b7e29b765cb23476f9ab874 Mon Sep 17 00:00:00 2001 From: Wilson Lin Date: Mon, 20 Jan 2020 00:23:57 +1100 Subject: [PATCH] Create TS typings and bundle with native modules for Node.js; directly publish Ruby and Node.js from workflows --- .github/workflows/nodejs.yml | 37 ++++++++++++++---- .github/workflows/ruby.yml | 16 ++++---- nodejs/.gitignore | 9 +++-- nodejs/install.js | 74 ------------------------------------ nodejs/lib/index.js | 14 ------- nodejs/package.json | 15 ++++---- nodejs/src/index.ts | 39 +++++++++++++++++++ nodejs/tsconfig.json | 32 ++++++++++++++++ 8 files changed, 119 insertions(+), 117 deletions(-) delete mode 100644 nodejs/install.js delete mode 100644 nodejs/lib/index.js create mode 100644 nodejs/src/index.ts create mode 100644 nodejs/tsconfig.json diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index fa478ed..49df72b 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -6,7 +6,7 @@ on: - 'v*' jobs: - nodejs: + build: runs-on: ${{ matrix.os }} strategy: matrix: @@ -26,10 +26,6 @@ jobs: id: version shell: bash run: echo ::set-output name=VERSION::${GITHUB_REF#refs/tags/v} - - name: Get file name - id: file - shell: bash - run: echo ::set-output name=FILE::${{ steps.version.outputs.VERSION }}-${{ matrix.ARCH }}-node${{ matrix.node }}.node - name: Set up Node.js uses: actions/setup-node@master with: @@ -43,12 +39,37 @@ jobs: - name: Build native module working-directory: ./nodejs run: | - npm install -g neon-cli - neon build --release + npm install + node node_modules/neon-cli/bin/cli.js build --release - uses: chrislennon/action-aws-cli@v1.1 - name: Upload to S3 env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} AWS_REGION: us-west-2 - run: aws s3 cp ./nodejs/native/index.node s3://${{ secrets.AWS_S3_BUCKET }}/hyperbuild/bin/${{ steps.file.outputs.FILE }} + run: aws s3 cp ./nodejs/native/index.node s3://${{ secrets.AWS_S3_BUCKET }}/hyperbuild/bin/nodejs/${{ steps.version.outputs.VERSION }}/${{ matrix.ARCH }}-node${{ matrix.node }}.node + package: + runs-on: ubuntu-latest + needs: build + steps: + - uses: actions/checkout@v1 + - name: Get version + id: version + shell: bash + run: echo ::set-output name=VERSION::${GITHUB_REF#refs/tags/v} + - name: Set up Node.js + uses: actions/setup-node@master + with: + node-version: 13.x + - uses: chrislennon/action-aws-cli@v1.1 + - name: Download built modules + run: | + - name: Pack and publish package + working-directory: ./nodejs + run: | + echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_AUTH_TOKEN }}" > "$HOME/.npmrc" + rm -rf dist + npm install + node node_modules/typescript/bin/tsc + aws s3 mv s3://${{ secrets.AWS_S3_BUCKET }}/hyperbuild/bin/nodejs/${{ steps.version.outputs.VERSION }}/ ./dist/. --recursive + npm publish diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 7b7e570..1df5764 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -74,13 +74,11 @@ jobs: mv "$f.nativelib" "$f" fi done - - name: Package gem + - name: Package and publish gem working-directory: ./ruby - run: gem build hyperbuild.gemspec - - uses: chrislennon/action-aws-cli@v1.1 - - name: Upload to S3 - env: - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - AWS_REGION: us-west-2 - run: aws s3 cp ./ruby/hyperbuild-${{ steps.version.outputs.VERSION }}.gem s3://${{ secrets.AWS_S3_BUCKET }}/hyperbuild/bin/${{ steps.version.outputs.VERSION }}.gem + run: | + gem build hyperbuild.gemspec + echo '---' > "$HOME/.gem/credentials" + echo ":rubygems_api_key: ${{ secrets.RUBYGEMS_API_KEY }}" >> "$HOME/.gem/credentials" + chmod 0600 "$HOME/.gem/credentials" + gem push hyperbuild-${{ steps.version.outputs.VERSION }}.gem diff --git a/nodejs/.gitignore b/nodejs/.gitignore index 2e0e748..0a4d628 100644 --- a/nodejs/.gitignore +++ b/nodejs/.gitignore @@ -1,5 +1,6 @@ -native/target -native/index.node -native/artifacts.json -native/Cargo.lock +/dist/* +/native/target +/native/index.node +/native/artifacts.json +/native/Cargo.lock node_modules/ diff --git a/nodejs/install.js b/nodejs/install.js deleted file mode 100644 index 22a07ea..0000000 --- a/nodejs/install.js +++ /dev/null @@ -1,74 +0,0 @@ -const childProcess = require('child_process'); -const fs = require('fs'); -const pack = require('./package'); -const path = require('path'); -const request = require('request'); - -console.log(`Installing Node.js hyperbuild ${pack.version}...`); - -const build = () => { - console.log(`Building from source...`); - const {status, signal, error} = childProcess.spawnSync(`neon`, [`build`, `--release`], { - stdio: ['ignore', 'inherit', 'inherit'], - encoding: 'utf8', - }); - if (error) { - throw error; - } - if (signal) { - throw new Error(`Build exited with signal ${signal}`); - } - if (status !== 0) { - throw new Error(`Build exited with status ${status}`); - } -}; - -const binaryPath = path.join(__dirname, "native", "index.node"); -const binaryName = [ - pack.version, - '-', - [process.platform, { - darwin: 'macos', - linux: 'linux', - win32: 'windows', - }], - '-', - [process.arch, { - x64: 'x86_64', - }], - '-node', - [process.versions.modules, { - // Sourced from https://nodejs.org/en/download/releases/. - 57: 8, - 59: 9, - 64: 10, - 67: 11, - 72: 12, - 79: 13, - }], - '.node', -].map(p => typeof p == 'string' - ? p - : p[1][p[0]] -).join(''); -const binaryUrl = `https://wilsonl.in/hyperbuild/bin/${binaryName}`; - -if (process.env.HYPERBUILD_NODEJS_SKIP_BIN_DOWNLOAD) { - console.log(`Skipping download of prebuilt native module binary`); - build(); -} else { - console.log(`Fetching ${binaryUrl}...`); - request(binaryUrl) - .on('response', res => { - if (res.statusCode !== 200) { - console.error(`Failed to download prebuilt native module with status ${res.statusCode}`); - build(); - } - }) - .on('error', err => { - console.error(err); - console.error(`Could not download prebuilt native module`); - build(); - }) - .pipe(fs.createWriteStream(binaryPath)); -} diff --git a/nodejs/lib/index.js b/nodejs/lib/index.js deleted file mode 100644 index c152c50..0000000 --- a/nodejs/lib/index.js +++ /dev/null @@ -1,14 +0,0 @@ -const hyperbuild = require('../native'); - -module.exports = { - minify: code => { - const buf = Buffer.from(code); - const len = hyperbuild.minify(buf); - return buf.slice(0, len).toString(); - }, - minify_in_place: buf => { - const len = hyperbuild.minify(buf); - // This does not do a copy. - return buf.slice(0, len); - }, -}; diff --git a/nodejs/package.json b/nodejs/package.json index dca1c49..7b37050 100644 --- a/nodejs/package.json +++ b/nodejs/package.json @@ -2,7 +2,10 @@ "name": "hyperbuild", "version": "0.0.25", "description": "Fast one-pass in-place HTML minifier written in Rust with context-aware whitespace handling", - "main": "lib/index.js", + "main": "dist/index.js", + "files": [ + "dist/**" + ], "repository": { "type": "git", "url": "git+https://github.com/wilsonzlin/hyperbuild.git" @@ -20,14 +23,10 @@ "url": "https://github.com/wilsonzlin/hyperbuild/issues" }, "homepage": "https://github.com/wilsonzlin/hyperbuild#readme", - "dependencies": { + "devDependencies": { + "@types/node": "^13.1.8", "neon-cli": "^0.3.3", - "request": "^2.88.0" - }, - "scripts": { - "install": "node install.js", - "prepack": "cp ../README.md .", - "postpack": "rm README.md" + "typescript": "^3.7.5" }, "keywords": [ "build", diff --git a/nodejs/src/index.ts b/nodejs/src/index.ts new file mode 100644 index 0000000..da07e85 --- /dev/null +++ b/nodejs/src/index.ts @@ -0,0 +1,39 @@ +const binaryName = [ + [process.platform, { + darwin: 'macos', + linux: 'linux', + win32: 'windows', + }], + '-', + [process.arch, { + x64: 'x86_64', + }], + '-node', + [process.versions.modules, { + // Sourced from https://nodejs.org/en/download/releases/. + 57: 8, + 59: 9, + 64: 10, + 67: 11, + 72: 12, + 79: 13, + }], + '.node', +].map(p => typeof p == 'string' + ? p + : p[1][p[0] as string] +).join(''); + +const hyperbuild = require(`./${binaryName}`); + +export const minify = (code: string): string => { + const buf = Buffer.from(code); + const len = hyperbuild.minify(buf); + return buf.slice(0, len).toString(); +}; + +export const minifyInPlace = (buf: Buffer): Buffer => { + const len = hyperbuild.minify(buf); + // This does not do a copy. + return buf.slice(0, len); +}; diff --git a/nodejs/tsconfig.json b/nodejs/tsconfig.json new file mode 100644 index 0000000..45a9c92 --- /dev/null +++ b/nodejs/tsconfig.json @@ -0,0 +1,32 @@ +{ + "include": [ + "src/**/*.ts" + ], + "compilerOptions": { + "allowJs": false, + "alwaysStrict": true, + "declaration": true, + "esModuleInterop": true, + "lib": [ + "es5", + "es6", + "es7", + "es2017" + ], + "module": "commonjs", + "noFallthroughCasesInSwitch": true, + "noImplicitAny": true, + "noImplicitReturns": true, + "noImplicitThis": true, + "noUnusedParameters": true, + "outDir": "dist", + "skipLibCheck": true, + "strict": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "strictPropertyInitialization": true, + "suppressImplicitAnyIndexErrors": true, + "target": "es6" + } +} +