Skip to content

Commit c85da08

Browse files
authored
Package the build_bundle.js script alongside ccfapp (#6704)
1 parent f54c0f8 commit c85da08

File tree

7 files changed

+90
-58
lines changed

7 files changed

+90
-58
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1313

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

1718
## [6.0.0-dev9]
1819

doc/build_apps/js_app_bundle.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ The key fields are:
338338
Once :ref:`submitted and accepted <governance/proposals:Submitting a New Proposal>`, a ``set_js_app`` proposal atomically (re-)deploys the complete JavaScript application.
339339
Any existing application endpoints and JavaScript modules are removed.
340340

341-
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.
342-
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`.
341+
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.
343342

344343
Bytecode cache
345344
~~~~~~~~~~~~~~

js/ccf-app/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*.tgz
22
/*.d.ts
33
/*.js
4-
/html
4+
/html
5+
/scripts

js/ccf-app/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@
3131
"ts-node": "^10.4.0",
3232
"typedoc": "^0.27.0",
3333
"typescript": "^5.7.2"
34+
},
35+
"bin": {
36+
"ccf-build-bundle": "scripts/build_bundle.js"
3437
}
3538
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env node
2+
3+
import {
4+
readdirSync,
5+
statSync,
6+
readFileSync,
7+
writeFileSync,
8+
existsSync,
9+
} from "fs";
10+
import { join, resolve, sep } from "path";
11+
12+
function getAllFiles(dirPath: string): string[] {
13+
const toSearch = [dirPath];
14+
const agg = [];
15+
16+
for (const filePath of toSearch) {
17+
if (statSync(filePath).isDirectory()) {
18+
for (const subfile of readdirSync(filePath)) {
19+
toSearch.push(join(filePath, subfile));
20+
}
21+
} else {
22+
agg.push(filePath);
23+
}
24+
}
25+
return agg;
26+
}
27+
28+
function removePrefix(s: string, prefix: string): string {
29+
if (s.startsWith(prefix)) {
30+
return s.slice(prefix.length).split(sep).join(sep);
31+
}
32+
console.log("Warn: tried to remove invalid prefix", s, prefix);
33+
return s;
34+
}
35+
36+
const args = process.argv.slice(2);
37+
38+
if (args.length < 1) {
39+
console.log("Usage: build_bundle <root_directory>");
40+
process.exit(1);
41+
}
42+
43+
function assertFileExists(path: string) {
44+
if (!existsSync(path)) {
45+
console.log("File not found: %s", path);
46+
process.exit(1);
47+
}
48+
}
49+
50+
const argRootDirPath = args[0];
51+
assertFileExists(argRootDirPath);
52+
const rootDirPath = resolve(argRootDirPath);
53+
const metadataPath = join(rootDirPath, "app.json");
54+
assertFileExists(metadataPath);
55+
const srcDirPath = join(rootDirPath, "src");
56+
assertFileExists(srcDirPath);
57+
58+
const metadata = JSON.parse(readFileSync(metadataPath, "utf-8"));
59+
const allFiles = getAllFiles(srcDirPath);
60+
61+
// The trailing / is included so that it is trimmed in removePrefix.
62+
// This produces "foo/bar.js" rather than "/foo/bar.js"
63+
const toTrim = srcDirPath + "/";
64+
65+
const modules = allFiles.map(function (filePath) {
66+
return {
67+
name: removePrefix(filePath, toTrim),
68+
module: readFileSync(filePath, "utf-8"),
69+
};
70+
});
71+
72+
const bundlePath = join(args[0], "bundle.json");
73+
const bundle = {
74+
metadata: metadata,
75+
modules: modules,
76+
};
77+
78+
console.log(
79+
`Writing bundle containing ${modules.length} modules to ${bundlePath}`,
80+
);
81+
writeFileSync(bundlePath, JSON.stringify(bundle));

tests/npm-app/build_bundle.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

tests/npm-app/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"private": true,
33
"scripts": {
4-
"build": "del-cli -f dist/ && rollup --config && cp app.json dist/ && node build_bundle.js dist/",
5-
"bundle": "node build_bundle.js dist",
4+
"build": "del-cli -f dist/ && rollup --config && cp app.json dist/ && npx ccf-build-bundle dist",
5+
"bundle": "npx ccf-build-bundle dist",
66
"test": "node --version"
77
},
88
"type": "module",

0 commit comments

Comments
 (0)