forked from ng-bootstrap/ng-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo-gen.js
89 lines (72 loc) · 2.68 KB
/
demo-gen.js
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
var fs = require('fs');
var glob = require('glob');
var mkdirp = require('mkdirp');
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function genDemoComponent(component, demo) {
return `import {Component} from '@angular/core';
@Component({
selector: 'ngbd-${component}-${demo}',
template: require('./${component}-${demo}.html')
})
export class Ngbd${capitalize(component)}${capitalize(demo)} {
}
`;
}
function genDemo(component, demo) {
const demoFolder = `./demo/src/app/components/${component}/demos/${demo}`;
const componentFile = `${demoFolder}/${component}-${demo}`;
const codeFile = `${componentFile}.ts`;
const markupFile = `${componentFile}.html`;
mkdirp.sync(demoFolder);
if (!fs.exists(codeFile)) {
fs.writeFileSync(codeFile, genDemoComponent(component, demo), {flag: 'w'});
}
if (!fs.exists(markupFile)) {
fs.writeFileSync(markupFile, ' ', {flag: 'w'});
}
}
function genDemosIndex(component) {
const base = `demo/src/app/components/${component}/demos`;
const path = `${base}/*/`;
const demoNames = glob.sync(path, {})
.map((dir) => {
const dirNoEndingSlash = dir.substr(0, dir.length - 1);
return dirNoEndingSlash.substr(dirNoEndingSlash.lastIndexOf('/') + 1);
})
.sort();
const demoImports =
demoNames
.map((demo) => {
return `import {Ngbd${capitalize(component)}${capitalize(demo)}} from './${demo}/${component}-${demo}';`;
})
.join('\n');
const demoDirectives = demoNames.map((demo) => { return `Ngbd${capitalize(component)}${capitalize(demo)}`; });
const demoSnippets = demoNames.map((demo) => {
return ` '${demo}': {
'code': require('!!prismjs-loader-loader?lang=typescript!./${demo}/${component}-${demo}'),
'markup': require('!!prismjs-loader-loader?lang=markup!./${demo}/${component}-${demo}.html')}`;
});
return `${demoImports}
export const DEMO_DIRECTIVES = [${demoDirectives.join(', ')}];
export const DEMO_SNIPPETS = {
${demoSnippets.join(',\n')}
};
`;
}
function genIndex(componentName) {
const componentDemosFolder = `./demo/src/app/components/${componentName}/demos`;
fs.writeFileSync(`${componentDemosFolder}/index.ts`, genDemosIndex(componentName), {flag: 'w'});
}
const args = process.argv;
const componentName = args[2];
const demoNames = args[3];
if (args.length === 4) {
demoNames.split(',').forEach((demo) => { genDemo(componentName, demo); });
genIndex(componentName);
} else if (args.length === 3) {
genIndex(componentName);
} else {
console.log('Usage: node misc/demo-gen.js [component] [demo]\n');
}