@leverege/build-tools 2.92.0 → 2.93.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +3 -0
- package/package.json +8 -3
- package/src/Utils.mjs +9 -0
- package/src/artifacts/ArtifactPuller.mjs +21 -0
- package/src/artifacts/ArtifactPusher.mjs +133 -0
- package/src/artifacts/Structs.mjs +63 -0
- package/src/artifacts/Utils.mjs +49 -0
- package/src/artifacts/pull-artifact.mjs +7 -0
- package/src/artifacts/push-artifact.mjs +54 -0
- package/leverege-build-tools-2.21.13.tgz +0 -0
- package/lib/server/build-tools.js +0 -138
- package/lib/server/firebaseDeploy.js +0 -195
- package/lib/server/firebaseServe.js +0 -116
- package/lib/server/getfbcfg.js +0 -25
- package/lib/server/getjson.js +0 -70
- package/lib/server/overwhelm.js +0 -447
- package/lib/server/push-my-chart.js +0 -18
- package/lib/server/refresh-npm-token.js +0 -135
- package/lib/server/tag-release.js +0 -681
- package/lib/server/unleash.js +0 -50
- package/lib/web/build-tools.js +0 -138
- package/lib/web/firebaseDeploy.js +0 -195
- package/lib/web/firebaseServe.js +0 -116
- package/lib/web/getfbcfg.js +0 -25
- package/lib/web/getjson.js +0 -70
- package/lib/web/overwhelm.js +0 -447
- package/lib/web/push-my-chart.js +0 -18
- package/lib/web/refresh-npm-token.js +0 -135
- package/lib/web/tag-release.js +0 -681
- package/lib/web/unleash.js +0 -50
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/* eslint-disable no-console */
|
|
3
|
-
// https://github.com/google/zx
|
|
4
|
-
/* eslint-disable max-len */
|
|
5
|
-
"use strict";
|
|
6
|
-
|
|
7
|
-
var _enquirer = _interopRequireDefault(require("enquirer"));
|
|
8
|
-
var _ansiColors = _interopRequireDefault(require("ansi-colors"));
|
|
9
|
-
var _zx = require("zx");
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
-
const {
|
|
12
|
-
prompt
|
|
13
|
-
} = _enquirer.default;
|
|
14
|
-
|
|
15
|
-
// This will preserve coloration from spawned child processes
|
|
16
|
-
process.env.FORCE_COLORS = 3;
|
|
17
|
-
process.env.FORCE_COLOR = '1';
|
|
18
|
-
let TARGET;
|
|
19
|
-
let CHANNEL;
|
|
20
|
-
|
|
21
|
-
// A single unnamed parameter is assumed to be a deploy target.
|
|
22
|
-
// If named parameters exist, they may not be stored in argv,
|
|
23
|
-
// so don't look for a larger length of argv to decide whether to look for named paramters
|
|
24
|
-
if (_zx.argv._.length === 1) {
|
|
25
|
-
TARGET = _zx.argv._[0];
|
|
26
|
-
} else {
|
|
27
|
-
TARGET = _zx.argv.target;
|
|
28
|
-
if (_zx.argv.channelName) {
|
|
29
|
-
CHANNEL = {};
|
|
30
|
-
CHANNEL.name = _zx.argv.channelName;
|
|
31
|
-
CHANNEL.expiration = _zx.argv.channelExpiration ?? '7d';
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// If target has not been defined on the command line,
|
|
36
|
-
// then enter interactive mode
|
|
37
|
-
const INTERACTIVE_MODE = !TARGET;
|
|
38
|
-
let exited = false;
|
|
39
|
-
const SECRETS_DIR = 'secrets';
|
|
40
|
-
const EXIT_EVENTS = ['SIGINT', 'exit', 'uncaughException', 'unhandledRejection'];
|
|
41
|
-
const [firebaserc, firebaseJson, gitBranch] = await Promise.all([_zx.fs.exists(_zx.path.join(process.cwd(), '.firebaserc')), _zx.fs.exists(_zx.path.join(process.cwd(), 'firebase.json')), (0, _zx.$)`git rev-parse --abbrev-ref HEAD -C ${process.cwd()}`.then(({
|
|
42
|
-
stdout
|
|
43
|
-
}) => {
|
|
44
|
-
return stdout.split('\n')[0];
|
|
45
|
-
})]);
|
|
46
|
-
if (!firebaserc) {
|
|
47
|
-
console.log(_zx.chalk.red('No .firebaserc file found. Make sure that you are executing firebaseDeploy from within a firebase project directory.'));
|
|
48
|
-
}
|
|
49
|
-
if (!firebaseJson) {
|
|
50
|
-
console.log(_zx.chalk.red('No firebase.json file found. Make sure that you are executing firebaseDeploy from within a firebase project directory.'));
|
|
51
|
-
}
|
|
52
|
-
if (!firebaserc || !firebaseJson) {
|
|
53
|
-
process.exit();
|
|
54
|
-
}
|
|
55
|
-
const firebasercContent = JSON.parse(await _zx.fs.readFile(_zx.path.join(process.cwd(), '.firebaserc'), {
|
|
56
|
-
encoding: 'utf-8'
|
|
57
|
-
}));
|
|
58
|
-
let maxSiteIdLength = 0;
|
|
59
|
-
const targets = Object.entries(firebasercContent.targets).reduce((prev, [projectId, config]) => {
|
|
60
|
-
Object.entries(config.hosting).forEach(([siteId, aliases]) => {
|
|
61
|
-
aliases.forEach(alias => {
|
|
62
|
-
// Store the longest length of site id for formatting later on
|
|
63
|
-
maxSiteIdLength = Math.max(siteId.length, maxSiteIdLength);
|
|
64
|
-
prev[alias] = {
|
|
65
|
-
projectId,
|
|
66
|
-
siteId,
|
|
67
|
-
alias,
|
|
68
|
-
production: config.production
|
|
69
|
-
};
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
return prev;
|
|
73
|
-
}, {});
|
|
74
|
-
if (INTERACTIVE_MODE) {
|
|
75
|
-
const {
|
|
76
|
-
targetEnv
|
|
77
|
-
} = await prompt([{
|
|
78
|
-
type: 'autocomplete',
|
|
79
|
-
name: 'targetEnv',
|
|
80
|
-
message: 'To which environment would you like to deploy?',
|
|
81
|
-
choices: Object.values(targets).map(t => {
|
|
82
|
-
const productionLabel = t.production ? _ansiColors.default.bold.red('--PRODUCTION--') : '';
|
|
83
|
-
return {
|
|
84
|
-
message: `${_ansiColors.default.bold.cyan('Site: ')}${t.siteId.padEnd(maxSiteIdLength)} ${_ansiColors.default.bold.green('Project: ')}${t.projectId} ${productionLabel}`,
|
|
85
|
-
value: t.alias
|
|
86
|
-
};
|
|
87
|
-
})
|
|
88
|
-
}]);
|
|
89
|
-
TARGET = targets[targetEnv];
|
|
90
|
-
if (TARGET.production) {
|
|
91
|
-
const {
|
|
92
|
-
confirmProductionDeploy
|
|
93
|
-
} = await prompt([{
|
|
94
|
-
type: 'confirm',
|
|
95
|
-
name: 'confirmProductionDeploy',
|
|
96
|
-
message: `${_ansiColors.default.bold.red('WARNING: ')} This is a ${_ansiColors.default.bold.yellow('PRODUCTION')} environment. Are you sure you want to deploy here?`
|
|
97
|
-
}]);
|
|
98
|
-
if (!confirmProductionDeploy) {
|
|
99
|
-
console.log('Cancelling deployment...');
|
|
100
|
-
process.exit();
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
const {
|
|
104
|
-
useChannel
|
|
105
|
-
} = await prompt([{
|
|
106
|
-
type: 'confirm',
|
|
107
|
-
name: 'useChannel',
|
|
108
|
-
message: 'Do you want to deploy to a temporary preview channel?'
|
|
109
|
-
}]);
|
|
110
|
-
if (useChannel) {
|
|
111
|
-
const {
|
|
112
|
-
channelName,
|
|
113
|
-
channelExpiration
|
|
114
|
-
} = await prompt([{
|
|
115
|
-
type: 'input',
|
|
116
|
-
name: 'channelName',
|
|
117
|
-
message: 'To which channel would you like to deploy?',
|
|
118
|
-
initial: gitBranch ?? 'temp-deploy'
|
|
119
|
-
}, {
|
|
120
|
-
type: 'input',
|
|
121
|
-
name: 'channelExpiration',
|
|
122
|
-
message: 'When should the channel expire?',
|
|
123
|
-
initial: '7d'
|
|
124
|
-
}]);
|
|
125
|
-
CHANNEL = {
|
|
126
|
-
name: channelName,
|
|
127
|
-
expiration: channelExpiration
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
} else {
|
|
131
|
-
TARGET = targets[TARGET];
|
|
132
|
-
}
|
|
133
|
-
if (!TARGET) {
|
|
134
|
-
console.log(_zx.chalk.red('No such target exists in .firebaserc config.'));
|
|
135
|
-
process.exit();
|
|
136
|
-
}
|
|
137
|
-
const [aliasFileExists, siteIdFileExists, sharedFileExists] = await Promise.all([_zx.fs.exists(_zx.path.join(process.cwd(), SECRETS_DIR, `${TARGET.alias}.env`)), _zx.fs.exists(_zx.path.join(process.cwd(), SECRETS_DIR, `${TARGET.siteId}.env`)), _zx.fs.exists(_zx.path.join(process.cwd(), SECRETS_DIR, 'shared.env'))]);
|
|
138
|
-
if (!aliasFileExists && !siteIdFileExists) {
|
|
139
|
-
console.log(_zx.chalk.red(`No env file for ${TARGET.alias} exists. Check your secrets directory and try again`));
|
|
140
|
-
process.exit();
|
|
141
|
-
}
|
|
142
|
-
const exec = [];
|
|
143
|
-
if (sharedFileExists) {
|
|
144
|
-
exec.push(_zx.fs.readFile(_zx.path.join(process.cwd(), SECRETS_DIR, 'shared.env'), {
|
|
145
|
-
encoding: 'utf-8'
|
|
146
|
-
}));
|
|
147
|
-
}
|
|
148
|
-
if (aliasFileExists) {
|
|
149
|
-
exec.push(_zx.fs.readFile(_zx.path.join(process.cwd(), SECRETS_DIR, `${TARGET.alias}.env`), {
|
|
150
|
-
encoding: 'utf-8'
|
|
151
|
-
}));
|
|
152
|
-
} else {
|
|
153
|
-
exec.push(_zx.fs.readFile(_zx.path.join(process.cwd(), SECRETS_DIR, `${TARGET.siteId}.env`), {
|
|
154
|
-
encoding: 'utf-8'
|
|
155
|
-
}));
|
|
156
|
-
}
|
|
157
|
-
console.log(`\n\n\n${_ansiColors.default.bold.green('====== Deployment Summary ======')}`);
|
|
158
|
-
console.log(`${_ansiColors.default.blue('Firebase project:')} ${TARGET.projectId} ${TARGET.production ? _ansiColors.default.yellow('--PRODUCTION--') : ''}`);
|
|
159
|
-
console.log(`${_ansiColors.default.blue('Site ID:')} ${TARGET.siteId}`);
|
|
160
|
-
console.log(`${_ansiColors.default.blue('Channel Name:')} ${CHANNEL?.name ?? _ansiColors.default.italic.gray('(none)')}`);
|
|
161
|
-
console.log(`${_ansiColors.default.blue('Channel Expiration date:')} ${CHANNEL?.name ? CHANNEL.expiration ?? _ansiColors.default.italic.gray('(none)') : _ansiColors.default.italic.gray('(n/a)')}`);
|
|
162
|
-
console.log(`${_ansiColors.default.blue('Shared .env file:')} ${sharedFileExists ? `${SECRETS_DIR}/shared.env` : _ansiColors.default.italic.gray('(none)')}`);
|
|
163
|
-
console.log(`${_ansiColors.default.blue('Additional .env overrides:')} ${aliasFileExists ? `${SECRETS_DIR}/${TARGET.alias}.env` : `${SECRETS_DIR}/${TARGET.siteId}.env`}`);
|
|
164
|
-
console.log(`${_ansiColors.default.bold.green('===============================')}`);
|
|
165
|
-
if (INTERACTIVE_MODE) {
|
|
166
|
-
const {
|
|
167
|
-
confirmSelections
|
|
168
|
-
} = await prompt([{
|
|
169
|
-
type: 'confirm',
|
|
170
|
-
name: 'confirmSelections',
|
|
171
|
-
message: 'Proceed with this deployment?'
|
|
172
|
-
}]);
|
|
173
|
-
if (!confirmSelections) {
|
|
174
|
-
console.log('Cancelling deployment...');
|
|
175
|
-
process.exit();
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
const envFileContent = (await Promise.all(exec)).join('\n');
|
|
179
|
-
EXIT_EVENTS.forEach(event => {
|
|
180
|
-
process.on(event, () => {
|
|
181
|
-
if (!exited) {
|
|
182
|
-
exited = true;
|
|
183
|
-
_zx.fs.removeSync(_zx.path.join(process.cwd(), '.env.temp'));
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
});
|
|
187
|
-
await _zx.fs.writeFile(_zx.path.join(process.cwd(), '.env.temp'), envFileContent);
|
|
188
|
-
try {
|
|
189
|
-
// deploying to a channel requires slightly different parameter structure
|
|
190
|
-
const deployType = CHANNEL ? `hosting:channel:deploy ${CHANNEL.name} ${CHANNEL.expiration ? `--expires ${CHANNEL.expiration}` : ''}`.split(' ') : 'deploy';
|
|
191
|
-
const hostingPrefix = CHANNEL ? '' : 'hosting:';
|
|
192
|
-
await (0, _zx.$)`npm run clean && DOTENV_CONFIG_PATH=${_zx.path.join(process.cwd(), '.env.temp')} DEPLOYMENT_TARGET=${TARGET.siteId} npm run build && firebase use ${TARGET.projectId} && firebase ${deployType} --only ${hostingPrefix}${TARGET.siteId}`;
|
|
193
|
-
} catch (err) {
|
|
194
|
-
console.error(err);
|
|
195
|
-
}
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// https://github.com/google/zx
|
|
3
|
-
/* eslint-disable max-len */
|
|
4
|
-
/* eslint-disable no-console */
|
|
5
|
-
"use strict";
|
|
6
|
-
|
|
7
|
-
var _enquirer = _interopRequireDefault(require("enquirer"));
|
|
8
|
-
var _zx = require("zx");
|
|
9
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
-
const {
|
|
11
|
-
prompt
|
|
12
|
-
} = _enquirer.default;
|
|
13
|
-
|
|
14
|
-
// This will preserve coloration from spawned child processes
|
|
15
|
-
process.env.FORCE_COLORS = 3;
|
|
16
|
-
process.env.FORCE_COLOR = '1';
|
|
17
|
-
let TARGET = _zx.argv._[0];
|
|
18
|
-
let exited = false;
|
|
19
|
-
const SECRETS_DIR = 'secrets';
|
|
20
|
-
const EXIT_EVENTS = ['SIGINT', 'exit', 'uncaughtException', 'unhandledRejection'];
|
|
21
|
-
const [firebaserc, firebaseJson, hasLocalConfig] = await Promise.all([_zx.fs.exists(_zx.path.join(process.cwd(), '.firebaserc')), _zx.fs.exists(_zx.path.join(process.cwd(), 'firebase.json')), _zx.fs.exists(_zx.path.join(process.cwd(), SECRETS_DIR, 'local.env'))]);
|
|
22
|
-
if (!hasLocalConfig) {
|
|
23
|
-
if (!firebaserc) {
|
|
24
|
-
console.log(_zx.chalk.red('No .firebaserc file found. Make sure that you are executing firebaseDeploy from within a firebase project directory.'));
|
|
25
|
-
}
|
|
26
|
-
if (!firebaseJson) {
|
|
27
|
-
console.log(_zx.chalk.red('No firebase.json file found. Make sure that you are executing firebaseDeploy from within a firebase project directory.'));
|
|
28
|
-
}
|
|
29
|
-
if (!firebaserc || !firebaseJson) {
|
|
30
|
-
process.exit();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
const firebasercContent = firebaserc ? JSON.parse(await _zx.fs.readFile(_zx.path.join(process.cwd(), '.firebaserc'), {
|
|
34
|
-
encoding: 'utf-8'
|
|
35
|
-
})) : {
|
|
36
|
-
targets: {}
|
|
37
|
-
};
|
|
38
|
-
const defaultTargets = {};
|
|
39
|
-
if (hasLocalConfig) {
|
|
40
|
-
defaultTargets.local = {
|
|
41
|
-
siteId: 'local',
|
|
42
|
-
alias: 'local'
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
const targets = Object.entries(firebasercContent.targets).reduce((prev, [projectId, config]) => {
|
|
46
|
-
Object.entries(config.hosting).forEach(([siteId, aliases]) => {
|
|
47
|
-
aliases.forEach(alias => {
|
|
48
|
-
prev[alias] = {
|
|
49
|
-
projectId,
|
|
50
|
-
siteId,
|
|
51
|
-
alias
|
|
52
|
-
};
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
return prev;
|
|
56
|
-
}, defaultTargets);
|
|
57
|
-
if (!TARGET) {
|
|
58
|
-
const choices = Object.keys(targets).map(t => ({
|
|
59
|
-
name: t,
|
|
60
|
-
value: t
|
|
61
|
-
}));
|
|
62
|
-
let targetEnv;
|
|
63
|
-
if (choices.length > 1) {
|
|
64
|
-
const res = await prompt({
|
|
65
|
-
type: 'autocomplete',
|
|
66
|
-
name: 'targetEnv',
|
|
67
|
-
message: 'Which environment would you like to serve?',
|
|
68
|
-
choices
|
|
69
|
-
});
|
|
70
|
-
targetEnv = res.targetEnv;
|
|
71
|
-
} else {
|
|
72
|
-
targetEnv = choices[0].value;
|
|
73
|
-
}
|
|
74
|
-
TARGET = targets[targetEnv];
|
|
75
|
-
} else {
|
|
76
|
-
TARGET = targets[TARGET];
|
|
77
|
-
}
|
|
78
|
-
if (!TARGET) {
|
|
79
|
-
console.log(_zx.chalk.red('No such target exists in .firebaserc config.'));
|
|
80
|
-
process.exit();
|
|
81
|
-
}
|
|
82
|
-
const [aliasFileExists, siteIdFileExists, sharedFileExists] = await Promise.all([_zx.fs.exists(_zx.path.join(process.cwd(), SECRETS_DIR, `${TARGET.alias}.env`)), _zx.fs.exists(_zx.path.join(process.cwd(), SECRETS_DIR, `${TARGET.siteId}.env`)), _zx.fs.exists(_zx.path.join(process.cwd(), SECRETS_DIR, 'shared.env'))]);
|
|
83
|
-
if (!aliasFileExists && !siteIdFileExists) {
|
|
84
|
-
console.log(_zx.chalk.red(`No env file for ${TARGET.alias} exists. Check your secrets directory and try again`));
|
|
85
|
-
process.exit();
|
|
86
|
-
}
|
|
87
|
-
const exec = [];
|
|
88
|
-
if (sharedFileExists) {
|
|
89
|
-
exec.push(_zx.fs.readFile(_zx.path.join(process.cwd(), SECRETS_DIR, 'shared.env'), {
|
|
90
|
-
encoding: 'utf-8'
|
|
91
|
-
}));
|
|
92
|
-
}
|
|
93
|
-
if (aliasFileExists) {
|
|
94
|
-
exec.push(_zx.fs.readFile(_zx.path.join(process.cwd(), SECRETS_DIR, `${TARGET.alias}.env`), {
|
|
95
|
-
encoding: 'utf-8'
|
|
96
|
-
}));
|
|
97
|
-
} else {
|
|
98
|
-
exec.push(_zx.fs.readFile(_zx.path.join(process.cwd(), SECRETS_DIR, `${TARGET.siteId}.env`), {
|
|
99
|
-
encoding: 'utf-8'
|
|
100
|
-
}));
|
|
101
|
-
}
|
|
102
|
-
const envFileContent = (await Promise.all(exec)).join('\n');
|
|
103
|
-
EXIT_EVENTS.forEach(event => {
|
|
104
|
-
process.on(event, () => {
|
|
105
|
-
if (!exited) {
|
|
106
|
-
exited = true;
|
|
107
|
-
_zx.fs.removeSync(_zx.path.join(process.cwd(), '.env.temp'));
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
await _zx.fs.writeFile(_zx.path.join(process.cwd(), '.env.temp'), envFileContent);
|
|
112
|
-
try {
|
|
113
|
-
await (0, _zx.$)`DOTENV_CONFIG_PATH=${_zx.path.join(process.cwd(), '.env.temp')} DEPLOYMENT_TARGET=${TARGET.siteId} npm run serve --colors`;
|
|
114
|
-
} catch (err) {
|
|
115
|
-
console.error(err);
|
|
116
|
-
}
|
package/lib/server/getfbcfg.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* This script parses a firebase config into something useful.
|
|
5
|
-
*
|
|
6
|
-
* firebase --project leverege-dev-john apps:sdkconfig | getfbcfg
|
|
7
|
-
*/
|
|
8
|
-
"use strict";
|
|
9
|
-
|
|
10
|
-
const stdbuf = [];
|
|
11
|
-
|
|
12
|
-
/* eslint-disable no-console */
|
|
13
|
-
process.stdin.on('data', d => {
|
|
14
|
-
stdbuf.push(d);
|
|
15
|
-
}).on('end', () => {
|
|
16
|
-
const rawCfg = stdbuf.join('').replace(/["\n\r]/g, '');
|
|
17
|
-
const keyVals = rawCfg.match(/\{(.*)\}/)[1].split(',');
|
|
18
|
-
const lastLine = keyVals.length - 1;
|
|
19
|
-
console.log('{');
|
|
20
|
-
for (let i = 0; i <= lastLine; i++) {
|
|
21
|
-
const kv = keyVals[i].trim().split(' ');
|
|
22
|
-
console.log(` "${kv[0].replace(/:/, '')}": "${kv[1]}"${i < lastLine ? ',' : ''}`);
|
|
23
|
-
}
|
|
24
|
-
console.log('}');
|
|
25
|
-
}).setEncoding('utf8');
|
package/lib/server/getjson.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* This script will allow the user to grab a value from a JSON file and
|
|
5
|
-
* optionally apply a regular expression to it. Given this test.json file:
|
|
6
|
-
*
|
|
7
|
-
* {
|
|
8
|
-
* "name": "@leverege/packagename",
|
|
9
|
-
* "main": "lib/index.js"
|
|
10
|
-
* }
|
|
11
|
-
*
|
|
12
|
-
* and invoking as:
|
|
13
|
-
* ./getjson --file ./test.json --key name --regex "^@\w{3}"
|
|
14
|
-
*
|
|
15
|
-
* will yield an output of: @lev
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Get a command line switch parser.
|
|
20
|
-
*/
|
|
21
|
-
"use strict";
|
|
22
|
-
|
|
23
|
-
var _fs = _interopRequireDefault(require("fs"));
|
|
24
|
-
var _commandLineArgs = _interopRequireDefault(require("command-line-args"));
|
|
25
|
-
var _commandLineUsage = _interopRequireDefault(require("command-line-usage"));
|
|
26
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
27
|
-
/* eslint-disable no-console */
|
|
28
|
-
const optionList = [
|
|
29
|
-
// Use optionList to tie into the Usage statements
|
|
30
|
-
{
|
|
31
|
-
name: 'file',
|
|
32
|
-
type: String,
|
|
33
|
-
alias: 'f',
|
|
34
|
-
description: 'file to parse JSON from'
|
|
35
|
-
}, {
|
|
36
|
-
name: 'key',
|
|
37
|
-
type: String,
|
|
38
|
-
alias: 'k',
|
|
39
|
-
description: 'JSON key whose value to grab'
|
|
40
|
-
}, {
|
|
41
|
-
name: 'regex',
|
|
42
|
-
type: String,
|
|
43
|
-
alias: 'r',
|
|
44
|
-
description: 'regular expression to apply to the value'
|
|
45
|
-
}];
|
|
46
|
-
const sections = [{
|
|
47
|
-
header: 'JSON variable fetching script',
|
|
48
|
-
content: ` This script is intended to give shell scripts a simple JSON
|
|
49
|
-
parsing capability.`
|
|
50
|
-
}, {
|
|
51
|
-
header: 'Options',
|
|
52
|
-
optionList
|
|
53
|
-
}];
|
|
54
|
-
const args = (0, _commandLineArgs.default)(optionList, {
|
|
55
|
-
partial: true
|
|
56
|
-
});
|
|
57
|
-
const help = (0, _commandLineUsage.default)(sections);
|
|
58
|
-
if (!args.file) {
|
|
59
|
-
console.error('\n***ERROR: Missing a --file param\n');
|
|
60
|
-
console.log(help);
|
|
61
|
-
process.exit(1);
|
|
62
|
-
}
|
|
63
|
-
try {
|
|
64
|
-
const json = JSON.parse(_fs.default.readFileSync(args.file, 'utf8'));
|
|
65
|
-
const value = json[args.key];
|
|
66
|
-
const match = new RegExp(args.regex || '.*').exec(value);
|
|
67
|
-
console.log(match ? match[match.length - 1] : '');
|
|
68
|
-
} catch (e) {
|
|
69
|
-
console.log(e);
|
|
70
|
-
}
|