-
Notifications
You must be signed in to change notification settings - Fork 5
/
generator.js
90 lines (77 loc) · 2.91 KB
/
generator.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
#!/usr/bin/env/ node
const chalk = require('chalk');
const _ = require('lodash');
const inquirer = require('inquirer');
const fs = require('fs-extra');
const path = require('path');
const walk = require('klaw-sync');
function resolve(dir) {
return path.join(__dirname, dir);
}
const TEMPLATE_PATH = resolve('templates');
// User prompts
const QUESTIONS = [
{
name: 'name',
type: 'input',
message: 'What is the component name? (kebab-case without Cdr/Cedar prefix)',
validate: (input) => {
if (_.startsWith(input.toLowerCase(), 'cdr')) return chalk.red('Name should be unprefixed (no cdr)');
if (_.startsWith(input.toLowerCase(), 'cedar')) return chalk.red('Name should be unprefixed (no cedar)');
if (/^([a-z]+(-[a-z]+)*)$/.test(input)) return true;
return chalk.red('Component name must be kebab-case');
},
},
];
// use answers
inquirer.prompt(QUESTIONS).then((answers) => {
const { name } = answers; // test-comp
const camelName = _.camelCase(name); // testComp
const pascalName = _.upperFirst(camelName); // TestComp
const compName = `Cdr${pascalName}`; // CdrTestComp
const tagName = _.kebabCase(compName); // cdr-test-comp
const fullName = `cedar-${name}`; // cedar-test-comp
const outDir = resolve(`src/components/${camelName}`);
// exit if the component already exists
if (fs.existsSync(outDir)) {
console.log(chalk.red.bold(`A component named '${name}' already exists`));
process.exit(1);
}
// copy template files
fs.copySync(TEMPLATE_PATH, outDir);
// go through files to update values
const files = walk(outDir);
files.forEach((file) => {
if (fs.lstatSync(file.path).isDirectory()) {
return;
}
let filePath = file.path;
// rename files that need it
if (file.path.indexOf('[name]') > 0) {
filePath = file.path.replace(/\[name\]/, compName);
fs.renameSync(file.path, filePath);
}
if (file.path.indexOf('[ex-name]') > 0) {
filePath = file.path.replace(/\[ex-name\]/, pascalName);
fs.renameSync(file.path, filePath);
}
// replace placeholders within files
fs.readFile(filePath, 'utf-8', (err, contents) => {
if (err) throw err;
const rewrite = contents
.replace(/\{NAME-PASCAL\}/g, pascalName)
.replace(/\{NAME-TAGNAME\}/g, tagName)
.replace(/\{NAME-KEBAB\}/g, name)
.replace(/\{NAME-CAMEL\}/g, camelName)
.replace(/\{NAME-FULLNAME\}/g, compName)
.replace(/\{NAME-FULLKEBAB\}/g, fullName);
fs.outputFileSync(filePath, rewrite);
});
});
// Done!
console.log(chalk.green(`Successfuly generated files for '${name}' in ${outDir}`));
console.log(chalk.white('To complete setup do the following:'));
console.log(chalk.white('- Export component in src/index.js'));
console.log(chalk.white('- Export component example in src/components/examples.js'));
console.log(chalk.white('- Add dev router path in src/dev/router.js'));
});