Skip to content

Commit

Permalink
fix(context): run transpiled code in correct way
Browse files Browse the repository at this point in the history
  • Loading branch information
whatasoda committed May 25, 2020
1 parent 12afccb commit c52ac9b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 77 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"build": "rimraf ./lib && tsc -p tsconfig.lib.json && cp package.json README.md LICENSE ./lib",
"test": "exit 0"
"test": "exit 0",
"prepare": "mkdir -p node_modules/typeshot && echo \"module.exports = require('../../src')\" > node_modules/typeshot/index.js"
},
"repository": {
"type": "git",
Expand Down
40 changes: 7 additions & 33 deletions sample/readme/generated/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,24 @@ console.log('Start Loading Generated File!');

// typeshot-start
// You can write comments here.
export type SampleType = {
foo: string;
bar: {
baz: number;
qux: Date;
};
};
export type SampleType = { foo: string; bar: { baz: number; qux: Date } };
export type SampleTypeArray = SampleType[];
export const SampleType__sample = { foo: 'foo', bar: { baz: 0, qux: new Date() } } as any;

export type FooAlias = {
foo: {
type: 'foo';
};
};
export type FooAlias = { foo: { type: 'foo' } };
export interface FooInterface {
foo: {
type: 'foo';
};
foo: { type: 'foo' };
}
export type Foo = {
fooo: {
foo: {
type: 'foo';
};
};
fooo: { foo: { type: 'foo' } };
};

export type BarAlias = {
bar: {
type: 'bar';
};
};
export type BarAlias = { bar: { type: 'bar' } };
export interface BarInterface {
bar: {
type: 'bar';
};
bar: { type: 'bar' };
}
export type Bar = {
fooo: {
bar: {
type: 'bar';
};
};
fooo: { bar: { type: 'bar' } };
};

// typeshot-end
Expand Down
39 changes: 7 additions & 32 deletions sample/test.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,21 @@ export const a = import('./readme/another-file');

// typeshot-start
export interface Hoge {
hoge: {
name: 'hoge';
value: SomeType;
};
fuga: {
name: 'fuga';
value: SomeInterface;
};
prettier: {
name: 'prettier';
value: Options;
};
hoge: { name: 'hoge'; value: SomeType };
fuga: { name: 'fuga'; value: SomeInterface };
prettier: { name: 'prettier'; value: Options };
}

// hogehoge

// Sample

export type Sample = {
hoge: {
name: 'hoge';
value: SomeType;
};
};
export type _Sample = {
name: 'hoge';
value: SomeType;
};
export type Sample = { hoge: { name: 'hoge'; value: SomeType } };
export type _Sample = { name: 'hoge'; value: SomeType };

// fuga

export type Sample0 = {
fuga: {
name: 'fuga';
value: SomeInterface;
};
};
export type _Sample0 = {
name: 'fuga';
value: SomeInterface;
};
export type Sample0 = { fuga: { name: 'fuga'; value: SomeInterface } };
export type _Sample0 = { name: 'fuga'; value: SomeInterface };

export {};
1 change: 1 addition & 0 deletions sample/test.typeshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type MappedType<T extends Record<string, any>> = {
[K in keyof T]: { name: K; value: T[K] };
};

require('prettier');
const Hoge = typeshot.createType<MappedType<SomeTypeMap>>([])({}).interface('Hoge');
typeshot.print`
export ${Hoge}
Expand Down
21 changes: 10 additions & 11 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ts from 'typescript';
import vm from 'vm';
import path from 'path';
import { Module } from 'module';
import type { Config, TypeToken } from './typeshot';

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

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

const parent = module;
const runSourceWithContext = (
source: ts.SourceFile,
sourceText: string,
Expand All @@ -34,17 +35,15 @@ const runSourceWithContext = (
): TypeshotContext => {
const prev = CURRENT_TYPESHOT_CONTEXT.current;
try {
const executableCode = ts.transpile(sourceText, options);
const { fileName } = source;

const MODULE = new Module(fileName, module);
const pure = MODULE.require;
MODULE.filename = fileName;
MODULE.require = ((id) => (id === 'typeshot' ? require('./typeshot') : pure.call(MODULE, id))) as typeof require;
Object.assign(MODULE.require, pure);

const { fileName: filename } = source;
const script = ts.transpile(sourceText, options);
CURRENT_TYPESHOT_CONTEXT.current = context;
vm.runInNewContext(executableCode, MODULE);

// https://github.com/nodejs/node/blob/6add5b31fcc4ae45a8603f886477c544a99e0188/lib/internal/bootstrap_node.js#L415
const module = new Module(filename, parent);
module.filename = filename;
module.paths = (Module as any)._nodeModulePaths(path.dirname(filename));
(module as any)._compile(script, filename);
} catch (e) {
// eslint-disable-next-line no-console
console.log(e);
Expand Down

0 comments on commit c52ac9b

Please sign in to comment.