-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
writer-json.ts
34 lines (29 loc) · 1.01 KB
/
writer-json.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
import * as path from "path";
import { ComponentDocApi, ComponentDocs } from "../rollup-plugin";
import Writer from "./Writer";
export interface WriteJsonOptions {
input: string;
inputDir: string;
outFile: string;
}
interface JsonOutput {
total: number;
components: ComponentDocApi[];
}
export default async function writeJson(components: ComponentDocs, options: WriteJsonOptions) {
const output: JsonOutput = {
total: components.size,
components: Array.from(components, ([moduleName, component]) => ({
...component,
filePath: path.join(options.inputDir, path.normalize(component.filePath)),
})).sort((a, b) => {
if (a.moduleName < b.moduleName) return -1;
if (a.moduleName > b.moduleName) return 1;
return 0;
}),
};
const output_path = path.join(process.cwd(), options.outFile);
const writer = new Writer({ parser: "json", printWidth: 80 });
await writer.write(output_path, JSON.stringify(output));
process.stdout.write(`created "${options.outFile}".\n`);
}