11import { readFile } from 'node:fs/promises'
22import { existsSync , readdirSync } from 'node:fs'
3- import { basename , dirname , resolve } from 'node:path'
3+ import { resolve } from 'node:path'
44
5- import { getBinaryFile } from './file-helper.js'
5+ import { readFileHelper } from './file-helper.js'
66import { findFilesRecursively , isDirectory } from './utils.js'
77
8- import type { AddOn , Environment , FrameworkDefinition } from './types.js'
8+ import type { AddOn , FrameworkDefinition } from './types.js'
99
1010export async function getAllAddOns (
1111 framework : FrameworkDefinition ,
@@ -50,7 +50,14 @@ export async function getAllAddOns(
5050 }
5151 const files : Record < string , string > = { }
5252 for ( const file of Object . keys ( absoluteFiles ) ) {
53- files [ file . replace ( assetsDir , '.' ) ] = absoluteFiles [ file ]
53+ files [ file . replace ( assetsDir , '.' ) ] = await readFileHelper ( file )
54+ }
55+
56+ const getFiles = ( ) => {
57+ return Promise . resolve ( Object . keys ( files ) )
58+ }
59+ const getFileContents = ( path : string ) => {
60+ return Promise . resolve ( files [ path ] )
5461 }
5562
5663 addOns . push ( {
@@ -61,6 +68,8 @@ export async function getAllAddOns(
6168 readme,
6269 files,
6370 deletedFiles : [ ] ,
71+ getFiles,
72+ getFileContents,
6473 } )
6574 }
6675 }
@@ -113,46 +122,9 @@ export async function loadRemoteAddOn(url: string): Promise<AddOn> {
113122 const response = await fetch ( url )
114123 const fileContent = await response . json ( )
115124 fileContent . id = url
116- return fileContent
117- }
118-
119- export async function copyAddOnFile (
120- environment : Environment ,
121- content : string ,
122- target : string ,
123- targetPath : string ,
124- templateFile : ( content : string , targetFileName : string ) => Promise < void > ,
125- ) {
126- let targetFile = basename ( target ) . replace ( / ^ _ d o t _ / , '.' )
127- let isTemplate = false
128- if ( targetFile . endsWith ( '.ejs' ) ) {
129- targetFile = targetFile . replace ( '.ejs' , '' )
130- isTemplate = true
131- }
132- let isAppend = false
133- if ( targetFile . endsWith ( '.append' ) ) {
134- targetFile = targetFile . replace ( '.append' , '' )
135- isAppend = true
136- }
137-
138- const finalTargetPath = resolve ( dirname ( targetPath ) , targetFile )
139-
140- const binaryContent = getBinaryFile ( content )
141- if ( binaryContent ) {
142- await environment . writeFile (
143- finalTargetPath ,
144- binaryContent as unknown as string ,
145- )
146- return
147- }
148-
149- if ( isTemplate ) {
150- await templateFile ( content , finalTargetPath )
151- } else {
152- if ( isAppend ) {
153- await environment . appendFile ( finalTargetPath , content )
154- } else {
155- await environment . writeFile ( finalTargetPath , content )
156- }
125+ return {
126+ ...fileContent ,
127+ getFiles : ( ) => Promise . resolve ( Object . keys ( fileContent . files ) ) ,
128+ getFileContents : ( path : string ) => Promise . resolve ( fileContent . files [ path ] ) ,
157129 }
158130}
0 commit comments