@vortex.so/cli
Version:
CLI to interact with Vortex.
120 lines (117 loc) • 3.83 kB
JavaScript
import { resolve } from 'node:path';
import process from 'node:process';
import { MobileProject } from '@trapezedev/project';
import c from 'chalk';
import fs from 'fs-extra';
import yaml from 'js-yaml';
import 'figures';
import 'jiti';
import { Log } from '../../utils/log/index.mjs';
import { isManifest } from './ship.manifest.mjs';
import { Manifest } from '../manifest/manifest.mjs';
const log = new 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.bold.green(name)} has been evolved to version ${c.bold(
newVersion
)}.`
);
log.ok(
`${c.bold.green(name)} build number has been evolved from ${c.bold(
oldBuild
)} to ${c.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.load(fs.readFileSync(filePath, "utf-8"));
} else if (filePath.includes("json")) {
file = await fs.readJson(filePath, { encoding: "utf-8" });
} else {
return false;
}
if (isManifest(file)) {
file.name = name;
file.description = description;
file.version = newVersion;
file.build = newBuild;
if (filePath.includes("yaml")) {
const yamlData = yaml.dump(file);
await fs.writeFile(filePath, yamlData, { encoding: "utf-8" });
} else if (filePath.includes("json")) {
await fs.writeJson(filePath, file, {
spaces: 2
});
}
isModified = true;
}
return isModified;
}
async function updateMobileFiles(operation) {
const { newVersion, oldBuild, newBuild } = operation.state;
const { ios, android } = Manifest.get();
let isModified = false;
const rootDir = process.cwd();
const iosDir = resolve(rootDir, ios?.path || "ios");
const androidDir = resolve(rootDir, android?.path || "android");
const config = {
ios: { path: ios?.path || "ios" },
android: { path: android?.path || "android" }
};
const project = new MobileProject(rootDir, config);
await project.load();
try {
const appTarget = project.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.ios?.setVersion(appTarget.name, build.name, newVersion);
await project.ios?.setBuild(appTarget.name, build.name, _newBuild);
}
log.ok(
`Updated iOS build from ${c.bold(oldBuild)} to ${c.bold(
_newBuild
)}.`
);
log.ok(`Updated iOS version to ${c.bold(newVersion)}.`);
const androidVersionCode = await project.android?.getVersionCode();
if (!androidVersionCode) {
throw new Error(`Couldn't find Android app in ${androidDir}`);
}
await project.android?.setVersionName(newVersion);
log.ok(`Updated Android version to ${c.bold(newVersion)}`);
await project.android?.setVersionCode(_newBuild);
log.ok(
`Updated Android version code from ${c.bold(
oldBuild
)} to ${c.bold(_newBuild)}.`
);
await project.commit();
isModified = true;
return isModified;
} catch (error) {
log.warn("Canceled, app update failed.");
log.fail(error);
return isModified;
}
}
export { updateFiles };