@hubspot/local-dev-lib 0.0.1-experimental.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/LICENSE +12 -0
- package/README.md +17 -0
- package/api/appsDev.d.ts +6 -0
- package/api/appsDev.js +29 -0
- package/api/customObjects.d.ts +8 -0
- package/api/customObjects.js +45 -0
- package/api/designManager.d.ts +5 -0
- package/api/designManager.js +18 -0
- package/api/developerTestAccounts.d.ts +7 -0
- package/api/developerTestAccounts.js +48 -0
- package/api/fileManager.d.ts +6 -0
- package/api/fileManager.js +63 -0
- package/api/fileMapper.d.ts +12 -0
- package/api/fileMapper.js +106 -0
- package/api/fileTransport.d.ts +4 -0
- package/api/fileTransport.js +39 -0
- package/api/functions.d.ts +8 -0
- package/api/functions.js +43 -0
- package/api/github.d.ts +11 -0
- package/api/github.js +71 -0
- package/api/hubdb.d.ts +12 -0
- package/api/hubdb.js +67 -0
- package/api/lighthouseScore.d.ts +6 -0
- package/api/lighthouseScore.js +26 -0
- package/api/localDevAuth.d.ts +8 -0
- package/api/localDevAuth.js +53 -0
- package/api/marketplaceValidation.d.ts +6 -0
- package/api/marketplaceValidation.js +26 -0
- package/api/projects.d.ts +40 -0
- package/api/projects.js +216 -0
- package/api/sandboxHubs.d.ts +7 -0
- package/api/sandboxHubs.js +49 -0
- package/api/sandboxSync.d.ts +4 -0
- package/api/sandboxSync.js +26 -0
- package/api/secrets.d.ts +6 -0
- package/api/secrets.js +37 -0
- package/api/validateHubl.d.ts +3 -0
- package/api/validateHubl.js +15 -0
- package/config/CLIConfiguration.d.ts +62 -0
- package/config/CLIConfiguration.js +467 -0
- package/config/configFile.d.ts +21 -0
- package/config/configFile.js +102 -0
- package/config/configUtils.d.ts +5 -0
- package/config/configUtils.js +87 -0
- package/config/config_DEPRECATED.d.ts +75 -0
- package/config/config_DEPRECATED.js +678 -0
- package/config/environment.d.ts +2 -0
- package/config/environment.js +60 -0
- package/config/getAccountIdentifier.d.ts +2 -0
- package/config/getAccountIdentifier.js +15 -0
- package/config/index.d.ts +41 -0
- package/config/index.js +260 -0
- package/constants/api.d.ts +15 -0
- package/constants/api.js +18 -0
- package/constants/auth.d.ts +37 -0
- package/constants/auth.js +38 -0
- package/constants/config.d.ts +18 -0
- package/constants/config.js +22 -0
- package/constants/environments.d.ts +15 -0
- package/constants/environments.js +18 -0
- package/constants/extensions.d.ts +6 -0
- package/constants/extensions.js +28 -0
- package/constants/files.d.ts +21 -0
- package/constants/files.js +24 -0
- package/constants/ports.d.ts +3 -0
- package/constants/ports.js +6 -0
- package/enums/build.d.ts +36 -0
- package/enums/build.js +39 -0
- package/enums/deploy.d.ts +11 -0
- package/enums/deploy.js +14 -0
- package/enums/project.d.ts +6 -0
- package/enums/project.js +9 -0
- package/errors/errors_DEPRECATED.d.ts +3 -0
- package/errors/errors_DEPRECATED.js +60 -0
- package/errors/index.d.ts +18 -0
- package/errors/index.js +63 -0
- package/http/addQueryParams.d.ts +2 -0
- package/http/addQueryParams.js +14 -0
- package/http/getAxiosConfig.d.ts +9 -0
- package/http/getAxiosConfig.js +66 -0
- package/http/index.d.ts +17 -0
- package/http/index.js +173 -0
- package/http/unauthed.d.ts +15 -0
- package/http/unauthed.js +38 -0
- package/lang/en.json +389 -0
- package/lib/archive.d.ts +3 -0
- package/lib/archive.js +117 -0
- package/lib/cms/functions.d.ts +8 -0
- package/lib/cms/functions.js +181 -0
- package/lib/cms/handleFieldsJS.d.ts +33 -0
- package/lib/cms/handleFieldsJS.js +148 -0
- package/lib/cms/modules.d.ts +14 -0
- package/lib/cms/modules.js +186 -0
- package/lib/cms/processFieldsJs.d.ts +1 -0
- package/lib/cms/processFieldsJs.js +97 -0
- package/lib/cms/templates.d.ts +65 -0
- package/lib/cms/templates.js +107 -0
- package/lib/cms/themes.d.ts +2 -0
- package/lib/cms/themes.js +34 -0
- package/lib/cms/uploadFolder.d.ts +7 -0
- package/lib/cms/uploadFolder.js +202 -0
- package/lib/cms/validate.d.ts +2 -0
- package/lib/cms/validate.js +40 -0
- package/lib/cms/watch.d.ts +4 -0
- package/lib/cms/watch.js +201 -0
- package/lib/customObjects.d.ts +5 -0
- package/lib/customObjects.js +42 -0
- package/lib/environment.d.ts +2 -0
- package/lib/environment.js +16 -0
- package/lib/escapeRegExp.d.ts +1 -0
- package/lib/escapeRegExp.js +7 -0
- package/lib/fileManager.d.ts +2 -0
- package/lib/fileManager.js +184 -0
- package/lib/fileMapper.d.ts +18 -0
- package/lib/fileMapper.js +317 -0
- package/lib/fs.d.ts +4 -0
- package/lib/fs.js +71 -0
- package/lib/github.d.ts +8 -0
- package/lib/github.js +167 -0
- package/lib/gitignore.d.ts +3 -0
- package/lib/gitignore.js +49 -0
- package/lib/hubdb.d.ts +17 -0
- package/lib/hubdb.js +133 -0
- package/lib/ignoreRules.d.ts +3 -0
- package/lib/ignoreRules.js +69 -0
- package/lib/logger.d.ts +56 -0
- package/lib/logger.js +146 -0
- package/lib/notify.d.ts +1 -0
- package/lib/notify.js +43 -0
- package/lib/oauth.d.ts +4 -0
- package/lib/oauth.js +34 -0
- package/lib/path.d.ts +14 -0
- package/lib/path.js +134 -0
- package/lib/personalAccessKey.d.ts +10 -0
- package/lib/personalAccessKey.js +163 -0
- package/lib/portManager.d.ts +10 -0
- package/lib/portManager.js +46 -0
- package/lib/text.d.ts +2 -0
- package/lib/text.js +24 -0
- package/lib/trackUsage.d.ts +1 -0
- package/lib/trackUsage.js +63 -0
- package/lib/urls.d.ts +2 -0
- package/lib/urls.js +24 -0
- package/models/FileSystemError.d.ts +6 -0
- package/models/FileSystemError.js +47 -0
- package/models/HubSpotHttpError.d.ts +24 -0
- package/models/HubSpotHttpError.js +197 -0
- package/models/OAuth2Manager.d.ts +12 -0
- package/models/OAuth2Manager.js +105 -0
- package/package.json +81 -0
- package/types/Accounts.d.ts +178 -0
- package/types/Accounts.js +2 -0
- package/types/Activity.d.ts +20 -0
- package/types/Activity.js +2 -0
- package/types/Api.d.ts +2 -0
- package/types/Api.js +2 -0
- package/types/Apps.d.ts +77 -0
- package/types/Apps.js +2 -0
- package/types/Archive.d.ts +9 -0
- package/types/Archive.js +2 -0
- package/types/Build.d.ts +41 -0
- package/types/Build.js +2 -0
- package/types/CLIOptions.d.ts +8 -0
- package/types/CLIOptions.js +2 -0
- package/types/ComponentStructure.d.ts +40 -0
- package/types/ComponentStructure.js +2 -0
- package/types/Config.d.ts +37 -0
- package/types/Config.js +2 -0
- package/types/Deploy.d.ts +42 -0
- package/types/Deploy.js +2 -0
- package/types/DesignManager.d.ts +10 -0
- package/types/DesignManager.js +2 -0
- package/types/Error.d.ts +37 -0
- package/types/Error.js +2 -0
- package/types/FieldsJS.d.ts +1 -0
- package/types/FieldsJS.js +2 -0
- package/types/FileManager.d.ts +71 -0
- package/types/FileManager.js +2 -0
- package/types/Files.d.ts +79 -0
- package/types/Files.js +2 -0
- package/types/Functions.d.ts +66 -0
- package/types/Functions.js +2 -0
- package/types/Github.d.ts +76 -0
- package/types/Github.js +2 -0
- package/types/Http.d.ts +29 -0
- package/types/Http.js +2 -0
- package/types/Hubdb.d.ts +109 -0
- package/types/Hubdb.js +2 -0
- package/types/HublValidation.d.ts +59 -0
- package/types/HublValidation.js +2 -0
- package/types/Lang.d.ts +10 -0
- package/types/Lang.js +2 -0
- package/types/Lighthouse.d.ts +25 -0
- package/types/Lighthouse.js +2 -0
- package/types/MarketplaceValidation.d.ts +28 -0
- package/types/MarketplaceValidation.js +2 -0
- package/types/Migration.d.ts +28 -0
- package/types/Migration.js +10 -0
- package/types/Modules.d.ts +16 -0
- package/types/Modules.js +2 -0
- package/types/PortManager.d.ts +11 -0
- package/types/PortManager.js +2 -0
- package/types/Project.d.ts +42 -0
- package/types/Project.js +2 -0
- package/types/ProjectLog.d.ts +9 -0
- package/types/ProjectLog.js +2 -0
- package/types/Sandbox.d.ts +155 -0
- package/types/Sandbox.js +2 -0
- package/types/Schemas.d.ts +39 -0
- package/types/Schemas.js +2 -0
- package/types/Secrets.d.ts +3 -0
- package/types/Secrets.js +2 -0
- package/types/Utils.d.ts +6 -0
- package/types/Utils.js +2 -0
- package/types/developerTestAccounts.d.ts +12 -0
- package/types/developerTestAccounts.js +2 -0
- package/utils/PortManagerServer.d.ts +26 -0
- package/utils/PortManagerServer.js +158 -0
- package/utils/accounts.d.ts +4 -0
- package/utils/accounts.js +28 -0
- package/utils/cms/fieldsJS.d.ts +2 -0
- package/utils/cms/fieldsJS.js +18 -0
- package/utils/cms/modules.d.ts +4 -0
- package/utils/cms/modules.js +54 -0
- package/utils/detectPort.d.ts +1 -0
- package/utils/detectPort.js +102 -0
- package/utils/git.d.ts +3 -0
- package/utils/git.js +71 -0
- package/utils/lang.d.ts +6 -0
- package/utils/lang.js +88 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createFunction = exports.createConfig = exports.createEndpoint = exports.isObjectOrFunction = void 0;
|
|
7
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const findup_sync_1 = __importDefault(require("findup-sync"));
|
|
10
|
+
const path_2 = require("../path");
|
|
11
|
+
const github_1 = require("../github");
|
|
12
|
+
const logger_1 = require("../logger");
|
|
13
|
+
const lang_1 = require("../../utils/lang");
|
|
14
|
+
const FileSystemError_1 = require("../../models/FileSystemError");
|
|
15
|
+
const i18nKey = 'lib.cms.functions';
|
|
16
|
+
function isObjectOrFunction(value) {
|
|
17
|
+
const type = typeof value;
|
|
18
|
+
return value != null && (type === 'object' || type === 'function');
|
|
19
|
+
}
|
|
20
|
+
exports.isObjectOrFunction = isObjectOrFunction;
|
|
21
|
+
function createEndpoint(endpointMethod, filename) {
|
|
22
|
+
return {
|
|
23
|
+
method: endpointMethod || 'GET',
|
|
24
|
+
file: filename,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
exports.createEndpoint = createEndpoint;
|
|
28
|
+
function createConfig({ endpointPath, endpointMethod, functionFile, }) {
|
|
29
|
+
return {
|
|
30
|
+
runtime: 'nodejs18.x',
|
|
31
|
+
version: '1.0',
|
|
32
|
+
environment: {},
|
|
33
|
+
secrets: [],
|
|
34
|
+
endpoints: {
|
|
35
|
+
[endpointPath]: createEndpoint(endpointMethod, functionFile),
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
exports.createConfig = createConfig;
|
|
40
|
+
function writeConfig(configFilePath, config) {
|
|
41
|
+
const configJson = JSON.stringify(config, null, ' ');
|
|
42
|
+
fs_extra_1.default.writeFileSync(configFilePath, configJson);
|
|
43
|
+
}
|
|
44
|
+
function updateExistingConfig(configFilePath, { endpointPath, endpointMethod, functionFile }) {
|
|
45
|
+
let configString;
|
|
46
|
+
try {
|
|
47
|
+
configString = fs_extra_1.default.readFileSync(configFilePath).toString();
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.updateExistingConfig.unableToReadFile`, {
|
|
51
|
+
configFilePath,
|
|
52
|
+
}));
|
|
53
|
+
throw new FileSystemError_1.FileSystemError({ cause: err }, {
|
|
54
|
+
filepath: configFilePath,
|
|
55
|
+
operation: 'read',
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
let config;
|
|
59
|
+
try {
|
|
60
|
+
config = JSON.parse(configString);
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.updateExistingConfig.invalidJSON`, {
|
|
64
|
+
configFilePath,
|
|
65
|
+
}));
|
|
66
|
+
throw new FileSystemError_1.FileSystemError({ cause: err }, {
|
|
67
|
+
filepath: configFilePath,
|
|
68
|
+
operation: 'read',
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
if (!isObjectOrFunction(config)) {
|
|
72
|
+
throw new Error((0, lang_1.i18n)(`${i18nKey}.updateExistingConfig.errors.configIsNotObjectError`, {
|
|
73
|
+
configFilePath,
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
if (config.endpoints) {
|
|
77
|
+
if (config.endpoints[endpointPath]) {
|
|
78
|
+
throw new Error((0, lang_1.i18n)(`${i18nKey}.updateExistingConfig.errors.endpointAreadyExistsError`, {
|
|
79
|
+
configFilePath,
|
|
80
|
+
endpointPath,
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
config.endpoints[endpointPath] = createEndpoint(endpointMethod, functionFile);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
config.endpoints = {
|
|
89
|
+
[endpointPath]: createEndpoint(endpointMethod, functionFile),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
writeConfig(configFilePath, config);
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.updateExistingConfig.couldNotUpdateFile`, {
|
|
97
|
+
configFilePath,
|
|
98
|
+
}));
|
|
99
|
+
throw new FileSystemError_1.FileSystemError({ cause: err }, {
|
|
100
|
+
filepath: configFilePath,
|
|
101
|
+
operation: 'read',
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
async function createFunction(functionInfo, dest, options = {}) {
|
|
106
|
+
const { functionsFolder, filename, endpointPath, endpointMethod } = functionInfo;
|
|
107
|
+
const allowExistingFile = options.allowExistingFile || false;
|
|
108
|
+
const ancestorFunctionsConfig = (0, findup_sync_1.default)('serverless.json', {
|
|
109
|
+
cwd: (0, path_2.getCwd)(),
|
|
110
|
+
nocase: true,
|
|
111
|
+
});
|
|
112
|
+
if (ancestorFunctionsConfig) {
|
|
113
|
+
throw new Error((0, lang_1.i18n)(`${i18nKey}.createFunction.errors.nestedConfigError`, {
|
|
114
|
+
ancestorConfigPath: path_1.default.dirname(ancestorFunctionsConfig),
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
const folderName = functionsFolder.endsWith('.functions')
|
|
118
|
+
? functionsFolder
|
|
119
|
+
: `${functionsFolder}.functions`;
|
|
120
|
+
const functionFile = filename.endsWith('.js') ? filename : `${filename}.js`;
|
|
121
|
+
const destPath = path_1.default.join(dest, folderName);
|
|
122
|
+
if (fs_extra_1.default.existsSync(destPath)) {
|
|
123
|
+
logger_1.logger.log((0, lang_1.i18n)(`${i18nKey}.createFunction.destPathAlreadyExists`, {
|
|
124
|
+
path: destPath,
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
fs_extra_1.default.mkdirp(destPath);
|
|
129
|
+
logger_1.logger.log((0, lang_1.i18n)(`${i18nKey}.createFunction.createdDest`, {
|
|
130
|
+
path: destPath,
|
|
131
|
+
}));
|
|
132
|
+
}
|
|
133
|
+
const functionFilePath = path_1.default.join(destPath, functionFile);
|
|
134
|
+
const configFilePath = path_1.default.join(destPath, 'serverless.json');
|
|
135
|
+
if (!allowExistingFile && fs_extra_1.default.existsSync(functionFilePath)) {
|
|
136
|
+
throw new Error((0, lang_1.i18n)(`${i18nKey}.createFunction.errors.jsFileConflictError`, {
|
|
137
|
+
functionFilePath,
|
|
138
|
+
}));
|
|
139
|
+
}
|
|
140
|
+
await (0, github_1.downloadGithubRepoContents)('HubSpot/cms-sample-assets', 'functions/sample-function.js', functionFilePath);
|
|
141
|
+
logger_1.logger.log((0, lang_1.i18n)(`${i18nKey}.createFunction.createdFunctionFile`, {
|
|
142
|
+
path: functionFilePath,
|
|
143
|
+
}));
|
|
144
|
+
if (fs_extra_1.default.existsSync(configFilePath)) {
|
|
145
|
+
updateExistingConfig(configFilePath, {
|
|
146
|
+
endpointPath,
|
|
147
|
+
endpointMethod,
|
|
148
|
+
functionFile,
|
|
149
|
+
});
|
|
150
|
+
logger_1.logger.log((0, lang_1.i18n)(`${i18nKey}.createFunction.createdFunctionFile`, {
|
|
151
|
+
path: functionFilePath,
|
|
152
|
+
}));
|
|
153
|
+
logger_1.logger.log((0, lang_1.i18n)(`${i18nKey}.createFunction.success`, {
|
|
154
|
+
endpointPath: endpointPath,
|
|
155
|
+
folderName,
|
|
156
|
+
}));
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
const config = createConfig({ endpointPath, endpointMethod, functionFile });
|
|
160
|
+
try {
|
|
161
|
+
writeConfig(configFilePath, config);
|
|
162
|
+
}
|
|
163
|
+
catch (err) {
|
|
164
|
+
logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.createFunction.failedToCreateFile`, {
|
|
165
|
+
configFilePath,
|
|
166
|
+
}));
|
|
167
|
+
throw new FileSystemError_1.FileSystemError({ cause: err }, {
|
|
168
|
+
filepath: configFilePath,
|
|
169
|
+
operation: 'write',
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
logger_1.logger.log((0, lang_1.i18n)(`${i18nKey}.createFunction.createdConfigFile`, {
|
|
173
|
+
path: configFilePath,
|
|
174
|
+
}));
|
|
175
|
+
logger_1.logger.log((0, lang_1.i18n)(`${i18nKey}.createFunction.success`, {
|
|
176
|
+
endpointPath: endpointPath,
|
|
177
|
+
folderName,
|
|
178
|
+
}));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
exports.createFunction = createFunction;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare class FieldsJs {
|
|
2
|
+
projectDir: string;
|
|
3
|
+
filePath: string;
|
|
4
|
+
rootWriteDir: string;
|
|
5
|
+
rejected: boolean;
|
|
6
|
+
fieldOptions: string;
|
|
7
|
+
outputPath?: string;
|
|
8
|
+
toJSON?: () => JSON;
|
|
9
|
+
constructor(projectDir: string, filePath: string, rootWriteDir?: string | null, fieldOptions?: string);
|
|
10
|
+
init(): Promise<this>;
|
|
11
|
+
convertFieldsJs(writeDir: string): Promise<string | void>;
|
|
12
|
+
/**
|
|
13
|
+
* If there has been a fields.json written to the output path, then copy it from the output
|
|
14
|
+
* directory to the project directory, respecting the path within the output directory.
|
|
15
|
+
* Ex: path/to/tmp/example.module/fields.json => path/to/project/example.module/fields.output.json
|
|
16
|
+
*/
|
|
17
|
+
saveOutput(): void;
|
|
18
|
+
/**
|
|
19
|
+
* Resolves the relative path to the fields.js within the project directory and returns
|
|
20
|
+
* directory name to write to in rootWriteDir directory.
|
|
21
|
+
*
|
|
22
|
+
* Ex: If rootWriteDir = 'path/to/temp', filePath = 'projectRoot/sample.module/fields.js'. Then getWriteDir() => path/to/temp/sample.module
|
|
23
|
+
*/
|
|
24
|
+
getWriteDir(): string;
|
|
25
|
+
getOutputPathPromise(): Promise<string | void>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Determines if file is a convertable fields.js file i.e., if it is called
|
|
29
|
+
* 'fields.js' and in a root or in a module folder, and if convertFields flag is true.
|
|
30
|
+
*/
|
|
31
|
+
export declare function isConvertableFieldJs(rootDir: string, filePath: string, convertFields?: boolean): boolean;
|
|
32
|
+
export declare function createTmpDirSync(prefix: string): string;
|
|
33
|
+
export declare function cleanupTmpDirSync(tmpDir: string): void;
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.cleanupTmpDirSync = exports.createTmpDirSync = exports.isConvertableFieldJs = exports.FieldsJs = void 0;
|
|
7
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
8
|
+
const os_1 = __importDefault(require("os"));
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const child_process_1 = require("child_process");
|
|
11
|
+
const escapeRegExp_1 = require("../escapeRegExp");
|
|
12
|
+
const modules_1 = require("../../utils/cms/modules");
|
|
13
|
+
const logger_1 = require("../logger");
|
|
14
|
+
const lang_1 = require("../../utils/lang");
|
|
15
|
+
const i18nKey = 'lib.cms.handleFieldsJs';
|
|
16
|
+
class FieldsJs {
|
|
17
|
+
projectDir;
|
|
18
|
+
filePath;
|
|
19
|
+
rootWriteDir;
|
|
20
|
+
rejected;
|
|
21
|
+
fieldOptions;
|
|
22
|
+
outputPath;
|
|
23
|
+
toJSON;
|
|
24
|
+
constructor(projectDir, filePath, rootWriteDir, fieldOptions = '') {
|
|
25
|
+
this.projectDir = projectDir;
|
|
26
|
+
this.filePath = filePath;
|
|
27
|
+
this.fieldOptions = fieldOptions;
|
|
28
|
+
this.rejected = false;
|
|
29
|
+
// Create tmpDir if no writeDir is given.
|
|
30
|
+
this.rootWriteDir =
|
|
31
|
+
rootWriteDir === undefined || rootWriteDir === null
|
|
32
|
+
? createTmpDirSync('hubspot-temp-fieldsjs-output-')
|
|
33
|
+
: rootWriteDir;
|
|
34
|
+
}
|
|
35
|
+
async init() {
|
|
36
|
+
const outputPath = await this.getOutputPathPromise();
|
|
37
|
+
this.outputPath = this.rejected ? undefined : outputPath;
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
// Converts a fields.js file into a fields.json file, writes, and returns of fields.json
|
|
41
|
+
convertFieldsJs(writeDir) {
|
|
42
|
+
const filePath = this.filePath;
|
|
43
|
+
const dirName = path_1.default.dirname(filePath);
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
const convertFieldsProcess = (0, child_process_1.fork)(path_1.default.join(__dirname, './processFieldsJs.js'), [], {
|
|
46
|
+
cwd: dirName,
|
|
47
|
+
env: {
|
|
48
|
+
dirName,
|
|
49
|
+
fieldOptions: this.fieldOptions,
|
|
50
|
+
filePath,
|
|
51
|
+
writeDir,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.convertFieldsJs.creating`, {
|
|
55
|
+
pid: convertFieldsProcess.pid || '',
|
|
56
|
+
}));
|
|
57
|
+
convertFieldsProcess.on('message', function (message) {
|
|
58
|
+
if (message.action === 'ERROR') {
|
|
59
|
+
reject(message.message);
|
|
60
|
+
}
|
|
61
|
+
else if (message.action === 'COMPLETE') {
|
|
62
|
+
resolve(message.finalPath);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
convertFieldsProcess.on('close', () => {
|
|
66
|
+
logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.convertFieldsJs.terminating`, {
|
|
67
|
+
pid: convertFieldsProcess.pid || '',
|
|
68
|
+
}));
|
|
69
|
+
});
|
|
70
|
+
}).catch((e) => {
|
|
71
|
+
throw new Error((0, lang_1.i18n)(`${i18nKey}.convertFieldsJs.errors.errorConverting`, { filePath }), { cause: e });
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* If there has been a fields.json written to the output path, then copy it from the output
|
|
76
|
+
* directory to the project directory, respecting the path within the output directory.
|
|
77
|
+
* Ex: path/to/tmp/example.module/fields.json => path/to/project/example.module/fields.output.json
|
|
78
|
+
*/
|
|
79
|
+
saveOutput() {
|
|
80
|
+
if (!this.outputPath || !fs_extra_1.default.existsSync(this.outputPath)) {
|
|
81
|
+
throw new Error((0, lang_1.i18n)(`${i18nKey}.saveOutput.errors.saveFailed`, {
|
|
82
|
+
path: this.filePath,
|
|
83
|
+
}));
|
|
84
|
+
}
|
|
85
|
+
const relativePath = path_1.default.relative(this.rootWriteDir, path_1.default.dirname(this.outputPath));
|
|
86
|
+
const savePath = path_1.default.join(this.projectDir, relativePath, 'fields.output.json');
|
|
87
|
+
try {
|
|
88
|
+
fs_extra_1.default.copyFileSync(this.outputPath, savePath);
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
throw new Error((0, lang_1.i18n)(`${i18nKey}.saveOutput.errors.saveFailed`, { path: savePath }), { cause: err });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Resolves the relative path to the fields.js within the project directory and returns
|
|
96
|
+
* directory name to write to in rootWriteDir directory.
|
|
97
|
+
*
|
|
98
|
+
* Ex: If rootWriteDir = 'path/to/temp', filePath = 'projectRoot/sample.module/fields.js'. Then getWriteDir() => path/to/temp/sample.module
|
|
99
|
+
*/
|
|
100
|
+
getWriteDir() {
|
|
101
|
+
const projectDirRegex = new RegExp(`^${(0, escapeRegExp_1.escapeRegExp)(this.projectDir)}`);
|
|
102
|
+
const relativePath = this.filePath.replace(projectDirRegex, '');
|
|
103
|
+
return path_1.default.dirname(path_1.default.join(this.rootWriteDir, relativePath));
|
|
104
|
+
}
|
|
105
|
+
getOutputPathPromise() {
|
|
106
|
+
const writeDir = this.getWriteDir();
|
|
107
|
+
return this.convertFieldsJs(writeDir).then(outputPath => outputPath);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
exports.FieldsJs = FieldsJs;
|
|
111
|
+
/**
|
|
112
|
+
* Determines if file is a convertable fields.js file i.e., if it is called
|
|
113
|
+
* 'fields.js' and in a root or in a module folder, and if convertFields flag is true.
|
|
114
|
+
*/
|
|
115
|
+
function isConvertableFieldJs(rootDir, filePath, convertFields = false) {
|
|
116
|
+
const allowedFieldsNames = ['fields.js', 'fields.mjs', 'fields.cjs'];
|
|
117
|
+
const regex = new RegExp(`^${(0, escapeRegExp_1.escapeRegExp)(rootDir)}`);
|
|
118
|
+
const relativePath = path_1.default.dirname(filePath.replace(regex, ''));
|
|
119
|
+
const baseName = path_1.default.basename(filePath);
|
|
120
|
+
const inModuleFolder = (0, modules_1.isModuleFolderChild)({ path: filePath, isLocal: true });
|
|
121
|
+
return !!(convertFields &&
|
|
122
|
+
allowedFieldsNames.includes(baseName) &&
|
|
123
|
+
(inModuleFolder || relativePath == path_1.default.sep));
|
|
124
|
+
}
|
|
125
|
+
exports.isConvertableFieldJs = isConvertableFieldJs;
|
|
126
|
+
// Try creating tempdir
|
|
127
|
+
function createTmpDirSync(prefix) {
|
|
128
|
+
let tmpDir;
|
|
129
|
+
try {
|
|
130
|
+
tmpDir = fs_extra_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), prefix));
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
throw new Error((0, lang_1.i18n)(`${i18nKey}.createTmpDirSync.errors.writeFailed`), {
|
|
134
|
+
cause: err,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return tmpDir;
|
|
138
|
+
}
|
|
139
|
+
exports.createTmpDirSync = createTmpDirSync;
|
|
140
|
+
// Try cleaning up resources from os's tempdir
|
|
141
|
+
function cleanupTmpDirSync(tmpDir) {
|
|
142
|
+
fs_extra_1.default.rm(tmpDir, { recursive: true }, err => {
|
|
143
|
+
if (err) {
|
|
144
|
+
throw new Error((0, lang_1.i18n)(`${i18nKey}.cleanupTmpDirSync.errors.deleteFailed`), { cause: err });
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
exports.cleanupTmpDirSync = cleanupTmpDirSync;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { PathInput, ValidationResult, ModuleDefinition } from '../../types/Modules';
|
|
2
|
+
export declare const ValidationIds: {
|
|
3
|
+
SRC_REQUIRED: string;
|
|
4
|
+
DEST_REQUIRED: string;
|
|
5
|
+
MODULE_FOLDER_REQUIRED: string;
|
|
6
|
+
MODULE_TO_MODULE_NESTING: string;
|
|
7
|
+
MODULE_NESTING: string;
|
|
8
|
+
};
|
|
9
|
+
export declare function validateSrcAndDestPaths(src?: PathInput, dest?: PathInput): Promise<Array<ValidationResult>>;
|
|
10
|
+
export declare function createModule(moduleDefinition: ModuleDefinition, name: string, dest: string, getInternalVersion: boolean, options?: {
|
|
11
|
+
allowExistingDir: boolean;
|
|
12
|
+
}): Promise<void>;
|
|
13
|
+
export declare function retrieveDefaultModule(name: string | undefined, dest: string): Promise<import("../../types/Github").GithubRepoFile[] | undefined>;
|
|
14
|
+
export declare const isModuleHTMLFile: (filePath: string) => boolean;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isModuleHTMLFile = exports.retrieveDefaultModule = exports.createModule = exports.validateSrcAndDestPaths = exports.ValidationIds = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
9
|
+
const path_2 = require("../path");
|
|
10
|
+
const fs_1 = require("../fs");
|
|
11
|
+
const github_1 = require("../github");
|
|
12
|
+
const logger_1 = require("../logger");
|
|
13
|
+
const modules_1 = require("../../utils/cms/modules");
|
|
14
|
+
const lang_1 = require("../../utils/lang");
|
|
15
|
+
const i18nKey = 'lib.cms.modules';
|
|
16
|
+
// Ids for testing
|
|
17
|
+
exports.ValidationIds = {
|
|
18
|
+
SRC_REQUIRED: 'SRC_REQUIRED',
|
|
19
|
+
DEST_REQUIRED: 'DEST_REQUIRED',
|
|
20
|
+
MODULE_FOLDER_REQUIRED: 'MODULE_FOLDER_REQUIRED',
|
|
21
|
+
MODULE_TO_MODULE_NESTING: 'MODULE_TO_MODULE_NESTING',
|
|
22
|
+
MODULE_NESTING: 'MODULE_NESTING',
|
|
23
|
+
};
|
|
24
|
+
const getValidationResult = (id, message) => ({ id, message });
|
|
25
|
+
async function validateSrcAndDestPaths(src, dest) {
|
|
26
|
+
const results = [];
|
|
27
|
+
if (!(0, modules_1.isPathInput)(src)) {
|
|
28
|
+
results.push(getValidationResult(exports.ValidationIds.SRC_REQUIRED, '`src` is required.'));
|
|
29
|
+
}
|
|
30
|
+
if (!(0, modules_1.isPathInput)(dest)) {
|
|
31
|
+
results.push(getValidationResult(exports.ValidationIds.DEST_REQUIRED, '`dest` is required.'));
|
|
32
|
+
}
|
|
33
|
+
if (results.length || !src || !dest) {
|
|
34
|
+
return results;
|
|
35
|
+
}
|
|
36
|
+
const [_src, _dest] = [src, dest].map(inputPath => {
|
|
37
|
+
const result = { ...inputPath };
|
|
38
|
+
if (result.isLocal) {
|
|
39
|
+
result.path = path_1.default.resolve((0, path_2.getCwd)(), result.path);
|
|
40
|
+
}
|
|
41
|
+
else if (result.isHubSpot) {
|
|
42
|
+
result.path = path_1.default.posix.normalize(result.path);
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
});
|
|
46
|
+
// src is a .module folder and dest is within a module. (Nesting)
|
|
47
|
+
// e.g. `upload foo.module bar.module/zzz`
|
|
48
|
+
if ((0, modules_1.isModuleFolder)(_src) && (0, modules_1.isModuleFolderChild)(_dest)) {
|
|
49
|
+
return results.concat(getValidationResult(exports.ValidationIds.MODULE_TO_MODULE_NESTING, '`src` is a module path and `dest` is within a module.'));
|
|
50
|
+
}
|
|
51
|
+
// src is a .module folder but dest is not
|
|
52
|
+
// e.g. `upload foo.module bar`
|
|
53
|
+
if ((0, modules_1.isModuleFolder)(_src) && !(0, modules_1.isModuleFolder)(_dest)) {
|
|
54
|
+
return results.concat(getValidationResult(exports.ValidationIds.MODULE_FOLDER_REQUIRED, '`src` is a module path but `dest` is not.'));
|
|
55
|
+
}
|
|
56
|
+
// src is a folder that includes modules and dest is within a module. (Nesting)
|
|
57
|
+
if (_src.isLocal && (0, modules_1.isModuleFolderChild)(_dest)) {
|
|
58
|
+
const stat = await fs_extra_1.default.stat(_src.path);
|
|
59
|
+
if (stat.isDirectory()) {
|
|
60
|
+
const files = await (0, fs_1.walk)(_src.path);
|
|
61
|
+
const srcHasModulesChildren = files.some(file => (0, modules_1.isModuleFolderChild)({ ..._src, path: file }));
|
|
62
|
+
if (srcHasModulesChildren) {
|
|
63
|
+
return results.concat(getValidationResult(exports.ValidationIds.MODULE_NESTING, '`src` contains modules and `dest` is within a module.'));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return results;
|
|
68
|
+
}
|
|
69
|
+
exports.validateSrcAndDestPaths = validateSrcAndDestPaths;
|
|
70
|
+
/* createModule() helper
|
|
71
|
+
* Takes a file and uses the constants above to transform the contents
|
|
72
|
+
*/
|
|
73
|
+
// Strings to replace in React module files
|
|
74
|
+
const MODULE_STRING_TRANSFORMATIONS = [
|
|
75
|
+
{
|
|
76
|
+
regex: /\/\* import global styles \*\//g,
|
|
77
|
+
string: 'import "./global-sample-react-module.css";',
|
|
78
|
+
fallback: '',
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
regex: /\/\* Default config \*\//g,
|
|
82
|
+
string: 'export const defaultModuleConfig = { \n moduleName: "sample_react-module", \n version: 0, \n};',
|
|
83
|
+
fallback: '',
|
|
84
|
+
},
|
|
85
|
+
];
|
|
86
|
+
const updateFileContents = async (file, metaData, getInternalVersion) => {
|
|
87
|
+
try {
|
|
88
|
+
let fileContents = await fs_extra_1.default.readFile(file, 'utf8'); // returns Promise
|
|
89
|
+
MODULE_STRING_TRANSFORMATIONS.forEach(entry => {
|
|
90
|
+
const replacementString = getInternalVersion
|
|
91
|
+
? entry.string
|
|
92
|
+
: entry.fallback;
|
|
93
|
+
fileContents = fileContents.replace(entry.regex, replacementString);
|
|
94
|
+
});
|
|
95
|
+
await fs_extra_1.default.writeFile(file, fileContents, 'utf8');
|
|
96
|
+
await fs_extra_1.default.appendFile(file, 'export const meta = ' + JSON.stringify(metaData, null, ' '));
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
const { message } = error;
|
|
100
|
+
throw new Error((0, lang_1.i18n)(`${i18nKey}.createModule.errors.fileUpdateFailure`, {
|
|
101
|
+
path: file,
|
|
102
|
+
errorMessage: message,
|
|
103
|
+
}));
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
async function createModule(moduleDefinition, name, dest, getInternalVersion, options = {
|
|
107
|
+
allowExistingDir: false,
|
|
108
|
+
}) {
|
|
109
|
+
const { moduleLabel, contentTypes, global, reactType: isReactModule, availableForNewContent, } = moduleDefinition;
|
|
110
|
+
const moduleMetaData = {
|
|
111
|
+
label: moduleLabel,
|
|
112
|
+
css_assets: [],
|
|
113
|
+
external_js: [],
|
|
114
|
+
global: global,
|
|
115
|
+
help_text: '',
|
|
116
|
+
content_types: contentTypes,
|
|
117
|
+
js_assets: [],
|
|
118
|
+
other_assets: [],
|
|
119
|
+
smart_type: 'NOT_SMART',
|
|
120
|
+
tags: [],
|
|
121
|
+
is_available_for_new_content: availableForNewContent,
|
|
122
|
+
};
|
|
123
|
+
const folderName = name.endsWith('.module') ? name : `${name}.module`;
|
|
124
|
+
const destPath = !isReactModule
|
|
125
|
+
? path_1.default.join(dest, folderName)
|
|
126
|
+
: path_1.default.join(dest, `${name}`);
|
|
127
|
+
if (!options.allowExistingDir && fs_extra_1.default.existsSync(destPath)) {
|
|
128
|
+
throw new Error((0, lang_1.i18n)(`${i18nKey}.createModule.errors.pathExists`, {
|
|
129
|
+
path: destPath,
|
|
130
|
+
}));
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
logger_1.logger.log((0, lang_1.i18n)(`${i18nKey}.createModule.creatingPath`, {
|
|
134
|
+
path: destPath,
|
|
135
|
+
}));
|
|
136
|
+
fs_extra_1.default.ensureDirSync(destPath);
|
|
137
|
+
}
|
|
138
|
+
logger_1.logger.log((0, lang_1.i18n)(`${i18nKey}.createModule.creatingModule`, {
|
|
139
|
+
path: destPath,
|
|
140
|
+
}));
|
|
141
|
+
// Filter out certain fetched files from the response
|
|
142
|
+
const moduleFileFilter = (src, dest) => {
|
|
143
|
+
const emailEnabled = moduleDefinition.contentTypes.includes('EMAIL');
|
|
144
|
+
switch (path_1.default.basename(src)) {
|
|
145
|
+
case 'meta.json':
|
|
146
|
+
fs_extra_1.default.writeJSONSync(dest, moduleMetaData, { spaces: 2 }); // writing a meta.json file to standard HubL modules
|
|
147
|
+
return false;
|
|
148
|
+
case 'module.js':
|
|
149
|
+
case 'module.css':
|
|
150
|
+
if (emailEnabled) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
return true;
|
|
154
|
+
case 'global-sample-react-module.css':
|
|
155
|
+
case 'stories':
|
|
156
|
+
case 'tests':
|
|
157
|
+
if (getInternalVersion) {
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
return false;
|
|
161
|
+
default:
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
// Download gitHub contents to the dest directory
|
|
166
|
+
const sampleAssetPath = !isReactModule
|
|
167
|
+
? 'Sample.module'
|
|
168
|
+
: 'SampleReactModule';
|
|
169
|
+
await (0, github_1.downloadGithubRepoContents)('HubSpot/cms-sample-assets', `modules/${sampleAssetPath}`, destPath, '', moduleFileFilter);
|
|
170
|
+
// Updating React module files after fetch
|
|
171
|
+
if (isReactModule) {
|
|
172
|
+
await updateFileContents(`${destPath}/index.tsx`, moduleMetaData, getInternalVersion);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
exports.createModule = createModule;
|
|
176
|
+
async function retrieveDefaultModule(name, dest) {
|
|
177
|
+
if (!name) {
|
|
178
|
+
const defaultReactModules = await (0, github_1.listGithubRepoContents)('HubSpot/cms-react', 'default-react-modules/src/components/modules/', 'dir');
|
|
179
|
+
return defaultReactModules;
|
|
180
|
+
}
|
|
181
|
+
await (0, github_1.downloadGithubRepoContents)('HubSpot/cms-react', `default-react-modules/src/components/modules/${name}`, dest);
|
|
182
|
+
}
|
|
183
|
+
exports.retrieveDefaultModule = retrieveDefaultModule;
|
|
184
|
+
const MODULE_HTML_EXTENSION_REGEX = new RegExp(/\.module(?:\/|\\)module\.html$/);
|
|
185
|
+
const isModuleHTMLFile = (filePath) => MODULE_HTML_EXTENSION_REGEX.test(filePath);
|
|
186
|
+
exports.isModuleHTMLFile = isModuleHTMLFile;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const semver_1 = __importDefault(require("semver"));
|
|
10
|
+
const url_1 = require("url");
|
|
11
|
+
const path_2 = require("../path");
|
|
12
|
+
const lang_1 = require("../../utils/lang");
|
|
13
|
+
const i18nKey = 'lib.cms.processFieldsJs';
|
|
14
|
+
const { dirName, fieldOptions, filePath, writeDir } = process.env;
|
|
15
|
+
const baseName = path_1.default.basename(filePath);
|
|
16
|
+
//TODO - Figure out agnostic logging
|
|
17
|
+
console.info((0, lang_1.i18n)(`${i18nKey}.converting`, {
|
|
18
|
+
src: dirName + `/${baseName}`,
|
|
19
|
+
dest: dirName + '/fields.json',
|
|
20
|
+
}));
|
|
21
|
+
/*
|
|
22
|
+
* How this works: dynamicImport() will always return either a Promise or undefined.
|
|
23
|
+
* In the case when it's a Promise, its expected that it will resolve to a function.
|
|
24
|
+
* This function has optional return type of Promise<Array> | Array. In order to have uniform handling,
|
|
25
|
+
* we wrap the return value of the function in a Promise.resolve(), and then process.
|
|
26
|
+
*/
|
|
27
|
+
const fieldsPromise = dynamicImport(filePath).catch(e => {
|
|
28
|
+
throw e;
|
|
29
|
+
});
|
|
30
|
+
fieldsPromise.then(fieldsFunc => {
|
|
31
|
+
const fieldsFuncType = typeof fieldsFunc;
|
|
32
|
+
if (fieldsFuncType !== 'function') {
|
|
33
|
+
throw new Error((0, lang_1.i18n)(`${i18nKey}.errors.notFunction`, {
|
|
34
|
+
path: filePath,
|
|
35
|
+
returned: fieldsFuncType,
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
return Promise.resolve(fieldsFunc(fieldOptions)).then(fields => {
|
|
39
|
+
if (!Array.isArray(fields)) {
|
|
40
|
+
throw new Error((0, lang_1.i18n)(`${i18nKey}.errors.notArray`, {
|
|
41
|
+
path: filePath,
|
|
42
|
+
returned: typeof fields,
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
const finalPath = path_1.default.join(writeDir, '/fields.json');
|
|
46
|
+
return fieldsArrayToJson(fields).then(json => {
|
|
47
|
+
if (!fs_1.default.existsSync(writeDir)) {
|
|
48
|
+
fs_1.default.mkdirSync(writeDir, { recursive: true });
|
|
49
|
+
}
|
|
50
|
+
fs_1.default.writeFileSync(finalPath, json);
|
|
51
|
+
//TODO - Figure out agnostic logging
|
|
52
|
+
console.log((0, lang_1.i18n)(`${i18nKey}.converted`, {
|
|
53
|
+
src: dirName + `/${baseName}`,
|
|
54
|
+
dest: dirName + '/fields.json',
|
|
55
|
+
}));
|
|
56
|
+
if (process) {
|
|
57
|
+
process.send({
|
|
58
|
+
action: 'COMPLETE',
|
|
59
|
+
finalPath,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
/*
|
|
66
|
+
* Polyfill for `Array.flat(Infinity)` since the `flat` is only available for Node v11+
|
|
67
|
+
* https://stackoverflow.com/a/15030117
|
|
68
|
+
*/
|
|
69
|
+
function flattenArray(arr) {
|
|
70
|
+
return arr.reduce((flat, toFlatten) => {
|
|
71
|
+
return flat.concat(Array.isArray(toFlatten) ? flattenArray(toFlatten) : toFlatten);
|
|
72
|
+
}, []);
|
|
73
|
+
}
|
|
74
|
+
async function fieldsArrayToJson(fields) {
|
|
75
|
+
const allFields = await Promise.all(flattenArray(fields));
|
|
76
|
+
const jsonFields = allFields.map(field => {
|
|
77
|
+
return typeof field['toJSON'] === 'function' ? field.toJSON() : field;
|
|
78
|
+
});
|
|
79
|
+
return JSON.stringify(jsonFields, null, 2);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Takes in a path to a javascript file and either dynamically imports it or requires it, and returns, depending on node version.
|
|
83
|
+
* @param {string} filePath - Path to javascript file
|
|
84
|
+
* @returns {Promise | undefined} - Returns _default_ exported content if ESM, or exported module content if CJS, or undefined if node version < 13.2 and file is .mjs.
|
|
85
|
+
*/
|
|
86
|
+
async function dynamicImport(filePath) {
|
|
87
|
+
if (semver_1.default.gte(process.version, '13.2.0')) {
|
|
88
|
+
const exported = await new Function(`return import("${(0, url_1.pathToFileURL)(filePath)}")`)();
|
|
89
|
+
return exported.default;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
if ((0, path_2.getExt)(filePath) == 'mjs') {
|
|
93
|
+
throw new Error((0, lang_1.i18n)(`${i18nKey}.errors.invalidMjsFile`));
|
|
94
|
+
}
|
|
95
|
+
return require(filePath);
|
|
96
|
+
}
|
|
97
|
+
}
|