Skip to content

Commit

Permalink
Making it possible to import the project as an NPM package using a Gi…
Browse files Browse the repository at this point in the history
…t URL
  • Loading branch information
ghadeeras committed Dec 1, 2024
1 parent 7fb1b28 commit 783e77f
Show file tree
Hide file tree
Showing 20 changed files with 119 additions and 67 deletions.
11 changes: 2 additions & 9 deletions .github/workflows/ci-build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ jobs:

strategy:
matrix:
node-version: [16.x]
wabt-version: [1.0.27]
node-version: [20.x]

steps:
- name: Checkout source code
Expand All @@ -32,16 +31,10 @@ jobs:
with:
node-version: ${{ matrix.node-version }}

- name: Use WABT ${{ matrix.wabt-version }}
uses: mwilliamson/setup-wabt-action@v1
with:
wabt-version: ${{ matrix.wabt-version }}

- name: Build Runtime WASM Modules
run: bash ./scripts/build-wa.sh

- name: NPM build/test
run: |
npm ci
npm run build --if-present
npm ci --ignore-scripts
npm test
16 changes: 5 additions & 11 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ jobs:

strategy:
matrix:
node-version: [16.x]
wabt-version: [1.0.27]
node-version: [20.x]

steps:
- name: Checkout source code
Expand All @@ -33,24 +32,19 @@ jobs:
with:
node-version: ${{ matrix.node-version }}

- name: Use WABT ${{ matrix.wabt-version }}
uses: mwilliamson/setup-wabt-action@v1
with:
wabt-version: ${{ matrix.wabt-version }}

- name: Build Runtime WASM Modules
run: bash ./scripts/build-wa.sh

- name: NPM build/test
run: |
npm ci
npm run build --if-present
npm ci --ignore-scripts
npm test
- name: Prepare for Releasing to 'dist'
run: |
cp -R docs ./out
cp README.md ./out
npm run prepare
cp -R docs ./lib
cp README.md ./lib
cp scripts/release.sh .
- name: Switch to 'dist' branch
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nyc_output
coverage
node_modules
out
lib
tsconfig.tsbuildinfo
5 changes: 2 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
"--require", "source-map-support/register",
"--timeout", "120000000",
// "--fgrep", ">>>",
"\"${workspaceFolder}/out/test/**/*.test.js\"",
"\"${workspaceFolder}/lib/test/**/*.test.js\"",
],
"console": "integratedTerminal",
"preLaunchTask": "tsc: build"
"console": "integratedTerminal"
}
]
}
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
{
"label": "wasm: build",
"type": "shell",
"command": "${cwd}/build-wasm.sh",
"command": "node ./scripts/build-wa.js",
"group": "build"
}
]
Expand Down
46 changes: 23 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "vibrato.js",
"version": "1.0.0",
"description": "Digital signal processing framework for the JavaScript programing language.",
"description": "Digital signal processing framework for the JavaScript programming language.",
"main": "index.js",
"type": "module",
"scripts": {
"test": "nyc --reporter=html -- mocha --recursive \"./out/test/**/*.test.js\"",
"pretest": "tsc --sourceMap false --declarationMap false"
"prepare": "node ./scripts/clean.js && node ./scripts/build-wa.js && npx tsc --project ./tsconfig.prepare.json && node ./scripts/finalize.js",
"pretest": "node ./scripts/clean.js && node ./scripts/build-wa.js && npx tsc",
"test": "nyc --reporter=html -- mocha --recursive \"./lib/test/**/*.test.js\""
},
"repository": {
"type": "git",
Expand All @@ -26,6 +27,12 @@
"url": "https://github.com/ghadeeras/vibrato.js/issues"
},
"homepage": "https://github.com/ghadeeras/vibrato.js#readme",
"files": [
"lib",
"index.js",
"index.d.ts",
"docs"
],
"devDependencies": {
"@types/chai": "^5.0.1",
"@types/mocha": "^10.0.9",
Expand Down
3 changes: 3 additions & 0 deletions scripts/build-wa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as routines from "./routines.js"

routines.compileRuntime()
8 changes: 4 additions & 4 deletions scripts/build-wa.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/bash

echo "Making sure runtime output directory exists ..."
mkdir -p ./out
rm -fR ./out/wa
mkdir ./out/wa || exit 1
mkdir -p ./lib
rm -fR ./lib/wa
mkdir ./lib/wa || exit 1

echo "Building runtime modules ..."
ls ./src/wa/*.wat \
| xargs -I {} basename {} ".wat" \
| xargs -I {} wat2wasm --output=./out/wa/{}.wasm ./src/wa/{}.wat || exit 1
| xargs -I {} wat2wasm --output=./lib/wa/{}.wasm ./src/wa/{}.wat || exit 1

echo "Success!"
3 changes: 3 additions & 0 deletions scripts/clean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as routines from "./routines.js"

routines.clean()
3 changes: 3 additions & 0 deletions scripts/finalize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as routines from "./routines.js"

routines.finalize()
10 changes: 5 additions & 5 deletions scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ rm -R docs
rm README.md

echo "Removing non-releaseable files ..."
find out -type f -not -name '*.wasm' -not -name '*.js' -not -name '*.ts' -not -name '*.md' -delete || exit 1
find lib -type f -not -name '*.wasm' -not -name '*.js' -not -name '*.ts' -not -name '*.md' -delete || exit 1

echo "Taking new snapshot ..."
mkdir latest
mv out/wa latest/wa || exit 1
mv out/prod latest/js || exit 1
mv out/docs docs || exit 1
mv out/README.md . || exit 1
mv lib/wa latest/wa || exit 1
mv lib/js latest/js || exit 1
mv lib/docs docs || exit 1
mv lib/README.md . || exit 1

echo "Creating archive ..."
rm latest.zip
Expand Down
41 changes: 41 additions & 0 deletions scripts/routines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import fs from 'fs'
import binaryen from "binaryen"

const src = "./src"
const lib = "./lib"
const waSrc = `${src}/wa`
const waLib = `${lib}/wa`
const jsLib = `${lib}/js`
const prodLib = `${lib}/prod`

export function compileRuntime() {
console.log("Making sure runtime output directory exists ...")
if (!fs.existsSync(lib)) {
fs.mkdirSync(lib)
}
if (!fs.existsSync(waLib)) {
fs.mkdirSync(waLib)
}

console.log("Building runtime modules ...")
const watFiles = fs.readdirSync(waSrc).filter(f => f.endsWith(".wat"))
for (const watFile of watFiles) {
console.log(`Compiling ${watFile} ...`)
const buffer = fs.readFileSync(`${waSrc}/${watFile}`)
const code = buffer.toString("utf-8")
const module = binaryen.parseText(code)
module.setFeatures(binaryen.Features.BulkMemory)
module.optimize()
const binary = module.emitBinary()
fs.writeFileSync(`${waLib}/${watFile.replace(".wat", ".wasm")}`, binary)
}
console.log("Success!")
}

export function clean() {
fs.rmSync(lib, { force: true, recursive: true })
}

export function finalize() {
fs.renameSync(prodLib, jsLib)
}
2 changes: 1 addition & 1 deletion src/test/expressions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const assembler = new Assembler([

describe("expressions", async () => {

const runtime = await rt.runtime("./out/wa", waNode.fsModulesLoader, assembler.rawMem)
const runtime = await rt.runtime("./lib/wa", waNode.fsModulesLoader, assembler.rawMem)
const mem = notNull(runtime.exports.mem, "Couldn't load Vibrato runtime!")
const test = assembler.exports<TestExports>(runtime)

Expand Down
2 changes: 1 addition & 1 deletion src/test/rt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as dt from '../prod/datatypes.js'

describe("Runtime", async () => {

const runtime = await rt.runtime("./out/wa", waNode.fsModulesLoader)
const runtime = await rt.runtime("./lib/wa", waNode.fsModulesLoader)
const mem = notNull(runtime.exports.mem, "Couldn't load Vibrato runtime 'mem' module !")
const space = notNull(runtime.exports.space, "Couldn't load Vibrato runtime 'space' module!")
const delay = notNull(runtime.exports.delay, "Couldn't load Vibrato runtime 'delay' module!")
Expand Down
2 changes: 1 addition & 1 deletion src/test/testutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class SpecContext {
async createTests() {
const values = this.collectValues()
const assembler = new Assembler(values)
const runtime = await rt.runtime("./out/wa", waNode.fsModulesLoader, assembler.rawMem)
const runtime = await rt.runtime("./lib/wa", waNode.fsModulesLoader, assembler.rawMem)
const mem = notNull(runtime.exports.mem, "Couldn't load Vibrato runtime!")
const test = assembler.exports<ValueExports>(runtime)

Expand Down
2 changes: 1 addition & 1 deletion src/test/values/literal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const assembler = new Assembler([

describe("Literal", async () => {

const runtime = await rt.runtime("./out/wa", waNode.fsModulesLoader, assembler.rawMem)
const runtime = await rt.runtime("./lib/wa", waNode.fsModulesLoader, assembler.rawMem)
const mem = notNull(runtime.exports.mem, "Couldn't load Vibrato runtime!")
const test = assembler.exports<TestExports>(runtime)

Expand Down
2 changes: 1 addition & 1 deletion src/test/values/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const assembler = new Assembler([

describe("Operations", async () => {

const runtime = await rt.runtime("./out/wa", waNode.fsModulesLoader, assembler.rawMem)
const runtime = await rt.runtime("./lib/wa", waNode.fsModulesLoader, assembler.rawMem)
const mem = notNull(runtime.exports.mem, "Couldn't load Vibrato runtime!")
const test = assembler.exports<TestExports>(runtime)

Expand Down
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./out", /* Redirect output structure to the directory. */
"outDir": "./lib", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
Expand Down Expand Up @@ -66,5 +66,6 @@
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
},
"include": ["./src"]
}
8 changes: 8 additions & 0 deletions tsconfig.prepare.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"sourceMap": false,
"declarationMap": false
},
"exclude": ["./src/test"]
}

0 comments on commit 783e77f

Please sign in to comment.