Skip to content

Commit 5e80d09

Browse files
committed
fmt
Signed-off-by: karthik2804 <[email protected]>
1 parent 8fe5c47 commit 5e80d09

File tree

32 files changed

+1935
-1657
lines changed

32 files changed

+1935
-1657
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"version": "3.1.0",
44
"description": "",
55
"scripts": {
6-
"fmt": "prettier --write \"src/**/*.{ts,tsx,js,jsx}\"",
7-
"fmt-check": "prettier --check \"src/**/*.{ts,tsx,js,jsx}\"",
6+
"fmt": "prettier --write \"packages/**/*.{ts,tsx,js,jsx}\"",
7+
"fmt-check": "prettier --check \"packages/**/*.{ts,tsx,js,jsx}\"",
88
"fmt-examples": "prettier --write \"examples/**/*.{ts,tsx,js,jsx}\" --ignore-patterm \"node_modules\""
99
},
1010
"sideEffects": false,
Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1-
import { getPackagesWithWasiDeps, processWasiDeps } from "../../dist/wasiDepsParser.js";
1+
import {
2+
getPackagesWithWasiDeps,
3+
processWasiDeps,
4+
} from '../../dist/wasiDepsParser.js';
25
class SpinSdkPlugin {
3-
constructor() {
4-
this.externals = {};
5-
}
6-
7-
static async init() {
8-
const { getWitImports } = await import("../../lib/wit_tools.js");
9-
let plugin = new SpinSdkPlugin();
10-
11-
let wasiDeps = getPackagesWithWasiDeps(process.cwd(), new Set(), true);
12-
let { witPaths, targetWorlds } = processWasiDeps(wasiDeps)
13-
14-
let imports = getWitImports(witPaths, targetWorlds);
15-
16-
imports.map((i) => {
17-
plugin.externals[i] = i;
18-
});
19-
20-
return plugin;
21-
}
22-
23-
apply(compiler) {
24-
if (compiler.options.externals && typeof compiler.options.externals === 'object') {
25-
this.externals = Object.assign({}, compiler.options.externals, this.externals);
26-
}
27-
compiler.options.externals = this.externals;
6+
constructor() {
7+
this.externals = {};
8+
}
9+
10+
static async init() {
11+
const { getWitImports } = await import('../../lib/wit_tools.js');
12+
let plugin = new SpinSdkPlugin();
13+
14+
let wasiDeps = getPackagesWithWasiDeps(process.cwd(), new Set(), true);
15+
let { witPaths, targetWorlds } = processWasiDeps(wasiDeps);
16+
17+
let imports = getWitImports(witPaths, targetWorlds);
18+
19+
imports.map(i => {
20+
plugin.externals[i] = i;
21+
});
22+
23+
return plugin;
24+
}
25+
26+
apply(compiler) {
27+
if (
28+
compiler.options.externals &&
29+
typeof compiler.options.externals === 'object'
30+
) {
31+
this.externals = Object.assign(
32+
{},
33+
compiler.options.externals,
34+
this.externals,
35+
);
2836
}
37+
compiler.options.externals = this.externals;
38+
}
2939
}
3040

31-
3241
export default SpinSdkPlugin;

packages/build-tools/src/build.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1-
import { calculateChecksum, fileExists, getExistingBuildData } from "./utils.js";
1+
import {
2+
calculateChecksum,
3+
fileExists,
4+
getExistingBuildData,
5+
} from './utils.js';
26

37
export function getBuildDataPath(src: string): string {
4-
return `${src}.buildData.json`;
8+
return `${src}.buildData.json`;
59
}
610

7-
export async function ShouldComponentize(src: string, outputPath: string, componentizeVersion: string) {
11+
export async function ShouldComponentize(
12+
src: string,
13+
outputPath: string,
14+
componentizeVersion: string,
15+
) {
16+
const sourceChecksum = await calculateChecksum(src);
17+
const existingBuildData = await getExistingBuildData(getBuildDataPath(src));
818

9-
const sourceChecksum = await calculateChecksum(src);
10-
const existingBuildData = await getExistingBuildData(getBuildDataPath(src));
19+
if (
20+
existingBuildData?.version == componentizeVersion &&
21+
existingBuildData?.checksum === sourceChecksum &&
22+
(await fileExists(outputPath))
23+
) {
24+
return false;
25+
}
1126

12-
if (existingBuildData?.version == componentizeVersion && existingBuildData?.checksum === sourceChecksum && await fileExists(outputPath)) {
13-
return false;
14-
}
15-
16-
return true
17-
}
27+
return true;
28+
}

packages/build-tools/src/cli.ts

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,38 @@ import { hideBin } from 'yargs/helpers';
33

44
// Defining the shape of the arguments using TypeScript
55
export interface CliArgs {
6-
input: string;
7-
output: string;
8-
triggerType: string;
9-
witPath?: string;
10-
aot?: boolean;
6+
input: string;
7+
output: string;
8+
triggerType: string;
9+
witPath?: string;
10+
aot?: boolean;
1111
}
1212

1313
export function getCliArgs(): CliArgs {
14-
let args = yargs(hideBin(process.argv))
15-
.option('input', {
16-
alias: 'i',
17-
describe: 'Path to the input file',
18-
demandOption: true
19-
})
20-
.option('wit-path', {
21-
alias: 'd',
22-
describe: 'Path to wit file or folder',
23-
})
24-
.option('output', {
25-
alias: 'o',
26-
describe: 'Path to the output file',
27-
default: 'component.wasm'
28-
})
29-
.option('trigger-type', {
30-
alias: '-n',
31-
describe: "Spin trigger to target",
32-
demandOption: true
33-
})
34-
.option('aot', {
35-
describe: "Enable Ahead of Time compilation",
36-
type: 'boolean',
37-
})
38-
.argv as CliArgs;
14+
let args = yargs(hideBin(process.argv))
15+
.option('input', {
16+
alias: 'i',
17+
describe: 'Path to the input file',
18+
demandOption: true,
19+
})
20+
.option('wit-path', {
21+
alias: 'd',
22+
describe: 'Path to wit file or folder',
23+
})
24+
.option('output', {
25+
alias: 'o',
26+
describe: 'Path to the output file',
27+
default: 'component.wasm',
28+
})
29+
.option('trigger-type', {
30+
alias: '-n',
31+
describe: 'Spin trigger to target',
32+
demandOption: true,
33+
})
34+
.option('aot', {
35+
describe: 'Enable Ahead of Time compilation',
36+
type: 'boolean',
37+
}).argv as CliArgs;
3938

40-
return args;
41-
}
39+
return args;
40+
}

packages/build-tools/src/index.ts

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,79 @@
11
#!/usr/bin/env node
22

3-
import { componentize } from "@bytecodealliance/componentize-js";
3+
import { componentize } from '@bytecodealliance/componentize-js';
44
//@ts-ignore https://github.com/bytecodealliance/ComponentizeJS/pull/198
5-
import { version } from "@bytecodealliance/componentize-js"
6-
import { getPackagesWithWasiDeps, processWasiDeps } from "./wasiDepsParser.js";
5+
import { version } from '@bytecodealliance/componentize-js';
6+
import { getPackagesWithWasiDeps, processWasiDeps } from './wasiDepsParser.js';
77
import { basename } from 'node:path';
88

9-
import { calculateChecksum, getPackageVersion, saveBuildData } from "./utils.js";
10-
import { getCliArgs } from "./cli.js";
11-
import { getBuildDataPath, ShouldComponentize } from "./build.js";
12-
import { readFile, writeFile } from "node:fs/promises";
13-
import { mergeWit } from "../lib/wit_tools.js"
9+
import {
10+
calculateChecksum,
11+
getPackageVersion,
12+
saveBuildData,
13+
} from './utils.js';
14+
import { getCliArgs } from './cli.js';
15+
import { getBuildDataPath, ShouldComponentize } from './build.js';
16+
import { readFile, writeFile } from 'node:fs/promises';
17+
import { mergeWit } from '../lib/wit_tools.js';
1418

1519
async function main() {
1620
try {
1721
// Parse CLI args
18-
let CliArgs = getCliArgs()
22+
let CliArgs = getCliArgs();
1923
let src = CliArgs.input;
2024
let outputPath = CliArgs.output;
2125

2226
// Can remove the explicit typing once https://github.com/bytecodealliance/ComponentizeJS/pull/198 is merged and released
2327
let componentizeVersion = version as string;
2428

2529
// Small optimization to skip componentization if the source file hasn't changed
26-
if (!await ShouldComponentize(src, outputPath, componentizeVersion)) {
27-
console.log("No changes detected in source file. Skipping componentization.");
30+
if (!(await ShouldComponentize(src, outputPath, componentizeVersion))) {
31+
console.log(
32+
'No changes detected in source file. Skipping componentization.',
33+
);
2834
return;
2935
}
30-
console.log("Componentizing...");
36+
console.log('Componentizing...');
3137

3238
// generate wit world string
3339
let wasiDeps = getPackagesWithWasiDeps(process.cwd(), new Set(), true);
3440

3541
let { witPaths, targetWorlds } = processWasiDeps(wasiDeps);
3642

3743
// Get inline wit by merging the wits specified by all the dependencies
38-
let inlineWit = mergeWit(witPaths, targetWorlds, "combined", "combined-wit:[email protected]");
44+
let inlineWit = mergeWit(
45+
witPaths,
46+
targetWorlds,
47+
'combined',
48+
'combined-wit:[email protected]',
49+
);
3950

4051
const source = await readFile(src, 'utf8');
4152

4253
const { component } = await componentize(source, inlineWit, {
4354
sourceName: basename(src),
4455
// TODO: CHeck if we need to enable http
4556
//@ts-ignore
46-
enableFeatures: ["http"],
57+
enableFeatures: ['http'],
4758
});
4859

4960
await writeFile(outputPath, component);
5061

5162
// Save the checksum of the input file along with the componentize version
52-
await saveBuildData(getBuildDataPath(src), await calculateChecksum(src), componentizeVersion);
63+
await saveBuildData(
64+
getBuildDataPath(src),
65+
await calculateChecksum(src),
66+
componentizeVersion,
67+
);
5368

54-
console.log("Component successfully written.");
69+
console.log('Component successfully written.');
70+
} catch (error) {
71+
console.error(
72+
'An error occurred during the componentization process:',
73+
error,
74+
);
75+
console.error('Error:', error);
5576
}
56-
catch (error) {
57-
console.error("An error occurred during the componentization process:", error);
58-
console.error("Error:", error);
59-
}
60-
6177
}
6278

6379
main();

0 commit comments

Comments
 (0)