Skip to content

Commit c52ac9b

Browse files
committed
fix(context): run transpiled code in correct way
1 parent 12afccb commit c52ac9b

File tree

5 files changed

+27
-77
lines changed

5 files changed

+27
-77
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"build": "rimraf ./lib && tsc -p tsconfig.lib.json && cp package.json README.md LICENSE ./lib",
8-
"test": "exit 0"
8+
"test": "exit 0",
9+
"prepare": "mkdir -p node_modules/typeshot && echo \"module.exports = require('../../src')\" > node_modules/typeshot/index.js"
910
},
1011
"repository": {
1112
"type": "git",

sample/readme/generated/sample.ts

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,24 @@ console.log('Start Loading Generated File!');
55

66
// typeshot-start
77
// You can write comments here.
8-
export type SampleType = {
9-
foo: string;
10-
bar: {
11-
baz: number;
12-
qux: Date;
13-
};
14-
};
8+
export type SampleType = { foo: string; bar: { baz: number; qux: Date } };
159
export type SampleTypeArray = SampleType[];
1610
export const SampleType__sample = { foo: 'foo', bar: { baz: 0, qux: new Date() } } as any;
1711

18-
export type FooAlias = {
19-
foo: {
20-
type: 'foo';
21-
};
22-
};
12+
export type FooAlias = { foo: { type: 'foo' } };
2313
export interface FooInterface {
24-
foo: {
25-
type: 'foo';
26-
};
14+
foo: { type: 'foo' };
2715
}
2816
export type Foo = {
29-
fooo: {
30-
foo: {
31-
type: 'foo';
32-
};
33-
};
17+
fooo: { foo: { type: 'foo' } };
3418
};
3519

36-
export type BarAlias = {
37-
bar: {
38-
type: 'bar';
39-
};
40-
};
20+
export type BarAlias = { bar: { type: 'bar' } };
4121
export interface BarInterface {
42-
bar: {
43-
type: 'bar';
44-
};
22+
bar: { type: 'bar' };
4523
}
4624
export type Bar = {
47-
fooo: {
48-
bar: {
49-
type: 'bar';
50-
};
51-
};
25+
fooo: { bar: { type: 'bar' } };
5226
};
5327

5428
// typeshot-end

sample/test.generated.ts

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,21 @@ export const a = import('./readme/another-file');
1111

1212
// typeshot-start
1313
export interface Hoge {
14-
hoge: {
15-
name: 'hoge';
16-
value: SomeType;
17-
};
18-
fuga: {
19-
name: 'fuga';
20-
value: SomeInterface;
21-
};
22-
prettier: {
23-
name: 'prettier';
24-
value: Options;
25-
};
14+
hoge: { name: 'hoge'; value: SomeType };
15+
fuga: { name: 'fuga'; value: SomeInterface };
16+
prettier: { name: 'prettier'; value: Options };
2617
}
2718

2819
// hogehoge
2920

3021
// Sample
3122

32-
export type Sample = {
33-
hoge: {
34-
name: 'hoge';
35-
value: SomeType;
36-
};
37-
};
38-
export type _Sample = {
39-
name: 'hoge';
40-
value: SomeType;
41-
};
23+
export type Sample = { hoge: { name: 'hoge'; value: SomeType } };
24+
export type _Sample = { name: 'hoge'; value: SomeType };
4225

4326
// fuga
4427

45-
export type Sample0 = {
46-
fuga: {
47-
name: 'fuga';
48-
value: SomeInterface;
49-
};
50-
};
51-
export type _Sample0 = {
52-
name: 'fuga';
53-
value: SomeInterface;
54-
};
28+
export type Sample0 = { fuga: { name: 'fuga'; value: SomeInterface } };
29+
export type _Sample0 = { name: 'fuga'; value: SomeInterface };
5530

5631
export {};

sample/test.typeshot.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type MappedType<T extends Record<string, any>> = {
2323
[K in keyof T]: { name: K; value: T[K] };
2424
};
2525

26+
require('prettier');
2627
const Hoge = typeshot.createType<MappedType<SomeTypeMap>>([])({}).interface('Hoge');
2728
typeshot.print`
2829
export ${Hoge}

src/context.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ts from 'typescript';
2-
import vm from 'vm';
2+
import path from 'path';
33
import { Module } from 'module';
44
import type { Config, TypeToken } from './typeshot';
55

@@ -26,6 +26,7 @@ const CURRENT_TYPESHOT_CONTEXT = { current: null as null | TypeshotContext };
2626

2727
export const getCurrentContext = () => CURRENT_TYPESHOT_CONTEXT.current;
2828

29+
const parent = module;
2930
const runSourceWithContext = (
3031
source: ts.SourceFile,
3132
sourceText: string,
@@ -34,17 +35,15 @@ const runSourceWithContext = (
3435
): TypeshotContext => {
3536
const prev = CURRENT_TYPESHOT_CONTEXT.current;
3637
try {
37-
const executableCode = ts.transpile(sourceText, options);
38-
const { fileName } = source;
39-
40-
const MODULE = new Module(fileName, module);
41-
const pure = MODULE.require;
42-
MODULE.filename = fileName;
43-
MODULE.require = ((id) => (id === 'typeshot' ? require('./typeshot') : pure.call(MODULE, id))) as typeof require;
44-
Object.assign(MODULE.require, pure);
45-
38+
const { fileName: filename } = source;
39+
const script = ts.transpile(sourceText, options);
4640
CURRENT_TYPESHOT_CONTEXT.current = context;
47-
vm.runInNewContext(executableCode, MODULE);
41+
42+
// https://github.com/nodejs/node/blob/6add5b31fcc4ae45a8603f886477c544a99e0188/lib/internal/bootstrap_node.js#L415
43+
const module = new Module(filename, parent);
44+
module.filename = filename;
45+
module.paths = (Module as any)._nodeModulePaths(path.dirname(filename));
46+
(module as any)._compile(script, filename);
4847
} catch (e) {
4948
// eslint-disable-next-line no-console
5049
console.log(e);

0 commit comments

Comments
 (0)