1 | var fs = require('fs');
|
2 | var path = require('path');
|
3 | var colors = require('colors');
|
4 | var fsExtra = require('fs-extra');
|
5 | var quit = require('./quit');
|
6 | var stdin = require('./helpers/stdin');
|
7 | var readFile = require('./readfilesync');
|
8 | var processTemplate = require('./helpers/process-template');
|
9 | var clean = require('./helpers/clean');
|
10 | var genTypes = require('./gen-types');
|
11 |
|
12 |
|
13 | module.exports = function (config) {
|
14 | var type = config._[1];
|
15 | var name = config._[2];
|
16 | var filePath;
|
17 | var folderPath;
|
18 | var file;
|
19 | var fileName;
|
20 | var relPath;
|
21 |
|
22 |
|
23 | if (name && name.indexOf('.js') === -1) {
|
24 | fileName = name + '.js';
|
25 | } else {
|
26 | fileName = name;
|
27 | }
|
28 |
|
29 | if (type === 'router') {
|
30 | folderPath = path.join(config.approot, config.clientfolder);
|
31 | filePath = path.join(folderPath, (fileName || 'router.js'));
|
32 | } else if (type === 'model') {
|
33 | if (!name) quit('please specify a name: ampersand gen model ' + '${your model name}'.magenta);
|
34 | folderPath = path.join(config.approot, config.clientfolder, config.modelfolder);
|
35 | filePath = path.join(folderPath, fileName);
|
36 | } else if (type === 'view') {
|
37 | if (!name) quit('please specify a name: ampersand gen view ' + '${your view name}'.magenta);
|
38 | folderPath = path.join(config.approot, config.clientfolder, config.viewfolder);
|
39 | filePath = path.join(folderPath, fileName);
|
40 | } else if (type === 'page') {
|
41 | if (!name) quit('please specify a name: ampersand gen page ' + '${your page name}'.magenta);
|
42 | folderPath = path.join(config.approot, config.clientfolder, config.pagefolder);
|
43 | filePath = path.join(folderPath, fileName);
|
44 | } else if (type === 'form') {
|
45 | if (!name) quit('please specify a path to the model: ampersand gen form ' + '${path to model}'.magenta);
|
46 | folderPath = path.join(config.approot, config.clientfolder, config.formsfolder);
|
47 | filePath = path.join(folderPath, path.basename(fileName));
|
48 | }
|
49 |
|
50 | if (name) {
|
51 | config.folderPath = folderPath;
|
52 | config.relPath = path.relative(process.cwd(), filePath);
|
53 | if (!config.force && fs.existsSync(filePath)) return quit('file already exists at: ' + (config.relPath).magenta + ' add ' + '-f'.magenta + ' to force');
|
54 | stdin(function (input) {
|
55 | if (input) {
|
56 | config.data = input;
|
57 | }
|
58 | if (type === 'model') {
|
59 | config.name = name;
|
60 | genTypes.model(config, function (err, result) {
|
61 | var modelFilePath = path.join(config.folderPath, result.modelFileName);
|
62 | var collectionFilePath = path.join(config.folderPath, result.collectionFileName);
|
63 | fsExtra.createFileSync(modelFilePath);
|
64 | fs.writeFileSync(modelFilePath, clean(result.model, config), 'utf8');
|
65 | console.log('\nnew ' + 'Model'.magenta + ' created as ' + path.relative(process.cwd(), modelFilePath).magenta);
|
66 | if (config.makecollection) {
|
67 | fsExtra.createFileSync(collectionFilePath);
|
68 | fs.writeFileSync(collectionFilePath, clean(result.collection, config), 'utf8');
|
69 | console.log('new ' + 'Collection'.magenta + ' for ' + name.magenta + ' created as ' + path.relative(process.cwd(), collectionFilePath).magenta);
|
70 | }
|
71 | console.log('');
|
72 | quit();
|
73 | });
|
74 | } else if (type === 'form') {
|
75 | genTypes.form({modelpath: name}, function (err, code) {
|
76 | fsExtra.createFileSync(filePath);
|
77 | fs.writeFileSync(filePath, clean(code, config), 'utf8');
|
78 | quit('new ' + 'Form'.magenta + ' for ' + path.basename(fileName).magenta + ' created as ' + path.relative(process.cwd(), filePath).magenta);
|
79 | });
|
80 | } else {
|
81 | file = readFile(config[type]);
|
82 | if (!file) return quit('no template file found at ' + config[type]);
|
83 | fsExtra.createFileSync(filePath);
|
84 | file = clean(processTemplate(file, config), config);
|
85 | fs.writeFileSync(filePath, file, 'utf8');
|
86 | quit('new ' + (type).magenta + ' created as ' + (config.relPath).magenta, 0);
|
87 | }
|
88 | });
|
89 | } else {
|
90 | quit('no such command');
|
91 | }
|
92 | }
|