-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathgenerate-component-builder.ts
103 lines (97 loc) · 3.72 KB
/
generate-component-builder.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
import path from 'path';
import fs from 'fs';
import { getComponentBuilderTemplate } from './templates/component-builder';
import { ComponentFile, PackageDefinition, getComponentList } from '../components';
import { watchItems } from '../utils';
// Default destination path for component builder
const defaultComponentBuilderOutputPath = 'src/temp/componentBuilder.ts';
const defaultComponentRootPath = 'src/components';
/**
* Generate component builder based on provided settings
* @param {object} [settings] settings for component builder generation
* @param {string} [settings.componentRootPath] path to components root
* @param {string} [settings.componentBuilderOutputPath] path to component builder output
* @param {PackageDefinition[]} [settings.packages] list of packages to include in component builder
* @param {ComponentFile[]} [settings.components] list of components to include in component builder
* @param {boolean} [settings.watch] whether to watch for changes to component builder sources
*/
export function generateComponentBuilder({
componentRootPath = defaultComponentRootPath,
componentBuilderOutputPath = defaultComponentBuilderOutputPath,
packages = [],
components = [],
watch,
}: {
componentRootPath?: string;
componentBuilderOutputPath?: string;
packages?: PackageDefinition[];
components?: ComponentFile[];
watch?: boolean;
} = {}) {
if (watch) {
watchComponentBuilder({ componentRootPath, componentBuilderOutputPath, packages, components });
} else {
writeComponentBuilder({ componentRootPath, componentBuilderOutputPath, packages, components });
}
}
/**
* Watch for changes to component builder sources
* @param {object} settings settings for component builder generation
* @param {string} settings.componentRootPath path to components root
* @param {string} settings.componentBuilderOutputPath path to component builder output
* @param {PackageDefinition[]} settings.packages list of packages to include in component builder
* @param {ComponentFile[]} settings.components list of components to include in component builder
*/
export function watchComponentBuilder({
componentRootPath,
componentBuilderOutputPath,
packages,
components,
}: {
componentRootPath: string;
componentBuilderOutputPath: string;
packages: PackageDefinition[];
components: ComponentFile[];
}) {
console.log(`Watching for changes to component builder sources in ${componentRootPath}...`);
watchItems(
[componentRootPath],
writeComponentBuilder.bind(null, {
componentRootPath,
componentBuilderOutputPath,
packages,
components,
})
);
}
/**
* Write component builder to file
* @param {object} settings settings for component builder generation
* @param {string} settings.componentRootPath path to components root
* @param {string} settings.componentBuilderOutputPath path to component builder output
* @param {PackageDefinition[]} settings.packages list of packages to include in component builder
* @param {ComponentFile[]} settings.components list of components to include in component builder
*/
export function writeComponentBuilder({
componentRootPath,
componentBuilderOutputPath,
packages,
components,
}: {
componentRootPath: string;
componentBuilderOutputPath: string;
packages: PackageDefinition[];
components: ComponentFile[];
}) {
const items: (ComponentFile | PackageDefinition)[] = [
...packages,
...components,
...getComponentList(componentRootPath),
];
const componentBuilderPath = path.resolve(componentBuilderOutputPath);
const fileContent = getComponentBuilderTemplate(items);
console.log(`Writing component builder to ${componentBuilderPath}`);
fs.writeFileSync(componentBuilderPath, fileContent, {
encoding: 'utf8',
});
}