-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·84 lines (79 loc) · 2.3 KB
/
main.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
#!/usr/bin/env node
var program = require('commander'),
winston = require('winston'),
fs = require('fs'),
path = require('path'),
build = require('./lib/build'),
u = require('./lib/util'),
logger = new winston.Logger({
transports: [
new winston.transports.Console({
handleExceptions: true,
colorize: true
})
],
exitOnError: false
}),
generate = function(entity, structure, options) {
if (!u.isAvailableEntity(entity)) {
return logger.error(['Provided entity "', entity, '" is not supported'].join(''));
}
var filePath = u.mkdirp(u.getEntityPath.apply(u, arguments)),
writeStream = fs.createWriteStream(filePath),
fileTestPath = null;
call(build.pipe, arguments).pipe(writeStream);
logger.info('Created ', filePath);
_generateTest(filePath, options && options.test);
},
_generateTest = function(filePath, mask) {
if (mask) {
var fileTestPath = u.getFileTestPath(filePath, mask);
u.createEmptyFile(fileTestPath);
logger.info('Test created ', fileTestPath);
}
},
destroy = function(entity, structure, options) {
if (!u.isAvailableEntity(entity)) {
return logger.error(['Provided entity "', entity, '" is not supported'].join(''));
}
var filePath = u.getEntityPath.apply(u, arguments),
fileTestPath = null;
fs.unlink(filePath, function (err) {
if (err) {
if (err.code === 'ENOENT') {
return logger.info('Nothing to remove');
} else {
throw err;
}
}
logger.info('Removed ', filePath);
_destroyTest(filePath, options && options.test);
});
},
_destroyTest = function(filePath, mask) {
if (mask) {
var fileTestPath = u.getFileTestPath(filePath, mask);
fs.unlink(fileTestPath, function (err) {
logger.info('Test removed ', fileTestPath);
});
}
},
call = function(fn, args) {
fn.logger = logger;
return fn.apply(fn, args);
};
program.version("0.0.1");
program
.command('generate <entity> <name>')
.description('Generates new entity')
.option('-m, --module <module>', 'Module name')
.option('-t, --test <test>', 'Set mask of the test file')
.alias('g')
.action(generate);
program
.command('destroy <entity> <name>')
.description('Destroy entity, that was generated by "generate" command')
.option('-t, --test <test>', 'Set mask of the test file')
.alias('d')
.action(destroy);
program.parse(process.argv);