-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathcli.ts
125 lines (107 loc) · 2.97 KB
/
cli.ts
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env node
import { promises as fs } from "fs";
import path from "path";
import opn from "open";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { renderTemplate } from "../plugin/render-template";
import TEMPLATE, { TemplateType } from "../plugin/template-types";
import { warn } from "../plugin/warn";
import { version } from "../plugin/version";
import { ModuleMeta, ModulePart, ModuleTree, ModuleUID, VisualizerData } from "../shared/types";
const argv = yargs(hideBin(process.argv))
.option("filename", {
describe: "Output file name",
type: "string",
default: "./stats.html",
})
.option("title", {
describe: "Output file title",
type: "string",
default: "Rollup Visualizer",
})
.option("template", {
describe: "Template type",
type: "string",
choices: TEMPLATE,
default: "treemap" as TemplateType,
})
.option("sourcemap", {
describe: "Provided files is sourcemaps",
type: "boolean",
default: false,
})
.option("open", {
describe: "Open generated tempate in default user agent",
type: "boolean",
default: false,
})
.help()
.parseSync();
const listOfFiles = argv._;
interface CliArgs {
filename: string;
title: string;
template: TemplateType;
sourcemap: boolean;
open: boolean;
}
const runForPluginJson = async ({ title, template, filename, open }: CliArgs, files: string[]) => {
if (files.length === 0) {
throw new Error("Empty file list");
}
const fileContents = await Promise.all(
files.map(async (file) => {
const textContent = await fs.readFile(file, { encoding: "utf-8" });
const data = JSON.parse(textContent) as VisualizerData;
return { file, data };
}),
);
const tree: ModuleTree = {
name: "root",
children: [],
};
const nodeParts: Record<ModuleUID, ModulePart> = {};
const nodeMetas: Record<ModuleUID, ModuleMeta> = {};
for (const { file, data } of fileContents) {
if (data.version !== version) {
warn(
`Version in ${file} is not supported (${data.version}). Current version ${version}. Skipping...`,
);
continue;
}
if (data.tree.name === "root") {
tree.children = tree.children.concat(data.tree.children);
} else {
tree.children.push(data.tree);
}
Object.assign(nodeParts, data.nodeParts);
Object.assign(nodeMetas, data.nodeMetas);
}
const data: VisualizerData = {
version,
tree,
nodeParts,
nodeMetas,
env: fileContents[0].data.env,
options: fileContents[0].data.options,
};
const fileContent = await renderTemplate(template, {
title,
data: JSON.stringify(data),
});
await fs.mkdir(path.dirname(filename), { recursive: true });
try {
await fs.unlink(filename);
} catch (err) {
// ignore
}
await fs.writeFile(filename, fileContent);
if (open) {
await opn(filename);
}
};
runForPluginJson(argv, listOfFiles as string[]).catch((err: Error) => {
warn(err.message);
process.exit(1);
});