This repository has been archived by the owner on Oct 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 139
/
build.js
122 lines (103 loc) · 3.52 KB
/
build.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
(function() {
var path = require('path'),
prompt = require('prompt'),
fs = require('fs-extra'),
async = require('async'),
crypto = require('crypto'),
Q = require('q'),
_ = require('lodash'),
createHash = require('crypto').createHash,
recursive = require('recursive-readdir'),
hidefile = require('hidefile'),
chcpContext;
module.exports = {
execute: execute
};
function execute(context) {
chcpContext = context;
var executeDfd = Q.defer(),
config = prepareConfig(context),
ignore = context.ignoredFiles;
recursive(chcpContext.sourceDirectory, ignore, function(err, files) {
var hashQueue = prepareFilesHashQueue(files);
async.parallelLimit(hashQueue, 10, function(err, result) {
result.sort((a, b) => {
return a.file.localeCompare(b.file)
});
var json = JSON.stringify(result, null, 2);
var manifestFile = chcpContext.manifestFilePath;
fs.writeFile(manifestFile, json, function(err) {
if (err) {
return console.log(err);
}
if (context.argv && context.argv.localdev) {
config.update = 'now';
}
var json = JSON.stringify(config, null, 2);
fs.writeFile(chcpContext.projectsConfigFilePath, json, function(err) {
if (err) {
return console.log(err);
}
console.log('Build ' + config.release + ' created in ' + chcpContext.sourceDirectory);
executeDfd.resolve(config);
});
});
});
});
return executeDfd.promise;
}
function prepareFilesHashQueue(files) {
var queue = [];
for (var i in files) {
var file = files[i];
if (!hidefile.isHiddenSync(file)) {
queue.push(hashFile.bind(null, file));
}
}
return queue;
}
function prepareConfig(context) {
var config = {};
try {
config = fs.readFileSync(context.defaultConfig, 'utf8');
config = JSON.parse(config);
config.release = process.env.VERSION || calculateTimestamp();
} catch (e) {
config = {
autogenerated: true,
release: calculateTimestamp()
};
}
if (context.argv && context.argv.content_url) {
config.content_url = context.argv.content_url;
}
console.log('Config', config);
return config;
}
function hashFile(filename, callback) {
var hash = crypto.createHash('md5'),
stream = fs.createReadStream(filename);
//stream.pipe(writeStream);
//console.log('Hashing: ', filename);
stream.on('data', function(data) {
hash.update(data, 'utf8');
});
stream.on('end', function() {
var result = hash.digest('hex'),
file = path.relative(chcpContext.sourceDirectory, filename).replace(new RegExp("\\\\", "g"), "/");
callback(null, {
file: file,
hash: result
});
});
}
function calculateTimestamp() {
var currentdate = new Date();
return currentdate.getFullYear() + '.' +
(((currentdate.getMonth() + 1) < 10) ? '0' + (currentdate.getMonth() + 1) : (currentdate.getMonth() + 1)) + '.' +
((currentdate.getDate() < 10) ? '0' + currentdate.getDate() : currentdate.getDate()) + '-' +
((currentdate.getHours() < 10) ? '0' + currentdate.getHours() : currentdate.getHours()) + '.' +
((currentdate.getMinutes() < 10) ? '0' + currentdate.getMinutes() : currentdate.getMinutes()) + '.' +
((currentdate.getSeconds() < 10) ? '0' + currentdate.getSeconds() : currentdate.getSeconds());
}
})();