-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.js
78 lines (67 loc) · 2.25 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
var ARTIFACT_NAME = 'sabbath-school-reader-latest.zip',
PROD_PROJECT_ID = 'blistering-inferno-8720',
STAGE_PROJECT_ID = 'sabbath-school-stage',
PROD_KEY_FILENAME = 'deploy-creds.json',
STAGE_KEY_FILENAME = 'deploy-creds-stage.json',
PROD_BUCKET = 'blistering-inferno-8720.appspot.com',
STAGE_BUCKET = 'sabbath-school-stage.appspot.com';
var fs = require('fs'),
archiver = require('archiver'),
gstorage = require('@google-cloud/storage');
var argv = require('optimist')
.usage("Build script\n" +
"Usage: $0 -b [string]")
.alias({"b": "branch"})
.describe({
"b": "branch"
})
.demand(["b"])
.argv;
var branch = argv.b;
if (branch.toLowerCase() == "master"){
projectId = PROD_PROJECT_ID;
keyFilename = PROD_KEY_FILENAME;
bucketName = PROD_BUCKET;
} else if (branch.toLowerCase() == "stage") {
projectId = STAGE_PROJECT_ID;
keyFilename = STAGE_KEY_FILENAME;
bucketName = STAGE_BUCKET;
} else {
return;
}
storage = gstorage({
projectId: projectId,
keyFilename: keyFilename
});
bucket = storage.bucket(bucketName);
// create a file to stream archive data to.
var output = fs.createWriteStream(__dirname + '/' + ARTIFACT_NAME);
var archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
// listen for all archive data to be written
output.on('close', function() {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and heethe output file descriptor has closed.');
bucket.upload("./"+ARTIFACT_NAME, function(err, file){
if (!err){
console.log(ARTIFACT_NAME + ' (' + archive.pointer() + ' bytes) uploaded');
} else {
console.log(err);
}
});
});
// good practice to catch this error explicitly
archive.on('error', function(err) {
throw err;
});
// pipe archive data to the file
archive.pipe(output);
// Appending reader related assets
var index = __dirname + '/index.html';
archive.append(fs.createReadStream(index), { name: 'index.html' });
archive.directory('css/');
archive.directory('fonts/');
archive.directory('js/');
// finalize the archive (ie we are done appending files but streams have to finish yet)
archive.finalize();