-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·64 lines (51 loc) · 1.66 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
#!/usr/bin/env node
var directory = process.cwd();
var prompt = require('prompt');
var fs = require('fs');
var read = fs.readFileSync;
var mkdirp = require('mkdirp');
var template = read(__dirname + '/componentTemplate.js', 'utf8');
var filename;
var componentName;
var styleExists = false;
var init = function(){
prompt.start();
var schema = {
properties: {
name : {
description: 'What would you like to name your component?',
type: 'string',
pattern: /^[$A-Z_][0-9A-Z_$]*$/i,
message: 'Name must start with a letter and have no spaces',
default: 'Component',
required: true
},
}
};
prompt.get(schema, function (err, result) {
if (err) { return onErr(err) }
componentName = result.name;
write(componentName);
});
function onErr(err){
console.log(err);
return 1;
}
function capitalize(s) {
return s[0].toUpperCase() + s.slice(1);
}
function write(componentName){
var componentName = capitalize(componentName);
// check if component already exists in current directory
if (fs.existsSync(componentName)) {
console.error('Component: ' + componentName + ' already exists, aborting!');
return;
}
template = template.replace(/{{displayName}}/g,capitalize(componentName));
fs.writeFile(componentName + '.js', template, function (err) {
if (err) return console.log(err);
console.log(componentName + ' > ' + componentName + '.js');
});
}
}
module.exports = init();