forked from thomaschampagne/elevate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.js
242 lines (184 loc) · 5.9 KB
/
make.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
var fs = require('fs');
var path = require('path');
var nodeCopy;
var HOOK_FOLDER = __dirname + '/hook/';
var EXT_FOLDER = HOOK_FOLDER + 'extension/';
var DIST_FOLDER = __dirname + '/dist/';
var BUILD_FOLDER = __dirname + '/builds/';
var action = process.argv.slice(2)[0];
setTimeout(function() {
if (typeof action === 'undefined' || (action !== 'init' && action !== 'dist' && action !== 'build' && action !== 'clean')) {
showUsage();
} else {
switch (action) {
case 'init':
init();
break;
case 'dist':
dist();
break;
case 'build':
build();
break;
case 'clean':
clean(function() {
console.log('builds/, dist/ and node_modules/ folders cleaned');
});
break;
}
}
}.bind(this), 0);
/**
*
*/
var init = function(callback) {
var exec = require('child_process').exec;
var child = exec('npm install', function(error, stdout, stderr) {
process.chdir(HOOK_FOLDER);
console.log(stdout);
if (error !== null) {
console.log('exec error: ' + error);
} else {
process.chdir(EXT_FOLDER);
exec('npm install', function(error, stdout, stderr) {
process.chdir('..');
console.log(stdout);
if (error !== null) {
console.log('exec error: ' + error);
} else {
console.log('Node dependencies are installed.');
if (typeof callback !== 'undefined') {
callback();
}
}
}.bind(this));
}
}.bind(this));
};
/**
*
*/
var dist = function(callback) {
init(function() {
// Init finish require are now possible
nodeCopy = require('ncp').ncp;
// Clean dist/ folder or create it..
cleanDistributionFolder();
console.log('Making distribution folder...');
var options = {
filter: function(filenameToCopy) {
if (filenameToCopy.match('/docs/') ||
filenameToCopy.match('/src/') ||
filenameToCopy.match('/tests/') ||
filenameToCopy.match('/test/') ||
filenameToCopy.match('/demo/') ||
filenameToCopy.match('/grunt/') ||
filenameToCopy.match('/.*\\.gzip$') ||
filenameToCopy.match('/.*\\.md$') ||
filenameToCopy.match('/package\\.json') ||
filenameToCopy.match('/bower\\.json')) {
return false;
}
return true;
}
}
// Copy extension/ folder to ../dist/ folder
nodeCopy(EXT_FOLDER, DIST_FOLDER, options, function(err) {
if (err) {
return console.error(err);
} else {
console.log('Distribution folder finished. Sources are in dist/');
if (typeof callback !== 'undefined') {
callback();
}
}
});
});
};
/**
*
*/
var build = function() {
dist(function() {
if (!fs.existsSync(BUILD_FOLDER)) {
fs.mkdirSync(BUILD_FOLDER);
}
// Switch to dist/ folder
process.chdir(DIST_FOLDER);
var buildName = generateBuildName(DIST_FOLDER + '/manifest.json');
var outputPath = BUILD_FOLDER + '/' + buildName;
var archiver = require('archiver');
var output = fs.createWriteStream(outputPath);
var zipArchive = archiver('zip');
output.on('close', function() {
console.log('Build finished in ' + BUILD_FOLDER + buildName);
});
zipArchive.pipe(output);
zipArchive.bulk([{
src: ['**/*'],
cwd: '.',
expand: true
}]);
zipArchive.finalize(function(err, bytes) {
if (err) {
throw err;
}
console.log('done:', base, bytes);
});
});
};
var clean = function(callback) {
deleteFolderRecursive('node_modules');
deleteFolderRecursive(EXT_FOLDER + 'node_modules');
deleteFolderRecursive(DIST_FOLDER);
deleteFolderRecursive(BUILD_FOLDER);
callback();
};
/**
*
*/
var showUsage = function() {
console.log('Usage:');
console.log('node ' + path.basename(__filename) + ' <init|dist|build|clean>\r\n');
console.log('init: Install dependencies');
console.log('dist: Create distribution folder');
console.log('build: Create archive of distribution folder');
console.log('clean: Clean builds/, dist/ and node_modules/ folders');
};
/**
* Cleanup distribution directory
*/
var cleanDistributionFolder = function() {
if (fs.existsSync(DIST_FOLDER)) {
console.log('Clean distribution folder');
deleteFolderRecursive(DIST_FOLDER);
fs.mkdirSync(DIST_FOLDER);
} else {
console.log('Create distribution folder');
fs.mkdirSync(DIST_FOLDER);
}
}
/**
*
*/
var deleteFolderRecursive = function(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function(file, index) {
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
/**
*
*/
var generateBuildName = function(manifestFile) {
var manifestData = JSON.parse(fs.readFileSync(manifestFile).toString());
var d = new Date();
return 'StravaPlus_v' + manifestData.version + '_' + d.toDateString().split(' ').join('_') + '_' + d.toLocaleTimeString().split(':').join('_') + '.zip';
};