-
Notifications
You must be signed in to change notification settings - Fork 1
/
setlife.js
57 lines (43 loc) · 1.49 KB
/
setlife.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
#!/usr/bin/env node
'use strict';
process.bin = process.title = 'setlife';
var program = require('commander');
var pkg = require('./package.json');
var fs = require('fs-extra');
var path = require('path');
var ejs = require('ejs');
var _ = require('lodash');
var generateRnComponent = function(name, options) {
console.log('Creating ' + name + ' component...');
var templatePath = path.join(process.cwd(), 'src/templates', 'component.ejs');
var destinationPath = path.join(process.cwd(), 'src/components', name + '.js');
var redux = options.redux;
fs.readFile(templatePath, 'utf8', function(err, data) {
var component = ejs.render(data, {
name: name,
redux: options.redux || false
});
fs.writeFile(destinationPath, component);
console.log('Created component: ' + name + '...');
});
};
// Parse command line options
program
.version(pkg.version)
.usage('<command> [options]');
program
.command('create-rn-component <name>')
.option('-r, --redux', 'Connect Redux state mappings')
.description('Generate a new React Native component.')
.action(function(name, options) {
generateRnComponent(name, options);
});
// Failsafe that shows the help dialogue if the command is not recognized (`$ react xyz`)
program.on('*', function(opt) {
program.help();
});
program.parse(process.argv);
// Handle case where no command is passed (`$ react`)
if (!process.argv.slice(2).length) {
program.help();
}