@contentful/app-scripts 2.3.1 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/bin.js CHANGED
@@ -89,6 +89,7 @@ async function runCommand(command, options) {
89
89
  .option('-n, --name <name>', 'Name of the function')
90
90
  .option('-e, --example <example>', 'Name of the reference example')
91
91
  .option('-l, --language <language>', 'Select a language for the function')
92
+ .option('--keep-package-json', 'The package.json file will create or overwrite the existing package.json file in the main directory')
92
93
  .action(async (options) => {
93
94
  await runCommand(index_1.generateFunction, options);
94
95
  });
@@ -1,4 +1,4 @@
1
- import { GenerateFunctionSettings } from '../types';
1
+ import { GenerateFunctionSettings, GenerateFunctionSettingsCLI } from '../types';
2
2
  export declare function buildGenerateFunctionSettingsInteractive(): Promise<GenerateFunctionSettings>;
3
- export declare function validateArguments(options: GenerateFunctionSettings): void;
4
- export declare function buildGenerateFunctionSettingsCLI(options: GenerateFunctionSettings): Promise<GenerateFunctionSettings>;
3
+ export declare function validateArguments(options: GenerateFunctionSettingsCLI): void;
4
+ export declare function buildGenerateFunctionSettingsCLI(options: GenerateFunctionSettingsCLI): Promise<GenerateFunctionSettingsCLI>;
@@ -66,6 +66,8 @@ function validateArguments(options) {
66
66
  }
67
67
  // Convert options to lowercase and trim whitespace
68
68
  for (const key in options) {
69
+ if (key === 'keepPackageJson')
70
+ continue;
69
71
  const optionKey = key;
70
72
  const value = options[optionKey].toLowerCase().trim();
71
73
  if (optionKey === 'language') {
@@ -92,6 +94,9 @@ async function buildGenerateFunctionSettingsCLI(options) {
92
94
  settings.language = options.language;
93
95
  settings.example = options.example;
94
96
  settings.name = options.name;
97
+ if (options.keepPackageJson !== undefined) {
98
+ settings.keepPackageJson = options.keepPackageJson;
99
+ }
95
100
  return settings;
96
101
  }
97
102
  catch (err) {
@@ -1,14 +1,14 @@
1
- import { GenerateFunctionSettings } from '../types';
2
- export declare function cloneFunction(localPath: string, settings: GenerateFunctionSettings): Promise<void>;
3
- export declare function getCloneURL(settings: GenerateFunctionSettings): string;
4
- export declare function touchupAppManifest(localPath: string, settings: GenerateFunctionSettings, renameFunctionFile: string): Promise<void>;
5
- export declare function moveFilesToFinalDirectory(localTmpPath: string, localFunctionsPath: string): void;
6
- export declare function renameClonedFiles(localTmpPath: string, settings: GenerateFunctionSettings): string;
1
+ import { GenerateFunctionSettingsInput } from '../types';
2
+ export declare function cloneFunction(localPath: string, settings: GenerateFunctionSettingsInput): Promise<void>;
3
+ export declare function getCloneURL(settings: GenerateFunctionSettingsInput): string;
4
+ export declare function touchupAppManifest(localPath: string, settings: GenerateFunctionSettingsInput, renameFunctionFile: string): Promise<void>;
5
+ export declare function moveFilesToFinalDirectory(localTmpPath: string, localFunctionsPath: string, localPath: string): void;
6
+ export declare function renameClonedFiles(localTmpPath: string, settings: GenerateFunctionSettingsInput): string;
7
7
  export declare function resolvePaths(localPath: string): {
8
8
  localTmpPath: string;
9
9
  localFunctionsPath: string;
10
10
  };
11
- export declare function cloneAndResolveManifests(cloneURL: string, localTmpPath: string, localPath: string, localFunctionsPath: string): Promise<void>;
11
+ export declare function cloneAndResolveManifests(cloneURL: string, localTmpPath: string, localPath: string, localFunctionsPath: string, keepPackageJson?: boolean): Promise<void>;
12
12
  export declare function clone(cloneURL: string, localFunctionsPath: string): Promise<any>;
13
13
  export declare function mergeAppManifest(localPath: string, localTmpPath: string): Promise<void>;
14
14
  export declare function updatePackageJsonWithBuild(localPath: string, localTmpPath: string): Promise<void>;
@@ -31,11 +31,13 @@ async function cloneFunction(localPath, settings) {
31
31
  console.log((0, logger_1.highlight)(`---- Cloning function ${chalk_1.default.cyan(settings.name)}...`));
32
32
  const { localTmpPath, localFunctionsPath } = resolvePaths(localPath);
33
33
  const cloneURL = getCloneURL(settings);
34
- await cloneAndResolveManifests(cloneURL, localTmpPath, localPath, localFunctionsPath);
34
+ // Pass keepPackageJson if available in settings (from GenerateFunctionSettingsCLI)
35
+ const keepPackageJson = 'keepPackageJson' in settings && typeof settings.keepPackageJson === 'boolean' ? settings.keepPackageJson : false;
36
+ await cloneAndResolveManifests(cloneURL, localTmpPath, localPath, localFunctionsPath, keepPackageJson);
35
37
  // now rename the function file. Find the file with a .ts or .js extension
36
38
  const renameFunctionFile = renameClonedFiles(localTmpPath, settings);
37
39
  // copy the cloned files to the functions directory
38
- moveFilesToFinalDirectory(localTmpPath, localFunctionsPath);
40
+ moveFilesToFinalDirectory(localTmpPath, localFunctionsPath, localPath);
39
41
  // now alter the app-manifest.json to point to the new function file
40
42
  await touchupAppManifest(localPath, settings, renameFunctionFile);
41
43
  }
@@ -58,8 +60,25 @@ async function touchupAppManifest(localPath, settings, renameFunctionFile) {
58
60
  entry.entryFile = `functions/${renameFunctionFile}`;
59
61
  await node_fs_1.default.writeFileSync(appManifestPath, JSON.stringify(appManifest, null, 2));
60
62
  }
61
- function moveFilesToFinalDirectory(localTmpPath, localFunctionsPath) {
62
- node_fs_1.default.cpSync(localTmpPath, localFunctionsPath, { recursive: true });
63
+ function moveFilesToFinalDirectory(localTmpPath, localFunctionsPath, localPath) {
64
+ // Create functions directory if it doesn't exist
65
+ if (!node_fs_1.default.existsSync(localFunctionsPath)) {
66
+ node_fs_1.default.mkdirSync(localFunctionsPath, { recursive: true });
67
+ }
68
+ // Get all files from tmp directory
69
+ const files = node_fs_1.default.readdirSync(localTmpPath);
70
+ // Copy each file except package.json, if it exists
71
+ for (const file of files) {
72
+ const sourcePath = (0, node_path_1.resolve)(localTmpPath, file);
73
+ if (file === 'package.json') {
74
+ const destPath = (0, node_path_1.resolve)(localPath, 'package.json');
75
+ node_fs_1.default.cpSync(sourcePath, destPath);
76
+ continue;
77
+ }
78
+ const destPath = (0, node_path_1.resolve)(localFunctionsPath, file);
79
+ node_fs_1.default.cpSync(sourcePath, destPath, { recursive: true });
80
+ }
81
+ // Clean up tmp directory
63
82
  node_fs_1.default.rmSync(localTmpPath, { recursive: true, force: true });
64
83
  }
65
84
  function renameClonedFiles(localTmpPath, settings) {
@@ -77,14 +96,18 @@ function resolvePaths(localPath) {
77
96
  const localFunctionsPath = (0, node_path_1.resolve)(localPath, 'functions');
78
97
  return { localTmpPath, localFunctionsPath };
79
98
  }
80
- async function cloneAndResolveManifests(cloneURL, localTmpPath, localPath, localFunctionsPath) {
99
+ async function cloneAndResolveManifests(cloneURL, localTmpPath, localPath, localFunctionsPath, keepPackageJson = false) {
81
100
  const tigedInstance = await clone(cloneURL, localTmpPath);
82
101
  // merge the manifest from the template folder to the root folder
83
102
  await mergeAppManifest(localPath, localTmpPath);
84
- // modify package.json build commands
85
- await updatePackageJsonWithBuild(localPath, localTmpPath);
103
+ // create a deep copy of the IGNORED_CLONED_FILES array
104
+ const ignoredFiles = Array.from(constants_1.IGNORED_CLONED_FILES);
105
+ if (!keepPackageJson) {
106
+ // modify package.json build commands
107
+ await updatePackageJsonWithBuild(localPath, localTmpPath);
108
+ ignoredFiles.push('package.json');
109
+ }
86
110
  // check if a tsconfig.json file exists already
87
- const ignoredFiles = constants_1.IGNORED_CLONED_FILES;
88
111
  const tsconfigExists = await (0, file_1.exists)((0, node_path_1.resolve)(localFunctionsPath, 'tsconfig.json'));
89
112
  if (tsconfigExists) {
90
113
  ignoredFiles.push('tsconfig.json');
@@ -137,6 +160,6 @@ async function updatePackageJsonWithBuild(localPath, localTmpPath) {
137
160
  });
138
161
  }
139
162
  else {
140
- (0, logger_1.warn)("Failed to add function build commands: ${packageJsonLocation} does not exist.");
163
+ (0, logger_1.warn)(`Failed to add function build commands: ${packageJsonLocation} does not exist.`);
141
164
  }
142
165
  }
@@ -4,7 +4,7 @@ exports.BANNED_FUNCTION_NAMES = exports.CONTENTFUL_FUNCTIONS_EXAMPLE_REPO_PATH =
4
4
  exports.EXAMPLES_PATH = 'contentful/apps/function-examples/';
5
5
  exports.APP_MANIFEST = 'app-manifest.json';
6
6
  exports.CONTENTFUL_APP_MANIFEST = 'contentful-app-manifest.json';
7
- exports.IGNORED_CLONED_FILES = [exports.APP_MANIFEST, exports.CONTENTFUL_APP_MANIFEST, `package.json`];
7
+ exports.IGNORED_CLONED_FILES = [exports.APP_MANIFEST, exports.CONTENTFUL_APP_MANIFEST];
8
8
  exports.REPO_URL = 'https://github.com/contentful/apps/function-examples';
9
9
  exports.ACCEPTED_EXAMPLE_FOLDERS = [
10
10
  'appevent-handler',
@@ -1,5 +1,5 @@
1
- import { GenerateFunctionSettings } from "../types";
1
+ import { GenerateFunctionSettingsCLI } from "../types";
2
2
  export declare const generateFunction: {
3
3
  interactive: () => Promise<void>;
4
- nonInteractive: (options: GenerateFunctionSettings) => Promise<void>;
4
+ nonInteractive: (options: GenerateFunctionSettingsCLI) => Promise<void>;
5
5
  };
package/lib/types.d.ts CHANGED
@@ -78,6 +78,10 @@ export interface GenerateFunctionSettings {
78
78
  example: string;
79
79
  language: Language;
80
80
  }
81
+ export interface GenerateFunctionSettingsCLI extends GenerateFunctionSettings {
82
+ keepPackageJson?: boolean;
83
+ }
84
+ export type GenerateFunctionSettingsInput = GenerateFunctionSettings | GenerateFunctionSettingsCLI;
81
85
  export interface AddLocationsOptions {
82
86
  organizationId?: string;
83
87
  definitionId?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/app-scripts",
3
- "version": "2.3.1",
3
+ "version": "2.4.0",
4
4
  "description": "A collection of scripts for building Contentful Apps",
5
5
  "author": "Contentful GmbH",
6
6
  "license": "MIT",
@@ -67,7 +67,7 @@
67
67
  "tiged": "^2.12.7",
68
68
  "zod": "^3.24.1"
69
69
  },
70
- "gitHead": "5569e704260c531e9e33ced31ad5d4820f867b1a",
70
+ "gitHead": "571371b92e6f7a3542d76192a15f002771708a86",
71
71
  "devDependencies": {
72
72
  "@types/adm-zip": "0.5.7",
73
73
  "@types/analytics-node": "3.1.14",