This repository has been archived by the owner on Oct 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
190 lines (162 loc) · 5.56 KB
/
index.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// (c) Andrew Wei
'use strict';
const _ = require('underscore.string');
const chalk = require('chalk');
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const yeoman = require('yeoman-generator');
const yosay = require('yosay');
module.exports = yeoman.Base.extend({
constructor: function() {
yeoman.Base.apply(this, arguments);
this.argument('projectname', {
type: String,
required: false
});
this.appname = _.slugify(this.projectname || this.appname);
this.option('skip-install', {
desc: 'Skips the installation of dependencies',
type: Boolean
});
},
initializing: function() {
this.pkg = require('../package.json');
this.secretKey = require('crypto').randomBytes(Math.ceil(50 * 3 / 4)).toString('base64');
this.paths = {
src: 'app',
build: 'build',
tmp: '.tmp'
};
},
prompting: {
welcome: function() {
let done = this.async();
this.log(yosay('\'Allo \'allo! Out of the box I include Browserify as well as Gulp to build your Django app.'));
if (!process.env.VIRTUAL_ENV && !this.options['skip-install']) {
this.log(chalk.yellow('\nWARNING: ') + 'You are not in a Python virtual environment. Please activate your Python environment before using this generator.');
return;
}
let prompts = [{
type: 'input',
name: 'appauthor',
message: 'Who is the author? (this will appear in the header of your source files)'
}];
this.prompt(prompts, (answers) => {
this.appauthor = answers.appauthor;
done();
});
},
database: function() {
let done = this.async();
let prompts = [{
type: 'list',
name: 'database',
message: 'Which database engine do you prefer?',
choices: [{
name: 'SQLite',
value: 'SQLite'
}, {
name: 'MySQL',
value: 'MySQL'
}, {
name: 'PostgreSQL',
value: 'PostgreSQL'
}]
}];
this.prompt(prompts, (answers) => {
this.database = answers.database;
done();
});
},
css: function() {
let done = this.async();
let prompts = [{
type: 'list',
name: 'css',
message: 'Which preprocessed CSS language do you prefer?',
choices: [{
name: 'Sass',
value: 'Sass'
}, {
name: 'Stylus',
value: 'Stylus'
}]
}];
this.prompt(prompts, (answers) => {
this.css = answers.css;
done();
});
}
},
writing: function() {
let files = glob.sync('**', { dot: true, cwd: this.sourceRoot(), nodir: true });
for (let i = 0; i < files.length; i++) {
let f = files[i];
let src = path.join(this.sourceRoot(), f);
let basename = path.basename(f);
switch (basename) {
case '.DS_Store':
// Do nothing
break;
case 'uwsgi.ini':
case 'nginx.conf':
this.template(src, path.join(path.dirname(f), `${this.appname}_${basename}`));
break;
case 'application':
let t = `${basename}.css`;
if (this.css === 'Sass') t = `${basename}.scss`;
if (this.css === 'Stylus') t = `${basename}.styl`;
this.template(src, path.join(path.dirname(f), t));
break;
case 'gitignore':
case 'secrets':
this.template(src, path.join(path.dirname(f), `.${basename}`));
break;
default:
if (~['.png', '.jpg', '.ico'].indexOf(path.extname(basename)))
this.copy(src, path.join(path.dirname(f), basename));
else
this.template(src, path.join(path.dirname(f), basename));
}
}
},
install: {
pip: function() {
let done = this.async();
if (this.options['skip-install']) {
this.log('Skipping pip dependency installation. You will have to manually run ' + chalk.yellow.bold('pip install -r requirements.txt') + '.');
done();
} else {
if (process.env.VIRTUAL_ENV) {
this.log(chalk.magenta('Installing pip dependencies...'));
this.spawnCommand('pip', ['install', '-r', 'requirements.txt']).on('exit', function() {
// Add environment variables to virtualenv.
let envs = this.readFileAsString(this.destinationPath('.secrets')).replace(/(^#.+$)/gm, '').replace(/(^\n)/gm, '');
let file = path.join(process.env.VIRTUAL_ENV, 'bin/activate');
let venv = this.readFileAsString(file) + '\n' + envs;
this.writeFileFromString(venv, file);
done();
}.bind(this));
} else {
this.log(chalk.red('Pip dependencies are not installed because there is no virtualenv detected. Please create/activate your virtualenv and manually install pip dependencies with ') + chalk.yellow.bold('pip install -r requirements.txt') + '.');
done();
}
}
},
npm: function() {
if (this.options['skip-install']) {
this.log('Skipping node dependency installation. You will have to manually run ' + chalk.yellow.bold('npm install') + '.');
} else {
this.log(chalk.magenta('Installing node modules for you using your ') + chalk.yellow.bold('package.json') + chalk.magenta('...'));
this.installDependencies({
skipMessage: true,
bower: false
});
}
}
},
end: function() {
this.log(chalk.green('Finished generating app! Reactivate your virtual environment to load the new environment variables.'));
}
});