Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export function used by CLI to merge outputs #189

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 7 additions & 85 deletions bin/cli.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
#!/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";
import { mergeJsonOutputs } from "../plugin";

const argv = yargs(hideBin(process.argv))
.option("filename", {
Expand All @@ -31,9 +25,10 @@ const argv = yargs(hideBin(process.argv))
default: "treemap" as TemplateType,
})
.option("sourcemap", {
describe: "Provided files is sourcemaps",
describe: "Provided files are sourcemaps",
type: "boolean",
default: false,
deprecated: true, // unused
})
.option("open", {
describe: "Open generated tempate in default user agent",
Expand All @@ -43,83 +38,10 @@ const argv = yargs(hideBin(process.argv))
.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) => {
mergeJsonOutputs({
inputs: argv._ as string[],
...argv,
}).catch((err: Error) => {
warn(err.message);
process.exit(1);
});
2 changes: 2 additions & 0 deletions plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { getSourcemapModules } from "./sourcemap";
import { renderTemplate } from "./render-template";
import { createFilter, Filter } from "../shared/create-filter";

export * from "./merge-outputs";

const WARN_SOURCEMAP_DISABLED =
"rollup output configuration missing sourcemap = true. You should add output.sourcemap = true or disable sourcemap in this plugin";

Expand Down
115 changes: 115 additions & 0 deletions plugin/merge-outputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { promises as fs } from "fs";
import path from "path";

import opn from "open";

import { renderTemplate } from "../plugin/render-template";
import { TemplateType } from "../plugin/template-types";
import { warn } from "../plugin/warn";
import { version } from "../plugin/version";
import { ModuleMeta, ModulePart, ModuleTree, ModuleUID, VisualizerData } from "../shared/types";

export interface MergeJsonOutputOptions {
/**
* List of input file paths to merge.
*/
inputs: string[];
/**
* Output file name.
*
* @default "./stats.html"
*/
filename?: string;
/**
* Output file title.
*
* @default "Rollup Visualizer"
*/
title?: string;
/**
* Template type
*
* @default "treemap"
*/
template?: TemplateType;
/**
* Open generated tempate in default user agent.
*
* @default false
*/
open?: boolean;
}

export async function mergeJsonOutputs(options: MergeJsonOutputOptions): Promise<void> {
const {
inputs,
filename = "./stats.html",
title = "Rollup Visualizer",
template = "treemap",
open = false,
} = options;

if (inputs.length === 0) {
throw new Error("Empty file list");
}

const fileContents = await Promise.all(
inputs.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);
}
}