11import { Config } from '@codifycli/schemas' ;
2- import * as fs from 'node:fs/promises'
3- import * as path from 'node:path'
4- import { validate } from 'uuid' ;
52
6- import { DashboardApiClient } from '../api/dashboard/index.js' ;
7- import { LoginHelper } from '../connect/login-helper.js' ;
83import { Project } from '../entities/project.js' ;
94import { SubProcessName , ctx } from '../events/context.js' ;
10- import { CODIFY_FILE_REGEX , CodifyParser } from '../parser/index.js' ;
5+ import { CodifyParser } from '../parser/index.js' ;
6+ import { CodifyResolver , type CodifySource , CodifySourceType } from '../parser/resolver.js' ;
117import { PluginManager , ResourceDefinitionMap } from '../plugins/plugin-manager.js' ;
128import { Reporter } from '../ui/reporters/reporter.js' ;
13- import { FileUtils } from '../utils/file.js' ;
149import { ShellUtils } from '../utils/shell.js' ;
1510
1611export interface InitializeArgs {
@@ -22,6 +17,7 @@ export interface InitializeArgs {
2217 forceEmptyProject ?: boolean ;
2318 codifyConfigs ?: Config [ ] ;
2419 noProgress ?: boolean ;
20+ allowTemplates ?: boolean ;
2521}
2622
2723export interface InitializationResult {
@@ -65,13 +61,15 @@ export class PluginInitOrchestrator {
6561 return CodifyParser . parseJson ( args . codifyConfigs ) ;
6662 }
6763
68- const codifyPath = await PluginInitOrchestrator . resolveCodifyRootPath ( args , reporter ) ;
69- ctx . subprocessStarted ( SubProcessName . PARSE ) ;
70-
71- const project = codifyPath
72- ? await CodifyParser . parse ( codifyPath )
73- : Project . empty ( )
64+ const source = await CodifyResolver . resolve ( {
65+ location : args . path ,
66+ allowTemplates : args . allowTemplates ,
67+ allowEmpty : args . allowEmptyProject ,
68+ } , reporter ) ;
7469
70+ ctx . subprocessStarted ( SubProcessName . PARSE ) ;
71+ const project = await PluginInitOrchestrator . parseSource ( source ) ;
72+ project . source = source ;
7573 ctx . subprocessFinished ( SubProcessName . PARSE ) ;
7674
7775 if ( args . transformProject ) {
@@ -81,59 +79,23 @@ export class PluginInitOrchestrator {
8179 return project ;
8280 }
8381
84- /** Resolve the root codify file to run.
85- * Order:
86- * 1. If path is specified, return that.
87- * 2. If path is a dir with only one *codify.json|*codify.jsonc|*codify.json5|*codify.yaml, return that.
88- * 3. If path is a UUID, return file from Codify cloud.
89- * 4. If multiple exists in the path (dir), then prompt the user to select one.
90- * 5. If no path is provided, run steps 2 - 4 for the current dir.
91- * 6. If none exists, return default file from codify cloud.
92- * 7. If user is not logged in, return an error.
93- *
94- * @param args
95- * @private
96- */
97- private static async resolveCodifyRootPath ( args : InitializeArgs , reporter : Reporter ) : Promise < string | undefined > {
98- const inputPath = args . path ?? process . cwd ( ) ;
99-
100- // Cloud files will be fetched and processed later in the parser.
101- const isCloud = validate ( inputPath ) ;
102- if ( isCloud ) {
103- return inputPath ;
104- }
105-
106- // Direct files can have its path returned.
107- const isPathDir = await FileUtils . isDir ( inputPath ) ;
108- if ( ! isPathDir ) {
109- return inputPath ;
110- }
111-
112- const filesInDir = await fs . readdir ( inputPath ) ;
113- const codifyFiles = filesInDir . filter ( ( f ) => CODIFY_FILE_REGEX . test ( f ) )
82+ private static async parseSource ( source : CodifySource | null ) : Promise < Project > {
83+ switch ( source ?. type ) {
84+ case CodifySourceType . LOCAL : {
85+ return CodifyParser . parse ( source . path ) ;
86+ }
11487
115- if ( codifyFiles . length === 1 ) {
116- return codifyFiles [ 0 ] ;
117- }
118-
119- if ( codifyFiles . length > 0 ) {
120- const answer = await reporter . promptOptions (
121- 'Multiple codify files found in dir. Please select one:' ,
122- codifyFiles ,
123- ) ;
88+ case CodifySourceType . REMOTE : {
89+ return CodifyParser . parse ( source . documentId ) ;
90+ }
12491
125- return path . join ( inputPath , codifyFiles [ answer ] ) ;
126- }
92+ case CodifySourceType . TEMPLATE : {
93+ return CodifyParser . parseTemplate ( source . template ) ;
94+ }
12795
128- if ( LoginHelper . get ( ) ?. isLoggedIn ) {
129- return ( await DashboardApiClient . getDefaultDocumentId ( ) ) ?? undefined ;
96+ default : {
97+ return Project . empty ( ) ;
98+ }
13099 }
131-
132- if ( args . allowEmptyProject ) {
133- return undefined ;
134- }
135-
136- throw new Error ( 'No codify files found.' ) ;
137100 }
138-
139101}
0 commit comments