-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.ts
More file actions
131 lines (102 loc) · 4.57 KB
/
Copy pathinit.ts
File metadata and controls
131 lines (102 loc) · 4.57 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import chalk from 'chalk';
import { LinuxDistro, OS } from '@codifycli/schemas';
import os from 'node:os';
import path from 'node:path';
import { PluginInitOrchestrator } from '../common/initialize-plugins.js';
import { ResourceConfig } from '../entities/resource-config.js';
import { ProcessName, SubProcessName, ctx } from '../events/context.js';
import { ResourceDefinitionMap } from '../plugins/plugin-manager.js';
import { Reporter } from '../ui/reporters/reporter.js';
import { FileUtils } from '../utils/file.js';
import { resolvePathWithVariables, tildify, untildify } from '../utils/index.js';
import { ShellUtils } from '../utils/shell.js';
export interface InitArgs {
path?: string;
verbosityLevel?: number;
includeSensitive?: boolean;
skipBanner?: boolean;
}
export const InitializeOrchestrator = {
async run(args: InitArgs, reporter: Reporter) {
await reporter.displayInitBanner(args.skipBanner)
ctx.processStarted(ProcessName.INIT)
await reporter.displayProgress();
const { pluginManager, resourceDefinitions } = await PluginInitOrchestrator.run({
...args,
forceEmptyProject: true,
}, reporter);
ctx.subprocessStarted(SubProcessName.IMPORT_RESOURCE)
const currentDistro = os.type() === OS.Linux ? await ShellUtils.getLinuxDistro() : undefined;
// Omit sensitive resources and resources not supported on the current OS
const typeIdsToImport = [...resourceDefinitions.keys()]
.filter((typeId) => args.includeSensitive || (!args.includeSensitive && (resourceDefinitions.get(typeId)?.sensitiveParameters ?? []).length === 0))
.filter((typeId) => this.filterByOperatingSystemAndDistro(typeId, resourceDefinitions, currentDistro))
const importResults = await Promise.all(typeIdsToImport.map(async (typeId) => {
try {
return await pluginManager.importResource({
core: { type: typeId },
parameters: {}
}, true);
} catch {
return null;
}
}))
ctx.subprocessFinished(SubProcessName.IMPORT_RESOURCE)
const flattenedResults = importResults.filter(Boolean).flatMap(p => p?.result).filter(Boolean)
const userSelectedTypes = await reporter.promptInitResultSelection([...new Set(flattenedResults.map((r) => r!.core.type))])
ctx.log('Resource types were chosen to be imported.')
const locationToSave = args.path ?? await this.promptSaveLocation(reporter);
ctx.log(`Save results to ${locationToSave}`)
await reporter.hide();
const resourcesRaw = flattenedResults.filter((r) => r && userSelectedTypes.includes(r.core.type))
.map((r) => ResourceConfig.fromJson(r!))
.map((r) => r.raw);
await FileUtils.writeFile(locationToSave, JSON.stringify(resourcesRaw, null, 2));
ctx.log('File successfully saved');
await reporter.displayMessage(`
🎉🎉 Codify successfully initialized. 🎉🎉
The imported configs were written to: ${locationToSave}
Use ${chalk.bgMagenta.bold(' codify plan ')} to compute changes and ${chalk.bgMagenta.bold(' codify apply ')} to apply them.
For more information visit: https://codifycli.com/docs.
Enjoy!
`)
ctx.processFinished(ProcessName.INIT);
process.exit(0);
},
async promptSaveLocation(reporter: Reporter): Promise<string> {
let locationToSave = '';
let input = '';
let isValidSaveLocation = false;
let error = false;
while (!isValidSaveLocation) {
input = (await reporter.promptInput(
`Where to save the new Codify configs? ${chalk.grey.dim(`(leave blank for ${tildify(process.cwd())}/codify.jsonc)`)}`,
error ? `Invalid location: ${input} already exists` : undefined,
`${tildify(process.cwd())}/codify.jsonc`)
)
input = input ?? `${process.cwd()}/codify.jsonc`;
locationToSave = path.resolve(untildify(resolvePathWithVariables(input)));
try {
isValidSaveLocation = !(await FileUtils.fileExists(locationToSave));
error = !isValidSaveLocation;
} catch {
isValidSaveLocation = false;
error = true;
}
}
return locationToSave;
},
filterByOperatingSystemAndDistro(typeId: string, resourceDefinitions: ResourceDefinitionMap, linuxDistro?: LinuxDistro): boolean {
const supportedOperatingSystems = resourceDefinitions.get(typeId)?.operatingSystems;
if (supportedOperatingSystems) {
return supportedOperatingSystems.includes(os.type() as OS);
}
if (os.type() === OS.Linux && linuxDistro) {
const distros = resourceDefinitions.get(typeId)?.linuxDistros;
if (distros) {
return distros.includes(linuxDistro);
}
}
return true;
}
};