-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialize-plugins.ts
More file actions
101 lines (81 loc) · 2.99 KB
/
initialize-plugins.ts
File metadata and controls
101 lines (81 loc) · 2.99 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
import * as fs from 'node:fs/promises'
import * as os from 'node:os'
import * as path from 'node:path'
import { Project } from '../entities/project.js';
import { SubProcessName, ctx } from '../events/context.js';
import { CODIFY_FILE_REGEX, CodifyParser } from '../parser/index.js';
import { DependencyMap, PluginManager } from '../plugins/plugin-manager.js';
import { Reporter } from '../ui/reporters/reporter.js';
export interface InitializeArgs {
path?: string;
secure?: boolean;
verbosityLevel?: number;
transformProject?: (project: Project) => Project | Promise<Project>;
allowEmptyProject?: boolean;
}
export interface InitializationResult {
typeIdsToDependenciesMap: DependencyMap
pluginManager: PluginManager,
project: Project,
}
export class PluginInitOrchestrator {
static async run(
args: InitializeArgs,
reporter: Reporter,
): Promise<InitializationResult> {
let project = await PluginInitOrchestrator.parse(
args.path,
args.allowEmptyProject ?? false,
reporter
)
if (args.transformProject) {
project = await args.transformProject(project);
}
ctx.subprocessStarted(SubProcessName.INITIALIZE_PLUGINS)
const pluginManager = new PluginManager();
const typeIdsToDependenciesMap = await pluginManager.initialize(project, args.secure, args.verbosityLevel);
ctx.subprocessFinished(SubProcessName.INITIALIZE_PLUGINS)
return { typeIdsToDependenciesMap, pluginManager, project };
}
private static async parse(
fileOrDir: string | undefined,
allowEmptyProject: boolean,
reporter: Reporter
): Promise<Project> {
ctx.subprocessStarted(SubProcessName.PARSE);
const pathToParse = (fileOrDir === undefined)
? await PluginInitOrchestrator.findCodifyJson()
: fileOrDir
if (!pathToParse && !allowEmptyProject) {
ctx.subprocessFinished(SubProcessName.PARSE);
ctx.subprocessStarted(SubProcessName.CREATE_ROOT_FILE)
const createRootCodifyFile = await reporter.promptConfirmation('\nNo codify file found. Do you want to create a root file at ~/codify.json?');
if (createRootCodifyFile) {
await fs.writeFile(
path.resolve(os.homedir(), 'codify.json'),
'[]',
{ encoding: 'utf8', flag: 'wx' }
); // flag: 'wx' prevents overwrites if the file exists
}
ctx.subprocessFinished(SubProcessName.CREATE_ROOT_FILE)
console.log('Created ~/codify.json file')
process.exit(0);
}
const project = pathToParse
? await CodifyParser.parse(pathToParse)
: Project.empty()
ctx.subprocessFinished(SubProcessName.PARSE);
return project
}
private static async findCodifyJson(dir?: string): Promise<null | string> {
dir = dir ?? process.cwd();
const filesInDir = await fs.readdir(dir);
if (filesInDir.some((f) => CODIFY_FILE_REGEX.test(f))) {
return dir;
}
if (dir.includes(os.homedir()) && dir !== os.homedir()) {
return this.findCodifyJson(path.dirname(dir))
}
return null;
}
}