@@ -8,7 +8,7 @@ import { APP_MANIFEST, CONTENTFUL_APP_MANIFEST, IGNORED_CLONED_FILES, REPO_URL }
88import { error , highlight , warn } from './logger' ;
99import { exists , mergeJsonIntoFile , whichExists } from './utils/file' ;
1010import { getAddBuildCommandFn } from './utils/package' ;
11- import { GenerateFunctionSettings } from '../types' ;
11+ import { GenerateFunctionSettingsInput } from '../types' ;
1212
1313const addBuildCommand = getAddBuildCommandFn ( {
1414 name : 'build:functions' ,
@@ -17,20 +17,22 @@ const addBuildCommand = getAddBuildCommandFn({
1717
1818export async function cloneFunction (
1919 localPath : string ,
20- settings : GenerateFunctionSettings
20+ settings : GenerateFunctionSettingsInput
2121) {
2222 try {
2323 console . log ( highlight ( `---- Cloning function ${ chalk . cyan ( settings . name ) } ...` ) ) ;
2424 const { localTmpPath, localFunctionsPath } = resolvePaths ( localPath ) ;
2525
2626 const cloneURL = getCloneURL ( settings ) ;
27- await cloneAndResolveManifests ( cloneURL , localTmpPath , localPath , localFunctionsPath ) ;
27+ // Pass keepPackageJson if available in settings (from GenerateFunctionSettingsCLI)
28+ const keepPackageJson = 'keepPackageJson' in settings && typeof settings . keepPackageJson === 'boolean' ? settings . keepPackageJson : false ;
29+ await cloneAndResolveManifests ( cloneURL , localTmpPath , localPath , localFunctionsPath , keepPackageJson ) ;
2830
2931 // now rename the function file. Find the file with a .ts or .js extension
3032 const renameFunctionFile = renameClonedFiles ( localTmpPath , settings ) ;
3133
3234 // copy the cloned files to the functions directory
33- moveFilesToFinalDirectory ( localTmpPath , localFunctionsPath ) ;
35+ moveFilesToFinalDirectory ( localTmpPath , localFunctionsPath , localPath ) ;
3436
3537 // now alter the app-manifest.json to point to the new function file
3638 await touchupAppManifest ( localPath , settings , renameFunctionFile ) ;
@@ -40,11 +42,11 @@ export async function cloneFunction(
4042 }
4143}
4244
43- export function getCloneURL ( settings : GenerateFunctionSettings ) {
45+ export function getCloneURL ( settings : GenerateFunctionSettingsInput ) {
4446 return `${ REPO_URL } /${ settings . example } /${ settings . language } ` ;
4547}
4648
47- export async function touchupAppManifest ( localPath : string , settings : GenerateFunctionSettings , renameFunctionFile : string ) {
49+ export async function touchupAppManifest ( localPath : string , settings : GenerateFunctionSettingsInput , renameFunctionFile : string ) {
4850 const appManifestPath = resolve ( localPath , CONTENTFUL_APP_MANIFEST ) ;
4951 const appManifest = JSON . parse ( fs . readFileSync ( appManifestPath , 'utf-8' ) ) ;
5052 const entry = appManifest [ "functions" ] [ appManifest [ "functions" ] . length - 1 ] ;
@@ -56,12 +58,32 @@ export async function touchupAppManifest(localPath: string, settings: GenerateFu
5658 await fs . writeFileSync ( appManifestPath , JSON . stringify ( appManifest , null , 2 ) ) ;
5759}
5860
59- export function moveFilesToFinalDirectory ( localTmpPath : string , localFunctionsPath : string ) {
60- fs . cpSync ( localTmpPath , localFunctionsPath , { recursive : true } ) ;
61+ export function moveFilesToFinalDirectory ( localTmpPath : string , localFunctionsPath : string , localPath : string ) {
62+ // Create functions directory if it doesn't exist
63+ if ( ! fs . existsSync ( localFunctionsPath ) ) {
64+ fs . mkdirSync ( localFunctionsPath , { recursive : true } ) ;
65+ }
66+
67+ // Get all files from tmp directory
68+ const files = fs . readdirSync ( localTmpPath ) ;
69+
70+ // Copy each file except package.json, if it exists
71+ for ( const file of files ) {
72+ const sourcePath = resolve ( localTmpPath , file ) ;
73+ if ( file === 'package.json' ) {
74+ const destPath = resolve ( localPath , 'package.json' ) ;
75+ fs . cpSync ( sourcePath , destPath ) ;
76+ continue ;
77+ }
78+ const destPath = resolve ( localFunctionsPath , file ) ;
79+ fs . cpSync ( sourcePath , destPath , { recursive : true } ) ;
80+ }
81+
82+ // Clean up tmp directory
6183 fs . rmSync ( localTmpPath , { recursive : true , force : true } ) ;
6284}
6385
64- export function renameClonedFiles ( localTmpPath : string , settings : GenerateFunctionSettings ) {
86+ export function renameClonedFiles ( localTmpPath : string , settings : GenerateFunctionSettingsInput ) {
6587 const files = fs . readdirSync ( localTmpPath ) ;
6688 const functionFile : string | undefined = files . find ( ( file : string ) => file . endsWith ( '.ts' ) || file . endsWith ( '.js' ) ) ;
6789 if ( ! functionFile ) {
@@ -78,17 +100,21 @@ export function resolvePaths(localPath: string) {
78100 return { localTmpPath, localFunctionsPath } ;
79101}
80102
81- export async function cloneAndResolveManifests ( cloneURL : string , localTmpPath : string , localPath : string , localFunctionsPath : string ) {
103+ export async function cloneAndResolveManifests ( cloneURL : string , localTmpPath : string , localPath : string , localFunctionsPath : string , keepPackageJson = false ) {
82104 const tigedInstance = await clone ( cloneURL , localTmpPath ) ;
83105
84106 // merge the manifest from the template folder to the root folder
85107 await mergeAppManifest ( localPath , localTmpPath ) ;
86108
87- // modify package.json build commands
88- await updatePackageJsonWithBuild ( localPath , localTmpPath ) ;
109+ // create a deep copy of the IGNORED_CLONED_FILES array
110+ const ignoredFiles = Array . from ( IGNORED_CLONED_FILES )
111+ if ( ! keepPackageJson ) {
112+ // modify package.json build commands
113+ await updatePackageJsonWithBuild ( localPath , localTmpPath ) ;
114+ ignoredFiles . push ( 'package.json' ) ;
115+ }
89116
90117 // check if a tsconfig.json file exists already
91- const ignoredFiles = IGNORED_CLONED_FILES
92118 const tsconfigExists = await exists ( resolve ( localFunctionsPath , 'tsconfig.json' ) ) ;
93119 if ( tsconfigExists ) {
94120 ignoredFiles . push ( 'tsconfig.json' )
@@ -144,6 +170,6 @@ export async function updatePackageJsonWithBuild(localPath: string, localTmpPath
144170 mergeFn : addBuildCommand ,
145171 } ) ;
146172 } else {
147- warn ( " Failed to add function build commands: ${packageJsonLocation} does not exist." ) ;
173+ warn ( ` Failed to add function build commands: ${ packageJsonLocation } does not exist.` ) ;
148174 }
149175}
0 commit comments