@hubspot/local-dev-lib 0.0.2 → 0.0.3
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/config/CLIConfiguration.d.ts +55 -0
- package/config/CLIConfiguration.js +392 -0
- package/config/configFile.d.ts +21 -0
- package/config/configFile.js +110 -0
- package/config/configUtils.d.ts +24 -0
- package/config/configUtils.js +85 -0
- package/config/environment.d.ts +3 -0
- package/config/environment.js +64 -0
- package/constants/auth.d.ts +24 -0
- package/constants/auth.js +31 -0
- package/constants/config.d.ts +8 -0
- package/constants/config.js +12 -0
- package/constants/environments.d.ts +13 -0
- package/constants/environments.js +16 -0
- package/constants/extensions.d.ts +1 -0
- package/constants/extensions.js +22 -0
- package/constants/index.d.ts +16 -0
- package/constants/index.js +12 -0
- package/errors/HubSpotAuthError.d.ts +3 -0
- package/errors/HubSpotAuthError.js +6 -0
- package/errors/cmsFieldsJsErrors.d.ts +9 -0
- package/errors/cmsFieldsJsErrors.js +31 -0
- package/errors/fileSystemErrors.d.ts +2 -0
- package/errors/fileSystemErrors.js +29 -0
- package/errors/standardErrors.d.ts +9 -0
- package/errors/standardErrors.js +63 -0
- package/index.d.ts +2 -0
- package/index.js +5 -0
- package/lib/archive.d.ts +7 -0
- package/lib/archive.js +113 -0
- package/lib/path.d.ts +13 -0
- package/lib/path.js +101 -0
- package/package.json +1 -4
- package/src/config/CLIConfiguration.d.ts +55 -0
- package/src/config/CLIConfiguration.js +392 -0
- package/src/config/configFile.d.ts +21 -0
- package/src/config/configFile.js +109 -0
- package/src/config/configUtils.d.ts +24 -0
- package/src/config/configUtils.js +85 -0
- package/src/config/environment.d.ts +3 -0
- package/src/config/environment.js +64 -0
- package/src/constants/auth.d.ts +24 -0
- package/src/constants/auth.js +31 -0
- package/src/constants/config.d.ts +8 -0
- package/src/constants/config.js +12 -0
- package/src/constants/environments.d.ts +13 -0
- package/src/constants/environments.js +16 -0
- package/src/constants/extensions.d.ts +2 -0
- package/src/constants/extensions.js +23 -0
- package/src/constants/files.d.ts +5 -0
- package/src/constants/files.js +8 -0
- package/src/constants/github.d.ts +4 -0
- package/src/constants/github.js +7 -0
- package/src/constants/index.d.ts +16 -0
- package/src/constants/index.js +12 -0
- package/src/errors/HubSpotAuthError.d.ts +3 -0
- package/src/errors/HubSpotAuthError.js +6 -0
- package/src/errors/cmsFieldsJsErrors.d.ts +9 -0
- package/src/errors/cmsFieldsJsErrors.js +31 -0
- package/src/errors/fileSystemErrors.d.ts +8 -0
- package/src/errors/fileSystemErrors.js +28 -0
- package/src/errors/standardErrors.d.ts +19 -0
- package/src/errors/standardErrors.js +67 -0
- package/src/http/requestOptions.d.ts +20 -0
- package/src/http/requestOptions.js +27 -0
- package/src/index.d.ts +2 -0
- package/src/index.js +5 -0
- package/src/lib/archive.d.ts +7 -0
- package/src/lib/archive.js +111 -0
- package/src/lib/cms/handleFieldsJS.d.ts +32 -0
- package/src/lib/cms/handleFieldsJS.js +143 -0
- package/src/lib/cms/index.d.ts +10 -0
- package/src/lib/cms/index.js +13 -0
- package/src/lib/cms/modules.d.ts +24 -0
- package/src/lib/cms/modules.js +124 -0
- package/src/lib/cms/themes.d.ts +2 -0
- package/src/lib/cms/themes.js +34 -0
- package/src/lib/environment.d.ts +1 -0
- package/src/lib/environment.js +16 -0
- package/src/lib/fs.d.ts +4 -0
- package/src/lib/fs.js +71 -0
- package/src/lib/github.d.ts +17 -0
- package/src/lib/github.js +133 -0
- package/src/lib/gitignore.d.ts +1 -0
- package/src/lib/gitignore.js +76 -0
- package/src/lib/index.d.ts +11 -0
- package/src/lib/index.js +14 -0
- package/src/lib/modules.d.ts +28 -0
- package/src/lib/modules.js +174 -0
- package/src/lib/path.d.ts +10 -0
- package/src/lib/path.js +84 -0
- package/src/lib/text.d.ts +1 -0
- package/src/lib/text.js +16 -0
- package/src/lib/urls.d.ts +2 -0
- package/src/lib/urls.js +20 -0
- package/src/types/Accounts.d.ts +50 -0
- package/src/types/Accounts.js +2 -0
- package/src/types/CLIOptions.d.ts +3 -0
- package/src/types/CLIOptions.js +2 -0
- package/src/types/Config.d.ts +10 -0
- package/src/types/Config.js +2 -0
- package/src/types/Error.d.ts +31 -0
- package/src/types/Error.js +2 -0
- package/src/types/Files.d.ts +8 -0
- package/src/types/Files.js +2 -0
- package/src/types/Github.d.ts +58 -0
- package/src/types/Github.js +2 -0
- package/src/types/LogCallbacks.d.ts +6 -0
- package/src/types/LogCallbacks.js +2 -0
- package/src/types/Modules.d.ts +5 -0
- package/src/types/Modules.js +2 -0
- package/src/types/Utils.d.ts +1 -0
- package/src/types/Utils.js +2 -0
- package/src/utils/encoding.d.ts +1 -0
- package/src/utils/encoding.js +11 -0
- package/src/utils/environment.d.ts +1 -0
- package/src/utils/environment.js +16 -0
- package/src/utils/escapeRegExp.d.ts +1 -0
- package/src/utils/escapeRegExp.js +7 -0
- package/src/utils/fieldsJS.d.ts +3 -0
- package/src/utils/fieldsJS.js +18 -0
- package/src/utils/files.d.ts +3 -0
- package/src/utils/files.js +35 -0
- package/src/utils/fs/read.d.ts +4 -0
- package/src/utils/fs/read.js +55 -0
- package/src/utils/fs/walk.d.ts +2 -0
- package/src/utils/fs/walk.js +44 -0
- package/src/utils/git.d.ts +4 -0
- package/src/utils/git.js +40 -0
- package/src/utils/handleFieldsJS.d.ts +35 -0
- package/src/utils/handleFieldsJS.js +158 -0
- package/src/utils/interpolation.d.ts +5 -0
- package/src/utils/interpolation.js +31 -0
- package/src/utils/lang.d.ts +8 -0
- package/src/utils/lang.js +83 -0
- package/src/utils/logger.d.ts +10 -0
- package/src/utils/logger.js +22 -0
- package/src/utils/modules.d.ts +4 -0
- package/src/utils/modules.js +53 -0
- package/src/utils/text.d.ts +1 -0
- package/src/utils/text.js +16 -0
- package/src/utils/urls.d.ts +2 -0
- package/src/utils/urls.js +20 -0
- package/types/Accounts.d.ts +50 -0
- package/types/Accounts.js +2 -0
- package/types/CLIOptions.d.ts +3 -0
- package/types/CLIOptions.js +2 -0
- package/types/Config.d.ts +10 -0
- package/types/Config.js +2 -0
- package/types/Error.d.ts +38 -0
- package/types/Error.js +2 -0
- package/types/LogCallbacks.d.ts +6 -0
- package/types/LogCallbacks.js +2 -0
- package/types/Utils.d.ts +1 -0
- package/types/Utils.js +2 -0
- package/utils/encoding.d.ts +1 -0
- package/utils/encoding.js +11 -0
- package/utils/environment.d.ts +1 -0
- package/utils/environment.js +16 -0
- package/utils/escapeRegExp.d.ts +1 -0
- package/utils/escapeRegExp.js +7 -0
- package/utils/git.d.ts +3 -0
- package/utils/git.js +104 -0
- package/utils/interpolation.d.ts +5 -0
- package/utils/interpolation.js +31 -0
- package/utils/lang.d.ts +3 -0
- package/utils/lang.js +58 -0
- package/utils/logger.d.ts +10 -0
- package/utils/logger.js +22 -0
- package/utils/text.d.ts +1 -0
- package/utils/text.js +16 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function base64EncodeString(valueToEncode: string): string;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.base64EncodeString = void 0;
|
|
4
|
+
function base64EncodeString(valueToEncode) {
|
|
5
|
+
if (typeof valueToEncode !== 'string') {
|
|
6
|
+
return valueToEncode;
|
|
7
|
+
}
|
|
8
|
+
const stringBuffer = Buffer.from(valueToEncode);
|
|
9
|
+
return encodeURIComponent(stringBuffer.toString('base64'));
|
|
10
|
+
}
|
|
11
|
+
exports.base64EncodeString = base64EncodeString;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getValidEnv(env: string, maskedProductionValue?: string): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getValidEnv = void 0;
|
|
4
|
+
const environments_1 = require("../constants/environments");
|
|
5
|
+
function getValidEnv(env, maskedProductionValue) {
|
|
6
|
+
const prodValue = maskedProductionValue
|
|
7
|
+
? maskedProductionValue
|
|
8
|
+
: environments_1.ENVIRONMENTS.PROD;
|
|
9
|
+
const returnVal = typeof env &&
|
|
10
|
+
typeof env === 'string' &&
|
|
11
|
+
env.toLowerCase() === environments_1.ENVIRONMENTS.QA
|
|
12
|
+
? environments_1.ENVIRONMENTS.QA
|
|
13
|
+
: prodValue;
|
|
14
|
+
return returnVal;
|
|
15
|
+
}
|
|
16
|
+
exports.getValidEnv = getValidEnv;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function escapeRegExp(string: string): string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.escapeRegExp = void 0;
|
|
4
|
+
function escapeRegExp(string) {
|
|
5
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
|
|
6
|
+
}
|
|
7
|
+
exports.escapeRegExp = escapeRegExp;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fieldsArrayToJson = void 0;
|
|
4
|
+
/*
|
|
5
|
+
* Polyfill for `Array.flat(Infinity)` since the `flat` is only available for Node v11+
|
|
6
|
+
* https://stackoverflow.com/a/15030117
|
|
7
|
+
*/
|
|
8
|
+
function flattenArray(arr) {
|
|
9
|
+
return arr.reduce((flat, toFlatten) => {
|
|
10
|
+
return flat.concat(Array.isArray(toFlatten) ? flattenArray(toFlatten) : toFlatten);
|
|
11
|
+
}, []);
|
|
12
|
+
}
|
|
13
|
+
//Transform fields array to JSON
|
|
14
|
+
function fieldsArrayToJson(fields) {
|
|
15
|
+
const flattened = flattenArray(fields);
|
|
16
|
+
return JSON.stringify(flattened, null, 2);
|
|
17
|
+
}
|
|
18
|
+
exports.fieldsArrayToJson = fieldsArrayToJson;
|
|
@@ -0,0 +1,35 @@
|
|
|
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.getThemePreviewUrl = exports.getThemeNameFromPath = exports.getThemeJSONPath = void 0;
|
|
7
|
+
const findup_sync_1 = __importDefault(require("findup-sync"));
|
|
8
|
+
const urls_1 = require("./urls");
|
|
9
|
+
const environments_1 = require("../constants/environments");
|
|
10
|
+
const CLIConfiguration_1 = __importDefault(require("../config/CLIConfiguration"));
|
|
11
|
+
function getThemeJSONPath(path) {
|
|
12
|
+
return (0, findup_sync_1.default)('theme.json', {
|
|
13
|
+
cwd: path,
|
|
14
|
+
nocase: true,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
exports.getThemeJSONPath = getThemeJSONPath;
|
|
18
|
+
function getThemeNameFromPath(filePath) {
|
|
19
|
+
const themeJSONPath = getThemeJSONPath(filePath);
|
|
20
|
+
if (!themeJSONPath)
|
|
21
|
+
return;
|
|
22
|
+
const pathParts = themeJSONPath.split('/');
|
|
23
|
+
if (pathParts.length < 2)
|
|
24
|
+
return;
|
|
25
|
+
return pathParts[pathParts.length - 2];
|
|
26
|
+
}
|
|
27
|
+
exports.getThemeNameFromPath = getThemeNameFromPath;
|
|
28
|
+
function getThemePreviewUrl(filePath, accountId) {
|
|
29
|
+
const themeName = getThemeNameFromPath(filePath);
|
|
30
|
+
if (!themeName)
|
|
31
|
+
return;
|
|
32
|
+
const baseUrl = (0, urls_1.getHubSpotWebsiteOrigin)(CLIConfiguration_1.default.getEnv() === 'qa' ? environments_1.ENVIRONMENTS.QA : environments_1.ENVIRONMENTS.PROD);
|
|
33
|
+
return `${baseUrl}/theme-previewer/${accountId}/edit/${encodeURIComponent(themeName)}`;
|
|
34
|
+
}
|
|
35
|
+
exports.getThemePreviewUrl = getThemePreviewUrl;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { FileData } from '../../types/Files';
|
|
2
|
+
export declare function getFileInfoAsync(dir: string, file: string): Promise<FileData>;
|
|
3
|
+
export declare function flattenAndRemoveSymlinks(filesData: Array<FileData>): Array<string>;
|
|
4
|
+
export declare function read(dir: string): Promise<Array<string>>;
|
|
@@ -0,0 +1,55 @@
|
|
|
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.read = exports.flattenAndRemoveSymlinks = exports.getFileInfoAsync = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const files_1 = require("../../constants/files");
|
|
10
|
+
function getFileInfoAsync(dir, file) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
const filepath = path_1.default.join(dir, file);
|
|
13
|
+
fs_1.default.lstat(filepath, (error, stats) => {
|
|
14
|
+
if (error) {
|
|
15
|
+
reject(error);
|
|
16
|
+
}
|
|
17
|
+
let type = files_1.STAT_TYPES.FILE;
|
|
18
|
+
if (stats.isSymbolicLink()) {
|
|
19
|
+
type = files_1.STAT_TYPES.SYMBOLIC_LINK;
|
|
20
|
+
}
|
|
21
|
+
else if (stats.isDirectory()) {
|
|
22
|
+
type = files_1.STAT_TYPES.DIRECTORY;
|
|
23
|
+
}
|
|
24
|
+
resolve({ filepath, type });
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
exports.getFileInfoAsync = getFileInfoAsync;
|
|
29
|
+
function flattenAndRemoveSymlinks(filesData) {
|
|
30
|
+
return filesData.reduce((acc, fileData) => {
|
|
31
|
+
switch (fileData.type) {
|
|
32
|
+
case files_1.STAT_TYPES.FILE:
|
|
33
|
+
return acc.concat(fileData.filepath);
|
|
34
|
+
case files_1.STAT_TYPES.DIRECTORY:
|
|
35
|
+
return acc.concat(fileData.files || []);
|
|
36
|
+
case files_1.STAT_TYPES.SYMBOLIC_LINK:
|
|
37
|
+
return acc;
|
|
38
|
+
default:
|
|
39
|
+
return acc;
|
|
40
|
+
}
|
|
41
|
+
}, []);
|
|
42
|
+
}
|
|
43
|
+
exports.flattenAndRemoveSymlinks = flattenAndRemoveSymlinks;
|
|
44
|
+
async function read(dir) {
|
|
45
|
+
const processFiles = (files) => Promise.all(files.map(file => getFileInfoAsync(dir, file)));
|
|
46
|
+
return fs_1.default.promises
|
|
47
|
+
.readdir(dir)
|
|
48
|
+
.then(processFiles)
|
|
49
|
+
.then(flattenAndRemoveSymlinks)
|
|
50
|
+
.catch(err => {
|
|
51
|
+
console.debug(err);
|
|
52
|
+
return [];
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
exports.read = read;
|
|
@@ -0,0 +1,44 @@
|
|
|
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.walk = exports.listFilesInDir = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const read_1 = require("./read");
|
|
9
|
+
const files_1 = require("../../constants/files");
|
|
10
|
+
const standardErrors_1 = require("../../errors/standardErrors");
|
|
11
|
+
function listFilesInDir(dir) {
|
|
12
|
+
return fs_1.default
|
|
13
|
+
.readdirSync(dir, { withFileTypes: true })
|
|
14
|
+
.filter(file => !file.isDirectory())
|
|
15
|
+
.map(file => file.name);
|
|
16
|
+
}
|
|
17
|
+
exports.listFilesInDir = listFilesInDir;
|
|
18
|
+
const generateRecursiveFilePromise = async (dir, file) => {
|
|
19
|
+
return (0, read_1.getFileInfoAsync)(dir, file).then(fileData => {
|
|
20
|
+
return new Promise(resolve => {
|
|
21
|
+
if (fileData.type === files_1.STAT_TYPES.DIRECTORY) {
|
|
22
|
+
walk(fileData.filepath).then(files => {
|
|
23
|
+
resolve({ ...fileData, files });
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
resolve(fileData);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
async function walk(dir) {
|
|
33
|
+
function processFiles(files) {
|
|
34
|
+
return Promise.all(files.map(file => generateRecursiveFilePromise(dir, file)));
|
|
35
|
+
}
|
|
36
|
+
return fs_1.default.promises
|
|
37
|
+
.readdir(dir)
|
|
38
|
+
.then(processFiles)
|
|
39
|
+
.then(read_1.flattenAndRemoveSymlinks)
|
|
40
|
+
.catch(err => {
|
|
41
|
+
(0, standardErrors_1.throwError)(err);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
exports.walk = walk;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function makeComparisonDir(filepath: string | null): string | null;
|
|
2
|
+
export declare const getGitComparisonDir: () => string | null;
|
|
3
|
+
export declare function isConfigPathInGitRepo(configPath: string): boolean;
|
|
4
|
+
export declare function configFilenameIsIgnoredByGitignore(ignoreFiles: Array<string>, configPath: string): boolean;
|
package/src/utils/git.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
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.configFilenameIsIgnoredByGitignore = exports.isConfigPathInGitRepo = exports.getGitComparisonDir = exports.makeComparisonDir = void 0;
|
|
7
|
+
const fs_extra_1 = require("fs-extra");
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const ignore_1 = __importDefault(require("ignore"));
|
|
10
|
+
const findup_sync_1 = __importDefault(require("findup-sync"));
|
|
11
|
+
function makeComparisonDir(filepath) {
|
|
12
|
+
if (typeof filepath !== 'string')
|
|
13
|
+
return null;
|
|
14
|
+
// Append sep to make comparisons easier e.g. 'foos'.startsWith('foo')
|
|
15
|
+
return path_1.default.dirname(path_1.default.resolve(filepath)).toLowerCase() + path_1.default.sep;
|
|
16
|
+
}
|
|
17
|
+
exports.makeComparisonDir = makeComparisonDir;
|
|
18
|
+
const getGitComparisonDir = () => makeComparisonDir((0, findup_sync_1.default)('.git'));
|
|
19
|
+
exports.getGitComparisonDir = getGitComparisonDir;
|
|
20
|
+
function isConfigPathInGitRepo(configPath) {
|
|
21
|
+
const gitDir = (0, exports.getGitComparisonDir)();
|
|
22
|
+
if (!gitDir)
|
|
23
|
+
return false;
|
|
24
|
+
const configDir = makeComparisonDir(configPath);
|
|
25
|
+
if (!configDir)
|
|
26
|
+
return false;
|
|
27
|
+
return configDir.startsWith(gitDir);
|
|
28
|
+
}
|
|
29
|
+
exports.isConfigPathInGitRepo = isConfigPathInGitRepo;
|
|
30
|
+
function configFilenameIsIgnoredByGitignore(ignoreFiles, configPath) {
|
|
31
|
+
return ignoreFiles.some(gitignore => {
|
|
32
|
+
const gitignoreContents = (0, fs_extra_1.readFileSync)(gitignore).toString();
|
|
33
|
+
const gitignoreConfig = (0, ignore_1.default)().add(gitignoreContents);
|
|
34
|
+
if (gitignoreConfig.ignores(path_1.default.relative(path_1.default.dirname(gitignore), configPath))) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
exports.configFilenameIsIgnoredByGitignore = configFilenameIsIgnoredByGitignore;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export declare class FieldsJs {
|
|
2
|
+
projectDir: string;
|
|
3
|
+
filePath: string;
|
|
4
|
+
rootWriteDir: string;
|
|
5
|
+
rejected: boolean;
|
|
6
|
+
fieldOptions: string;
|
|
7
|
+
outputPath?: string;
|
|
8
|
+
constructor(projectDir: string, filePath: string, rootWriteDir?: string, fieldOptions?: string);
|
|
9
|
+
init(): Promise<this>;
|
|
10
|
+
convertFieldsJs(writeDir: string): Promise<string | void>;
|
|
11
|
+
/**
|
|
12
|
+
* If there has been a fields.json written to the output path, then copy it from the output
|
|
13
|
+
* directory to the project directory, respecting the path within the output directory.
|
|
14
|
+
* Ex: path/to/tmp/example.module/fields.json => path/to/project/example.module/fields.output.json
|
|
15
|
+
*/
|
|
16
|
+
saveOutput(): void;
|
|
17
|
+
/**
|
|
18
|
+
* Resolves the relative path to the fields.js within the project directory and returns
|
|
19
|
+
* directory name to write to in rootWriteDir directory.
|
|
20
|
+
*
|
|
21
|
+
* Ex: If rootWriteDir = 'path/to/temp', filePath = 'projectRoot/sample.module/fields.js'. Then getWriteDir() => path/to/temp/sample.module
|
|
22
|
+
*/
|
|
23
|
+
getWriteDir(): string;
|
|
24
|
+
getOutputPathPromise(): Promise<string | void>;
|
|
25
|
+
}
|
|
26
|
+
type FieldsArray<T> = Array<T | FieldsArray<T>>;
|
|
27
|
+
export declare function fieldsArrayToJson<T>(fields: FieldsArray<T>): string;
|
|
28
|
+
/**
|
|
29
|
+
* Determines if file is a convertable fields.js file i.e., if it is called
|
|
30
|
+
* 'fields.js' and in a root or in a module folder, and if convertFields flag is true.
|
|
31
|
+
*/
|
|
32
|
+
export declare function isConvertableFieldJs(rootDir: string, filePath: string, convertFields?: boolean): boolean;
|
|
33
|
+
export declare function createTmpDirSync(prefix: string): string;
|
|
34
|
+
export declare function cleanupTmpDirSync(tmpDir: string): void;
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,158 @@
|
|
|
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.fieldsArrayToJson = 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("../lib/modules");
|
|
13
|
+
const logger_1 = require("./logger");
|
|
14
|
+
const standardErrors_1 = require("../errors/standardErrors");
|
|
15
|
+
const i18nKey = 'utils.handleFieldsJs';
|
|
16
|
+
class FieldsJs {
|
|
17
|
+
projectDir;
|
|
18
|
+
filePath;
|
|
19
|
+
rootWriteDir;
|
|
20
|
+
rejected;
|
|
21
|
+
fieldOptions;
|
|
22
|
+
outputPath;
|
|
23
|
+
constructor(projectDir, filePath, rootWriteDir, fieldOptions = '') {
|
|
24
|
+
this.projectDir = projectDir;
|
|
25
|
+
this.filePath = filePath;
|
|
26
|
+
this.fieldOptions = fieldOptions;
|
|
27
|
+
this.rejected = false;
|
|
28
|
+
// Create tmpDir if no writeDir is given.
|
|
29
|
+
this.rootWriteDir =
|
|
30
|
+
rootWriteDir === undefined
|
|
31
|
+
? createTmpDirSync('hubspot-temp-fieldsjs-output-')
|
|
32
|
+
: rootWriteDir;
|
|
33
|
+
}
|
|
34
|
+
async init() {
|
|
35
|
+
const outputPath = await this.getOutputPathPromise();
|
|
36
|
+
this.outputPath = this.rejected ? undefined : outputPath;
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
// Converts a fields.js file into a fields.json file, writes, and returns of fields.json
|
|
40
|
+
convertFieldsJs(writeDir) {
|
|
41
|
+
const filePath = this.filePath;
|
|
42
|
+
const dirName = path_1.default.dirname(filePath);
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
const convertFieldsProcess = (0, child_process_1.fork)(path_1.default.join(__dirname, './processFieldsJs.js'), [], {
|
|
45
|
+
cwd: dirName,
|
|
46
|
+
env: {
|
|
47
|
+
dirName,
|
|
48
|
+
fieldOptions: this.fieldOptions,
|
|
49
|
+
filePath,
|
|
50
|
+
writeDir,
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
(0, logger_1.debug)(`${i18nKey}.convertFieldsJs.creating`, {
|
|
54
|
+
pid: convertFieldsProcess.pid || '',
|
|
55
|
+
});
|
|
56
|
+
convertFieldsProcess.on('message', function (message) {
|
|
57
|
+
if (message.action === 'ERROR') {
|
|
58
|
+
reject(message.message);
|
|
59
|
+
}
|
|
60
|
+
else if (message.action === 'COMPLETE') {
|
|
61
|
+
resolve(message.finalPath);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
convertFieldsProcess.on('close', () => {
|
|
65
|
+
(0, logger_1.debug)(`${i18nKey}.convertFieldsJs.terminating`, {
|
|
66
|
+
pid: convertFieldsProcess.pid || '',
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}).catch((e) => {
|
|
70
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.convertFieldsJs`, { filePath }, e);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* If there has been a fields.json written to the output path, then copy it from the output
|
|
75
|
+
* directory to the project directory, respecting the path within the output directory.
|
|
76
|
+
* Ex: path/to/tmp/example.module/fields.json => path/to/project/example.module/fields.output.json
|
|
77
|
+
*/
|
|
78
|
+
saveOutput() {
|
|
79
|
+
if (!this.outputPath || !fs_extra_1.default.existsSync(this.outputPath)) {
|
|
80
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.saveOutput`, { path: this.filePath });
|
|
81
|
+
}
|
|
82
|
+
const relativePath = path_1.default.relative(this.rootWriteDir, path_1.default.dirname(this.outputPath));
|
|
83
|
+
const savePath = path_1.default.join(this.projectDir, relativePath, 'fields.output.json');
|
|
84
|
+
try {
|
|
85
|
+
fs_extra_1.default.copyFileSync(this.outputPath, savePath);
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.saveOutput`, { path: savePath }, err);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Resolves the relative path to the fields.js within the project directory and returns
|
|
93
|
+
* directory name to write to in rootWriteDir directory.
|
|
94
|
+
*
|
|
95
|
+
* Ex: If rootWriteDir = 'path/to/temp', filePath = 'projectRoot/sample.module/fields.js'. Then getWriteDir() => path/to/temp/sample.module
|
|
96
|
+
*/
|
|
97
|
+
getWriteDir() {
|
|
98
|
+
const projectDirRegex = new RegExp(`^${(0, escapeRegExp_1.escapeRegExp)(this.projectDir)}`);
|
|
99
|
+
const relativePath = this.filePath.replace(projectDirRegex, '');
|
|
100
|
+
return path_1.default.dirname(path_1.default.join(this.rootWriteDir, relativePath));
|
|
101
|
+
}
|
|
102
|
+
getOutputPathPromise() {
|
|
103
|
+
const writeDir = this.getWriteDir();
|
|
104
|
+
return this.convertFieldsJs(writeDir).then(outputPath => outputPath);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
exports.FieldsJs = FieldsJs;
|
|
108
|
+
/*
|
|
109
|
+
* Polyfill for `Array.flat(Infinity)` since the `flat` is only available for Node v11+
|
|
110
|
+
* https://stackoverflow.com/a/15030117
|
|
111
|
+
*/
|
|
112
|
+
function flattenArray(arr) {
|
|
113
|
+
return arr.reduce((flat, toFlatten) => {
|
|
114
|
+
return flat.concat(Array.isArray(toFlatten) ? flattenArray(toFlatten) : toFlatten);
|
|
115
|
+
}, []);
|
|
116
|
+
}
|
|
117
|
+
//Transform fields array to JSON
|
|
118
|
+
function fieldsArrayToJson(fields) {
|
|
119
|
+
const flattened = flattenArray(fields);
|
|
120
|
+
return JSON.stringify(flattened, null, 2);
|
|
121
|
+
}
|
|
122
|
+
exports.fieldsArrayToJson = fieldsArrayToJson;
|
|
123
|
+
/**
|
|
124
|
+
* Determines if file is a convertable fields.js file i.e., if it is called
|
|
125
|
+
* 'fields.js' and in a root or in a module folder, and if convertFields flag is true.
|
|
126
|
+
*/
|
|
127
|
+
function isConvertableFieldJs(rootDir, filePath, convertFields = false) {
|
|
128
|
+
const allowedFieldsNames = ['fields.js', 'fields.mjs', 'fields.cjs'];
|
|
129
|
+
const regex = new RegExp(`^${(0, escapeRegExp_1.escapeRegExp)(rootDir)}`);
|
|
130
|
+
const relativePath = path_1.default.dirname(filePath.replace(regex, ''));
|
|
131
|
+
const baseName = path_1.default.basename(filePath);
|
|
132
|
+
const inModuleFolder = (0, modules_1.isModuleFolderChild)({ path: filePath, isLocal: true });
|
|
133
|
+
return !!(convertFields &&
|
|
134
|
+
allowedFieldsNames.includes(baseName) &&
|
|
135
|
+
(inModuleFolder || relativePath == '/'));
|
|
136
|
+
}
|
|
137
|
+
exports.isConvertableFieldJs = isConvertableFieldJs;
|
|
138
|
+
// Try creating tempdir
|
|
139
|
+
function createTmpDirSync(prefix) {
|
|
140
|
+
let tmpDir;
|
|
141
|
+
try {
|
|
142
|
+
tmpDir = fs_extra_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), prefix));
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.createTmpDirSync`, {}, err);
|
|
146
|
+
}
|
|
147
|
+
return tmpDir;
|
|
148
|
+
}
|
|
149
|
+
exports.createTmpDirSync = createTmpDirSync;
|
|
150
|
+
// Try cleaning up resources from os's tempdir
|
|
151
|
+
function cleanupTmpDirSync(tmpDir) {
|
|
152
|
+
fs_extra_1.default.rm(tmpDir, { recursive: true }, err => {
|
|
153
|
+
if (err) {
|
|
154
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.cleanupTmpDirSync`, {}, err);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
exports.cleanupTmpDirSync = cleanupTmpDirSync;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.interpolate = void 0;
|
|
4
|
+
const delimiters = {
|
|
5
|
+
interpolation: {
|
|
6
|
+
start: '{{',
|
|
7
|
+
end: '}}',
|
|
8
|
+
},
|
|
9
|
+
};
|
|
10
|
+
function generateReplaceFn(matchedText, startIndex, replacementString) {
|
|
11
|
+
return function (currentStringValue) {
|
|
12
|
+
return `${currentStringValue.slice(0, startIndex)}${replacementString || ''}${currentStringValue.slice(startIndex + matchedText.length)}`;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function interpolate(stringValue, interpolationData) {
|
|
16
|
+
const interpolationIdentifierRegEx = new RegExp(`${delimiters.interpolation.start}(.*?)${delimiters.interpolation.end}`, 'g');
|
|
17
|
+
const replaceQueue = [];
|
|
18
|
+
let match;
|
|
19
|
+
// while & reduce necessary because RegExp.exec is stateful and only runs
|
|
20
|
+
// from beginning to end of string
|
|
21
|
+
while ((match = interpolationIdentifierRegEx.exec(stringValue)) != null) {
|
|
22
|
+
const { 0: matchedText, 1: rawIdentifier, index } = match;
|
|
23
|
+
const identifier = rawIdentifier.trim();
|
|
24
|
+
if (identifier) {
|
|
25
|
+
replaceQueue.unshift(generateReplaceFn(matchedText, index, interpolationData[identifier]));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const compiledString = replaceQueue.reduce((currentValue, replaceFn) => replaceFn(currentValue), stringValue);
|
|
29
|
+
return compiledString;
|
|
30
|
+
}
|
|
31
|
+
exports.interpolate = interpolate;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
type InterpolationData = {
|
|
2
|
+
[identifier: string]: string | number;
|
|
3
|
+
};
|
|
4
|
+
export declare function interpolate(stringValue: string, interpolationData: InterpolationData): string;
|
|
5
|
+
export declare function i18n(lookupDotNotation: string, options?: {
|
|
6
|
+
[identifier: string]: string | number;
|
|
7
|
+
}): string;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.i18n = exports.interpolate = void 0;
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
const fs_extra_1 = require("fs-extra");
|
|
6
|
+
const js_yaml_1 = require("js-yaml");
|
|
7
|
+
const MISSING_LANGUAGE_DATA_PREFIX = '[Missing language data]';
|
|
8
|
+
let locale = '';
|
|
9
|
+
let languageObj;
|
|
10
|
+
function loadLanguageFromYaml() {
|
|
11
|
+
if (languageObj)
|
|
12
|
+
return;
|
|
13
|
+
try {
|
|
14
|
+
const nodeLocale = Intl.DateTimeFormat()
|
|
15
|
+
.resolvedOptions()
|
|
16
|
+
.locale.split('-')[0];
|
|
17
|
+
const languageFilePath = (0, path_1.join)(__dirname, `../lang/${nodeLocale}.lyaml`);
|
|
18
|
+
const languageFileExists = (0, fs_extra_1.existsSync)(languageFilePath);
|
|
19
|
+
// Fall back to using the default language file
|
|
20
|
+
locale = languageFileExists ? nodeLocale : 'en';
|
|
21
|
+
languageObj = (0, js_yaml_1.load)((0, fs_extra_1.readFileSync)((0, path_1.join)(__dirname, `../lang/${locale}.lyaml`), 'utf8'));
|
|
22
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
throw new Error(`Error loading language data: ${e}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function getTextValue(lookupDotNotation) {
|
|
28
|
+
const lookupProps = [locale, ...lookupDotNotation.split('.')];
|
|
29
|
+
const missingTextData = `${MISSING_LANGUAGE_DATA_PREFIX}: ${lookupProps.join('.')}`;
|
|
30
|
+
let textValue = languageObj;
|
|
31
|
+
let previouslyCheckedProp = lookupProps[0];
|
|
32
|
+
lookupProps.forEach(prop => {
|
|
33
|
+
previouslyCheckedProp = prop;
|
|
34
|
+
if (typeof textValue === 'object') {
|
|
35
|
+
textValue = textValue[prop];
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
console.debug(`Unable to access language property: ${lookupProps.join('.')}. Failed to access prop "${previouslyCheckedProp}".`);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
if (typeof textValue !== 'string') {
|
|
42
|
+
return missingTextData;
|
|
43
|
+
}
|
|
44
|
+
return textValue;
|
|
45
|
+
}
|
|
46
|
+
const delimiters = {
|
|
47
|
+
interpolation: {
|
|
48
|
+
start: '{{',
|
|
49
|
+
end: '}}',
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
function generateReplaceFn(matchedText, startIndex, replacementString) {
|
|
53
|
+
return function (currentStringValue) {
|
|
54
|
+
return `${currentStringValue.slice(0, startIndex)}${replacementString || ''}${currentStringValue.slice(startIndex + matchedText.length)}`;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function interpolate(stringValue, interpolationData) {
|
|
58
|
+
const interpolationIdentifierRegEx = new RegExp(`${delimiters.interpolation.start}(.*?)${delimiters.interpolation.end}`, 'g');
|
|
59
|
+
const replaceQueue = [];
|
|
60
|
+
let match;
|
|
61
|
+
// while & reduce necessary because RegExp.exec is stateful and only runs
|
|
62
|
+
// from beginning to end of string
|
|
63
|
+
while ((match = interpolationIdentifierRegEx.exec(stringValue)) != null) {
|
|
64
|
+
const { 0: matchedText, 1: rawIdentifier, index } = match;
|
|
65
|
+
const identifier = rawIdentifier.trim();
|
|
66
|
+
if (identifier) {
|
|
67
|
+
replaceQueue.unshift(generateReplaceFn(matchedText, index, interpolationData[identifier]));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const compiledString = replaceQueue.reduce((currentValue, replaceFn) => replaceFn(currentValue), stringValue);
|
|
71
|
+
return compiledString;
|
|
72
|
+
}
|
|
73
|
+
exports.interpolate = interpolate;
|
|
74
|
+
function i18n(lookupDotNotation, options = {}) {
|
|
75
|
+
if (typeof lookupDotNotation !== 'string') {
|
|
76
|
+
throw new Error(`i18n must be passed a string value for lookupDotNotation, received ${typeof lookupDotNotation}`);
|
|
77
|
+
}
|
|
78
|
+
const textValue = getTextValue(lookupDotNotation);
|
|
79
|
+
const shouldInterpolate = !textValue.startsWith(MISSING_LANGUAGE_DATA_PREFIX);
|
|
80
|
+
return shouldInterpolate ? interpolate(textValue, options) : textValue;
|
|
81
|
+
}
|
|
82
|
+
exports.i18n = i18n;
|
|
83
|
+
loadLanguageFromYaml();
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { LogCallbacks } from '../types/LogCallbacks';
|
|
2
|
+
export declare function log<T extends string>(key: T, callbacks?: LogCallbacks<T>, debugKey?: string, debugInterpolation?: {
|
|
3
|
+
[key: string]: string | number;
|
|
4
|
+
}): void;
|
|
5
|
+
export declare function makeTypedLogger<T extends readonly string[]>(callbacks?: LogCallbacks<T[number]>, debugKey?: string): (key: T[number], debugInterpolation?: {
|
|
6
|
+
[key: string]: string | number;
|
|
7
|
+
} | undefined) => void;
|
|
8
|
+
export declare function debug(identifier: string, interpolation?: {
|
|
9
|
+
[key: string]: string | number;
|
|
10
|
+
}): void;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.debug = exports.makeTypedLogger = exports.log = void 0;
|
|
4
|
+
const lang_1 = require("./lang");
|
|
5
|
+
function log(key, callbacks, debugKey, debugInterpolation) {
|
|
6
|
+
if (callbacks && callbacks[key]) {
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
8
|
+
callbacks[key]();
|
|
9
|
+
}
|
|
10
|
+
else if (debugKey) {
|
|
11
|
+
debug(debugKey, debugInterpolation);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.log = log;
|
|
15
|
+
function makeTypedLogger(callbacks, debugKey) {
|
|
16
|
+
return (key, debugInterpolation) => log(key, callbacks, `${debugKey}.${key}`, debugInterpolation);
|
|
17
|
+
}
|
|
18
|
+
exports.makeTypedLogger = makeTypedLogger;
|
|
19
|
+
function debug(identifier, interpolation) {
|
|
20
|
+
console.debug((0, lang_1.i18n)(`debug.${identifier}`, interpolation));
|
|
21
|
+
}
|
|
22
|
+
exports.debug = debug;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { PathInput } from '../types/Modules';
|
|
2
|
+
export declare function isPathInput(pathInput?: PathInput): boolean;
|
|
3
|
+
export declare function isModuleFolder(pathInput: PathInput): boolean;
|
|
4
|
+
export declare function isModuleFolderChild(pathInput: PathInput, ignoreLocales?: boolean): boolean;
|