Skip to content

Commit

Permalink
Package the build_bundle.js script alongside ccfapp (#6704)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjen1-msft authored Dec 17, 2024
1 parent f54c0f8 commit c85da08
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 58 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Expose `ccf:http::parse_accept_header()` and `ccf::http::AcceptHeaderField` (#6706).
- Added `ccf::cose::AbstractCOSESignaturesConfig` subsystem to expose COSE signature configuration to application handlers (#6707).
- Package `build_bundle.ts` under `npx ccf-build-bundle` to allow javascript users to build a ccf schema bundle (#6704).

## [6.0.0-dev9]

Expand Down
3 changes: 1 addition & 2 deletions doc/build_apps/js_app_bundle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,7 @@ The key fields are:
Once :ref:`submitted and accepted <governance/proposals:Submitting a New Proposal>`, a ``set_js_app`` proposal atomically (re-)deploys the complete JavaScript application.
Any existing application endpoints and JavaScript modules are removed.

If you are using ``npm`` or similar to build your app it may make sense to convert your app into a proposal-ready JSON bundle during packaging.
For an example of how this could be done, see :ccf_repo:`tests/npm-app/build_bundle.js` from one of CCF's test applications, called by ``npm build`` from the corresponding :ccf_repo:`tests/npm-app/package.json`.
If you are using ``npm`` to build your app, we package a `ccf-build-bundle` script alongside `ccf-app`. This can be run using `npx --package @microsoft/ccf-app ccf-build-bundle path/to/root/of/app` to package the `app.json` and all javascript modules under `src` into a proposal-ready JSON bundle.

Bytecode cache
~~~~~~~~~~~~~~
Expand Down
3 changes: 2 additions & 1 deletion js/ccf-app/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*.tgz
/*.d.ts
/*.js
/html
/html
/scripts
3 changes: 3 additions & 0 deletions js/ccf-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@
"ts-node": "^10.4.0",
"typedoc": "^0.27.0",
"typescript": "^5.7.2"
},
"bin": {
"ccf-build-bundle": "scripts/build_bundle.js"
}
}
81 changes: 81 additions & 0 deletions js/ccf-app/src/scripts/build_bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env node

import {
readdirSync,
statSync,
readFileSync,
writeFileSync,
existsSync,
} from "fs";
import { join, resolve, sep } from "path";

function getAllFiles(dirPath: string): string[] {
const toSearch = [dirPath];
const agg = [];

for (const filePath of toSearch) {
if (statSync(filePath).isDirectory()) {
for (const subfile of readdirSync(filePath)) {
toSearch.push(join(filePath, subfile));
}
} else {
agg.push(filePath);
}
}
return agg;
}

function removePrefix(s: string, prefix: string): string {
if (s.startsWith(prefix)) {
return s.slice(prefix.length).split(sep).join(sep);
}
console.log("Warn: tried to remove invalid prefix", s, prefix);
return s;
}

const args = process.argv.slice(2);

if (args.length < 1) {
console.log("Usage: build_bundle <root_directory>");
process.exit(1);
}

function assertFileExists(path: string) {
if (!existsSync(path)) {
console.log("File not found: %s", path);
process.exit(1);
}
}

const argRootDirPath = args[0];
assertFileExists(argRootDirPath);
const rootDirPath = resolve(argRootDirPath);
const metadataPath = join(rootDirPath, "app.json");
assertFileExists(metadataPath);
const srcDirPath = join(rootDirPath, "src");
assertFileExists(srcDirPath);

const metadata = JSON.parse(readFileSync(metadataPath, "utf-8"));
const allFiles = getAllFiles(srcDirPath);

// The trailing / is included so that it is trimmed in removePrefix.
// This produces "foo/bar.js" rather than "/foo/bar.js"
const toTrim = srcDirPath + "/";

const modules = allFiles.map(function (filePath) {
return {
name: removePrefix(filePath, toTrim),
module: readFileSync(filePath, "utf-8"),
};
});

const bundlePath = join(args[0], "bundle.json");
const bundle = {
metadata: metadata,
modules: modules,
};

console.log(
`Writing bundle containing ${modules.length} modules to ${bundlePath}`,
);
writeFileSync(bundlePath, JSON.stringify(bundle));
53 changes: 0 additions & 53 deletions tests/npm-app/build_bundle.js

This file was deleted.

4 changes: 2 additions & 2 deletions tests/npm-app/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"private": true,
"scripts": {
"build": "del-cli -f dist/ && rollup --config && cp app.json dist/ && node build_bundle.js dist/",
"bundle": "node build_bundle.js dist",
"build": "del-cli -f dist/ && rollup --config && cp app.json dist/ && npx ccf-build-bundle dist",
"bundle": "npx ccf-build-bundle dist",
"test": "node --version"
},
"type": "module",
Expand Down

0 comments on commit c85da08

Please sign in to comment.