-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinitialize-plugins.ts
More file actions
139 lines (113 loc) · 4.21 KB
/
Copy pathinitialize-plugins.ts
File metadata and controls
139 lines (113 loc) · 4.21 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
132
133
134
135
136
137
138
139
import { Config } from '@codifycli/schemas';
import * as fs from 'node:fs/promises'
import * as path from 'node:path'
import { validate } from 'uuid';
import { DashboardApiClient } from '../api/dashboard/index.js';
import { LoginHelper } from '../connect/login-helper.js';
import { Project } from '../entities/project.js';
import { SubProcessName, ctx } from '../events/context.js';
import { CODIFY_FILE_REGEX, CodifyParser } from '../parser/index.js';
import { PluginManager, ResourceDefinitionMap } from '../plugins/plugin-manager.js';
import { Reporter } from '../ui/reporters/reporter.js';
import { FileUtils } from '../utils/file.js';
import { ShellUtils } from '../utils/shell.js';
export interface InitializeArgs {
path?: string;
secure?: boolean;
verbosityLevel?: number;
transformProject?: (project: Project) => Project | Promise<Project>;
allowEmptyProject?: boolean;
forceEmptyProject?: boolean;
codifyConfigs?: Config[];
noProgress?: boolean;
}
export interface InitializationResult {
resourceDefinitions: ResourceDefinitionMap
pluginManager: PluginManager,
project: Project,
}
export class PluginInitOrchestrator {
static async run(
args: InitializeArgs,
reporter: Reporter,
): Promise<InitializationResult> {
await ShellUtils.validateShell();
const project = await PluginInitOrchestrator.parseProject(
args,
reporter
);
if (!args.noProgress) ctx.subprocessStarted(SubProcessName.INITIALIZE_PLUGINS)
const pluginManager = new PluginManager();
const resourceDefinitions = await pluginManager.initialize(project, args.secure, args.verbosityLevel);
if (!args.noProgress) ctx.subprocessFinished(SubProcessName.INITIALIZE_PLUGINS)
project.removeResourcesUsingOsFilter();
await project.removeResourcesUsingDistroFilter();
return { resourceDefinitions, pluginManager, project };
}
private static async parseProject(
args: InitializeArgs,
reporter: Reporter,
): Promise<Project> {
if (args.forceEmptyProject) {
return Project.empty();
}
if (args.codifyConfigs) {
return CodifyParser.parseJson(args.codifyConfigs);
}
const codifyPath = await PluginInitOrchestrator.resolveCodifyRootPath(args, reporter);
ctx.subprocessStarted(SubProcessName.PARSE);
const project = codifyPath
? await CodifyParser.parse(codifyPath)
: Project.empty()
ctx.subprocessFinished(SubProcessName.PARSE);
if (args.transformProject) {
return args.transformProject(project);
}
return project;
}
/** Resolve the root codify file to run.
* Order:
* 1. If path is specified, return that.
* 2. If path is a dir with only one *codify.json|*codify.jsonc|*codify.json5|*codify.yaml, return that.
* 3. If path is a UUID, return file from Codify cloud.
* 4. If multiple exists in the path (dir), then prompt the user to select one.
* 5. If no path is provided, run steps 2 - 4 for the current dir.
* 6. If none exists, return default file from codify cloud.
* 7. If user is not logged in, return an error.
*
* @param args
* @private
*/
private static async resolveCodifyRootPath(args: InitializeArgs, reporter: Reporter): Promise<string | undefined> {
const inputPath = args.path ?? process.cwd();
// Cloud files will be fetched and processed later in the parser.
const isCloud = validate(inputPath);
if (isCloud) {
return inputPath;
}
// Direct files can have its path returned.
const isPathDir = await FileUtils.isDir(inputPath);
if (!isPathDir) {
return inputPath;
}
const filesInDir = await fs.readdir(inputPath);
const codifyFiles = filesInDir.filter((f) => CODIFY_FILE_REGEX.test(f))
if (codifyFiles.length === 1) {
return codifyFiles[0];
}
if (codifyFiles.length > 0) {
const answer = await reporter.promptOptions(
'Multiple codify files found in dir. Please select one:',
codifyFiles,
);
return path.join(inputPath, codifyFiles[answer]);
}
if (LoginHelper.get()?.isLoggedIn) {
return (await DashboardApiClient.getDefaultDocumentId()) ?? undefined;
}
if (args.allowEmptyProject) {
return undefined;
}
throw new Error('No codify files found.');
}
}