-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsnapshot.ts
More file actions
45 lines (41 loc) · 1.45 KB
/
snapshot.ts
File metadata and controls
45 lines (41 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import fs from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { snapshot } from '@webcontainer/snapshot';
import {x} from 'tinyexec';
import { createHash } from "node:crypto";
const PACKAGE_JSON = {
name: 'example',
type: 'module',
version: '0.0.0',
dependencies: {
"@bomb.sh/args": "latest",
"@clack/core": "1.0.0-alpha.0",
"@clack/prompts": "1.0.0-alpha.0"
}
}
const IGNORE_FILES = ['*.md', '*.d.*', '*.map', 'LICENSE', 'license'];
const rootDir = new URL('../', import.meta.url);
const snapshotDir = new URL(`./snapshot-${hash()}/`, rootDir);
const outFile = new URL('./public/snapshot', rootDir);
async function run() {
await fs.mkdir(snapshotDir, { recursive: true });
await fs.writeFile(new URL('package.json', snapshotDir), JSON.stringify(PACKAGE_JSON));
await x('npm', ['install'], {
nodeOptions: {
cwd: fileURLToPath(snapshotDir),
}
})
for await (const file of fs.glob(IGNORE_FILES.map(file => `**/${file}`), { cwd: fileURLToPath(snapshotDir) })) {
await fs.rm(new URL(file, snapshotDir));
}
const output = await snapshot(fileURLToPath(snapshotDir));
await fs.writeFile(outFile, output);
await fs.rm(snapshotDir, { recursive: true, force: true });
console.log('snapshot generated');
}
run();
function hash() {
return createHash("shake256", { outputLength: 8 })
.update(Date.now().toString())
.digest("hex");
}