-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
executable file
·145 lines (134 loc) · 4.29 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
#!/usr/bin/env node
const {argv} = require('yargs')
.usage('$0 [options] <assetsFolder>')
.demand(1)
.env('CDN_UPLOADER')
.option('app-prefix', {
alias: 'a',
describe: 'Application prefix used in the CDN url',
demand: true,
type: 'string',
})
.option('key-filename', {
alias: 'k',
describe: `JSON key file used to authenticate with Google Cloud Platform.
If not set, the credentials option is used.`,
type: 'string',
})
.option('credentials', {
alias: 'c',
describe: `Stringified and base64 encoded version of the JSON key file used to authenticate with Google Cloud Platform.
Can also be set as CDN_UPLOADER_CREDENTIALS environment variable`,
type: 'string',
})
.option('bucket-name', {
alias: 'b',
default: 'fiaas-assets',
describe: 'Google Cloud Storage bucket to use.',
type: 'string',
})
.option('project-id', {
alias: 'p',
default: 'fiaas-gke',
describe: 'Google Cloud Storage projectId.',
type: 'string',
})
.option('cache-control', {
default: 'public, max-age=2592000',
describe: 'Override the cache-control header for the assets',
type: 'string',
})
.option('flatten', {
alias: 'f',
describe: 'Flatten filestructure',
default: false,
type: 'boolean',
})
.option('dry-run', {
alias: 'n',
describe: 'Print a list of which files would be uploaded',
type: 'boolean',
})
.option('resumable', {
alias: 'r',
describe: 'Resumable upload',
default: true,
type: 'boolean',
})
.option('validation', {
alias: 'V',
describe: 'Validation for upload',
default: true,
type: 'boolean',
})
.option('batch-size', {
alias: 's',
describe: 'How many files to upload in each batch',
default: 100,
type: 'number',
})
.help()
.version()
.alias('help', ['h', '?'])
.alias('version', 'v');
const updateNotifier = require('update-notifier');
const { upload, getAllAssetsToUpload } = require('./lib/uploader');
const pkg = require('./package.json');
updateNotifier({ pkg }).notify();
const options = { ...argv, assetsFolder: argv._[0]};
function loadCredentials() {
if (options.keyFilename) {
return require(options.keyFilename);
} if (options.credentials) {
try {
return JSON.parse(
Buffer.from(options.credentials, 'base64').toString('utf8')
);
} catch (err) {
console.error('Unable to parse credentials string', err);
process.exit(1);
}
} else {
console.error(
'You must either specify the key-filename or the credentials string to authenticate with Google Cloud Platform'
);
process.exit(1);
}
return '';
}
const getGoogleUrl = dest =>
`https://storage.googleapis.com/${options.bucketName}/${dest}`;
if (options.dryRun) {
// Lazy load these deps
const { blue, yellow, green } = require('chalk');
const table = require('text-table');
const text = getAllAssetsToUpload(options)
.map(({ path, destination }) => ({
file: path,
destination: getGoogleUrl(destination),
}))
.map(({ file, destination }) => [
blue(file),
yellow('->'),
green(destination),
]);
console.log('---Files that would be uploaded---');
console.log(table(text));
} else {
// Avoid loading credentials if we're in a dry-run
upload(Object.assign(options, { credentials: loadCredentials() })).then(
({results: uploadedAssets, errors}) => {
if (Array.isArray(uploadedAssets) && uploadedAssets.length > 0) {
console.log('\n---Uploaded assets---');
uploadedAssets
.map(item => item.destination)
.map(getGoogleUrl)
.forEach(s => console.log(s));
}
if (Array.isArray(errors) && errors.length > 0) {
console.log('\n---Failing assets---');
errors.forEach(e => console.log(`${e.item.path}: ${e.message}`));
}
}
);
}