@contentful/app-scripts 2.1.2 → 2.1.4
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 +1 -2
- package/lib/generate-function/build-generate-function-settings.d.ts +1 -0
- package/lib/generate-function/build-generate-function-settings.js +24 -19
- package/lib/generate-function/clone.js +2 -2
- package/lib/generate-function/constants.js +1 -1
- package/lib/generate-function/index.js +25 -4
- package/lib/generate-function/types.d.ts +3 -0
- package/lib/generate-function/types.js +8 -1
- package/package.json +4 -4
package/lib/bin.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { GenerateFunctionSettings } from '../types';
|
|
2
2
|
export declare function buildGenerateFunctionSettingsInteractive(): Promise<GenerateFunctionSettings>;
|
|
3
|
+
export declare function validateArguments(options: GenerateFunctionSettings): void;
|
|
3
4
|
export declare function buildGenerateFunctionSettingsCLI(options: GenerateFunctionSettings): Promise<GenerateFunctionSettings>;
|
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.buildGenerateFunctionSettingsInteractive = buildGenerateFunctionSettingsInteractive;
|
|
7
|
+
exports.validateArguments = validateArguments;
|
|
7
8
|
exports.buildGenerateFunctionSettingsCLI = buildGenerateFunctionSettingsCLI;
|
|
8
9
|
const inquirer_1 = __importDefault(require("inquirer"));
|
|
9
10
|
const path_1 = __importDefault(require("path"));
|
|
@@ -12,6 +13,7 @@ const constants_1 = require("./constants");
|
|
|
12
13
|
const ora_1 = __importDefault(require("ora"));
|
|
13
14
|
const chalk_1 = __importDefault(require("chalk"));
|
|
14
15
|
const logger_1 = require("./logger");
|
|
16
|
+
const types_1 = require("./types");
|
|
15
17
|
async function buildGenerateFunctionSettingsInteractive() {
|
|
16
18
|
const baseSettings = await inquirer_1.default.prompt([
|
|
17
19
|
{
|
|
@@ -19,9 +21,7 @@ async function buildGenerateFunctionSettingsInteractive() {
|
|
|
19
21
|
message: `Function name (${path_1.default.basename(process.cwd())}):`,
|
|
20
22
|
},
|
|
21
23
|
]);
|
|
22
|
-
|
|
23
|
-
throw new Error(`Invalid function name: ${baseSettings.name}`);
|
|
24
|
-
}
|
|
24
|
+
validateFunctionName(baseSettings);
|
|
25
25
|
const filteredSources = await (0, get_github_folder_names_1.getGithubFolderNames)();
|
|
26
26
|
const sourceSpecificSettings = await inquirer_1.default.prompt([
|
|
27
27
|
{
|
|
@@ -45,25 +45,38 @@ async function buildGenerateFunctionSettingsInteractive() {
|
|
|
45
45
|
baseSettings.language = sourceSpecificSettings.language;
|
|
46
46
|
return baseSettings;
|
|
47
47
|
}
|
|
48
|
+
function validateFunctionName(baseSettings) {
|
|
49
|
+
if (constants_1.BANNED_FUNCTION_NAMES.includes(baseSettings.name)) {
|
|
50
|
+
throw new types_1.ValidationError(chalk_1.default.red(`Invalid function name: ${baseSettings.name} is not allowed.`));
|
|
51
|
+
}
|
|
52
|
+
else if (!(/^[a-z0-9]+$/i.test(baseSettings.name))) {
|
|
53
|
+
throw new types_1.ValidationError(chalk_1.default.red(`Invalid function name: ${baseSettings.name}. Note that function names must be alphanumeric.`));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
48
56
|
function validateArguments(options) {
|
|
49
57
|
const requiredParams = ['name', 'example', 'language'];
|
|
50
58
|
if (!requiredParams.every((key) => key in options)) {
|
|
51
|
-
throw new
|
|
59
|
+
throw new types_1.ValidationError(chalk_1.default.red('You must specify a function name, an example, and a language'));
|
|
52
60
|
}
|
|
53
|
-
|
|
54
|
-
|
|
61
|
+
validateFunctionName(options);
|
|
62
|
+
// Check if the language is valid
|
|
63
|
+
if (!constants_1.ACCEPTED_LANGUAGES.includes(options.language)) {
|
|
64
|
+
(0, logger_1.warn)(`Invalid language: ${options.language}. Defaulting to TypeScript.`);
|
|
65
|
+
options.language = 'typescript';
|
|
55
66
|
}
|
|
56
67
|
// Convert options to lowercase and trim whitespace
|
|
57
68
|
for (const key in options) {
|
|
58
69
|
const optionKey = key;
|
|
59
70
|
const value = options[optionKey].toLowerCase().trim();
|
|
60
71
|
if (optionKey === 'language') {
|
|
61
|
-
// Assert that the value is of type Language
|
|
62
72
|
options[optionKey] = value;
|
|
63
73
|
}
|
|
64
|
-
else {
|
|
74
|
+
else if (optionKey === 'example') {
|
|
65
75
|
options[optionKey] = value;
|
|
66
76
|
}
|
|
77
|
+
else { // don't want to lowercase function names
|
|
78
|
+
options[optionKey] = options[optionKey].trim();
|
|
79
|
+
}
|
|
67
80
|
}
|
|
68
81
|
}
|
|
69
82
|
async function buildGenerateFunctionSettingsCLI(options) {
|
|
@@ -74,16 +87,9 @@ async function buildGenerateFunctionSettingsCLI(options) {
|
|
|
74
87
|
// Check if the source exists
|
|
75
88
|
const filteredSources = await (0, get_github_folder_names_1.getGithubFolderNames)();
|
|
76
89
|
if (!filteredSources.includes(options.example)) {
|
|
77
|
-
throw new
|
|
78
|
-
}
|
|
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';
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
settings.language = options.language;
|
|
90
|
+
throw new types_1.ValidationError(`Invalid example name: ${options.example}. Please choose from: ${filteredSources.join(', ')}`);
|
|
86
91
|
}
|
|
92
|
+
settings.language = options.language;
|
|
87
93
|
settings.example = options.example;
|
|
88
94
|
settings.name = options.name;
|
|
89
95
|
return settings;
|
|
@@ -91,10 +97,9 @@ async function buildGenerateFunctionSettingsCLI(options) {
|
|
|
91
97
|
catch (err) {
|
|
92
98
|
console.log(`
|
|
93
99
|
${chalk_1.default.red('Validation failed')}
|
|
94
|
-
${err.message}
|
|
95
100
|
`);
|
|
96
101
|
// eslint-disable-next-line no-process-exit
|
|
97
|
-
|
|
102
|
+
throw err;
|
|
98
103
|
}
|
|
99
104
|
finally {
|
|
100
105
|
validateSpinner.stop();
|
|
@@ -41,11 +41,11 @@ async function cloneFunction(localPath, settings) {
|
|
|
41
41
|
}
|
|
42
42
|
catch (e) {
|
|
43
43
|
(0, logger_1.error)(`Failed to clone function ${(0, logger_1.highlight)(chalk_1.default.cyan(settings.name))}`, e);
|
|
44
|
-
|
|
44
|
+
throw Error(chalk_1.default.red('Failed to clone function ') + (0, logger_1.highlight)(chalk_1.default.cyan(settings.name)));
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
function getCloneURL(settings) {
|
|
48
|
-
return `${constants_1.REPO_URL}/${settings.example}/${settings.language}`;
|
|
48
|
+
return `${constants_1.REPO_URL}/${settings.example}/${settings.language}`;
|
|
49
49
|
}
|
|
50
50
|
async function touchupAppManifest(localPath, settings, renameFunctionFile) {
|
|
51
51
|
const appManifest = JSON.parse(node_fs_1.default.readFileSync(`${localPath}/${constants_1.CONTENTFUL_APP_MANIFEST}`, 'utf-8'));
|
|
@@ -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, `package.json`];
|
|
7
|
+
exports.IGNORED_CLONED_FILES = [exports.APP_MANIFEST, exports.CONTENTFUL_APP_MANIFEST, `package.json`];
|
|
8
8
|
exports.REPO_URL = 'https://github.com/contentful/apps/function-examples';
|
|
9
9
|
exports.ACCEPTED_EXAMPLE_FOLDERS = [
|
|
10
10
|
'appevent-handler',
|
|
@@ -3,13 +3,34 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
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
|
+
const types_1 = require("./types");
|
|
6
7
|
const interactive = async () => {
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
try {
|
|
9
|
+
const generateFunctionSettings = await (0, build_generate_function_settings_1.buildGenerateFunctionSettingsInteractive)();
|
|
10
|
+
return (0, create_function_1.create)(generateFunctionSettings);
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
if (e instanceof types_1.ValidationError) {
|
|
14
|
+
console.error(e.message);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
console.error(e);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
9
20
|
};
|
|
10
21
|
const nonInteractive = async (options) => {
|
|
11
|
-
|
|
12
|
-
|
|
22
|
+
try {
|
|
23
|
+
const generateFunctionSettings = await (0, build_generate_function_settings_1.buildGenerateFunctionSettingsCLI)(options);
|
|
24
|
+
return (0, create_function_1.create)(generateFunctionSettings);
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
if (e instanceof types_1.ValidationError) {
|
|
28
|
+
console.error(e.message);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
console.error(e);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
13
34
|
};
|
|
14
35
|
exports.generateFunction = {
|
|
15
36
|
interactive,
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.InvalidTemplateError = exports.HTTPResponseError = void 0;
|
|
3
|
+
exports.ValidationError = exports.InvalidTemplateError = exports.HTTPResponseError = void 0;
|
|
4
4
|
class HTTPResponseError extends Error {
|
|
5
5
|
}
|
|
6
6
|
exports.HTTPResponseError = HTTPResponseError;
|
|
7
7
|
class InvalidTemplateError extends Error {
|
|
8
8
|
}
|
|
9
9
|
exports.InvalidTemplateError = InvalidTemplateError;
|
|
10
|
+
class ValidationError extends Error {
|
|
11
|
+
constructor(message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "ValidationError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.ValidationError = ValidationError;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/app-scripts",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.4",
|
|
4
4
|
"description": "A collection of scripts for building Contentful Apps",
|
|
5
5
|
"author": "Contentful GmbH",
|
|
6
6
|
"license": "MIT",
|
|
@@ -55,9 +55,9 @@
|
|
|
55
55
|
"bottleneck": "2.19.5",
|
|
56
56
|
"chalk": "4.1.2",
|
|
57
57
|
"commander": "12.1.0",
|
|
58
|
-
"contentful-management": "11.
|
|
58
|
+
"contentful-management": "11.48.0",
|
|
59
59
|
"dotenv": "16.4.7",
|
|
60
|
-
"esbuild": "^0.25.
|
|
60
|
+
"esbuild": "^0.25.1",
|
|
61
61
|
"ignore": "7.0.3",
|
|
62
62
|
"inquirer": "8.2.6",
|
|
63
63
|
"lodash": "4.17.21",
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
"tiged": "^2.12.7",
|
|
68
68
|
"zod": "^3.24.1"
|
|
69
69
|
},
|
|
70
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "4538fa97b37141989cf9788881901c326e0c26c1",
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"@types/adm-zip": "0.5.7",
|
|
73
73
|
"@types/analytics-node": "3.1.14",
|