@ts-dev-tools/core
Version:
TS dev tools Core
71 lines (70 loc) • 3.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPackageNameFromFilepath = void 0;
exports.createTestProject = createTestProject;
exports.createProjectForTestFile = createProjectForTestFile;
exports.deleteTestProject = deleteTestProject;
const node_fs_1 = require("node:fs");
const node_os_1 = require("node:os");
const node_path_1 = require("node:path");
const file_system_1 = require("./file-system");
const test_cache_1 = require("./test-cache");
const rootDirPath = (0, node_path_1.resolve)(__dirname, "../../../..");
const corePackageDirPath = (0, node_path_1.resolve)(__dirname, "..", "..");
const testProjectDir = (0, node_path_1.resolve)("__tests__/test-project");
const defaultPackageJsonPath = (0, node_path_1.join)(testProjectDir, "package.json");
const getPackageNameFromFilepath = (filepath) => {
const relativeFilepath = (0, node_path_1.relative)(rootDirPath, filepath);
const parts = relativeFilepath.split("/");
if (parts.length < 2) {
throw new Error(`Invalid filepath: ${filepath}`);
}
const packageName = parts[1];
if (!packageName) {
throw new Error(`Package name could not be determined from the filepath: ${filepath}`);
}
return packageName;
};
exports.getPackageNameFromFilepath = getPackageNameFromFilepath;
const getTestProjectDirPath = (filename) => {
const testProjectRootDirPath = (0, node_path_1.join)((0, node_os_1.tmpdir)(), "ts-dev-tools");
const relativeFilepath = (0, node_path_1.relative)(rootDirPath, filename);
const testProjectDirName = `test-${relativeFilepath.toLowerCase().replace(/[^a-z0-9]/g, "-")}`;
return (0, node_path_1.join)(testProjectRootDirPath, testProjectDirName);
};
async function defaultProjectGenerator(testProjectDirPath) {
// Fake node_modules
const corePackageRootPath = (0, node_path_1.join)(testProjectDirPath, "node_modules/@ts-dev-tools/core");
await (0, file_system_1.recreateFolderRecursive)(corePackageRootPath);
(0, node_fs_1.copyFileSync)((0, node_path_1.join)(corePackageDirPath, "package.json"), (0, node_path_1.join)(corePackageRootPath, "package.json"));
// Fake migrations
const tsDevToolsDistPath = (0, node_path_1.join)(corePackageRootPath, "dist");
(0, node_fs_1.symlinkSync)((0, node_path_1.resolve)(__dirname, ".."), tsDevToolsDistPath);
(0, node_fs_1.copyFileSync)(defaultPackageJsonPath, (0, node_path_1.join)(testProjectDirPath, "package.json"));
}
async function createTestProject(packageName, testDirPath, useCache, testProjectGenerator = defaultProjectGenerator) {
await (0, file_system_1.recreateFolderRecursive)(testDirPath);
const cacheName = testProjectGenerator.name;
const testCacheDirPath = (0, test_cache_1.getTestCacheDirPath)(packageName, cacheName);
if (useCache && (0, test_cache_1.testCacheDirExists)(packageName, cacheName)) {
await (0, file_system_1.copyFolder)(testCacheDirPath, testDirPath);
return testDirPath;
}
await testProjectGenerator(testDirPath);
// Add mandatory project fixtures
const gitHooksDirPath = (0, node_path_1.join)(testDirPath, ".git/hooks");
await (0, file_system_1.recreateFolderRecursive)(gitHooksDirPath);
if (useCache) {
await (0, file_system_1.copyFolder)(testDirPath, testCacheDirPath);
}
return testDirPath;
}
async function createProjectForTestFile(filepath, useCache, testProjectGenerator = defaultProjectGenerator) {
const testDirPath = getTestProjectDirPath(filepath);
const packageName = (0, exports.getPackageNameFromFilepath)(filepath);
return createTestProject(packageName, testDirPath, useCache, testProjectGenerator);
}
async function deleteTestProject(filepath) {
const testProjectDirPath = getTestProjectDirPath(filepath);
await (0, file_system_1.deleteFolderRecursive)(testProjectDirPath);
}