|
| 1 | +import * as fs from 'node:fs/promises' |
| 2 | +import * as os from 'node:os' |
| 3 | +import * as path from 'node:path' |
| 4 | + |
| 5 | +import { Project } from '../entities/project.js'; |
| 6 | +import { SubProcessName, ctx } from '../events/context.js'; |
| 7 | +import { CODIFY_FILE_REGEX, CodifyParser } from '../parser/index.js'; |
| 8 | +import { DependencyMap, PluginManager } from '../plugins/plugin-manager.js'; |
| 9 | +import { Reporter } from '../ui/reporters/reporter.js'; |
| 10 | + |
| 11 | +export interface InitializeArgs { |
| 12 | + path?: string; |
| 13 | + secure?: boolean; |
| 14 | + transformProject?: (project: Project) => Project | Promise<Project>; |
| 15 | + allowEmptyProject?: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +export interface InitializationResult { |
| 19 | + typeIdsToDependenciesMap: DependencyMap |
| 20 | + pluginManager: PluginManager, |
| 21 | + project: Project, |
| 22 | +} |
| 23 | + |
| 24 | +export class InitializeOrchestrator { |
| 25 | + static async run( |
| 26 | + args: InitializeArgs, |
| 27 | + reporter: Reporter, |
| 28 | + ): Promise<InitializationResult> { |
| 29 | + let project = await InitializeOrchestrator.parse( |
| 30 | + args.path, |
| 31 | + args.allowEmptyProject ?? false, |
| 32 | + reporter |
| 33 | + ) |
| 34 | + if (args.transformProject) { |
| 35 | + project = await args.transformProject(project); |
| 36 | + } |
| 37 | + |
| 38 | + ctx.subprocessStarted(SubProcessName.INITIALIZE_PLUGINS) |
| 39 | + const pluginManager = new PluginManager(); |
| 40 | + const typeIdsToDependenciesMap = await pluginManager.initialize(project, args.secure); |
| 41 | + ctx.subprocessFinished(SubProcessName.INITIALIZE_PLUGINS) |
| 42 | + |
| 43 | + return { typeIdsToDependenciesMap, pluginManager, project }; |
| 44 | + } |
| 45 | + |
| 46 | + private static async parse( |
| 47 | + fileOrDir: string | undefined, |
| 48 | + allowEmptyProject: boolean, |
| 49 | + reporter: Reporter |
| 50 | + ): Promise<Project> { |
| 51 | + ctx.subprocessStarted(SubProcessName.PARSE); |
| 52 | + |
| 53 | + const pathToParse = (fileOrDir === undefined) |
| 54 | + ? await InitializeOrchestrator.findCodifyJson() |
| 55 | + : fileOrDir |
| 56 | + |
| 57 | + if (!pathToParse && !allowEmptyProject) { |
| 58 | + ctx.subprocessFinished(SubProcessName.PARSE); |
| 59 | + ctx.subprocessStarted(SubProcessName.CREATE_ROOT_FILE) |
| 60 | + const createRootCodifyFile = await reporter.promptConfirmation('\nNo codify file found. Do you want to create a root file at ~/codify.json?'); |
| 61 | + |
| 62 | + if (createRootCodifyFile) { |
| 63 | + await fs.writeFile( |
| 64 | + path.resolve(os.homedir(), 'codify.json'), |
| 65 | + '[]', |
| 66 | + { encoding: 'utf8', flag: 'wx' } |
| 67 | + ); // flag: 'wx' prevents overwrites if the file exists |
| 68 | + } |
| 69 | + |
| 70 | + ctx.subprocessFinished(SubProcessName.CREATE_ROOT_FILE) |
| 71 | + |
| 72 | + console.log('Created ~/codify.json file') |
| 73 | + process.exit(0); |
| 74 | + } |
| 75 | + |
| 76 | + const project = pathToParse |
| 77 | + ? await CodifyParser.parse(pathToParse) |
| 78 | + : Project.empty() |
| 79 | + |
| 80 | + ctx.subprocessFinished(SubProcessName.PARSE); |
| 81 | + |
| 82 | + return project |
| 83 | + } |
| 84 | + |
| 85 | + private static async findCodifyJson(dir?: string): Promise<null | string> { |
| 86 | + dir = dir ?? process.cwd(); |
| 87 | + |
| 88 | + const filesInDir = await fs.readdir(dir); |
| 89 | + if (filesInDir.some((f) => CODIFY_FILE_REGEX.test(f))) { |
| 90 | + return dir; |
| 91 | + } |
| 92 | + |
| 93 | + if (dir.includes(os.homedir()) && dir !== os.homedir()) { |
| 94 | + return this.findCodifyJson(path.dirname(dir)) |
| 95 | + } |
| 96 | + |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments