-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdn-assembler.js
executable file
·144 lines (123 loc) · 3.91 KB
/
cdn-assembler.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
#!/usr/bin/env node
/*
* @license
* Copyright 2020 Brigham Young University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
"use strict";
const fs = require('fs-extra');
const yaml = require('node-yaml');
const path = require('path');
const moment = require('moment');
const log = require('winston');
const args = require('yargs')
.usage('$0 [args]')
// .config('config', 'path to CDN config YAML file', yaml.readSync)
.option('config', {
alias: 'c',
describe: 'path to CDN config YAML file'
})
.describe('env', 'environment')
.alias('env', 'e')
.choices('env', ['prd', 'stg', 'dev'])
.option('bucket', {
alias: 'b',
describe: 'target bucket',
})
.option('cdn-host', {
alias: 'h',
describe: 'CDN Hosting Hostname'
})
.option('github-credentials', {
describe: 'path to JSON file containing Github credentials, like {"user": "abc", "token": "def"}'
})
.option('github-user', {
describe: 'GitHub user to use'
})
.option('github-token', {describe: 'Github access token'})
.implies({
'github-user': 'github-token',
'github-token': 'github-user'
})
.conflicts('github-credentials', 'github-token')
.conflicts('github-credentials', 'github-user')
.group(['github-user', 'github-token', 'github-credentials'], 'GitHub Authentication:')
.option('dry-run', {describe: 'Skips the upload step'})
.option('work-dir', {
alias: 'w',
describe: 'Directory in which to assemble the CDN contents. Must be empty.'
})
.describe('verbose', 'turns on verbose logging')
.alias('verbose', 'v')
.boolean('verbose')
.default('work-dir', '.tmp')
.boolean('dry-run')
.boolean('force-build')
.demandOption(['config', 'bucket', 'cdn-host'], 'You must specify all of: config, bucket, and cdn-host')
.env("CDN")
.help()
.check(checkArgs)
.argv;
let start = moment();
runAssembler(args)
.then(() => {
console.log(`======= Finished Assembly in ${start.fromNow(true)} =======`)
})
.catch(err => {
console.error('ERROR:', err);
process.exit(1);
});
async function runAssembler(args) {
if (args.verbose) {
log.level = 'debug';
}
let config = await loadConfig(args.config);
const run = require('../index');
await run(config, args.bucket, {
dryRun: args.dryRun,
workDir: args.workDir,
githubCredentials: await loadGithubCredentials(args),
env: args.env,
cdnHost: args.cdnHost,
forceBuild: args.forceBuild,
})
}
async function loadConfig(location) {
return yaml.read(path.resolve(location));
}
async function loadGithubCredentials(args) {
if (args.githubUser && args.githubToken) {
return {
user: args.githubUser,
token: args.githubToken
};
} else if (args.githubCredentials) {
return fs.readJson(args.githubCredentials);
} else {
return null;
}
}
function checkArgs(argv, opts) {
fs.accessSync(argv.config);
if (argv.githubCredentials) {
fs.accessSync(argv.githubCredentials);
}
if (fs.pathExistsSync(argv.workDir)) {
let contents = fs.readdirSync(argv.workDir);
if (contents.length) {
console.warn(`${argv.workDir} isn't empty; contents will be overwritten!`)
}
}
return true;
}