-
Notifications
You must be signed in to change notification settings - Fork 37
/
sync-refs.ts
58 lines (48 loc) 路 1.64 KB
/
sync-refs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { Command } from '@oclif/command';
import * as path from 'path';
import { cli } from 'cli-ux';
import { existsSync } from 'fs';
import * as fs from 'fs-extra';
import { refsPath, frontendRefsPath } from '../lib/flag';
import TerrainCLI from '../TerrainCLI';
import runCommand from '../lib/runCommand';
export default class SyncRefs extends Command {
static description = 'Sync configuration with frontend app.';
static flags = {
'refs-path': refsPath,
dest: frontendRefsPath,
};
async run() {
const { flags } = this.parse(SyncRefs);
// Command execution path.
const execPath = flags.dest;
// Command to be performed.
const command = async () => {
// Append "refs.terrain.json" to flags.dest path if file unavailable.
// The fs.copyFileSync command requires the full file path.
const destFullPath = flags.dest.endsWith('refs.terrain.json')
? flags.dest
: path.join(flags.dest, 'refs.terrain.json');
cli.action.start(
`Syncing refs from '${flags['refs-path']}' to '${destFullPath}'`,
);
fs.copyFileSync(flags['refs-path'], destFullPath);
cli.action.stop();
};
// Error check to be performed upon each backtrack iteration.
const errorCheck = () => {
if (existsSync(execPath) && !existsSync('refs.terrain.json')) {
TerrainCLI.error(
'The "refs.terrain.json" file was not found in the project root directory.',
'Failed to Sync Refs',
);
}
};
// Attempt to execute command while backtracking through file tree.
await runCommand(
execPath,
command,
errorCheck,
);
}
}