@vortex.so/cli
Version:
CLI to interact with Vortex.
129 lines (123 loc) • 4.47 kB
JavaScript
;
const path = require('node:path');
const process = require('node:process');
const project = require('@trapezedev/project');
const c = require('chalk');
const fs = require('fs-extra');
const yaml = require('js-yaml');
require('figures');
require('jiti');
const index = require('../../utils/log/index.cjs');
const ship_manifest = require('./ship.manifest.cjs');
const manifest = require('../manifest/manifest.cjs');
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
const process__default = /*#__PURE__*/_interopDefaultCompat(process);
const c__default = /*#__PURE__*/_interopDefaultCompat(c);
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
const yaml__default = /*#__PURE__*/_interopDefaultCompat(yaml);
const log = new index.Log("Ship");
async function updateFiles(operation) {
const { name, newVersion, oldBuild, newBuild } = operation.state;
const manifestFiles = ["vortex.yaml", "package.json"];
for (const filePath of manifestFiles) {
await updateManifestFile(filePath, operation);
}
if (operation.state.type?.includes("me")) {
await updateMobileFiles(operation);
}
log.ok(
`${c__default.bold.green(name)} has been evolved to version ${c__default.bold(
newVersion
)}.`
);
log.ok(
`${c__default.bold.green(name)} build number has been evolved from ${c__default.bold(
oldBuild
)} to ${c__default.bold(newBuild)}.`
);
return operation;
}
async function updateManifestFile(filePath, operation) {
const { name, description, newVersion, newBuild } = operation.state;
let isModified = false;
let file;
if (filePath.includes("yaml")) {
file = yaml__default.load(fs__default.readFileSync(filePath, "utf-8"));
} else if (filePath.includes("json")) {
file = await fs__default.readJson(filePath, { encoding: "utf-8" });
} else {
return false;
}
if (ship_manifest.isManifest(file)) {
file.name = name;
file.description = description;
file.version = newVersion;
file.build = newBuild;
if (filePath.includes("yaml")) {
const yamlData = yaml__default.dump(file);
await fs__default.writeFile(filePath, yamlData, { encoding: "utf-8" });
} else if (filePath.includes("json")) {
await fs__default.writeJson(filePath, file, {
spaces: 2
});
}
isModified = true;
}
return isModified;
}
async function updateMobileFiles(operation) {
const { newVersion, oldBuild, newBuild } = operation.state;
const { ios, android } = manifest.Manifest.get();
let isModified = false;
const rootDir = process__default.cwd();
const iosDir = path.resolve(rootDir, ios?.path || "ios");
const androidDir = path.resolve(rootDir, android?.path || "android");
const config = {
ios: { path: ios?.path || "ios" },
android: { path: android?.path || "android" }
};
const project$1 = new project.MobileProject(rootDir, config);
await project$1.load();
try {
const appTarget = project$1.ios?.getAppTarget();
if (!appTarget) {
throw new Error(`Couldn't find iOS App target in ${iosDir}`);
}
if (!appTarget.buildConfigurations) {
throw new Error(
`Couldn't find iOS App target build configurations in ${iosDir}`
);
}
const _newBuild = newBuild ?? 1;
for (const build of appTarget.buildConfigurations) {
await project$1.ios?.setVersion(appTarget.name, build.name, newVersion);
await project$1.ios?.setBuild(appTarget.name, build.name, _newBuild);
}
log.ok(
`Updated iOS build from ${c__default.bold(oldBuild)} to ${c__default.bold(
_newBuild
)}.`
);
log.ok(`Updated iOS version to ${c__default.bold(newVersion)}.`);
const androidVersionCode = await project$1.android?.getVersionCode();
if (!androidVersionCode) {
throw new Error(`Couldn't find Android app in ${androidDir}`);
}
await project$1.android?.setVersionName(newVersion);
log.ok(`Updated Android version to ${c__default.bold(newVersion)}`);
await project$1.android?.setVersionCode(_newBuild);
log.ok(
`Updated Android version code from ${c__default.bold(
oldBuild
)} to ${c__default.bold(_newBuild)}.`
);
await project$1.commit();
isModified = true;
return isModified;
} catch (error) {
log.warn("Canceled, app update failed.");
log.fail(error);
return isModified;
}
}
exports.updateFiles = updateFiles;