Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(js): setup release flow #608

Merged
merged 13 commits into from
Nov 27, 2024
Merged
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*.ts text
*.json text
yarn.lock merge=union

osipov-mit marked this conversation as resolved.
Show resolved Hide resolved
# Verify
*.verified.txt text eol=lf working-tree-encoding=UTF-8
*.verified.xml text eol=lf working-tree-encoding=UTF-8
Expand Down
40 changes: 3 additions & 37 deletions .github/workflows/ci-js.yml → .github/workflows/js-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ on:
paths-ignore:
- 'net/**'
- 'Sails.Net.sln'
push:
branches: [master]
paths:
- js/**
workflow_dispatch:
osipov-mit marked this conversation as resolved.
Show resolved Hide resolved

env:
Expand Down Expand Up @@ -46,6 +42,9 @@ jobs:
sudo wget -c https://github.com/WebAssembly/binaryen/releases/download/$BINARYEN_VERSION/binaryen-$BINARYEN_VERSION-x86_64-linux.tar.gz -O - | sudo tar -xz -C .
sudo cp binaryen-$BINARYEN_VERSION/bin/wasm-opt /usr/bin/

- name: 'Prepare: build parser'
run: ./js/scripts/build-parser.sh

- name: 'Prepare: build all'
run: yarn build

Expand All @@ -65,36 +64,3 @@ jobs:

- name: 'Test: run'
run: yarn test

publish-to-npm:
if: github.event_name == 'push'

runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Use node 20.x
uses: actions/setup-node@v4
with:
node-version: 20.x

- name: 'Prepare: install dependencies'
run: yarn install

- name: 'Install: binaryen'
working-directory: /usr
run: |
sudo wget -c https://github.com/WebAssembly/binaryen/releases/download/$BINARYEN_VERSION/binaryen-$BINARYEN_VERSION-x86_64-linux.tar.gz -O - | sudo tar -xz -C .
sudo cp binaryen-$BINARYEN_VERSION/bin/wasm-opt /usr/bin/

- name: 'Prepare: build all'
run: yarn build

- name: Publish
run: |
export token=$(printenv npm_token)
echo "//registry.npmjs.org/:_authToken=$token" > .npmrc
npx lerna publish from-package --yes --no-private
env:
npm_token: ${{ secrets.NPM_TOKEN }}
82 changes: 82 additions & 0 deletions .github/workflows/js-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: '[js] Release'

on:
push:
branches:
- master
paths:
- package.json

env:
BUILD_MODE: release

jobs:
release:
name: Create Release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: 'Setup NodeJS 20.x'
uses: actions/setup-node@v4
with:
node-version: 20.x

- name: 'Get version from package.json'
run: |
VERSION=$(jq -r '.version' package.json)
echo "VERSION=$VERSION" >> $GITHUB_ENV
echo "TAG_NAME=Sails-JS/v$VERSION" >> $GITHUB_ENV
osipov-mit marked this conversation as resolved.
Show resolved Hide resolved

- name: 'Get previous version from npm registry'
run: |
PREVIOUS_VERSION=$(npm show @js/package version)
echo "PREVIOUS_VERSION=$PREVIOUS_VERSION" >> $GITHUB_ENV

- name: 'Compare versions'
run: |
if [ ${{ env.VERSION }} == ${{ env.PREVIOUS_VERSION }} ]; then
osipov-mit marked this conversation as resolved.
Show resolved Hide resolved
echo "No new version to release"
echo "skip=true" >> $GITHUB_ENV
else
echo "New version to release"
echo "skip=false" >> $GITHUB_ENV
fi

- name: 'Get release notes'
if: env.skip != 'true'
osipov-mit marked this conversation as resolved.
Show resolved Hide resolved
run: |
awk '/## ${{ env.VERSION }}/{flag=1;next}/---/{flag=0} flag' ./js/CHANGELOG.md >> release_notes.txt
echo "RELEASE_NOTES<<EOF" >> $GITHUB_ENV
cat release_notes.txt >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
rm release_notes.txt

- name: 'Prepare: install dependencies and build pkgs'
if: env.skip != 'true'
run: |
yarn install
yarn build

- name: 'Create new tag'
if: env.skip != 'true'
run: |
git tag ${{ env.TAG_NAME }}
git push origin ${{ env.TAG_NAME }}

- name: 'Create Release'
if: env.skip != 'true'
uses: softprops/action-gh-release@v2
with:
body: ${{ env.RELEASE_NOTES }}
tag_name: ${{ env.TAG_NAME }}

- name: 'Publish to npm registry'
if: env.skip != 'true'
run: |
export token=$(printenv npm_token)
echo "//registry.npmjs.org/:_authToken=$token" > .npmrc
npx lerna publish from-package --yes --no-private
env:
npm_token: ${{ secrets.NPM_TOKEN }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ net/**/*.trx
node_modules/
js/**/lib/
js/cli/build/
js/parser/parser.wasm
js/test/demo/
.pnp.*
.yarn/*
Expand All @@ -26,6 +25,7 @@ js/test/demo/
!.yarn/sdks
!.yarn/versions
.npmrc
js/parser/parser.wasm
osipov-mit marked this conversation as resolved.
Show resolved Hide resolved

# Verify
*.received.*
Expand Down
9 changes: 9 additions & 0 deletions js/CHANGELOG.md
osipov-mit marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# CHANGELOG

## 0.3.1 (draft)

### Compatibility
- Sails-RS v0.6.3

### Changes
- Setup automated releases in https://github.com/gear-tech/sails/pull/608
2 changes: 1 addition & 1 deletion js/cli/src/generate/types-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class TypesGenerator extends BaseGenerator {
}

public generate() {
this._out.line('declare global {').increaseIndent();
this._out.line('declare global {', false).increaseIndent();
for (const { name, def, docs } of this._program.types) {
this._out.lines(formatDocs(docs), false);

Expand Down
3 changes: 3 additions & 0 deletions js/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sails-rs": "0.6.3"
}
79 changes: 39 additions & 40 deletions js/parser/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,53 @@
import { writeFileSync, readFileSync, existsSync, rmSync } from 'fs';
import { execSync } from 'child_process';
import { writeFileSync, rmSync } from 'fs';
import commonjs from '@rollup/plugin-commonjs';
import typescript from 'rollup-plugin-typescript2';
import config from '../config.json' assert { type: 'json' };
import { readFile } from 'fs/promises';

function checkParserFile() {
return {
name: 'check-parser-file',
buildStart() {
if (!existsSync('./parser.wasm')) {
throw new Error('parser.wasm file not found');
}
},
};
async function getStreamFromRelease(version, cs) {
const link = `https://github.com/gear-tech/sails/releases/download/rs%2Fv${version}/sails_idl_parser.wasm`;
const res = await fetch(link);

if (!res.ok) {
throw new Error(`Failed to fetch parser from ${link}`);
}

return res.body.pipeThrough(cs);
}

function compressParser(type) {
return {
name: 'compress-parser',
async closeBundle() {
const buf = readFileSync('./parser.wasm');
async function getStreamFromFile(cs) {
const file = await readFile('./parser.wasm');
return new Response(file).body.pipeThrough(cs);
}

const cs = new CompressionStream('gzip');
async function getBase64Parser(version) {
const cs = new CompressionStream('gzip');

const compressedReadableStream = new Response(buf).body.pipeThrough(cs);
const stream =
process.env.BUILD_MODE?.toLowerCase() === 'release'
? await getStreamFromRelease(version, cs)
: await getStreamFromFile(cs);

const reader = compressedReadableStream.getReader();
const reader = stream.getReader();

let resultArr = [];
let resultArr = [];

while (true) {
const read = await reader.read();
while (true) {
const read = await reader.read();

if (read.done) break;
if (read.done) break;

resultArr = resultArr.concat(Array.from(read.value));
}
resultArr = resultArr.concat(Array.from(read.value));
}

const base64Bytes = Buffer.from(Uint8Array.from(resultArr).buffer).toString('base64');
return Buffer.from(Uint8Array.from(resultArr).buffer).toString('base64');
}

function writeCompressedWasmParser(type) {
return {
name: 'write-wasm-parser',
async closeBundle() {
const base64Bytes = await getBase64Parser(config['sails-rs']);

if (type === 'cjs') {
writeFileSync(
Expand All @@ -59,16 +70,6 @@ function cleanOldBuild() {
};
}

function buildParserWasm() {
return {
name: 'build-parser-wasm',
buildStart() {
execSync('cargo build -p sails-idl-parser --target=wasm32-unknown-unknown --release');
execSync('wasm-opt -O4 -o ./parser.wasm ../../target/wasm32-unknown-unknown/release/sails_idl_parser.wasm');
},
};
}

export default [
{
input: 'src/index.ts',
Expand All @@ -81,13 +82,11 @@ export default [
},
],
plugins: [
buildParserWasm(),
checkParserFile(),
cleanOldBuild(),
typescript({
tsconfig: 'tsconfig.build.json',
}),
compressParser('es'),
writeCompressedWasmParser('es'),
],
},
{
Expand All @@ -107,7 +106,7 @@ export default [
tsconfig: 'tsconfig.cjs.json',
}),
commonjs(),
compressParser('cjs'),
writeCompressedWasmParser('cjs'),
],
},
];
5 changes: 5 additions & 0 deletions js/scripts/build-parser.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
echo "[*] Building parser wasm"
cargo build -p sails-idl-parser --target=wasm32-unknown-unknown --release
echo "[*] Optimizing parser wasm"
wasm-opt -O4 -o ./js/parser/parser.wasm ./target/wasm32-unknown-unknown/release/sails_idl_parser.wasm
ls -l ./js/parser/parser.wasm
9 changes: 9 additions & 0 deletions js/scripts/bump-versions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version=$1

echo "Bumping versions to $version"

pkgs=("package.json" "./js/package.json" "./js/cli/package.json" "./js/parser/package.json" "./js/types/package.json" "./js/util/package.json")

for pkg in ${pkgs[@]}; do
jq ".version = \"$version\"" $pkg > tmp.$$.json && mv tmp.$$.json $pkg
done
3 changes: 2 additions & 1 deletion js/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"forceConsistentCasingInFileNames": true,
"strict": false,
"noImplicitAny": false,
"skipLibCheck": true
"skipLibCheck": true,
"resolveJsonModule": true
}
}