@hubspot/project-parsing-lib 0.10.3 → 0.11.0-beta.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/package.json +48 -11
- package/src/exports/constants.d.ts +1 -0
- package/src/exports/constants.js +1 -0
- package/src/exports/migrate.d.ts +1 -0
- package/src/exports/migrate.js +1 -0
- package/src/exports/profiles.d.ts +3 -0
- package/src/exports/profiles.js +2 -0
- package/src/exports/projects.d.ts +3 -0
- package/src/exports/projects.js +2 -0
- package/src/exports/schema.d.ts +2 -0
- package/src/exports/schema.js +1 -0
- package/src/exports/themes.d.ts +2 -0
- package/src/exports/themes.js +1 -0
- package/src/exports/transform.d.ts +2 -0
- package/src/exports/transform.js +1 -0
- package/src/exports/translate.d.ts +3 -0
- package/src/exports/translate.js +2 -0
- package/src/exports/uid.d.ts +1 -0
- package/src/exports/uid.js +1 -0
- package/src/lang/copy.d.ts +1 -1
- package/src/lang/copy.js +21 -31
- package/src/lib/constants.d.ts +38 -38
- package/src/lib/constants.js +120 -153
- package/src/lib/errors.d.ts +2 -2
- package/src/lib/errors.js +19 -29
- package/src/lib/files.d.ts +1 -1
- package/src/lib/files.js +36 -47
- package/src/lib/localDev.d.ts +1 -1
- package/src/lib/localDev.js +8 -16
- package/src/lib/migrate.js +34 -40
- package/src/lib/migrateThemes.d.ts +3 -4
- package/src/lib/migrateThemes.js +31 -38
- package/src/lib/profiles.d.ts +1 -1
- package/src/lib/profiles.js +12 -20
- package/src/lib/project.js +11 -17
- package/src/lib/schemas.d.ts +2 -2
- package/src/lib/schemas.js +8 -11
- package/src/lib/transform.d.ts +1 -1
- package/src/lib/transform.js +50 -64
- package/src/lib/translate.d.ts +3 -0
- package/src/lib/translate.js +52 -0
- package/src/lib/types.d.ts +1 -1
- package/src/lib/types.js +1 -2
- package/src/lib/uid.js +9 -14
- package/src/lib/utils.d.ts +1 -1
- package/src/lib/utils.js +7 -14
- package/src/lib/validation.d.ts +3 -3
- package/src/lib/validation.js +46 -46
- package/src/index.d.ts +0 -19
- package/src/index.js +0 -94
package/src/lib/localDev.js
CHANGED
|
@@ -1,23 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getLocalDevProjectNodes = getLocalDevProjectNodes;
|
|
7
|
-
exports.getRemovedNodesAndNodesWithErrors = getRemovedNodesAndNodesWithErrors;
|
|
8
|
-
exports.getLocalDevProfileData = getLocalDevProfileData;
|
|
9
|
-
const path_1 = __importDefault(require("path"));
|
|
10
|
-
const isDeepEqual_1 = require("@hubspot/local-dev-lib/isDeepEqual");
|
|
11
|
-
function getLocalDevProjectNodes(intermediateNodesIndexedByUid, projectSourceDir, projectNodesAtLastUpload) {
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { isDeepEqual } from '@hubspot/local-dev-lib/isDeepEqual';
|
|
3
|
+
export function getLocalDevProjectNodes(intermediateNodesIndexedByUid, projectSourceDir, projectNodesAtLastUpload) {
|
|
12
4
|
const localDevProjectNodes = {};
|
|
13
5
|
Object.entries(intermediateNodesIndexedByUid).forEach(([uid, node]) => {
|
|
14
6
|
const component = intermediateNodesIndexedByUid[uid];
|
|
15
|
-
const componentConfigPath =
|
|
7
|
+
const componentConfigPath = path.join(projectSourceDir, component.metaFilePath);
|
|
16
8
|
let configUpdatedSinceLastUpload = false;
|
|
17
9
|
if (projectNodesAtLastUpload) {
|
|
18
10
|
const componentAtLastUpload = projectNodesAtLastUpload[uid];
|
|
19
11
|
if (componentAtLastUpload) {
|
|
20
|
-
configUpdatedSinceLastUpload = !
|
|
12
|
+
configUpdatedSinceLastUpload = !isDeepEqual(component.config, componentAtLastUpload.config);
|
|
21
13
|
}
|
|
22
14
|
else {
|
|
23
15
|
// Component is net new
|
|
@@ -27,7 +19,7 @@ function getLocalDevProjectNodes(intermediateNodesIndexedByUid, projectSourceDir
|
|
|
27
19
|
localDevProjectNodes[uid] = {
|
|
28
20
|
...node,
|
|
29
21
|
localDev: {
|
|
30
|
-
componentRoot:
|
|
22
|
+
componentRoot: path.dirname(componentConfigPath),
|
|
31
23
|
componentConfigPath,
|
|
32
24
|
configUpdatedSinceLastUpload,
|
|
33
25
|
removed: false,
|
|
@@ -37,7 +29,7 @@ function getLocalDevProjectNodes(intermediateNodesIndexedByUid, projectSourceDir
|
|
|
37
29
|
});
|
|
38
30
|
return localDevProjectNodes;
|
|
39
31
|
}
|
|
40
|
-
function getRemovedNodesAndNodesWithErrors(intermediateNodesIndexedByUid, projectNodesAtLastUpload, parsingErrors) {
|
|
32
|
+
export function getRemovedNodesAndNodesWithErrors(intermediateNodesIndexedByUid, projectNodesAtLastUpload, parsingErrors) {
|
|
41
33
|
const nodesWithErrors = {};
|
|
42
34
|
Object.entries(projectNodesAtLastUpload).forEach(([uid, node]) => {
|
|
43
35
|
// Node is still in the project
|
|
@@ -68,7 +60,7 @@ function getRemovedNodesAndNodesWithErrors(intermediateNodesIndexedByUid, projec
|
|
|
68
60
|
});
|
|
69
61
|
return nodesWithErrors;
|
|
70
62
|
}
|
|
71
|
-
function getLocalDevProfileData(profileVariables) {
|
|
63
|
+
export function getLocalDevProfileData(profileVariables) {
|
|
72
64
|
if (!profileVariables) {
|
|
73
65
|
return {};
|
|
74
66
|
}
|
package/src/lib/migrate.js
CHANGED
|
@@ -1,36 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const fs_2 = __importDefault(require("fs"));
|
|
10
|
-
const transform_1 = require("./transform");
|
|
11
|
-
const constants_1 = require("./constants");
|
|
12
|
-
const index_1 = require("../index");
|
|
13
|
-
const utils_1 = require("./utils");
|
|
1
|
+
import { walk } from '@hubspot/local-dev-lib/fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import { mapToUserFacingType } from './transform.js';
|
|
5
|
+
import { METAFILE_EXTENSION, Components, HS_PROJECT_JSON_FILENAME, } from './constants.js';
|
|
6
|
+
import { translate } from './translate.js';
|
|
7
|
+
import { loadJsonFile } from './utils.js';
|
|
14
8
|
const IR_FILENAME = 'ir.json';
|
|
15
9
|
const filesDirectory = 'files';
|
|
16
10
|
const OUTPUT_IR_FILE = 'FULL_IR.json';
|
|
17
|
-
async function migrate(inputDir, outputDir) {
|
|
18
|
-
if (!
|
|
11
|
+
export async function migrate(inputDir, outputDir) {
|
|
12
|
+
if (!fs.existsSync(inputDir)) {
|
|
19
13
|
throw new Error(`Input directory ${inputDir} does not exist`);
|
|
20
14
|
}
|
|
21
|
-
const hsProjectJsonPath =
|
|
22
|
-
if (!
|
|
23
|
-
throw new Error(`${
|
|
15
|
+
const hsProjectJsonPath = path.join(inputDir, HS_PROJECT_JSON_FILENAME);
|
|
16
|
+
if (!fs.existsSync(hsProjectJsonPath)) {
|
|
17
|
+
throw new Error(`${HS_PROJECT_JSON_FILENAME} file does not exist in ${inputDir}`);
|
|
24
18
|
}
|
|
25
19
|
let hsProjectJson;
|
|
26
20
|
try {
|
|
27
|
-
hsProjectJson =
|
|
21
|
+
hsProjectJson = loadJsonFile(hsProjectJsonPath);
|
|
28
22
|
}
|
|
29
23
|
catch (e) {
|
|
30
|
-
throw new Error(`Error parsing ${
|
|
24
|
+
throw new Error(`Error parsing ${HS_PROJECT_JSON_FILENAME}: ${e}`);
|
|
31
25
|
}
|
|
32
|
-
const files = await
|
|
33
|
-
const sourceCodeOutputDir =
|
|
26
|
+
const files = await walk(inputDir);
|
|
27
|
+
const sourceCodeOutputDir = path.join(outputDir, 'src');
|
|
34
28
|
// Create the output directory if it doesn't exist
|
|
35
29
|
ensureDirExists(sourceCodeOutputDir);
|
|
36
30
|
files.forEach((filename) => {
|
|
@@ -38,38 +32,38 @@ async function migrate(inputDir, outputDir) {
|
|
|
38
32
|
if (!isIRFile(filename)) {
|
|
39
33
|
return;
|
|
40
34
|
}
|
|
41
|
-
const irDirName =
|
|
42
|
-
const IR =
|
|
35
|
+
const irDirName = path.dirname(filename);
|
|
36
|
+
const IR = loadJsonFile(filename);
|
|
43
37
|
const { metaFilePath } = IR;
|
|
44
38
|
const projectConfig = convertIRToProjectConfig(IR);
|
|
45
|
-
const fullOutputPath =
|
|
39
|
+
const fullOutputPath = path.join(sourceCodeOutputDir, getTargetDirectoryFromComponentType(projectConfig.type));
|
|
46
40
|
// Ensure the output directory exists
|
|
47
|
-
const currentFilesDirectory =
|
|
41
|
+
const currentFilesDirectory = path.join(irDirName, filesDirectory);
|
|
48
42
|
ensureDirExists(currentFilesDirectory);
|
|
49
|
-
|
|
43
|
+
fs.cpSync(currentFilesDirectory, fullOutputPath, {
|
|
50
44
|
recursive: true,
|
|
51
45
|
});
|
|
52
46
|
// Use the metaFilePath if provided, otherwise use the project UID
|
|
53
|
-
const hsmetaFilePath =
|
|
54
|
-
?
|
|
55
|
-
: `${projectConfig.uid}${
|
|
47
|
+
const hsmetaFilePath = path.join(fullOutputPath, metaFilePath
|
|
48
|
+
? path.basename(metaFilePath)
|
|
49
|
+
: `${projectConfig.uid}${METAFILE_EXTENSION}`);
|
|
56
50
|
// Write the hsmeta file to the output directory
|
|
57
|
-
|
|
51
|
+
fs.writeFileSync(hsmetaFilePath, JSON.stringify(projectConfig, null, 2));
|
|
58
52
|
});
|
|
59
|
-
const IR = await
|
|
53
|
+
const IR = await translate({
|
|
60
54
|
projectSourceDir: sourceCodeOutputDir,
|
|
61
55
|
platformVersion: hsProjectJson.platformVersion,
|
|
62
56
|
}, { skipValidation: true });
|
|
63
57
|
// Write the IR file to the output directory
|
|
64
|
-
|
|
58
|
+
fs.writeFileSync(path.join(outputDir, OUTPUT_IR_FILE), JSON.stringify(IR, null, 2));
|
|
65
59
|
}
|
|
66
60
|
function ensureDirExists(dir) {
|
|
67
|
-
if (!
|
|
68
|
-
|
|
61
|
+
if (!fs.existsSync(dir)) {
|
|
62
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
69
63
|
}
|
|
70
64
|
}
|
|
71
65
|
function isIRFile(filename) {
|
|
72
|
-
const { base } =
|
|
66
|
+
const { base } = path.parse(filename);
|
|
73
67
|
return base.toLowerCase() === IR_FILENAME;
|
|
74
68
|
}
|
|
75
69
|
function convertIRToProjectConfig(IR) {
|
|
@@ -77,19 +71,19 @@ function convertIRToProjectConfig(IR) {
|
|
|
77
71
|
return {
|
|
78
72
|
config,
|
|
79
73
|
uid,
|
|
80
|
-
type:
|
|
74
|
+
type: mapToUserFacingType(componentType),
|
|
81
75
|
// We are assuming no dependencies for now since this will be a freshly migrated
|
|
82
76
|
// project and the current supported components don't have non-hierarchical dependencies
|
|
83
77
|
dependencies: undefined,
|
|
84
78
|
};
|
|
85
79
|
}
|
|
86
80
|
function getTargetDirectoryFromComponentType(componentType) {
|
|
87
|
-
const component =
|
|
81
|
+
const component = Components[componentType];
|
|
88
82
|
if (!component) {
|
|
89
83
|
throw Error('Unsupported component type');
|
|
90
84
|
}
|
|
91
85
|
const parentDirectory = component.parentComponent
|
|
92
|
-
?
|
|
86
|
+
? Components[component.parentComponent].dir
|
|
93
87
|
: '';
|
|
94
|
-
return
|
|
88
|
+
return path.join(parentDirectory, component.dir);
|
|
95
89
|
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
type ThemeDetails = {
|
|
1
|
+
export type ThemeDetails = {
|
|
2
2
|
configFilepath: string;
|
|
3
3
|
themePath: string;
|
|
4
4
|
themeConfig: {
|
|
5
5
|
secret_names?: string[];
|
|
6
6
|
};
|
|
7
7
|
};
|
|
8
|
-
type ReactThemeDetails = {
|
|
8
|
+
export type ReactThemeDetails = {
|
|
9
9
|
configFilepath: string;
|
|
10
10
|
themePath: string;
|
|
11
11
|
themeConfig: {
|
|
12
12
|
secretNames?: string[];
|
|
13
13
|
};
|
|
14
14
|
};
|
|
15
|
-
type ProjectThemeDetails = {
|
|
15
|
+
export type ProjectThemeDetails = {
|
|
16
16
|
legacyThemeDetails: ThemeDetails[];
|
|
17
17
|
legacyReactThemeDetails: ReactThemeDetails[];
|
|
18
18
|
};
|
|
@@ -23,4 +23,3 @@ export declare function migrateThemes(projectDir: string, projectSourceDir: stri
|
|
|
23
23
|
migrated: boolean;
|
|
24
24
|
failureReason?: string;
|
|
25
25
|
}>;
|
|
26
|
-
export {};
|
package/src/lib/migrateThemes.js
CHANGED
|
@@ -1,33 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const path_1 = __importDefault(require("path"));
|
|
11
|
-
const util_1 = require("util");
|
|
12
|
-
const fs_2 = require("@hubspot/local-dev-lib/fs");
|
|
13
|
-
const constants_1 = require("./constants");
|
|
14
|
-
const utils_1 = require("./utils");
|
|
15
|
-
const copy_1 = require("../lang/copy");
|
|
16
|
-
const mkdtempAsync = (0, util_1.promisify)(fs_1.default.mkdtemp);
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import os from 'os';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { promisify } from 'util';
|
|
5
|
+
import { walk } from '@hubspot/local-dev-lib/fs';
|
|
6
|
+
import { THEME_KEY, CMS_ASSETS_KEY, METAFILE_EXTENSION } from './constants.js';
|
|
7
|
+
import { loadJsonFile } from './utils.js';
|
|
8
|
+
import { errorMessages } from '../lang/copy.js';
|
|
9
|
+
const mkdtempAsync = promisify(fs.mkdtemp);
|
|
17
10
|
const LEGACY_THEME_CONFIG = 'theme.json';
|
|
18
11
|
const LEGACY_REACT_THEME_CONFIG = 'cms-assets.json';
|
|
19
|
-
async function getProjectThemeDetails(projectSourceDir) {
|
|
20
|
-
const files = await
|
|
12
|
+
export async function getProjectThemeDetails(projectSourceDir) {
|
|
13
|
+
const files = await walk(projectSourceDir, ['node_modules']);
|
|
21
14
|
const legacyThemeDetails = [];
|
|
22
15
|
const legacyReactThemeDetails = [];
|
|
23
16
|
files.forEach((filename) => {
|
|
24
17
|
const isLegacyThemeConfig = filename.endsWith(LEGACY_THEME_CONFIG);
|
|
25
18
|
const isLegacyReactThemeConfig = filename.endsWith(LEGACY_REACT_THEME_CONFIG);
|
|
26
19
|
if (isLegacyThemeConfig || isLegacyReactThemeConfig) {
|
|
27
|
-
const parsedConfig =
|
|
20
|
+
const parsedConfig = loadJsonFile(filename);
|
|
28
21
|
const themeDetails = {
|
|
29
22
|
configFilepath: filename,
|
|
30
|
-
themePath:
|
|
23
|
+
themePath: path.dirname(filename),
|
|
31
24
|
themeConfig: parsedConfig,
|
|
32
25
|
};
|
|
33
26
|
if (isLegacyThemeConfig) {
|
|
@@ -44,10 +37,10 @@ async function getProjectThemeDetails(projectSourceDir) {
|
|
|
44
37
|
};
|
|
45
38
|
}
|
|
46
39
|
function buildNewTheme(componentKey, migratedProjectTempDir, themeDetails) {
|
|
47
|
-
const themeName =
|
|
48
|
-
const componentDir =
|
|
49
|
-
const newThemeDir =
|
|
50
|
-
|
|
40
|
+
const themeName = path.basename(themeDetails.themePath);
|
|
41
|
+
const componentDir = path.join(migratedProjectTempDir, componentKey);
|
|
42
|
+
const newThemeDir = path.join(componentDir, themeName);
|
|
43
|
+
fs.cpSync(themeDetails.themePath, newThemeDir, {
|
|
51
44
|
recursive: true,
|
|
52
45
|
});
|
|
53
46
|
const newThemeConfig = {
|
|
@@ -75,20 +68,20 @@ function buildNewTheme(componentKey, migratedProjectTempDir, themeDetails) {
|
|
|
75
68
|
}
|
|
76
69
|
}
|
|
77
70
|
if (updatedLegacyThemeConfig) {
|
|
78
|
-
const newLegacyConfigFilepath =
|
|
79
|
-
|
|
71
|
+
const newLegacyConfigFilepath = path.join(newThemeDir, path.basename(themeDetails.configFilepath));
|
|
72
|
+
fs.writeFileSync(newLegacyConfigFilepath, JSON.stringify(updatedLegacyThemeConfig, null, 2));
|
|
80
73
|
}
|
|
81
|
-
|
|
74
|
+
fs.writeFileSync(path.join(componentDir, `${themeName}${METAFILE_EXTENSION}`), JSON.stringify(newThemeConfig, null, 2));
|
|
82
75
|
}
|
|
83
|
-
async function migrateThemes(projectDir, projectSourceDir) {
|
|
84
|
-
const { legacyThemeDetails, legacyReactThemeDetails
|
|
76
|
+
export async function migrateThemes(projectDir, projectSourceDir) {
|
|
77
|
+
const { legacyThemeDetails, legacyReactThemeDetails } = await getProjectThemeDetails(projectSourceDir);
|
|
85
78
|
// Migrate the project to a temporary directory to avoid file conflicts in the original project
|
|
86
|
-
const migratedProjectTempDir = await mkdtempAsync(
|
|
79
|
+
const migratedProjectTempDir = await mkdtempAsync(path.join(os.tmpdir(), 'hubspot-migrated-project-'));
|
|
87
80
|
let isRootReactThemeProject = false;
|
|
88
81
|
legacyReactThemeDetails.forEach(themeDetails => {
|
|
89
82
|
isRootReactThemeProject = themeDetails.themePath === projectSourceDir;
|
|
90
83
|
if (!isRootReactThemeProject) {
|
|
91
|
-
buildNewTheme(
|
|
84
|
+
buildNewTheme(CMS_ASSETS_KEY, migratedProjectTempDir, themeDetails);
|
|
92
85
|
}
|
|
93
86
|
});
|
|
94
87
|
// Do not support migrating themes that live at the root of the project src directory
|
|
@@ -97,14 +90,14 @@ async function migrateThemes(projectDir, projectSourceDir) {
|
|
|
97
90
|
legacyThemeDetails,
|
|
98
91
|
legacyReactThemeDetails,
|
|
99
92
|
migrated: false,
|
|
100
|
-
failureReason:
|
|
93
|
+
failureReason: errorMessages.migrateThemes.rootReactThemeNotSupported,
|
|
101
94
|
};
|
|
102
95
|
}
|
|
103
96
|
legacyThemeDetails.forEach(themeDetails => {
|
|
104
|
-
buildNewTheme(
|
|
97
|
+
buildNewTheme(THEME_KEY, migratedProjectTempDir, themeDetails);
|
|
105
98
|
});
|
|
106
99
|
// Copy the rest of the project files to the temp directory
|
|
107
|
-
|
|
100
|
+
fs.cpSync(projectSourceDir, migratedProjectTempDir, {
|
|
108
101
|
recursive: true,
|
|
109
102
|
filter: src => {
|
|
110
103
|
return (!legacyThemeDetails.some(file => src.includes(file.themePath)) &&
|
|
@@ -112,13 +105,13 @@ async function migrateThemes(projectDir, projectSourceDir) {
|
|
|
112
105
|
},
|
|
113
106
|
});
|
|
114
107
|
// Archive the legacy theme files
|
|
115
|
-
const archiveDest =
|
|
116
|
-
|
|
108
|
+
const archiveDest = path.join(projectDir, 'archive');
|
|
109
|
+
fs.renameSync(projectSourceDir, archiveDest);
|
|
117
110
|
// Copy the new theme config files to the project source directory
|
|
118
|
-
|
|
111
|
+
fs.cpSync(migratedProjectTempDir, projectSourceDir, {
|
|
119
112
|
recursive: true,
|
|
120
113
|
});
|
|
121
|
-
|
|
114
|
+
fs.rmSync(migratedProjectTempDir, { recursive: true });
|
|
122
115
|
return {
|
|
123
116
|
legacyThemeDetails,
|
|
124
117
|
legacyReactThemeDetails,
|
package/src/lib/profiles.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FileParseResult, HsProfileFile, HSProfileVariables } from './types';
|
|
1
|
+
import { FileParseResult, HsProfileFile, HSProfileVariables } from './types.js';
|
|
2
2
|
export declare function getIsProfileFile(filename: string): boolean;
|
|
3
3
|
export declare function getHsProfileFilename(profileName: string): string;
|
|
4
4
|
export declare function getHsProfileName(profileFilename: string): string;
|
package/src/lib/profiles.js
CHANGED
|
@@ -1,32 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exports.getHsProfileFilename = getHsProfileFilename;
|
|
5
|
-
exports.getHsProfileName = getHsProfileName;
|
|
6
|
-
exports.getHsProfileVariables = getHsProfileVariables;
|
|
7
|
-
exports.validateProfileVariables = validateProfileVariables;
|
|
8
|
-
exports.applyHsProfileVariables = applyHsProfileVariables;
|
|
9
|
-
const constants_1 = require("./constants");
|
|
10
|
-
const copy_1 = require("../lang/copy");
|
|
11
|
-
const profileFilenameRegex = new RegExp(`^${constants_1.profileFilePrefix}\\.([^.]+)\\.json$`);
|
|
1
|
+
import { PROFILE_FILE_PREFIX } from './constants.js';
|
|
2
|
+
import { errorMessages } from '../lang/copy.js';
|
|
3
|
+
const profileFilenameRegex = new RegExp(`^${PROFILE_FILE_PREFIX}\\.([^.]+)\\.json$`);
|
|
12
4
|
const profileInsertRegex = /\${(.*?)}/g;
|
|
13
|
-
function getIsProfileFile(filename) {
|
|
5
|
+
export function getIsProfileFile(filename) {
|
|
14
6
|
return profileFilenameRegex.test(filename);
|
|
15
7
|
}
|
|
16
|
-
function getHsProfileFilename(profileName) {
|
|
17
|
-
return `${
|
|
8
|
+
export function getHsProfileFilename(profileName) {
|
|
9
|
+
return `${PROFILE_FILE_PREFIX}.${profileName}.json`;
|
|
18
10
|
}
|
|
19
|
-
function getHsProfileName(profileFilename) {
|
|
11
|
+
export function getHsProfileName(profileFilename) {
|
|
20
12
|
const match = profileFilename.match(profileFilenameRegex);
|
|
21
13
|
return match ? match[1] : '';
|
|
22
14
|
}
|
|
23
|
-
function getHsProfileVariables(hsProfileContents) {
|
|
15
|
+
export function getHsProfileVariables(hsProfileContents) {
|
|
24
16
|
const profileVariablesAreValid = hsProfileContents?.variables &&
|
|
25
17
|
typeof hsProfileContents.variables === 'object' &&
|
|
26
18
|
!Array.isArray(hsProfileContents.variables);
|
|
27
19
|
return profileVariablesAreValid ? hsProfileContents.variables : {};
|
|
28
20
|
}
|
|
29
|
-
function validateProfileVariables(profileVariables, filename) {
|
|
21
|
+
export function validateProfileVariables(profileVariables, filename) {
|
|
30
22
|
const errors = [];
|
|
31
23
|
Object.entries(profileVariables).forEach(([key, value]) => {
|
|
32
24
|
try {
|
|
@@ -48,10 +40,10 @@ function validateProfileVariables(profileVariables, filename) {
|
|
|
48
40
|
}
|
|
49
41
|
function validateProfileVariable(profileVariableType, key, file) {
|
|
50
42
|
if (profileVariableType === 'undefined') {
|
|
51
|
-
throw new Error(
|
|
43
|
+
throw new Error(errorMessages.profile.missingValue(key, file));
|
|
52
44
|
}
|
|
53
45
|
if (!['string', 'number', 'boolean'].includes(profileVariableType)) {
|
|
54
|
-
throw new Error(
|
|
46
|
+
throw new Error(errorMessages.profile.invalidValue(key));
|
|
55
47
|
}
|
|
56
48
|
}
|
|
57
49
|
function interpolate(file, template, profileVariables) {
|
|
@@ -61,7 +53,7 @@ function interpolate(file, template, profileVariables) {
|
|
|
61
53
|
return String(profileVariables[key]);
|
|
62
54
|
});
|
|
63
55
|
}
|
|
64
|
-
function applyHsProfileVariables(fileParseResults, hsProfileContents) {
|
|
56
|
+
export function applyHsProfileVariables(fileParseResults, hsProfileContents) {
|
|
65
57
|
const profileVariables = getHsProfileVariables(hsProfileContents);
|
|
66
58
|
return fileParseResults.map(fileParseResult => {
|
|
67
59
|
const { content, file } = fileParseResult;
|
package/src/lib/project.js
CHANGED
|
@@ -1,25 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
exports.getProjectMetadata = getProjectMetadata;
|
|
7
|
-
const constants_1 = require("./constants");
|
|
8
|
-
const files_1 = require("./files");
|
|
9
|
-
const path_1 = __importDefault(require("path"));
|
|
10
|
-
const fs_1 = __importDefault(require("fs"));
|
|
11
|
-
async function getProjectMetadata(projectSrcDir) {
|
|
1
|
+
import { Components } from './constants.js';
|
|
2
|
+
import { locateHsMetaFiles } from './files.js';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
export async function getProjectMetadata(projectSrcDir) {
|
|
12
6
|
const hsMetaFiles = [];
|
|
13
7
|
const components = {};
|
|
14
8
|
let metafiles = [];
|
|
15
|
-
if (
|
|
16
|
-
metafiles = await
|
|
9
|
+
if (fs.existsSync(projectSrcDir)) {
|
|
10
|
+
metafiles = await locateHsMetaFiles(projectSrcDir, { silent: true });
|
|
17
11
|
}
|
|
18
|
-
Object.keys(
|
|
19
|
-
const { parentComponent, singularComponent, dir } =
|
|
20
|
-
const componentPath =
|
|
12
|
+
Object.keys(Components).forEach(componentType => {
|
|
13
|
+
const { parentComponent, singularComponent, dir } = Components[componentType];
|
|
14
|
+
const componentPath = path.join(projectSrcDir, parentComponent ? path.join(parentComponent, dir) : dir);
|
|
21
15
|
const metaFilesByType = metafiles
|
|
22
|
-
.filter(metafile =>
|
|
16
|
+
.filter(metafile => path.parse(metafile.file).dir === componentPath)
|
|
23
17
|
.map(metaFile => metaFile.file);
|
|
24
18
|
hsMetaFiles.push(...metaFilesByType);
|
|
25
19
|
components[componentType] = {
|
package/src/lib/schemas.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { AnySchema } from 'ajv
|
|
2
|
-
import { TranslationContext } from './types';
|
|
1
|
+
import type { AnySchema } from 'ajv';
|
|
2
|
+
import { TranslationContext } from './types.js';
|
|
3
3
|
export declare function getIntermediateRepresentationSchema(translationContext: TranslationContext): Promise<Record<string, AnySchema>>;
|
package/src/lib/schemas.js
CHANGED
|
@@ -1,24 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const index_1 = require("@hubspot/local-dev-lib/errors/index");
|
|
6
|
-
const copy_1 = require("../lang/copy");
|
|
7
|
-
async function getIntermediateRepresentationSchema(translationContext) {
|
|
1
|
+
import { http } from '@hubspot/local-dev-lib/http';
|
|
2
|
+
import { isHubSpotHttpError } from '@hubspot/local-dev-lib/errors/index';
|
|
3
|
+
import { errorMessages } from '../lang/copy.js';
|
|
4
|
+
export async function getIntermediateRepresentationSchema(translationContext) {
|
|
8
5
|
if (!translationContext.accountId) {
|
|
9
|
-
throw new Error(
|
|
6
|
+
throw new Error(errorMessages.api.accountIdIsRequiredToFetchSchemas);
|
|
10
7
|
}
|
|
11
8
|
try {
|
|
12
9
|
const { accountId, platformVersion } = translationContext;
|
|
13
|
-
const { data } = await
|
|
10
|
+
const { data } = await http.get(accountId, {
|
|
14
11
|
url: `project-components-external/project-schemas/v3/${platformVersion}`,
|
|
15
12
|
});
|
|
16
13
|
return data;
|
|
17
14
|
}
|
|
18
15
|
catch (e) {
|
|
19
|
-
if (
|
|
16
|
+
if (isHubSpotHttpError(e)) {
|
|
20
17
|
throw e;
|
|
21
18
|
}
|
|
22
|
-
throw new Error(
|
|
19
|
+
throw new Error(errorMessages.api.failedToFetchSchemas, { cause: e });
|
|
23
20
|
}
|
|
24
21
|
}
|
package/src/lib/transform.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Dependencies, FileParseResult, HsProfileFile, IntermediateRepresentation, Transformation, TranslationContext } from './types';
|
|
1
|
+
import { Dependencies, FileParseResult, HsProfileFile, IntermediateRepresentation, Transformation, TranslationContext } from './types.js';
|
|
2
2
|
export declare function mapToInternalType(type: string): string;
|
|
3
3
|
export declare function mapToUserFacingType(type: string): string;
|
|
4
4
|
export declare function mapToUserFriendlyName(internalType: string): string;
|