@@ -8,7 +8,7 @@ import { APP_MANIFEST, CONTENTFUL_APP_MANIFEST, IGNORED_CLONED_FILES, REPO_URL }
8
8
import { error , highlight , warn } from './logger' ;
9
9
import { exists , mergeJsonIntoFile , whichExists } from './utils/file' ;
10
10
import { getAddBuildCommandFn } from './utils/package' ;
11
- import { GenerateFunctionSettings } from '../types' ;
11
+ import { GenerateFunctionSettingsInput } from '../types' ;
12
12
13
13
const addBuildCommand = getAddBuildCommandFn ( {
14
14
name : 'build:functions' ,
@@ -17,20 +17,22 @@ const addBuildCommand = getAddBuildCommandFn({
17
17
18
18
export async function cloneFunction (
19
19
localPath : string ,
20
- settings : GenerateFunctionSettings
20
+ settings : GenerateFunctionSettingsInput
21
21
) {
22
22
try {
23
23
console . log ( highlight ( `---- Cloning function ${ chalk . cyan ( settings . name ) } ...` ) ) ;
24
24
const { localTmpPath, localFunctionsPath } = resolvePaths ( localPath ) ;
25
25
26
26
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 ) ;
28
30
29
31
// now rename the function file. Find the file with a .ts or .js extension
30
32
const renameFunctionFile = renameClonedFiles ( localTmpPath , settings ) ;
31
33
32
34
// copy the cloned files to the functions directory
33
- moveFilesToFinalDirectory ( localTmpPath , localFunctionsPath ) ;
35
+ moveFilesToFinalDirectory ( localTmpPath , localFunctionsPath , localPath ) ;
34
36
35
37
// now alter the app-manifest.json to point to the new function file
36
38
await touchupAppManifest ( localPath , settings , renameFunctionFile ) ;
@@ -40,11 +42,11 @@ export async function cloneFunction(
40
42
}
41
43
}
42
44
43
- export function getCloneURL ( settings : GenerateFunctionSettings ) {
45
+ export function getCloneURL ( settings : GenerateFunctionSettingsInput ) {
44
46
return `${ REPO_URL } /${ settings . example } /${ settings . language } ` ;
45
47
}
46
48
47
- export async function touchupAppManifest ( localPath : string , settings : GenerateFunctionSettings , renameFunctionFile : string ) {
49
+ export async function touchupAppManifest ( localPath : string , settings : GenerateFunctionSettingsInput , renameFunctionFile : string ) {
48
50
const appManifestPath = resolve ( localPath , CONTENTFUL_APP_MANIFEST ) ;
49
51
const appManifest = JSON . parse ( fs . readFileSync ( appManifestPath , 'utf-8' ) ) ;
50
52
const entry = appManifest [ "functions" ] [ appManifest [ "functions" ] . length - 1 ] ;
@@ -56,12 +58,32 @@ export async function touchupAppManifest(localPath: string, settings: GenerateFu
56
58
await fs . writeFileSync ( appManifestPath , JSON . stringify ( appManifest , null , 2 ) ) ;
57
59
}
58
60
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
61
83
fs . rmSync ( localTmpPath , { recursive : true , force : true } ) ;
62
84
}
63
85
64
- export function renameClonedFiles ( localTmpPath : string , settings : GenerateFunctionSettings ) {
86
+ export function renameClonedFiles ( localTmpPath : string , settings : GenerateFunctionSettingsInput ) {
65
87
const files = fs . readdirSync ( localTmpPath ) ;
66
88
const functionFile : string | undefined = files . find ( ( file : string ) => file . endsWith ( '.ts' ) || file . endsWith ( '.js' ) ) ;
67
89
if ( ! functionFile ) {
@@ -78,17 +100,21 @@ export function resolvePaths(localPath: string) {
78
100
return { localTmpPath, localFunctionsPath } ;
79
101
}
80
102
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 ) {
82
104
const tigedInstance = await clone ( cloneURL , localTmpPath ) ;
83
105
84
106
// merge the manifest from the template folder to the root folder
85
107
await mergeAppManifest ( localPath , localTmpPath ) ;
86
108
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
+ }
89
116
90
117
// check if a tsconfig.json file exists already
91
- const ignoredFiles = IGNORED_CLONED_FILES
92
118
const tsconfigExists = await exists ( resolve ( localFunctionsPath , 'tsconfig.json' ) ) ;
93
119
if ( tsconfigExists ) {
94
120
ignoredFiles . push ( 'tsconfig.json' )
@@ -144,6 +170,6 @@ export async function updatePackageJsonWithBuild(localPath: string, localTmpPath
144
170
mergeFn : addBuildCommand ,
145
171
} ) ;
146
172
} 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.` ) ;
148
174
}
149
175
}
0 commit comments