@enplug/scripts
Version:
Enplug scripts
130 lines (121 loc) • 4.34 kB
JavaScript
;
const commandLineArgs = require('command-line-args');
const path = require('path');
const chalk = require('chalk');
const rootPath = process.cwd();
const pkg = require(path.join(rootPath, 'package.json'));
const updateUrlsToStaging = require('./functions/updateUrlsToStaging');
const promptForBucket = require('./functions/promptForBucket');
const confirmDestination = require('./functions/confirmDestination');
const syncDir = require('./functions/syncDir');
const buildAngular = require('./functions/buildAngular');
const replaceAppUrl = require('./functions/replaceAppUrl');
const runCITests = require('./functions/runCITests');
const determineEnv = require('./functions/determineEnv');
const { syncTranslations, validateTranslations } = require('./functions/translations/translations');
let bucket;
// Parse inline arguments
const optionDefinitions = [
{ name: 'bucket', alias: 'b', type: String },
{ name: 'configuration', type: String },
{ name: 'ignoreversion', type: Boolean },
{ name: 'nooffline', type: Boolean },
{ name: 'notranslations', type: Boolean },
{ name: 'prefix', alias: 'p', type: String },
{ name: 'root', alias: 'r', type: String },
{ name: 'target', alias: 't', type: String },
{ name: 'test', type: Boolean },
{ name: 'version', alias: 'v', type: String },
{ name: 'webplayer', type: Boolean },
{ name: 'electron', type: Boolean },
{ name: 'replaceUrl', type: Boolean },
{ name: 'releaseUrlSuffix', type: String },
{ name: 'releaseAppId', type: String },
{ name: 'runCITests', type: Boolean },
];
const options = commandLineArgs(optionDefinitions);
const destination = pkg.config.destination;
const version = pkg.version;
let prefix;
let env;
function runBuild() {
console.log(`\n${chalk.default.greenBright('Building Angular project')}\n`);
return buildAngular(pkg, options, destination, version).then(function () {
var fixUrlsToStaging = bucket.endsWith('enplug.in')
? updateUrlsToStaging(rootPath, 'dist', options.nooffline)
: Promise.resolve([]);
return fixUrlsToStaging;
}).catch(error => {
console.error(`${chalk.default.red('BUILD ERROR')}`)
console.error(error);
});
}
promptForBucket(bucket, pkg.config.aws.buckets)
.then(selectedBucket => {
bucket = selectedBucket;
env = determineEnv(bucket);
})
.then(async () => { // Validate translations for production release
if (!options.notranslations) {
const shouldUpload = await validateTranslations();
if (!shouldUpload) {
throw new Error('Release aborted');
}
}
}) // Building the app.
.then(runBuild)
.then(_ => {
if (options.test) {
}
}) // Determining the target.
.then(_ => {
prefix = path.posix.join(destination, version);
if (options.ignoreversion) {
prefix = destination;
}
return confirmDestination(bucket, prefix);
}) // Uploading app files to S3.
.then(response => {
if (response.confirm) {
return syncDir(pkg, bucket, prefix, 'dist/');
} else {
return Promise.reject('Release aborted');
}
}) // Replacing app urls that our servers use. Only if `--replaceUrl` option present.
.then(releaseURI => {
if (options.replaceUrl) {
let appId = pkg.config.appId;
if (options.releaseAppId) {
appId = options.releaseAppId;
}
let dashboardUrl;
let playerSideUrl;
if (pkg.config.appSideUrl === 'dashboard') {
dashboardUrl = releaseURI;
} else if (pkg.config.appSideUrl === 'playerSide') {
playerSideUrl = releaseURI;
}
return replaceAppUrl(appId, dashboardUrl, playerSideUrl, options.releaseUrlSuffix, env);
} else {
return Promise.resolve(false);
}
}) // Running E2E tests on CI. Only when `--runCITests` option present.
.then(urlReplaced => {
if (urlReplaced && options.runCITests) {
return runCITests(env).catch(e => {
console.log(chalk.default.red('Could not run CI Tests: '), e);
});
} else {
return Promise.resolve();
}
}) // Upload translations to Crowdin
.then(() => {
if (!options.notranslations) {
return syncTranslations(undefined, bucket);
}
})
.catch(error => {
console.error(`${chalk.default.red('BUILD ERROR')}`);
console.error(error);
});