1 | #!/usr/bin/env node
|
2 |
|
3 | var path = require('path');
|
4 | var fs = require('fs');
|
5 | var fse = require('fs-extra');
|
6 | var _ = require('lodash');
|
7 | var mkdirp = require('mkdirp');
|
8 | var program = require('commander');
|
9 |
|
10 | var ems = require('..');
|
11 | var pkg = require('../package.json');
|
12 |
|
13 | program
|
14 | .version(pkg.version)
|
15 | .usage('<target>')
|
16 | .option('-f, --force', 'force to overwrite if app already exists')
|
17 | .on('--help', function(){
|
18 | console.log(' Examples:');
|
19 | console.log('');
|
20 | console.log(' $ ems new my-spa-application');
|
21 | console.log(' $ ems new /path/to/my-spa-application');
|
22 | console.log('');
|
23 | })
|
24 | .parse(process.argv);
|
25 |
|
26 | if (!process.argv.slice(2).length) {
|
27 | program.outputHelp();
|
28 | } else {
|
29 | var target = path.resolve(process.cwd(), program.args[0]);
|
30 | var name = path.basename(target);
|
31 | if(fs.existsSync(target)) {
|
32 | if(!program.force) {
|
33 | console.error('Folder at path %s already exists', target);
|
34 | process.exit(0);
|
35 | } else {
|
36 | console.log('Folder already exists. Overwrite folder.');
|
37 | }
|
38 | }
|
39 |
|
40 | // process exit event
|
41 | process.on('exit', function(){
|
42 | console.log();
|
43 | console.log(' next:');
|
44 | console.log(' $ cd %s', path.relative(process.cwd(), target));
|
45 | console.log(' $ npm install');
|
46 | console.log(' $ ems start');
|
47 | console.log();
|
48 | console.log(' install dependencies with local bower, e.g.:');
|
49 | console.log(' $ npm run bower install --save jquery');
|
50 | console.log();
|
51 | console.log(' precompile assets with:');
|
52 | console.log(' $ ems precompile');
|
53 | console.log();
|
54 | console.log(' more infos: https://github.com/naxmefy/express-mincer-spa');
|
55 | console.log();
|
56 | });
|
57 |
|
58 | // setup app object
|
59 | var app = {
|
60 | name: name,
|
61 | target: target,
|
62 | ems: ems
|
63 | };
|
64 |
|
65 | // Generate Files
|
66 | mkdir(target, function(newPath) {
|
67 | mkdir(path.resolve(newPath, 'assets'), function(newPath) {
|
68 | mkdir(path.resolve(newPath, 'css'), function(newPath) {
|
69 | writeTemplateTo('app.styl', newPath, app);
|
70 | writeTemplateTo('font-fix.styl', newPath, app);
|
71 | });
|
72 | mkdir(path.resolve(newPath, 'js'), function(newPath) {
|
73 | writeTemplateTo('app.coffee', newPath, app);
|
74 | });
|
75 | mkdir(path.resolve(newPath, 'vendor'), function(newPath) {
|
76 | writeTemplateTo('.gitignore-assets', newPath, app, '.gitignore');
|
77 | });
|
78 | });
|
79 |
|
80 | mkdir(path.resolve(newPath, 'public'), function(newPath) {
|
81 | mkdir(path.resolve(newPath, 'assets'), function(newPath) {
|
82 | writeTemplateTo('.gitignore-assets', newPath, app, '.gitignore');
|
83 | });
|
84 | writeTemplateTo('favicon.ico', newPath, app, 'favicon.ico', true);
|
85 | writeTemplateTo('robots.txt', newPath, app);
|
86 | });
|
87 |
|
88 | mkdir(path.resolve(newPath, 'views'), function(newPath) {
|
89 | writeTemplateTo('index.jade', newPath, app);
|
90 | });
|
91 |
|
92 | writeTemplateTo('.bowerrc', newPath, app);
|
93 | writeTemplateTo('.editorconfig', newPath, app);
|
94 | writeTemplateTo('.gitignore-main', newPath, app, '.gitignore');
|
95 | writeTemplateTo('bower.json', newPath, app);
|
96 | writeTemplateTo('index.js', newPath, app);
|
97 | writeTemplateTo('LICENSE', newPath, app);
|
98 | writeTemplateTo('package.json', newPath, app);
|
99 | writeTemplateTo('README.md', newPath, app);
|
100 | });
|
101 | }
|
102 |
|
103 |
|
104 | /**
|
105 | * Check if the given directory `path` is empty.
|
106 | *
|
107 | * @param {String} path
|
108 | * @param {Function} fn
|
109 | */
|
110 | function emptyDirectory(path, fn) {
|
111 | fs.readdir(path, function(err, files){
|
112 | if (err && 'ENOENT' != err.code) throw err;
|
113 | fn(!files || !files.length);
|
114 | });
|
115 | }
|
116 |
|
117 | function writeTemplateTo(templateName, directory, app, as, copy) {
|
118 | if(as == null) {
|
119 | as = templateName;
|
120 | }
|
121 |
|
122 | if(copy !== true) {
|
123 | copy = false;
|
124 | }
|
125 |
|
126 | var file = path.resolve(__dirname, '..', 'application-templates', templateName);
|
127 | var target = path.resolve(directory, as);
|
128 | if(copy === false) {
|
129 | var data = fs.readFileSync(
|
130 | file,
|
131 | {"encoding":"UTF-8"}
|
132 | );
|
133 | var template = _.template(data)(app);
|
134 | write(target, template);
|
135 | } else {
|
136 | fse.copy(file, target);
|
137 | }
|
138 | }
|
139 |
|
140 | /**
|
141 | * echo str > path.
|
142 | *
|
143 | * @param {String} path
|
144 | * @param {String} str
|
145 | */
|
146 |
|
147 | function write(path, str) {
|
148 | fs.writeFile(path, str);
|
149 | console.log(' \x1b[36mcreate\x1b[0m : ' + path);
|
150 | }
|
151 |
|
152 | /**
|
153 | * Mkdir -p.
|
154 | *
|
155 | * @param {String} path
|
156 | * @param {Function} fn
|
157 | */
|
158 |
|
159 | function mkdir(path, fn) {
|
160 | mkdirp(path, 0755, function(err){
|
161 | if (err) throw err;
|
162 | console.log(' \033[36mcreate\033[0m : ' + path);
|
163 | fn && fn(path);
|
164 | });
|
165 | }
|
166 |
|
167 | /**
|
168 | * Exit with the given `str`.
|
169 | *
|
170 | * @param {String} str
|
171 | */
|
172 |
|
173 | function abort(str) {
|
174 | console.error(str);
|
175 | process.exit(1);
|
176 | } |
\ | No newline at end of file |