Skip to content

Commit

Permalink
feat(parser) move parser from sails-js to own library (#491)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitry Osipov <dmith.osipov@gmail.com>
  • Loading branch information
Zewasik and osipov-mit authored Sep 4, 2024
1 parent 15a64cd commit e88e9ed
Show file tree
Hide file tree
Showing 76 changed files with 1,823 additions and 24 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/ci-js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
push:
branches: [master]
paths:
- js/**
- js/sails/**
workflow_dispatch:

env:
Expand Down Expand Up @@ -35,7 +35,7 @@ jobs:
node-version: 20.x

- name: "Install: pkg dependencies"
working-directory: js
working-directory: js/sails
run: yarn install

- name: "Install: binaryen"
Expand All @@ -46,10 +46,10 @@ jobs:
- name: "Prepare: build parser"
run: |
cargo build -p sails-idl-parser --target=wasm32-unknown-unknown --release
wasm-opt -O4 -o ./js/parser.wasm ./target/wasm32-unknown-unknown/release/sails_idl_parser.wasm
wasm-opt -O4 -o ./js/sails/parser.wasm ./target/wasm32-unknown-unknown/release/sails_idl_parser.wasm
- name: "Prepare: build sails-js"
working-directory: js
working-directory: js/sails
run: yarn build

- name: "Prepare: build demo app"
Expand All @@ -69,13 +69,13 @@ jobs:
run: sleep 180

- name: "Prepare: generate ts lib"
working-directory: js
working-directory: js/sails
run: |
node lib/app.js generate ../examples/demo/client/demo.idl -o ./test/demo
node lib/app.js generate ../../examples/demo/client/demo.idl -o ./test/demo
node test/modify-import.js
- name: "Test: run"
working-directory: js
working-directory: js/sails
run: yarn test

publish-to-npm:
Expand All @@ -90,7 +90,7 @@ jobs:
uses: EndBug/version-check@v2
id: check
with:
file-name: js/package.json
file-name: js/sails/package.json
file-url: https://unpkg.com/sails-js@latest/package.json
static-checking: localIsNew

Expand All @@ -102,7 +102,7 @@ jobs:

- name: "Prepare: install dependencies"
if: steps.check.outputs.changed == 'true'
working-directory: js
working-directory: js/sails
run: yarn install

- name: "Install: binaryen"
Expand All @@ -115,16 +115,16 @@ jobs:
if: steps.check.outputs.changed == 'true'
run: |
cargo build -p sails-idl-parser --target=wasm32-unknown-unknown --release
wasm-opt -O4 -o ./js/parser.wasm ./target/wasm32-unknown-unknown/release/sails_idl_parser.wasm
wasm-opt -O4 -o ./js/sails/parser.wasm ./target/wasm32-unknown-unknown/release/sails_idl_parser.wasm
- name: "Prepare: build sails-js"
if: steps.check.outputs.changed == 'true'
working-directory: js
working-directory: js/sails
run: yarn build

- name: Publish
if: steps.check.outputs.changed == 'true'
working-directory: js/lib
working-directory: js/sails/lib
run: |
export token=$(printenv npm_token)
npm config set //registry.npmjs.org/:_authToken=$token
Expand Down
11 changes: 5 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
.vscode
*.log


target/

# js
node_modules/
js/lib/
js/parser.wasm
js/test/demo/
js/.yarn/*
!js/.yarn/releases
js/sails/lib/
js/sails/parser.wasm
js/sails/test/demo/
js/sails/.yarn/*
!js/sails/.yarn/releases
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ build-parser:
@echo "Building idlparser"
@cargo build -p sails-idl-parser --target=wasm32-unknown-unknown --release
@ls -lah ./target/wasm32-unknown-unknown/release/sails_idl_parser.wasm
@cp ./target/wasm32-unknown-unknown/release/sails_idl_parser.wasm js/parser.wasm
@cp ./target/wasm32-unknown-unknown/release/sails_idl_parser.wasm js/sails/parser.wasm
@cp ./target/wasm32-unknown-unknown/release/sails_idl_parser.wasm js/parser/parser.wasm

build-proxy:
# Just a regular build using the `wasm32-unknown-unknown` target.
Expand All @@ -28,4 +29,4 @@ build-proxy-idl:

build-js:
@echo "Building sails-js"
@cd js && yarn build
@cd js/sails && yarn build
11 changes: 11 additions & 0 deletions js/parser/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"singleQuote": true,
"trailingComma": "all",
"endOfLine": "lf",
"printWidth": 120,
"semi": true,
"bracketSpacing": true,
"bracketSameLine": true,
"arrowParens": "always",
"tabWidth": 2
}
36 changes: 36 additions & 0 deletions js/parser/compress-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as fs from 'fs';

const main = async () => {
const buf = fs.readFileSync('./parser.wasm');

const cs = new CompressionStream('gzip');

const compressedReadableStream = new Response(buf).body.pipeThrough(cs);

const reader = compressedReadableStream.getReader();

let resultArr = [];

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

if (read.done) break;

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

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

fs.writeFileSync('./lib/wasm-bytes.js', `export default '${base64Bytes}'`);
fs.writeFileSync(
'./lib/cjs/wasm-bytes.js',
`Object.defineProperty(exports, '__esModule', { value: true });\n\nvar wasmParserBytes = '${base64Bytes}';\n\nexports.default = wasmParserBytes;`,
);
};

main()
.then(() => process.exit(0))
.catch((error) => {
console.log(error);
process.exit(1);
});
35 changes: 35 additions & 0 deletions js/parser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "idl-parser-js",
"version": "0.1.0",
"description": "IDL parser for TypeScript",
"main": "index.js",
"type": "module",
"license": "GPL-3.0",
"author": "Gear Technologies",
"bugs": {
"url": "https://github.com/gear-tech/sails/issues"
},
"homepage": "https://github.com/gear-tech/sails/tree/master/js#readme",
"repository": {
"type": "git",
"url": "git+https://githib.com/gear-tech/sails.git"
},
"keywords": [
"gear",
"sails",
"parser"
],
"scripts": {
"build": "rm -rf lib && rollup --config rollup.config.js && node compress-parser.js"
},
"devDependencies": {
"ts-node": "10.9.2",
"typescript": "5.5.4",
"@rollup/plugin-commonjs": "26.0.1",
"@rollup/plugin-typescript": "11.1.6",
"@types/node": "22.4.1",
"rollup": "4.21.0",
"rollup-plugin-peer-deps-external": "2.2.4",
"rollup-plugin-typescript2": "0.36.0"
}
}
55 changes: 55 additions & 0 deletions js/parser/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { cpSync, writeFileSync } from 'fs';
import commonjs from '@rollup/plugin-commonjs';
import typescript from 'rollup-plugin-typescript2';

function writePackageJson(type) {
return {
name: 'write-package-json',
closeBundle() {
if (type === 'cjs') {
writeFileSync('./lib/cjs/package.json', JSON.stringify({ type: 'commonjs' }));
} else {
cpSync('./package.json', 'lib/package.json');
}
},
};
}

export default [
{
input: 'src/index.ts',
output: [
{
dir: 'lib',
format: 'es',
preserveModules: true,
strict: false,
},
],
plugins: [
typescript({
tsconfig: 'tsconfig.build.json',
}),
writePackageJson('es'),
],
},
{
input: 'src/index.ts',
output: [
{
dir: 'lib/cjs',
format: 'cjs',
preserveModules: true,
exports: 'named',
strict: false,
},
],
plugins: [
typescript({
tsconfig: 'tsconfig.cjs.json',
}),
commonjs(),
writePackageJson('cjs'),
],
},
];
4 changes: 4 additions & 0 deletions js/parser/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './parser.js';
export { Program, Ctor, CtorFunc } from './program.js';
export { Service, ServiceFunc, ServiceEvent, FuncParam } from './service.js';
export { TypeDef, EnumDef, EnumVariant } from './types.js';
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions js/parser/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"declaration": true,
"outDir": "./lib",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": false,
"noImplicitAny": false,
"skipLibCheck": true
},
"include": ["src"]
}
Loading

0 comments on commit e88e9ed

Please sign in to comment.