Skip to content

Commit

Permalink
Support registering scoped compiler instances
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Nov 8, 2019
1 parent d6208d0 commit c1c22f8
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ _Environment variable denoted in parentheses._
* `-D, --ignore-diagnostics [code]` Ignore TypeScript warnings by diagnostic code (`TS_NODE_IGNORE_DIAGNOSTICS`)
* `-O, --compiler-options [opts]` JSON object to merge with compiler options (`TS_NODE_COMPILER_OPTIONS`)
* `--cwd` Specify working directory for config resolution (`TS_NODE_CWD`, default: `process.cwd()`)
* `--scope` Scope compiler to files within `cwd` (`TS_NODE_SCOPE`, default: `false`)
* `--files` Load files from `tsconfig.json` on startup (`TS_NODE_FILES`, default: `false`)
* `--pretty` Use pretty diagnostic formatter (`TS_NODE_PRETTY`, default: `false`)
* `--skip-project` Skip project config resolution and loading (`TS_NODE_SKIP_PROJECT`, default: `false`)
Expand Down
5 changes: 5 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const args = arg({
'--require': [String],

// CLI options.
'--scope': Boolean,
'--files': Boolean,
'--help': Boolean,
'--version': arg.COUNT,
Expand All @@ -42,6 +43,7 @@ const args = arg({
'-i': '--interactive',
'-p': '--print',
'-r': '--require',
'-s': '--scope',
'-h': '--help',
'-v': '--version',
'-T': '--transpile-only',
Expand All @@ -56,6 +58,7 @@ const args = arg({

const {
'--cwd': cwd = DEFAULTS.cwd || process.cwd(),
'--scope': scope = DEFAULTS.scope,
'--help': help = false,
'--version': version = 0,
'--files': files = DEFAULTS.files,
Expand Down Expand Up @@ -95,6 +98,7 @@ Options:
-O, --compiler-options [opts] JSON object to merge with compiler options
--cwd Specify working directory for config resolution
--scope Scope compiler to files within \`cwd\`
--files Load files from \`tsconfig.json\` on startup
--pretty Use pretty diagnostic formatter
--skip-project Skip reading \`tsconfig.json\`
Expand Down Expand Up @@ -125,6 +129,7 @@ const EVAL_INSTANCE = { input: '', output: '', version: 0, lines: 0 }
// Register the TypeScript compiler instance.
const service = register({
cwd,
scope,
files,
pretty,
typeCheck,
Expand Down
34 changes: 34 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,40 @@ describe('ts-node', function () {
expect(() => require(path)).to.not.throw()
})

it('should support compiler scopes', function () {
const calls: string[] = []

registered.enabled(false)

const compilers = [
register({ cwd: join(TEST_DIR, 'scope/a'), scope: true }),
register({ cwd: join(TEST_DIR, 'scope/b'), scope: true })
]

compilers.forEach(c => {
const old = c.compile
c.compile = (code, fileName, lineOffset) => {
calls.push(fileName)

return old(code, fileName, lineOffset)
}
})

try {
expect(require('../tests/scope/a').ext).to.equal('.ts')
expect(require('../tests/scope/b').ext).to.equal('.ts')
} finally {
compilers.forEach(c => c.enabled(false))
}

expect(calls).to.deep.equal([
join(TEST_DIR, 'scope/a/index.ts'),
join(TEST_DIR, 'scope/b/index.ts')
])

expect(() => require('../tests/module')).to.throw()
})

it('should compile through js and ts', function () {
const m = require('../tests/complex')

Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const VERSION = require('../package.json').version
*/
export interface Options {
cwd?: string | null
scope?: boolean | null
pretty?: boolean | null
typeCheck?: boolean | null
transpileOnly?: boolean | null
Expand Down Expand Up @@ -124,6 +125,7 @@ export interface TypeInfo {
*/
export const DEFAULTS: Options = {
cwd: process.env.TS_NODE_CWD,
scope: yn(process.env.TS_NODE_SCOPE),
files: yn(process.env['TS_NODE_FILES']),
pretty: yn(process.env['TS_NODE_PRETTY']),
compiler: process.env['TS_NODE_COMPILER'],
Expand Down Expand Up @@ -238,7 +240,8 @@ export function register (opts: Options = {}): Register {
).map(str => new RegExp(str))

// Require the TypeScript compiler and configuration.
const cwd = options.cwd || process.cwd()
const cwd = options.cwd ? resolve(options.cwd) : process.cwd()
const isScoped = options.scope ? (fileName: string) => relative(cwd, fileName).charAt(0) !== '.' : () => true
const typeCheck = options.typeCheck === true || options.transpileOnly !== true
const compiler = require.resolve(options.compiler || 'typescript', { paths: [cwd, __dirname] })
const ts: typeof _ts = require(compiler)
Expand Down Expand Up @@ -448,10 +451,10 @@ export function register (opts: Options = {}): Register {

let active = true
const enabled = (enabled?: boolean) => enabled === undefined ? active : (active = !!enabled)
const ignored = (fileName: string) => !active || shouldIgnore(fileName, ignore)
const ignored = (fileName: string) => !active || !isScoped(fileName) || shouldIgnore(fileName, ignore)
const register: Register = { cwd, compile, getTypeInfo, extensions, ts, ignored, enabled }

// Expose registered instance globally
// Expose registered instance globally.
process[REGISTER_INSTANCE] = { config }

// Register the extensions.
Expand Down
3 changes: 3 additions & 0 deletions tests/scope/a/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import path from 'path'

export const ext = path.extname(__filename)
5 changes: 5 additions & 0 deletions tests/scope/a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"esModuleInterop": true
}
}
3 changes: 3 additions & 0 deletions tests/scope/b/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import path from 'path'

export const ext = path.extname(__filename)
5 changes: 5 additions & 0 deletions tests/scope/b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"esModuleInterop": true
}
}

0 comments on commit c1c22f8

Please sign in to comment.