Skip to content

Commit

Permalink
Rename Register to Service; re-export as Register for backwards… (
Browse files Browse the repository at this point in the history
#1158)

* Rename `Register` to `Service`; re-export as `Register` for backwards compatibility

* rename variable 'register' to 'service'
  • Loading branch information
cspotcode authored Dec 3, 2020
1 parent c4a6a02 commit 286c294
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
8 changes: 4 additions & 4 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { diffLines } from 'diff'
import { Script } from 'vm'
import { readFileSync, statSync, realpathSync } from 'fs'
import { homedir } from 'os'
import { VERSION, TSError, parse, Register, register } from './index'
import { VERSION, TSError, parse, Service, register } from './index'

/**
* Eval filename for REPL/debug.
Expand Down Expand Up @@ -284,7 +284,7 @@ function getCwd (dir?: string, scriptMode?: boolean, scriptPath?: string) {
/**
* Evaluate a script.
*/
function evalAndExit (service: Register, state: EvalState, module: Module, code: string, isPrinted: boolean) {
function evalAndExit (service: Service, state: EvalState, module: Module, code: string, isPrinted: boolean) {
let result: any

;(global as any).__filename = module.filename
Expand Down Expand Up @@ -312,7 +312,7 @@ function evalAndExit (service: Register, state: EvalState, module: Module, code:
/**
* 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)
Expand Down Expand Up @@ -351,7 +351,7 @@ function exec (code: string, filename: string) {
/**
* Start a CLI REPL.
*/
function startRepl (service: Register, state: EvalState, code?: string) {
function startRepl (service: Service, state: EvalState, code?: string) {
// Eval incoming code before the REPL starts.
if (code) {
try {
Expand Down
6 changes: 3 additions & 3 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ describe('ts-node', function () {
})

describe('register', function () {
let registered: tsNodeTypes.Register
let registered: tsNodeTypes.Service
let moduleTestPath: string
before(() => {
registered = register({
Expand Down Expand Up @@ -797,7 +797,7 @@ describe('ts-node', function () {
})

describe('create', () => {
let service: tsNodeTypes.Register
let service: tsNodeTypes.Service
before(() => {
service = create({ compilerOptions: { target: 'es5' }, skipProject: true })
})
Expand All @@ -824,7 +824,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`)
}
Expand Down
29 changes: 18 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const REGISTER_INSTANCE = Symbol.for('ts-node.register.instance')
declare global {
namespace NodeJS {
interface Process {
[REGISTER_INSTANCE]?: Register
[REGISTER_INSTANCE]?: Service
}
}
}
Expand Down Expand Up @@ -354,9 +354,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
Expand All @@ -366,6 +366,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.
*/
Expand Down Expand Up @@ -396,7 +403,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)
Expand All @@ -417,7 +424,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()
Expand Down Expand Up @@ -1006,12 +1013,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) {
Expand All @@ -1027,15 +1034,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)
}

Expand All @@ -1044,7 +1051,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)
Expand Down

0 comments on commit 286c294

Please sign in to comment.