-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·99 lines (82 loc) · 2.83 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env node
const doT = require('dot');
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const minimist = require('minimist');
const process = require('process');
function generateComponent(args) {
const {componentName, basePath, componentsFolder, requirePath, boilerPlate} = args;
const settings = {
componentName,
requirePath,
};
function processTemplate(template, args) {
const {componentName, basePath, componentsFolder, requirePath, boilerPlate} = args;
const sourceName = path.join(boilerPlate, template);
const targetName = path.join(
basePath,
componentsFolder,
componentName,
template.replace('$COMPONENT_NAME$', componentName).replace('.dot', '')
);
const source = fs.readFileSync(sourceName, 'UTF8');
const compiled = doT.template(source);
const processed = compiled(settings);
fs.writeFileSync(targetName, processed);
}
function processTemplates(templates, args) {
const componentFolderPath = path.join(basePath, componentsFolder, componentName);
mkdirp(componentFolderPath).then(made => {
for (var i = 0; i < templates.length; i++) {
processTemplate(templates[i], args);
}
}).catch(err => {
console.err(err);
process.exit(1);
});
}
fs.readdir(boilerPlate, function(err, templates) {
if (err) {
console.error(err);
process.exit(1);
}
processTemplates(templates, args);
});
}
function parseArgs(args) {
const parsed = minimist(args);
return parsed;
}
function main(args) {
const options = parseArgs(args);
const COMPONENT_NAME = process.env.COMPONENT_NAME;
const BASE_PATH = process.env.BASE_PATH;
const COMPONENTS_FOLDER = process.env.COMPONENTS_FOLDER;
const REQUIRE_PATH = process.env.REQUIRE_PATH;
const BOILERPLATE = process.env.BOILERPLATE;
const componentName = options.c || options.componentName || COMPONENT_NAME;
const basePath = options.p || options.basePath || BASE_PATH;
const componentsFolder = options.f || options.componentsFolder || COMPONENTS_FOLDER;
const requirePath = options.r || options.requirePath || REQUIRE_PATH;
const boilerPlate = options.b || options.boilerPlate || BOILERPLATE;
if (!componentName || !basePath || !componentsFolder || !requirePath || !boilerPlate) {
console.log('Missing arguments');
if (!componentName) console.log('- componentName');
if (!basePath) console.log('- basePath');
if (!componentsFolder) console.log('- componentsFolder');
if (!requirePath) console.log('- requirePath');
if (!boilerPlate) console.log('- boilerPlate');
process.exit(1);
}
const merged = {
componentName,
basePath,
componentsFolder,
requirePath,
boilerPlate,
};
doT.templateSettings.strip = false;
generateComponent(merged);
}
main(process.argv.slice(2));