forked from jhillman/coprogen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.js
More file actions
executable file
·86 lines (71 loc) · 2.33 KB
/
codegen.js
File metadata and controls
executable file
·86 lines (71 loc) · 2.33 KB
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
#! /usr/bin/env node
'use strict';
var fs = require('fs'),
CodeGenerator = require('./lib'),
argv = require('optimist').argv,
generator,
generatorMap,
processorMap,
destinationPath = process.cwd(),
templateDataPath = process.cwd() + '/code-generator.json',
templateData,
usage
;
generator = new CodeGenerator();
generatorMap = {
'content-provider': generator.generateContentProvider
};
processorMap = {
'content-provider': function(destination, templateData, cb) {
if (!templateData.relationships) {
templateData.relationships = [];
}
cb(destination, templateData);
}
};
function processData(destination, templateData) {
var dataJson;
if (argv.data) {
if (fs.existsSync(argv.data)) {
dataJson = require(process.cwd() + '/' + argv.data);
} else {
dataJson = JSON.parse(argv.data);
}
for (var key in dataJson) {
templateData[key] = dataJson[key];
}
}
processorMap[templateData.type](destination, templateData, function(destination, templateData) {
generatorMap[templateData.type](destination, templateData, function(err) {
if (err) console.log('CodeGenerator error: ' + err);
console.log('CodeGenerater has completed successfully!');
});
});
};
usage = function() {
console.log('\nCodeGenerator Usage:\n\n' +
'Run from a directory that contains a code-generator.json \n' +
'config file, or provide the path as an argument:\n' +
' codegen --path <relative path to CodeGenerator config file>\n\n' +
'You may also provide the destination directory as an argument:\n' +
' codegen --dest <relative path to the destination directory>\n\n' +
'Finally, you may also override or provide additional template \n' +
'data with the data parameter:\n' +
' codegen --data \'{"authority": "com.custom.authority"}\'\n'+
' codegen --data <relative path to .json file>');
process.exit(1);
};
if (argv.path) {
templateDataPath = process.cwd() + '/' + argv.path;
}
if (argv.dest) {
destinationPath = process.cwd() + '/' + argv.dest;
}
fs.exists(templateDataPath, function(exists) {
if (exists) {
templateData = require(templateDataPath);
processData(destinationPath, templateData);
} else {
usage();
}
});