nav-new-publication-cli
Version:
Add new publication to the navigator web project
215 lines (214 loc) • 7.42 kB
JavaScript
#!/usr/bin/env node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const prompts_1 = __importDefault(require("prompts"));
const picocolors_1 = require("picocolors");
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const readWritePublications_1 = require("./readWritePublications");
const prompts_2 = require("./prompts");
const fileUpdates_1 = require("./fileUpdates");
const checkers_1 = require("./checkers");
const program = new commander_1.Command();
const handleSigTerm = () => process.exit(0);
process.on('SIGINT', handleSigTerm);
process.on('SIGTERM', handleSigTerm);
(async () => {
const packageObj = await (0, readWritePublications_1.loadJson)((0, path_1.resolve)('./package.json'), {
error: 'Error loading package.json',
success: null
});
program
.version(packageObj.version, '-v, --version', 'Output the current version of create new publication.')
.helpOption('-h, --help', 'Display this help message.')
.option('--db, --debug', 'output extra debugging')
.option('-p, --platform <type>', 'platform')
.option('-t, --title <type>', 'publication title')
.option('-c, --cue-name <type>', 'name in cue')
.option('--vr', '--vr-steps <type>', 'number of vr steps')
.option('-f, --func-steps <type>', 'number of functional steps')
.option('-d, --domain <type>', 'domain of site')
.allowUnknownOption();
program.parse(process.argv);
})();
const opts = program.opts();
const platforms = [
{
title: 'nationals',
value: 'nationals'
},
{
title: 'ronionals',
value: 'ronionals',
},
{
title: 'regionals',
value: 'regionals',
},
{
title: "visionals",
value: "visionals",
}
];
async function run() {
const currentDir = process.cwd();
const webRepoPath = currentDir.includes('web') ? currentDir : null;
if (!webRepoPath) {
throw new Error('This script must be run inside the web repository.');
}
const publicationJson = (0, path_1.resolve)(webRepoPath, 'drone', 'publication.json');
if (!(0, fs_extra_1.existsSync)(publicationJson)) {
throw new Error(`Required file not found: ${publicationJson}`);
}
const publicationList = await (0, readWritePublications_1.loadJson)(publicationJson, {
error: 'Error loading publication list',
success: 'Publication list loaded successfully'
});
const platformObj = platforms.find(({ title }) => title === opts.platform);
let config = {
domain: opts.domain,
platform: (platformObj) ? platformObj.value : null,
title: opts.title,
stackAlias: opts.cueName,
vr: opts.vrSteps,
func: opts.funcSteps,
arn: {
dev: '',
qa: '',
preprod: '',
prod: '',
},
autoScaling: {
min: 5,
max: 20,
}
};
config = await (0, prompts_2.createPrompts)(config, 'platform', {
type: 'select',
name: 'platform',
message: 'What Platform is it on?',
choices: platforms,
errorMsg: 'Platform is required'
});
console.log(config, publicationList, publicationList[config.platform]);
config = await (0, prompts_2.createPrompts)(config, 'title', {
type: 'text',
name: 'title',
errorMsg: 'Publication Name is required',
message: 'What is the Name of the publication?',
validate: (0, checkers_1.checkName)(publicationList[config.platform])
});
config = await (0, prompts_2.createPrompts)(config, 'cueName', [
{
type: 'confirm',
name: 'hasCue',
message: 'Is the publication name different in cue?',
initial: false
},
{
type: prev => prev ? 'text' : null,
name: 'stackAlias',
message: 'What is the publication\'s name in cue?',
validate: checkers_1.checkNotEmpty
}
]);
if (!config.domain) {
const { domain } = await (0, prompts_1.default)({
type: 'text',
name: 'domain',
message: 'What is the publication\'s domain?',
validate: checkers_1.checkDomain
});
config.domain = domain;
}
if (!config.vr) {
const { vr } = await (0, prompts_1.default)({
type: 'number',
name: 'vr',
message: 'How many vr steps for this publication?',
initial: 5
});
config.vr = vr;
}
if (!config.func) {
const { func } = await (0, prompts_1.default)({
type: 'number',
name: 'func',
message: 'How many Functional steps for this publication?',
initial: 14
});
config.func = func;
}
const awsResponses = await (0, prompts_1.default)([
{
type: 'confirm',
name: 'arnTrue',
message: 'Do you know the certificateArn for each environment?',
initial: true
},
{
type: (prev, values) => values.arnTrue ? 'text' : null,
name: 'devCertificateArn',
message: 'What is certificateArn for dev environment?',
validate: checkers_1.checkArn
},
{
type: (prev, values) => values.arnTrue ? 'text' : null,
name: 'qaCertificateArn',
message: 'What is certificateArn for QA environment?',
validate: checkers_1.checkArn
},
{
type: (prev, values) => values.arnTrue ? 'text' : null,
name: 'preprodCertificateArn',
message: 'What is certificateArn for PreProd environment?',
validate: checkers_1.checkArn
},
{
type: (prev, values) => values.arnTrue ? 'text' : null,
name: 'prodCertificateArn',
message: 'What is certificateArn for Prod environment?',
validate: checkers_1.checkArn
},
{
type: 'number',
name: 'autoScalingMin',
message: 'AWS autoscaling, what is the minimum capacity?',
initial: 5,
},
{
type: 'number',
name: 'autoScalingMax',
message: 'AWS autoscaling, what is the maximum capacity?',
initial: 20,
},
]);
config.arn = {
dev: awsResponses.devCertificateArn,
qa: awsResponses.qaCertificateArn,
preprod: awsResponses.preprodCertificateArn,
prod: awsResponses.prodCertificateArn,
};
config.autoScaling = {
min: awsResponses.autoScalingMin,
max: awsResponses.autoScalingMax,
};
await (0, fileUpdates_1.updateFiles)(config);
}
;
async function exit(reason) {
console.log();
console.log('Aborting installation.');
if (reason.command) {
console.log(` ${(0, picocolors_1.cyan)(reason.command)} has failed.`);
}
else {
console.log((0, picocolors_1.red)('Unexpected error. Please report it as a bug:') + '\n', reason);
}
process.exit(1);
}
run().catch(exit);