11import * as fs from 'node:fs/promises' ;
22import path from 'node:path' ;
3+ import { validate } from 'uuid'
34
45import { InternalError } from '../common/errors.js' ;
56import { ConfigBlock } from '../entities/config.js' ;
67import { Project } from '../entities/project.js' ;
8+ import { FileUtils } from '../utils/file.js' ;
9+ import { CloudParser } from './cloud/cloud-parser.js' ;
710import { ConfigFactory } from './config-factory.js' ;
811import { FileType , InMemoryFile , ParsedConfig } from './entities.js' ;
912import { JsonParser } from './json/json-parser.js' ;
1013import { Json5Parser } from './json5/json-parser.js' ;
1114import { JsoncParser } from './jsonc/json-parser.js' ;
12- import { FileReader } from './reader.js' ;
15+ import { CloudReader } from './reader/cloud-reader.js' ;
16+ import { FileReader } from './reader/file-reader.js' ;
1317import { SourceMapCache } from './source-maps.js' ;
1418import { YamlParser } from './yaml/yaml-parser.js' ;
1519
@@ -20,13 +24,13 @@ class Parser {
2024 [ FileType . JSON ] : new JsonParser ( ) ,
2125 [ FileType . YAML ] : new YamlParser ( ) ,
2226 [ FileType . JSON5 ] : new Json5Parser ( ) ,
23- [ FileType . JSONC ] : new JsoncParser ( )
27+ [ FileType . JSONC ] : new JsoncParser ( ) ,
28+ [ FileType . CLOUD ] : new CloudParser ( ) ,
2429 }
2530
2631 async parse ( dirOrFile : string ) : Promise < Project > {
27- const absolutePath = path . resolve ( dirOrFile ) ;
2832 const sourceMaps = new SourceMapCache ( )
29- const codifyFiles = await this . getFilePaths ( absolutePath )
33+ const codifyFiles = await this . getFilePaths ( dirOrFile )
3034
3135 const configs = await this . readFiles ( codifyFiles )
3236 . then ( ( files ) => this . parseContents ( files , sourceMaps ) )
@@ -36,28 +40,43 @@ class Parser {
3640 }
3741
3842 private async getFilePaths ( dirOrFile : string ) : Promise < string [ ] > {
39- const isDirectory = ( await fs . lstat ( dirOrFile ) ) . isDirectory ( ) ;
43+ // A cloud file is represented as an uuid. Skip file checks if it's a cloud file;
44+ if ( validate ( dirOrFile ) ) {
45+ return [ dirOrFile ] ;
46+ }
47+
48+ const absolutePath = path . resolve ( dirOrFile ) ;
49+ const isDirectory = ( await fs . lstat ( absolutePath ) ) . isDirectory ( ) ;
4050
4151 // A single file was passed in. We need to test if the file satisfies the codify file regex
4252 if ( ! isDirectory ) {
43- const fileName = path . basename ( dirOrFile ) ;
53+ const fileName = path . basename ( absolutePath ) ;
4454 if ( ! CODIFY_FILE_REGEX . test ( fileName ) ) {
45- throw new Error ( `Invalid file path provided ${ dirOrFile } ${ fileName } . Expected the file to be *.codify.jsonc, *.codify.json5, *.codify.json, or *.codify.yaml ` )
55+ throw new Error ( `Invalid file path provided ${ absolutePath } ${ fileName } . Expected the file to be *.codify.jsonc, *.codify.json5, *.codify.json, or *.codify.yaml ` )
4656 }
4757
48- return [ dirOrFile ] ;
58+ return [ absolutePath ] ;
4959 }
5060
51- const filesInDir = await fs . readdir ( dirOrFile ) ;
61+ const filesInDir = await fs . readdir ( absolutePath ) ;
5262
5363 return filesInDir
5464 . filter ( ( name ) => CODIFY_FILE_REGEX . test ( name ) )
55- . map ( ( name ) => path . join ( dirOrFile , name ) )
65+ . map ( ( name ) => path . join ( absolutePath , name ) )
5666 }
5767
5868 private readFiles ( filePaths : string [ ] ) : Promise < InMemoryFile [ ] > {
69+ const cloudReader = new CloudReader ( ) ;
70+ const fileReader = new FileReader ( ) ;
71+
5972 return Promise . all ( filePaths . map (
60- ( p ) => FileReader . read ( p )
73+ async ( p ) => {
74+ if ( validate ( p ) && ! ( await FileUtils . fileExists ( p ) ) ) {
75+ return cloudReader . read ( p )
76+ }
77+
78+ return fileReader . read ( p )
79+ }
6180 ) )
6281 }
6382
0 commit comments