Skip to content

Commit

Permalink
#83, test importing ts-morph-structures in another project.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvincent committed Mar 21, 2024
1 parent 71d21d8 commit cc2c501
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions stage_2_snapshot/fixtures/import-dist/cmdLine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import driver from "./driver.js";
driver(process.argv[process.argv.length - 1]);
30 changes: 30 additions & 0 deletions stage_2_snapshot/fixtures/import-dist/driver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
ModuleKind,
ModuleResolutionKind,
Project,
ScriptTarget,
} from "ts-morph";

import {
getTypeAugmentedStructure,
VoidTypeNodeToTypeStructureConsole,
} from "ts-morph-structures";

const TSC_CONFIG = {
"compilerOptions": {
"lib": ["es2022"],
"module": ModuleKind.ESNext,
"target": ScriptTarget.ESNext,
"moduleResolution": ModuleResolutionKind.NodeNext,
},
skipAddingFilesFromTsConfig: true,
};
const project = new Project(TSC_CONFIG);

export default function driver(pathToFile) {
const sourceFile = project.addSourceFileAtPath(pathToFile);
const structure = getTypeAugmentedStructure(
sourceFile, VoidTypeNodeToTypeStructureConsole, true
).rootStructure;
process.stdout.write(JSON.stringify(structure) + "\n\n");
}
23 changes: 23 additions & 0 deletions stage_2_snapshot/fixtures/import-dist/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "ts-morph-structures_import-test",
"version": "0.1.0",
"description": "Structure classes for ts-morph, including type structure classes.",
"type": "module",
"engines": {
"node": ">=18.19"
},
"scripts": {
"start": "node ./cmdLine.js"
},
"author": "Alexander J. Vincent <[email protected]>",
"license": "ISC",
"devDependencies": {
"mixin-decorators": "^1.0.1",
"ts-morph": "^22.0.0",
"ts-morph-structures": "__TSM_STRUCTURES__",
"ts-node": "^10.9.2",
"tslib": "^2.6.2",
"type-fest": "^4.12.0",
"typescript": "^5.4.2"
}
}
66 changes: 66 additions & 0 deletions stage_2_snapshot/spec-snapshot/build-checks/import-dist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import fs from "fs/promises";
import path from "path";
import which from "which";
import { exec } from "child_process";
import { promisify } from "util";

import tempDirWithCleanup from "#utilities/source/tempDirWithCleanup.js";

import {
ModuleSourceDirectory,
pathToModule,
projectDir
} from "#utilities/source/AsyncSpecModules.js";

import getTS_SourceFile from "#utilities/source/getTS_SourceFile.js";

import {
getTypeAugmentedStructure,
VoidTypeNodeToTypeStructureConsole,
} from "#stage_two/snapshot/source/exports.js";

const execPromise = promisify(exec);

const stageDir: ModuleSourceDirectory = {
importMeta: import.meta,
pathToDirectory: "../../.."
};

const sourceDir = pathToModule(stageDir, "fixtures/import-dist");
const cleanup = await tempDirWithCleanup();
const targetDir = cleanup.tempDir;

// copy package files
{
await fs.cp(sourceDir, targetDir, { recursive: true });
await fs.cp(path.join(projectDir, "node_modules"), path.join(targetDir, "node_modules"), { recursive: true });
}

const npm = await which("npm");

it("Driver generates a valid set of classes", async () => {
const DefaultMapModule = pathToModule(stageDir, "fixtures/stage_utilities/DefaultMap.ts");
const DefaultMapStructure = getTypeAugmentedStructure(
getTS_SourceFile(stageDir, "fixtures/stage_utilities/DefaultMap.ts"), VoidTypeNodeToTypeStructureConsole, true
).rootStructure;

try {
await execPromise(
npm + " install " + projectDir,
{ cwd: targetDir }
);

const {stdout} = await execPromise(
npm + " start " + DefaultMapModule,
{ cwd: targetDir }
);

expect(
JSON.parse(stdout.substring(stdout.indexOf("{")))
).toEqual(JSON.parse(JSON.stringify(DefaultMapStructure)));
}
finally {
cleanup.resolve(null);
await cleanup.promise;
}
}, 1000 * 30);

0 comments on commit cc2c501

Please sign in to comment.