@hubspot/project-parsing-lib 0.0.9 → 0.0.11-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/package.json +1 -1
- package/src/index.d.ts +1 -0
- package/src/index.js +3 -1
- package/src/lib/migrate.d.ts +1 -0
- package/src/lib/migrate.js +95 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hubspot/project-parsing-lib",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11-experimental.0",
|
|
4
4
|
"description": "Parsing library for converting projects directory structures to their intermediate representation",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "src/index.js",
|
package/src/index.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ export declare function translate(translationContext: TranslationContext, transl
|
|
|
3
3
|
export declare function translateForLocalDev(translationContext: TranslationContext): Promise<IntermediateRepresentationLocalDev>;
|
|
4
4
|
export { isTranslationError } from './lib/errors';
|
|
5
5
|
export { IntermediateRepresentation, IntermediateRepresentationNode, IntermediateRepresentationLocalDev, IntermediateRepresentationNodeLocalDev, TranslationContext, };
|
|
6
|
+
export { migrate } from './lib/migrate';
|
package/src/index.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.isTranslationError = void 0;
|
|
6
|
+
exports.migrate = exports.isTranslationError = void 0;
|
|
7
7
|
exports.translate = translate;
|
|
8
8
|
exports.translateForLocalDev = translateForLocalDev;
|
|
9
9
|
const files_1 = require("./lib/files");
|
|
@@ -48,3 +48,5 @@ async function translateForLocalDev(translationContext) {
|
|
|
48
48
|
}
|
|
49
49
|
var errors_1 = require("./lib/errors");
|
|
50
50
|
Object.defineProperty(exports, "isTranslationError", { enumerable: true, get: function () { return errors_1.isTranslationError; } });
|
|
51
|
+
var migrate_1 = require("./lib/migrate");
|
|
52
|
+
Object.defineProperty(exports, "migrate", { enumerable: true, get: function () { return migrate_1.migrate; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function migrate(inputDir: string, outputDir: string): Promise<void>;
|
|
@@ -0,0 +1,95 @@
|
|
|
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.migrate = migrate;
|
|
7
|
+
const fs_1 = require("@hubspot/local-dev-lib/fs");
|
|
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 IR_FILENAME = 'ir.json';
|
|
13
|
+
const filesDirectory = 'files';
|
|
14
|
+
const hsProjectJsonFilename = 'hsproject.json';
|
|
15
|
+
const defaultSrcDir = 'src';
|
|
16
|
+
async function migrate(inputDir, outputDir) {
|
|
17
|
+
if (!fs_2.default.existsSync(inputDir)) {
|
|
18
|
+
throw new Error(`Input directory ${inputDir} does not exist`);
|
|
19
|
+
}
|
|
20
|
+
const hsProjectJsonPath = path_1.default.join(inputDir, hsProjectJsonFilename);
|
|
21
|
+
if (!fs_2.default.existsSync(hsProjectJsonPath)) {
|
|
22
|
+
throw new Error(`hsproject.json file does not exist in ${inputDir}`);
|
|
23
|
+
}
|
|
24
|
+
const files = await (0, fs_1.walk)(inputDir);
|
|
25
|
+
// Create the output directory if it doesn't exist
|
|
26
|
+
ensureDirExists(outputDir);
|
|
27
|
+
let hsProjectJson;
|
|
28
|
+
try {
|
|
29
|
+
hsProjectJson = loadJsonFile(hsProjectJsonPath);
|
|
30
|
+
}
|
|
31
|
+
catch (e) {
|
|
32
|
+
console.error(`Error loading ${hsProjectJsonPath}`, e);
|
|
33
|
+
throw e;
|
|
34
|
+
}
|
|
35
|
+
// Copy the hsproject.json file to the output directory
|
|
36
|
+
fs_2.default.writeFileSync(path_1.default.join(outputDir, hsProjectJsonFilename), JSON.stringify(hsProjectJson, null, 2));
|
|
37
|
+
files.forEach((filename) => {
|
|
38
|
+
// Skip everything that's not an IR file
|
|
39
|
+
if (!isIRFile(filename)) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const irDirName = path_1.default.dirname(filename);
|
|
43
|
+
const IR = loadJsonFile(filename);
|
|
44
|
+
const { metaFilePath } = IR;
|
|
45
|
+
const projectConfig = convertIRToProjectConfig(IR);
|
|
46
|
+
const fullOutputPath = path_1.default.join(outputDir, getTargetDirectoryFromComponentType(hsProjectJson?.srcDir, projectConfig.type));
|
|
47
|
+
// Ensure the output directory exists
|
|
48
|
+
const currentFilesDirectory = path_1.default.join(irDirName, filesDirectory);
|
|
49
|
+
ensureDirExists(currentFilesDirectory);
|
|
50
|
+
fs_2.default.cpSync(currentFilesDirectory, fullOutputPath, {
|
|
51
|
+
recursive: true,
|
|
52
|
+
});
|
|
53
|
+
// Use the metaFilePath if provided, otherwise use the project UID
|
|
54
|
+
const hsmetaFilePath = path_1.default.join(fullOutputPath, metaFilePath
|
|
55
|
+
? path_1.default.basename(metaFilePath)
|
|
56
|
+
: `${projectConfig.uid}${constants_1.metafileExtension}`);
|
|
57
|
+
// Write the hsmeta file to the output directory
|
|
58
|
+
fs_2.default.writeFileSync(hsmetaFilePath, JSON.stringify(projectConfig, null, 2));
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function ensureDirExists(dir) {
|
|
62
|
+
if (!fs_2.default.existsSync(dir)) {
|
|
63
|
+
fs_2.default.mkdirSync(dir, { recursive: true });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function isIRFile(filename) {
|
|
67
|
+
const { base } = path_1.default.parse(filename);
|
|
68
|
+
return base.toLowerCase() === IR_FILENAME;
|
|
69
|
+
}
|
|
70
|
+
function loadJsonFile(filename) {
|
|
71
|
+
return JSON.parse(fs_2.default.readFileSync(filename, {
|
|
72
|
+
encoding: 'utf-8',
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
75
|
+
function convertIRToProjectConfig(IR) {
|
|
76
|
+
const { config, uid, componentType } = IR;
|
|
77
|
+
return {
|
|
78
|
+
config,
|
|
79
|
+
uid,
|
|
80
|
+
type: (0, transform_1.mapToUserFacingType)(componentType),
|
|
81
|
+
// We are assuming no dependencies for now since this will be a freshly migrated
|
|
82
|
+
// project and the current supported components don't have non-hierarchical dependencies
|
|
83
|
+
dependencies: {},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function getTargetDirectoryFromComponentType(projectSourceDir, componentType) {
|
|
87
|
+
const component = constants_1.Components[componentType];
|
|
88
|
+
if (!component) {
|
|
89
|
+
throw Error('Unsupported component type');
|
|
90
|
+
}
|
|
91
|
+
const parentDirectory = component.parentComponent
|
|
92
|
+
? constants_1.Components[component.parentComponent].dir
|
|
93
|
+
: '';
|
|
94
|
+
return path_1.default.join(projectSourceDir || defaultSrcDir, parentDirectory, component.dir);
|
|
95
|
+
}
|