Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50665 views
1
var fs = require('fs'),
2
path = require('path'),
3
flatiron = require('../../flatiron'),
4
common = flatiron.common,
5
app = flatiron.app;
6
7
module.exports = function create(name, type, callback) {
8
type = type || 'http';
9
10
var existsSync = fs.existsSync || path.existsSync,
11
root = path.join(process.cwd(), name),
12
scaffold = path.join(__dirname, '..', '..', '..', 'scaffolds', type),
13
info = {};
14
15
if (!existsSync(scaffold)) {
16
app.log.error('Scaffold named ' + type.yellow + ' does not exist');
17
return callback();
18
}
19
20
//
21
// Prompts user for details about their app to put in `package.json`.
22
//
23
function prompt (next) {
24
var fields = ['name', 'author', 'description', 'homepage'];
25
app.prompt.override = {name: name};
26
app.prompt.start();
27
app.prompt.addProperties(info, fields, next);
28
}
29
30
//
31
// Creates directories specified in `/scaffolds/:type/directories.json`.
32
//
33
function createDirs(next) {
34
var dirs = common.directories.normalize(
35
common.mixin({}, flatiron.constants.DIRECTORIES, { '#ROOT': root }),
36
JSON.parse(fs.readFileSync(path.join(scaffold, 'directories.json'), 'utf8'))
37
);
38
39
Object.keys(dirs).forEach(function (name) {
40
app.log.info('Creating directory ' + name.grey);
41
});
42
43
common.directories.create(dirs, next);
44
}
45
46
//
47
// Creates files specified in `/scaffolds/:type/files.json`.
48
//
49
function createFiles(next) {
50
var files = common.directories.normalize(
51
common.mixin({}, flatiron.constants.DIRECTORIES, { '#ROOT': root }),
52
JSON.parse(fs.readFileSync(path.join(scaffold, 'files.json'), 'utf8'))
53
);
54
55
function copyFile(file, nextFile) {
56
app.log.info('Writing file ' + file.grey);
57
common.cpr(path.join(scaffold, file), files[file], nextFile);
58
}
59
60
common.async.mapSeries(Object.keys(files), copyFile, next);
61
}
62
63
//
64
// Creates a templated package.json from `/scaffolds/:type/package.json`.
65
//
66
function createPackage(next) {
67
var pkg = JSON.parse(fs.readFileSync(path.join(scaffold, 'package.json'), 'utf8'));
68
69
pkg.dependencies.flatiron = flatiron.version;
70
71
flatiron.common.mixin(pkg, info);
72
73
app.log.info('Writing ' + 'package.json'.grey);
74
fs.writeFile(path.join(root, 'package.json'), JSON.stringify(pkg, null, 2) + '\n', next);
75
}
76
77
app.log.info('Creating application ' + (name ? name.magenta : ''));
78
app.log.info('Using ' + type.yellow + ' scaffold.');
79
common.async.series([
80
prompt,
81
createDirs,
82
createPackage,
83
createFiles
84
], function onComplete(next) {
85
app.log.info('Application ' + info.name.magenta + ' is now ready');
86
callback();
87
}
88
);
89
}
90
91
module.exports.usage = [
92
'Generates a flatiron skeleton application. If no <type>',
93
'is specified an HTTP application will be created.',
94
'<type> can currently be either cli or http',
95
'',
96
'create <app-name> <type>',
97
];
98
99