@contentful/app-scripts 2.1.0 → 2.1.1

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
@@ -86,10 +86,9 @@ async function runCommand(command, options) {
86
86
  commander_1.program
87
87
  .command('generate-function')
88
88
  .description('Generate a new Contentful Function')
89
- .option('--name <name>', 'Name of the function')
90
- .option('--template <language>', 'Select a template and language for the function')
91
- .option('--example <example_name>', 'Select an example function to generate')
92
- .option('--language <language>', 'Select a language for the function')
89
+ .option('-n, --name <name>', 'Name of the function')
90
+ .option('-e, --example <example>', 'Name of the reference example')
91
+ .option('-l, --language <language>', 'Select a language for the function')
93
92
  .action(async (options) => {
94
93
  await runCommand(index_1.generateFunction, options);
95
94
  });
@@ -1,3 +1,3 @@
1
- import { GenerateFunctionSettings, GenerateFunctionOptions } from '../types';
2
- export declare function buildGenerateFunctionSettings(): Promise<GenerateFunctionSettings>;
3
- export declare function buildGenerateFunctionSettingsFromOptions(options: GenerateFunctionOptions): Promise<GenerateFunctionSettings>;
1
+ import { GenerateFunctionSettings } from '../types';
2
+ export declare function buildGenerateFunctionSettingsInteractive(): Promise<GenerateFunctionSettings>;
3
+ export declare function buildGenerateFunctionSettingsCLI(options: GenerateFunctionSettings): Promise<GenerateFunctionSettings>;
@@ -3,8 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.buildGenerateFunctionSettings = buildGenerateFunctionSettings;
7
- exports.buildGenerateFunctionSettingsFromOptions = buildGenerateFunctionSettingsFromOptions;
6
+ exports.buildGenerateFunctionSettingsInteractive = buildGenerateFunctionSettingsInteractive;
7
+ exports.buildGenerateFunctionSettingsCLI = buildGenerateFunctionSettingsCLI;
8
8
  const inquirer_1 = __importDefault(require("inquirer"));
9
9
  const path_1 = __importDefault(require("path"));
10
10
  const get_github_folder_names_1 = require("./get-github-folder-names");
@@ -12,131 +12,80 @@ const constants_1 = require("./constants");
12
12
  const ora_1 = __importDefault(require("ora"));
13
13
  const chalk_1 = __importDefault(require("chalk"));
14
14
  const logger_1 = require("./logger");
15
- async function buildGenerateFunctionSettings() {
15
+ async function buildGenerateFunctionSettingsInteractive() {
16
16
  const baseSettings = await inquirer_1.default.prompt([
17
17
  {
18
18
  name: 'name',
19
19
  message: `Function name (${path_1.default.basename(process.cwd())}):`,
20
20
  },
21
+ ]);
22
+ if (constants_1.BANNED_FUNCTION_NAMES.includes(baseSettings.name)) {
23
+ throw new Error(`Invalid function name: ${baseSettings.name}`);
24
+ }
25
+ const filteredSources = await (0, get_github_folder_names_1.getGithubFolderNames)();
26
+ const sourceSpecificSettings = await inquirer_1.default.prompt([
21
27
  {
22
- name: 'sourceType',
23
- message: 'Do you want to start with a blank template or use one of our examples?',
28
+ name: 'example',
29
+ message: 'Select an example:',
30
+ type: 'list',
31
+ choices: filteredSources,
32
+ },
33
+ {
34
+ name: 'language',
35
+ message: 'Select a language',
24
36
  type: 'list',
25
37
  choices: [
26
- { name: 'Template', value: 'template' },
27
- { name: 'Example', value: 'example' },
38
+ { name: 'TypeScript', value: 'typescript' },
39
+ { name: 'JavaScript', value: 'javascript' },
28
40
  ],
29
- default: 'template',
41
+ default: 'typescript',
30
42
  }
31
43
  ]);
32
- if (constants_1.BANNED_FUNCTION_NAMES.includes(baseSettings.name)) {
33
- throw new Error(`Invalid function name: ${baseSettings.name}`);
34
- }
35
- let sourceSpecificSettings;
36
- if (baseSettings.sourceType === 'template') {
37
- sourceSpecificSettings = await inquirer_1.default.prompt([
38
- {
39
- name: 'language',
40
- message: 'Pick a template',
41
- type: 'list',
42
- choices: [
43
- { name: 'TypeScript', value: 'typescript' },
44
- { name: 'JavaScript', value: 'javascript' },
45
- ],
46
- default: 'typescript',
47
- }
48
- ]);
49
- sourceSpecificSettings.sourceName = sourceSpecificSettings.language.toLowerCase();
50
- }
51
- else {
52
- const availableExamples = await (0, get_github_folder_names_1.getGithubFolderNames)();
53
- const filteredExamples = availableExamples.filter((template) => constants_1.ACCEPTED_EXAMPLE_FOLDERS.includes(template));
54
- sourceSpecificSettings = await inquirer_1.default.prompt([
55
- {
56
- name: 'sourceName',
57
- message: 'Select an example:',
58
- type: 'list',
59
- choices: filteredExamples,
60
- },
61
- {
62
- name: 'language',
63
- message: 'Pick a template',
64
- type: 'list',
65
- choices: [
66
- { name: 'TypeScript', value: 'typescript' },
67
- { name: 'JavaScript', value: 'javascript' },
68
- ],
69
- default: 'typescript',
70
- }
71
- ]);
72
- }
73
- baseSettings.sourceName = sourceSpecificSettings.sourceName;
44
+ baseSettings.example = sourceSpecificSettings.example;
74
45
  baseSettings.language = sourceSpecificSettings.language;
75
46
  return baseSettings;
76
47
  }
77
48
  function validateArguments(options) {
78
- const templateRequired = ['name', 'template'];
79
- const exampleRequired = ['name', 'example', 'language'];
49
+ const requiredParams = ['name', 'example', 'language'];
50
+ if (!requiredParams.every((key) => key in options)) {
51
+ throw new Error('You must specify a function name, an example, and a language');
52
+ }
80
53
  if (constants_1.BANNED_FUNCTION_NAMES.includes(options.name)) {
81
54
  throw new Error(`Invalid function name: ${options.name}`);
82
55
  }
83
- if ('template' in options) {
84
- if (!templateRequired.every((key) => key in options)) {
85
- throw new Error('You must specify a function name and a template');
56
+ // Convert options to lowercase and trim whitespace
57
+ for (const key in options) {
58
+ const optionKey = key;
59
+ const value = options[optionKey].toLowerCase().trim();
60
+ if (optionKey === 'language') {
61
+ // Assert that the value is of type Language
62
+ options[optionKey] = value;
86
63
  }
87
- }
88
- else if ('example' in options) {
89
- if (!exampleRequired.every((key) => key in options)) {
90
- throw new Error('You must specify a function name, an example, and a language');
64
+ else {
65
+ options[optionKey] = value;
91
66
  }
92
67
  }
93
- else {
94
- throw new Error('You must specify either --template or --example');
95
- }
96
68
  }
97
- async function buildGenerateFunctionSettingsFromOptions(options) {
69
+ async function buildGenerateFunctionSettingsCLI(options) {
98
70
  const validateSpinner = (0, ora_1.default)('Validating your input\n').start();
99
71
  const settings = {};
100
72
  try {
101
73
  validateArguments(options);
102
- for (const key in options) { // convert all options to lowercase and trim
103
- const optionKey = key;
104
- options[optionKey] = options[optionKey].toLowerCase().trim();
74
+ // Check if the source exists
75
+ const filteredSources = await (0, get_github_folder_names_1.getGithubFolderNames)();
76
+ if (!filteredSources.includes(options.example)) {
77
+ throw new Error(`Invalid example name: ${options.example}. Please choose from: ${filteredSources.join(', ')}`);
105
78
  }
106
- if ('example' in options) {
107
- if ('template' in options) {
108
- throw new Error('Cannot specify both --template and --example');
109
- }
110
- if (!constants_1.ACCEPTED_EXAMPLE_FOLDERS.includes(options.example)) {
111
- throw new Error(`Invalid example name: ${options.example}`);
112
- }
113
- if (!constants_1.ACCEPTED_LANGUAGES.includes(options.language)) {
114
- (0, logger_1.warn)(`Invalid language: ${options.language}. Defaulting to TypeScript.`);
115
- settings.language = 'typescript';
116
- }
117
- else {
118
- settings.language = options.language;
119
- }
120
- settings.sourceType = 'example';
121
- settings.sourceName = options.example;
122
- settings.name = options.name;
79
+ // Check if the language is valid
80
+ if (!constants_1.ACCEPTED_LANGUAGES.includes(options.language)) {
81
+ (0, logger_1.warn)(`Invalid language: ${options.language}. Defaulting to TypeScript.`);
82
+ settings.language = 'typescript';
123
83
  }
124
- else if ('template' in options) {
125
- if ('language' in options && options.language && options.language != options.template) {
126
- console.warn(`Ignoring language option: ${options.language}. Defaulting to ${options.template}.`);
127
- }
128
- if (!constants_1.ACCEPTED_LANGUAGES.includes(options.template)) {
129
- console.warn(`Invalid language: ${options.template}. Defaulting to TypeScript.`);
130
- settings.language = 'typescript';
131
- settings.sourceName = 'typescript';
132
- }
133
- else {
134
- settings.language = options.template;
135
- settings.sourceName = options.template;
136
- }
137
- settings.sourceType = 'template';
138
- settings.name = options.name;
84
+ else {
85
+ settings.language = options.language;
139
86
  }
87
+ settings.example = options.example;
88
+ settings.name = options.name;
140
89
  return settings;
141
90
  }
142
91
  catch (err) {
@@ -45,11 +45,7 @@ async function cloneFunction(localPath, settings) {
45
45
  }
46
46
  }
47
47
  function getCloneURL(settings) {
48
- let cloneURL = `${constants_1.REPO_URL}/${settings.sourceName}`; // this is the default for template
49
- if (settings.sourceType === 'example') {
50
- cloneURL = `${constants_1.REPO_URL}/${settings.sourceName}/${settings.language}`;
51
- }
52
- return cloneURL;
48
+ return `${constants_1.REPO_URL}/${settings.example}/${settings.language}`; // this is the default for template
53
49
  }
54
50
  async function touchupAppManifest(localPath, settings, renameFunctionFile) {
55
51
  const appManifest = JSON.parse(node_fs_1.default.readFileSync(`${localPath}/${constants_1.CONTENTFUL_APP_MANIFEST}`, 'utf-8'));
@@ -1,5 +1,5 @@
1
- import { GenerateFunctionOptions } from "../types";
1
+ import { GenerateFunctionSettings } from "../types";
2
2
  export declare const generateFunction: {
3
3
  interactive: () => Promise<void>;
4
- nonInteractive: (options: GenerateFunctionOptions) => Promise<void>;
4
+ nonInteractive: (options: GenerateFunctionSettings) => Promise<void>;
5
5
  };
@@ -4,11 +4,11 @@ exports.generateFunction = void 0;
4
4
  const build_generate_function_settings_1 = require("./build-generate-function-settings");
5
5
  const create_function_1 = require("./create-function");
6
6
  const interactive = async () => {
7
- const generateFunctionSettings = await (0, build_generate_function_settings_1.buildGenerateFunctionSettings)();
7
+ const generateFunctionSettings = await (0, build_generate_function_settings_1.buildGenerateFunctionSettingsInteractive)();
8
8
  return (0, create_function_1.create)(generateFunctionSettings);
9
9
  };
10
10
  const nonInteractive = async (options) => {
11
- const generateFunctionSettings = await (0, build_generate_function_settings_1.buildGenerateFunctionSettingsFromOptions)(options);
11
+ const generateFunctionSettings = await (0, build_generate_function_settings_1.buildGenerateFunctionSettingsCLI)(options);
12
12
  return (0, create_function_1.create)(generateFunctionSettings);
13
13
  };
14
14
  exports.generateFunction = {
package/lib/types.d.ts CHANGED
@@ -71,21 +71,9 @@ export interface BuildFunctionsOptions {
71
71
  esbuildConfig?: string;
72
72
  watch?: boolean;
73
73
  }
74
- export type SourceType = 'template' | 'example';
75
74
  export type Language = 'javascript' | 'typescript';
76
- export type AcceptedFunctionExamples = 'appevent-handler';
77
- export type SourceName = Language | AcceptedFunctionExamples;
78
75
  export interface GenerateFunctionSettings {
79
76
  name: string;
80
- sourceType: SourceType;
81
- sourceName: SourceName;
77
+ example: string;
82
78
  language: Language;
83
79
  }
84
- export type GenerateFunctionOptions = {
85
- name: string;
86
- } & ({
87
- example: AcceptedFunctionExamples;
88
- language: Language;
89
- } | {
90
- template: Language;
91
- });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/app-scripts",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
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": "c5dc99f22aa80565484497549a0b4f4dc5e35b7b",
70
+ "gitHead": "c86c0866476bf0cae4f72ff8730c2ff9c120a593",
71
71
  "devDependencies": {
72
72
  "@types/adm-zip": "0.5.7",
73
73
  "@types/analytics-node": "3.1.14",