diff --git a/src/bin.ts b/src/bin.ts index ad938fb57..9ec856798 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -4,18 +4,13 @@ import { join, resolve, dirname } from 'path' import { inspect } from 'util' import Module = require('module') import arg = require('arg') -import { - parse, - register, - TSError, - VERSION -} from './index' import { EVAL_FILENAME, EvalState, createRepl, ReplService } from './repl' +import { VERSION, TSError, parse, register } from './index' /** * Main `bin` functionality. diff --git a/src/index.spec.ts b/src/index.spec.ts index 0f6232c69..0f7e0f908 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -672,7 +672,7 @@ describe('ts-node', function () { }) describe('register', function () { - let registered: tsNodeTypes.Register + let registered: tsNodeTypes.Service let moduleTestPath: string before(() => { registered = register({ @@ -825,7 +825,7 @@ describe('ts-node', function () { }) describe('create', () => { - let service: tsNodeTypes.Register + let service: tsNodeTypes.Service before(() => { service = create({ compilerOptions: { target: 'es5' }, skipProject: true }) }) @@ -852,7 +852,7 @@ describe('ts-node', function () { }) describe('issue #1098', () => { - function testIgnored (ignored: tsNodeTypes.Register['ignored'], allowed: string[], disallowed: string[]) { + function testIgnored (ignored: tsNodeTypes.Service['ignored'], allowed: string[], disallowed: string[]) { for (const ext of allowed) { expect(ignored(join(__dirname, `index${ext}`))).equal(false, `should accept ${ext} files`) } diff --git a/src/index.ts b/src/index.ts index 5eb2ab42d..47b986374 100644 --- a/src/index.ts +++ b/src/index.ts @@ -42,7 +42,7 @@ export const REGISTER_INSTANCE = Symbol.for('ts-node.register.instance') declare global { namespace NodeJS { interface Process { - [REGISTER_INSTANCE]?: Register + [REGISTER_INSTANCE]?: Service } } } @@ -356,9 +356,9 @@ export class TSError extends BaseError { } /** - * Return type for registering `ts-node`. + * Primary ts-node service, which wraps the TypeScript API and can compile TypeScript to JavaScript */ -export interface Register { +export interface Service { ts: TSCommon config: _ts.ParsedCommandLine options: RegisterOptions @@ -368,6 +368,13 @@ export interface Register { getTypeInfo (code: string, fileName: string, position: number): TypeInfo } +/** + * Re-export of `Service` interface for backwards-compatibility + * @deprecated use `Service` instead + * @see Service + */ +export type Register = Service + /** * Cached fs operation wrapper. */ @@ -398,7 +405,7 @@ export function getExtensions (config: _ts.ParsedCommandLine) { /** * Register TypeScript compiler instance onto node.js */ -export function register (opts: RegisterOptions = {}): Register { +export function register (opts: RegisterOptions = {}): Service { const originalJsHandler = require.extensions['.js'] // tslint:disable-line const service = create(opts) const { tsExtensions, jsExtensions } = getExtensions(service.config) @@ -419,7 +426,7 @@ export function register (opts: RegisterOptions = {}): Register { /** * Create TypeScript compiler instance. */ -export function create (rawOptions: CreateOptions = {}): Register { +export function create (rawOptions: CreateOptions = {}): Service { const dir = rawOptions.dir ?? DEFAULTS.dir const compilerName = rawOptions.compiler ?? DEFAULTS.compiler const cwd = dir ? resolve(dir) : process.cwd() @@ -1008,12 +1015,12 @@ function reorderRequireExtension (ext: string) { function registerExtensions ( preferTsExts: boolean | null | undefined, extensions: string[], - register: Register, + service: Service, originalJsHandler: (m: NodeModule, filename: string) => any ) { // Register new extensions. for (const ext of extensions) { - registerExtension(ext, register, originalJsHandler) + registerExtension(ext, service, originalJsHandler) } if (preferTsExts) { @@ -1029,15 +1036,15 @@ function registerExtensions ( */ function registerExtension ( ext: string, - register: Register, + service: Service, originalHandler: (m: NodeModule, filename: string) => any ) { const old = require.extensions[ext] || originalHandler // tslint:disable-line require.extensions[ext] = function (m: any, filename) { // tslint:disable-line - if (register.ignored(filename)) return old(m, filename) + if (service.ignored(filename)) return old(m, filename) - if (register.options.experimentalEsmLoader) { + if (service.options.experimentalEsmLoader) { assertScriptCanLoadAsCJS(filename) } @@ -1046,7 +1053,7 @@ function registerExtension ( m._compile = function (code: string, fileName: string) { debug('module._compile', fileName) - return _compile.call(this, register.compile(code, fileName), fileName) + return _compile.call(this, service.compile(code, fileName), fileName) } return old(m, filename) diff --git a/src/repl.ts b/src/repl.ts index 3cadb91b1..122abbf51 100644 --- a/src/repl.ts +++ b/src/repl.ts @@ -3,7 +3,7 @@ import { homedir } from 'os' import { join } from 'path' import { Recoverable, start } from 'repl' import { Script } from 'vm' -import { Register, CreateOptions, TSError } from './index' +import { Service, CreateOptions, TSError } from './index' import { readFileSync, statSync } from 'fs' import { Console } from 'console' import * as tty from 'tty' @@ -19,7 +19,7 @@ export interface ReplService { /** * Bind this REPL to a ts-node compiler service. A compiler service must be bound before `eval`-ing code or starting the REPL */ - setService (service: Register): void + setService (service: Service): void evalCode (code: string): void /** * `eval` implementation compatible with node's REPL API @@ -39,7 +39,7 @@ export interface ReplService { } export interface CreateReplOptions { - service?: Register + service?: Service state?: EvalState stdin?: NodeJS.ReadableStream stdout?: NodeJS.WritableStream @@ -69,7 +69,7 @@ export function createRepl (options: CreateReplOptions = {}) { } return replService - function setService (_service: Register) { + function setService (_service: Service) { service = _service } @@ -160,7 +160,7 @@ export function createEvalAwarePartialHost (state: EvalState): EvalAwarePartialH /** * Evaluate the code snippet. */ -function _eval (service: Register, state: EvalState, input: string) { +function _eval (service: Service, state: EvalState, input: string) { const lines = state.lines const isCompletion = !/\n$/.test(input) const undo = appendEval(state, input) @@ -199,7 +199,7 @@ function exec (code: string, filename: string) { /** * Start a CLI REPL. */ -function startRepl (replService: ReplService, service: Register, state: EvalState, code?: string) { +function startRepl (replService: ReplService, service: Service, state: EvalState, code?: string) { // Eval incoming code before the REPL starts. if (code) { try {