simba-cli
Version:
A simple CLI to handle most basic and everyday usage cli command wishes
101 lines • 4.82 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setSupplierAppEnvironment = void 0;
const yargs_1 = __importDefault(require("yargs"));
const path_1 = __importDefault(require("path"));
const process_1 = require("process");
const fs_1 = require("fs");
const log_1 = require("../../log");
// Example command: simba env vt-app --prod --deploy-version=2022070101 --version=4.2.3
const setSupplierAppEnvironment = () => {
const options = yargs_1.default.argv;
const cwDir = (0, process_1.cwd)();
const isProd = !!options.prod;
const deploymentVersion = options['deploy-version'];
const versionNumber = options['version-number'];
// update config file
try {
const configPath = path_1.default.normalize(cwDir + '/lib/utils/serverconstants.dart');
const file = (0, fs_1.readFileSync)(configPath).toString();
const lines = file.split('\n');
// used to iterate through the config file and file marks for each update
const marks = [
{
mark: 'static bool production',
tranform: (cur, mark) => {
return cur.substring(0, cur.indexOf(mark) + mark.length) + ` = ${isProd};`;
}
},
{
mark: 'static String deploymentVersion',
tranform: (cur, mark) => {
return deploymentVersion ? cur.substring(0, cur.indexOf(mark) + mark.length) + ` = '${deploymentVersion}';` : cur;
}
}
];
for (let a = 0; a < lines.length; a++) {
const line = lines[a];
for (const item of marks) {
if (line.includes(item.mark)) {
const newline = item.tranform(line, item.mark);
lines[a] = newline;
}
}
}
(0, fs_1.writeFileSync)(configPath, lines.join('\n'));
// update google-services.json file
// const gsBasePath = '/android/app';
// const googleServices = readFileSync(
// path.normalize(cwDir + gsBasePath + `/google-services ${isProd ? 'prod' : 'dev'}.json`)
// ).toString();
// writeFileSync(path.normalize(cwDir + gsBasePath + `/google-services.json`), googleServices);
// updates web index.html version as well
if (deploymentVersion) {
const webPath = path_1.default.normalize(cwDir + '/web/index.html');
const html = (0, fs_1.readFileSync)(webPath).toString();
const htmlCode = html.split('\n');
const htmlVersionMark = '"main.dart.js';
for (let a = 0; a < htmlCode.length; a++) {
const line = htmlCode[a];
if (line.includes(htmlVersionMark)) {
const pos = line.indexOf(htmlVersionMark);
let rightText = line.substring(pos + htmlVersionMark.length);
rightText = rightText.substring(rightText.indexOf('"'));
const newline = `${line.substring(0, pos + htmlVersionMark.length)}?version=${deploymentVersion}${rightText}`;
htmlCode[a] = newline;
}
}
(0, fs_1.writeFileSync)(webPath, htmlCode.join('\n'));
}
// pubspec.yaml version update
if (versionNumber) {
const pubspecPath = path_1.default.normalize(cwDir + '/pubspec.yaml');
const content = (0, fs_1.readFileSync)(pubspecPath).toString();
const lines = content.split('\n');
const versionMark = 'version: ';
let versionCode = versionNumber.split('.').join('');
if (versionCode.length < 5) {
versionCode = (versionCode + '00000').substring(0, 5);
}
for (let a = 0; a < lines.length; a++) {
const line = lines[a];
if (line.startsWith(versionMark)) {
const pos = line.indexOf(versionMark);
const newline = `${line.substring(0, pos + versionMark.length)}${versionNumber}+${versionCode}`;
lines[a] = newline;
}
}
(0, fs_1.writeFileSync)(pubspecPath, lines.join('\n'));
}
}
catch (e) {
throw Error('Error while reading and updating Supplier app config files');
}
log_1.Log.info(`Environment now pointing to ${isProd ? 'production' : 'dev'}`);
console.log('Please, run `flutter clean` command before next build to ensure a clean build.');
};
exports.setSupplierAppEnvironment = setSupplierAppEnvironment;
//# sourceMappingURL=sp-app.js.map