@mbc-cqrs-serverless/cli
Version:
a CLI to get started with MBC CQRS serverless framework
117 lines (116 loc) • 4.87 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = uiAction;
const child_process_1 = require("child_process");
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const rimraf_1 = require("rimraf");
const ui_1 = require("../ui");
const repoUrl = 'https://gitlab.com/mbc-net/common/mbc-cqrs-ui-common.git';
const componentOptions = ['all', 'appsync', 'component'];
/* eslint-disable no-console */
async function uiAction(options, command) {
ui_1.logger.info(`Executing command '${command.name()}' for application with options '${JSON.stringify(options)}'`);
const { branch, auth, component, pathDir, token = '' } = options;
if (componentOptions.findIndex((optionName) => optionName === component) === -1) {
ui_1.logger.error(`Please choose correct component options: ${componentOptions.join(', ')}`);
}
// Check command run in base src
if (!(0, fs_1.existsSync)(path_1.default.join(process.cwd(), 'tsconfig.json'))) {
ui_1.logger.error('Please run command in base folder');
return;
}
// Check tsconfig.json contain path @ms
const tsconfig = JSON.parse((0, fs_1.readFileSync)(path_1.default.join(process.cwd(), 'tsconfig.json'), 'utf8'));
if (tsconfig?.compilerOptions &&
tsconfig?.compilerOptions?.paths &&
tsconfig?.compilerOptions?.paths.hasOwnProperty('@ms/*')) {
ui_1.logger.error('The project already contain mbc-cqrs-ui-common');
return;
}
// Copy source
installTemplate({
auth,
token,
pathDir,
branch,
component,
});
// Modify tsconfig path alias
if (!tsconfig?.compilerOptions) {
tsconfig.compilerOptions = {};
}
if (!tsconfig?.compilerOptions?.paths) {
tsconfig.compilerOptions.paths = {};
}
tsconfig.compilerOptions.paths['@ms/*'] = [`./${pathDir}/*`];
(0, fs_1.writeFileSync)(path_1.default.join(process.cwd(), 'tsconfig.json'), JSON.stringify(tsconfig, null, 2), {
encoding: 'utf8',
});
// Modify package.json
modifyDependencies({ pathDir, component });
// npm install
ui_1.logger.title('deps', `Installing dependencies`);
const logs = (0, child_process_1.execSync)('npm i');
console.log(logs.toString());
ui_1.logger.success(`Dependencies installed`);
}
const installTemplate = ({ auth, token, pathDir, branch, component, }) => {
let gitUrl = repoUrl;
if (auth === 'SSH') {
gitUrl = 'git@gitlab.com:mbc-net/common/mbc-cqrs-ui-common.git';
}
else if (auth === 'HTTPS - Token') {
gitUrl = repoUrl.replace(/^https:\/\//, `https://${token}@`);
}
// Copy source
const destDir = path_1.default.join(process.cwd(), pathDir);
console.log('Adding MBC common ui in', destDir);
(0, fs_1.mkdirSync)(destDir, { recursive: true });
const logs = (0, child_process_1.execSync)(`git clone --branch ${branch} ${gitUrl} ${destDir}`);
console.log(logs.toString());
// remove .git
(0, rimraf_1.rimrafSync)(`${destDir}/.git`);
if (component === 'component') {
(0, rimraf_1.rimrafSync)(`${destDir}/appsync`);
}
else if (component === 'appsync') {
;
['components', 'lib', 'modules', 'styles', 'types'].forEach((name) => (0, rimraf_1.rimrafSync)(`${destDir}/${name}`));
}
};
const modifyDependencies = ({ pathDir, component, }) => {
const destDir = path_1.default.join(process.cwd(), pathDir);
const srcPackage = JSON.parse((0, fs_1.readFileSync)(`${destDir}/package.json`, 'utf8'));
const destPackage = JSON.parse((0, fs_1.readFileSync)(path_1.default.join(process.cwd(), 'package.json'), 'utf8'));
const modifiedPackage = getModifyPackage({
srcPackage,
destPackage,
component,
});
(0, fs_1.writeFileSync)(path_1.default.join(process.cwd(), 'package.json'), JSON.stringify(modifiedPackage, null, 2), {
encoding: 'utf8',
});
(0, rimraf_1.rimrafSync)(`${destDir}/package.json`);
};
const getModifyPackage = ({ srcPackage, destPackage, component, }) => {
// modify dependencies
if (srcPackage?.dependencies) {
if (!destPackage.dependencies) {
destPackage.dependencies = {};
}
for (const key of Object.keys(srcPackage.dependencies)) {
if (!destPackage.dependencies[key]) {
if (component === 'component' && key === 'aws-amplify')
continue;
if (component === 'appsync' && key !== 'aws-amplify')
continue;
destPackage.dependencies[key] = srcPackage.dependencies[key];
}
}
}
return destPackage;
};