@wellsite/version-generator
Version:
Generates Versions based on git information
125 lines • 6.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.runVersionGenerator = runVersionGenerator;
const commander_1 = require("commander");
const _1 = require(".");
const path_1 = require("path");
const android_version_1 = require("./android-version");
/**
* Run the version generator with the given options
* @param dir - Directory to use for command execution and output file path
* @param outputFilePath - Optional output file path(s) (relative to dir if not absolute)
* @param format - Output format (string or json)
* @param androidOptions - Optional Android version code generation options
* @param iosOptions - Optional ios version code generation options
* @returns The generated version data
*/
async function runVersionGenerator(dir, outputFilePath, format = 'string', androidOptions, iosOptions) {
// Resolve the directory to an absolute path, or use current working directory if not provided
const resolvedDir = dir ? (0, path_1.resolve)(dir) : process.cwd();
const normalizedFormat = format.toLowerCase();
// Validate format option
if (normalizedFormat !== 'string' && normalizedFormat !== 'json') {
throw new Error('Format must be either "string" or "json"');
}
// Generate the version
const versionInfo = await (0, _1.generateAndWriteVersion)(resolvedDir, outputFilePath, {
android: androidOptions
? {
enabled: androidOptions.enabled,
packageName: androidOptions.packageName,
serviceAccountKey: androidOptions.serviceAccountKey,
track: androidOptions.track,
majorVersionIncrement: androidOptions.majorVersionIncrement,
currentMajorVersion: androidOptions.currentMajorVersion || 0, // Default to 0 if not provided
}
: undefined,
ios: iosOptions
? {
enabled: iosOptions.enabled,
bundleId: iosOptions.bundleId,
apiKeyId: iosOptions.apiKeyId,
apiIssuerId: iosOptions.apiIssuerId,
apiPrivateKey: iosOptions.apiPrivateKey,
appReleaseVersion: '', // This will be set in generatePackageVersion
}
: undefined,
});
// Output based on format and outputFilePath
if (!outputFilePath || (Array.isArray(outputFilePath) && outputFilePath.length === 0)) {
// No output file specified - output to console based on format
if (normalizedFormat === 'string') {
console.log(versionInfo.version);
}
else {
console.log(JSON.stringify(versionInfo, null, 2));
}
}
else {
// Output file(s) provided - output additional information
const fileCount = Array.isArray(outputFilePath) ? outputFilePath.length : 1;
const fileWord = fileCount === 1 ? 'file' : 'files';
console.log(`Successfully generated version: ${versionInfo.version} (written to ${fileCount} ${fileWord})`);
}
return versionInfo;
}
// Only run the CLI if this file is executed directly
if (require.main === module) {
const program = new commander_1.Command();
program
.name('generate-version')
.description('Generate a version based on git information')
.option('-d, --dir <path>', 'Directory to use for command execution and output file path (defaults to current working directory)')
.option('--output-file <path>', 'Output file path (relative to --dir if not absolute) where the version file should be written. Can be specified multiple times to write to multiple locations.', (value, previous) => {
// If previous is undefined, initialize as empty array
// Otherwise, add the new value to the array
return previous === undefined ? [value] : [...previous, value];
}, [])
.option('-f, --format <format>', 'Output format (string or json)', 'string')
.option('--android', 'Enable Android version code generation')
.option('--android-package <packageName>', 'Android package name')
.option('--android-service-account-key <key>', 'Service account key JSON for Google Play API authentication')
.option('--android-track <track>', 'Track to check for version codes')
.option('--android-major-increment <increment>', `Increment to add to version code on major version change (default: ${android_version_1.DEFAULT_MAJOR_VERSION_INCREMENT})`, String(android_version_1.DEFAULT_MAJOR_VERSION_INCREMENT))
.option('--ios', 'Enable iOS build number generation')
.option('--ios-bundle-id <bundleId>', 'iOS bundle ID')
.option('--ios-api-key-id <keyId>', 'App Store Connect API Key ID')
.option('--ios-api-issuer-id <issuerId>', 'App Store Connect API Issuer ID')
.option('--ios-api-private-key <key>', 'App Store Connect API Private Key')
.action(async (options) => {
try {
// Prepare Android options if --android flag is set
const androidOptions = options.android
? {
enabled: true,
packageName: options.androidPackage,
serviceAccountKey: options.androidServiceAccountKey,
track: options.androidTrack,
majorVersionIncrement: options.androidMajorIncrement
? parseInt(options.androidMajorIncrement, 10)
: undefined,
// We don't set currentMajorVersion here as it will be determined from the git tag in generatePackageVersion
}
: undefined;
// Prepare iOS options if --ios flag is set
const iosOptions = options.ios
? {
enabled: true,
bundleId: options.iosBundleId,
apiKeyId: options.iosApiKeyId,
apiIssuerId: options.iosApiIssuerId,
apiPrivateKey: options.iosApiPrivateKey,
}
: undefined;
await runVersionGenerator(options.dir, options.outputFile, options.format, androidOptions, iosOptions);
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
console.error(`Error: ${errorMessage}`);
process.exit(1);
}
});
program.parse();
}
//# sourceMappingURL=cli.js.map