diff --git a/src/commands/update-build-index.ts b/src/commands/update-build-index.ts new file mode 100644 index 0000000..c7b7df9 --- /dev/null +++ b/src/commands/update-build-index.ts @@ -0,0 +1,33 @@ +import { Command } from '@oclif/command' +import { readFileSync } from 'fs' +import { writeFile } from 'fs/promises' +import { YAMLMap } from 'yaml/types' + +import { DEVICE_CONFIG_FLAGS, loadDeviceConfigs } from '../config/device' +import { ADEVTOOL_DIR, MAIN_BUILD_INDEX_PART } from '../config/paths' +import { fetchBuildIndex } from '../images/build-index' +import { showGitDiff } from '../util/cli' +import { yamlStringifyNoFold } from '../util/yaml' + +export default class UpdateBuildIndex extends Command { + static description = 'fetch main (non-beta) build index and if it has changed, update build-index-main.yml file in-place and show git diff' + + static flags = DEVICE_CONFIG_FLAGS + + async run() { + let { flags } = this.parse(UpdateBuildIndex) + let devices = await loadDeviceConfigs(flags.devices) + + let index: YAMLMap = await fetchBuildIndex(devices) + + let yaml = yamlStringifyNoFold(index) + + if (readFileSync(MAIN_BUILD_INDEX_PART).toString() === yaml) { + this.log('main build index is up-to-date') + process.exit(1) + } + + await writeFile(MAIN_BUILD_INDEX_PART, yaml) + showGitDiff(ADEVTOOL_DIR, MAIN_BUILD_INDEX_PART) + } +} diff --git a/src/util/cli.ts b/src/util/cli.ts index c81c7b6..1d59670 100644 --- a/src/util/cli.ts +++ b/src/util/cli.ts @@ -1,5 +1,8 @@ -import ora from 'ora' +import assert from 'assert' import chalk from 'chalk' +import child_proc from 'child_process' +import ora from 'ora' +import path from 'path' export type ProgressCallback = (progress: string) => void @@ -25,3 +28,13 @@ export async function withSpinner(action: string, callback: (spinner: or return ret } + +export function showGitDiff(repoPath: string, filePath?: string) { + let args = ['-C', repoPath, `diff`] + if (filePath !== undefined) { + args.push(path.relative(repoPath, filePath)) + } + + let ret = child_proc.spawnSync('git', args, { stdio: 'inherit' }) + assert(ret.status === 0) +}