Comparing version 1.3.0 to 1.4.0
@@ -6,3 +6,2 @@ "use strict"; | ||
var arrify = require('arrify'); | ||
var extend = require('xtend'); | ||
var Module = require('module'); | ||
@@ -14,3 +13,3 @@ var minimist = require('minimist'); | ||
var index_1 = require('./index'); | ||
var strings = ['eval', 'print', 'compiler', 'project', 'ignoreWarnings', 'require', 'cacheDirectory']; | ||
var strings = ['eval', 'print', 'compiler', 'project', 'ignoreWarnings', 'require', 'cacheDirectory', 'ignore']; | ||
var booleans = ['help', 'fast', 'lazy', 'version', 'disableWarnings', 'cache']; | ||
@@ -35,3 +34,3 @@ var aliases = { | ||
var name = arg.replace(/^--?/, ''); | ||
if (/=/.test(name)) { | ||
if (/=/.test(name) || /^--no-/.test(arg)) { | ||
return true; | ||
@@ -85,3 +84,3 @@ } | ||
if (argv.help) { | ||
console.log("\nUsage: ts-node [options] [ -e script | script.ts ] [arguments]\n\nOptions:\n\n -e, --eval [code] Evaluate code\n -p, --print [code] Evaluate code and print result\n -r, --require [path] Require a node module for execution\n -C, --compiler [name] Specify a custom TypeScript compiler\n -I, --ignoreWarnings [code] Ignore TypeScript warnings by diagnostic code\n -D, --disableWarnings Ignore every TypeScript warning\n -P, --project [path] Path to TypeScript project (or `false`)\n -O, --compilerOptions [opts] JSON compiler options to merge with compilation\n -L, --lazy Lazily load TypeScript compilation\n -F, --fast Run TypeScript compilation in transpile mode\n --no-cache Disable the TypeScript cache\n --cache-directory Configure the TypeScript cache directory\n"); | ||
console.log("\nUsage: ts-node [options] [ -e script | script.ts ] [arguments]\n\nOptions:\n\n -e, --eval [code] Evaluate code\n -p, --print [code] Evaluate code and print result\n -r, --require [path] Require a node module for execution\n -C, --compiler [name] Specify a custom TypeScript compiler\n -I, --ignoreWarnings [code] Ignore TypeScript warnings by diagnostic code\n -D, --disableWarnings Ignore every TypeScript warning\n -P, --project [path] Path to TypeScript project (or `false`)\n -O, --compilerOptions [opts] JSON object to merge with compiler options\n -L, --lazy Lazily load TypeScript compilation on demand\n -F, --fast Run TypeScript compilation in transpile mode\n --ignore [regexp], --no-ignore Set the ignore check (default: `/node_modules/`)\n --no-cache Disable the TypeScript cache\n --cache-directory Configure the TypeScript cache directory\n"); | ||
process.exit(0); | ||
@@ -101,7 +100,16 @@ } | ||
var isPrinted = argv.print != null; | ||
var service = index_1.register(extend(argv, { | ||
var service = index_1.register({ | ||
fast: argv.fast, | ||
lazy: argv.lazy, | ||
cache: argv.cache, | ||
cacheDirectory: argv.cacheDirectory, | ||
compiler: argv.compiler, | ||
project: argv.project, | ||
ignore: typeof argv.ignore === 'boolean' ? argv.ignore : arrify(argv.ignore), | ||
ignoreWarnings: arrify(argv.ignoreWarnings), | ||
disableWarnings: argv.disableWarnings, | ||
compilerOptions: argv.compilerOptions, | ||
getFile: isEval ? getFileEval : index_1.getFile, | ||
fileExists: isEval ? fileExistsEval : index_1.fileExists, | ||
ignoreWarnings: arrify(argv.ignoreWarnings) | ||
})); | ||
fileExists: isEval ? fileExistsEval : index_1.fileExists | ||
}); | ||
var EVAL_FILENAME = '[eval].ts'; | ||
@@ -108,0 +116,0 @@ var EVAL_PATH = path_1.join(cwd, EVAL_FILENAME); |
@@ -23,10 +23,11 @@ import { BaseError } from 'make-error'; | ||
export interface Options { | ||
fast?: boolean; | ||
lazy?: boolean; | ||
cache?: boolean; | ||
fast?: boolean | null; | ||
lazy?: boolean | null; | ||
cache?: boolean | null; | ||
cacheDirectory?: string; | ||
compiler?: string; | ||
project?: string; | ||
project?: boolean | string; | ||
ignore?: boolean | string[]; | ||
ignoreWarnings?: Array<number | string>; | ||
disableWarnings?: boolean; | ||
disableWarnings?: boolean | null; | ||
getFile?: (fileName: string) => string; | ||
@@ -45,3 +46,3 @@ fileExists?: (fileName: string) => boolean; | ||
} | ||
export declare function register(opts?: Options): () => Register; | ||
export declare function register(options?: Options): () => Register; | ||
export declare function fileExists(fileName: string): boolean; | ||
@@ -48,0 +49,0 @@ export declare function getDirectories(path: string): string[]; |
@@ -12,5 +12,5 @@ "use strict"; | ||
var extend = require('xtend'); | ||
var arrify = require('arrify'); | ||
var mkdirp = require('mkdirp'); | ||
var crypto = require('crypto'); | ||
var yn = require('yn'); | ||
var make_error_1 = require('make-error'); | ||
@@ -20,31 +20,49 @@ var tsconfig_1 = require('tsconfig'); | ||
exports.VERSION = pkg.version; | ||
var DEFAULT_OPTIONS = { | ||
var DEFAULTS = { | ||
getFile: getFile, | ||
fileExists: fileExists, | ||
cache: process.env.TS_NODE_CACHE, | ||
cacheDirectory: process.env.TS_NODE_CACHE_DIRECTORY || path_1.join(os_1.tmpdir(), 'ts-node'), | ||
disableWarnings: process.env.TS_NODE_DISABLE_WARNINGS, | ||
cache: yn(process.env.TS_NODE_CACHE), | ||
cacheDirectory: process.env.TS_NODE_CACHE_DIRECTORY, | ||
disableWarnings: yn(process.env.TS_NODE_DISABLE_WARNINGS), | ||
compiler: process.env.TS_NODE_COMPILER, | ||
compilerOptions: process.env.TS_NODE_COMPILER_OPTIONS, | ||
project: process.env.TS_NODE_PROJECT, | ||
ignoreWarnings: process.env.TS_NODE_IGNORE_WARNINGS, | ||
fast: process.env.TS_NODE_FAST | ||
ignore: split(process.env.TS_NODE_IGNORE), | ||
ignoreWarnings: split(process.env.TS_NODE_IGNORE_WARNINGS), | ||
fast: yn(process.env.TS_NODE_FAST) | ||
}; | ||
function register(opts) { | ||
var options = extend(DEFAULT_OPTIONS, opts); | ||
function split(value) { | ||
return value ? value.split(/ *, */g) : []; | ||
} | ||
function slash(value) { | ||
return value.replace(/\\/g, '/'); | ||
} | ||
function register(options) { | ||
if (options === void 0) { options = {}; } | ||
var compiler = options.compiler || 'typescript'; | ||
var ignoreWarnings = (options.ignoreWarnings || DEFAULTS.ignoreWarnings).map(Number); | ||
var disableWarnings = !!(options.disableWarnings == null ? DEFAULTS.disableWarnings : options.disableWarnings); | ||
var getFile = options.getFile || DEFAULTS.getFile; | ||
var fileExists = options.fileExists || DEFAULTS.fileExists; | ||
var shouldCache = !!(options.cache == null ? DEFAULTS.cache : options.cache); | ||
var fast = !!(options.fast == null ? DEFAULTS.fast : options.fast); | ||
var project = options.project || DEFAULTS.project; | ||
var cacheDirectory = options.cacheDirectory || DEFAULTS.cacheDirectory || path_1.join(os_1.tmpdir(), 'ts-node'); | ||
var result; | ||
options.compiler = options.compiler || 'typescript'; | ||
options.ignoreWarnings = arrify(options.ignoreWarnings).map(Number); | ||
options.compilerOptions = typeof options.compilerOptions === 'string' ? | ||
var ignore = ((typeof options.ignore === 'boolean' ? | ||
(options.ignore === false ? [] : undefined) : | ||
(options.ignore || DEFAULTS.ignore)) || | ||
['^node_modules/']).map(function (str) { return new RegExp(str); }); | ||
var compilerOptions = typeof options.compilerOptions === 'string' ? | ||
JSON.parse(options.compilerOptions) : | ||
options.compilerOptions; | ||
function load() { | ||
var project = { cache: {}, versions: {}, sourceMaps: {} }; | ||
var cache = { contents: {}, versions: {}, sourceMaps: {} }; | ||
sourceMapSupport.install({ | ||
environment: 'node', | ||
retrieveSourceMap: function (fileName) { | ||
if (project.sourceMaps[fileName]) { | ||
if (cache.sourceMaps[fileName]) { | ||
return { | ||
url: project.sourceMaps[fileName], | ||
map: options.getFile(project.sourceMaps[fileName]) | ||
url: cache.sourceMaps[fileName], | ||
map: getFile(cache.sourceMaps[fileName]) | ||
}; | ||
@@ -55,6 +73,6 @@ } | ||
var cwd = process.cwd(); | ||
var ts = require(options.compiler); | ||
var config = readConfig(options, cwd, ts); | ||
var configDiagnostics = formatDiagnostics(config.errors, options, cwd, ts); | ||
var cachedir = path_1.join(path_1.resolve(cwd, options.cacheDirectory), getCompilerDigest(ts, options, config)); | ||
var ts = require(compiler); | ||
var config = readConfig(compilerOptions, project, cwd, ts); | ||
var configDiagnostics = formatDiagnostics(config.errors, ignoreWarnings, disableWarnings, cwd, ts); | ||
var cachedir = path_1.join(path_1.resolve(cwd, cacheDirectory), getCompilerDigest({ version: ts.version, fast: fast, ignoreWarnings: ignoreWarnings, disableWarnings: disableWarnings, config: config, compiler: compiler })); | ||
mkdirp.sync(cachedir); | ||
@@ -65,3 +83,3 @@ if (configDiagnostics.length) { | ||
if (config.options.allowJs) { | ||
registerExtension('.js'); | ||
registerExtension('.js', ignore, service); | ||
} | ||
@@ -71,3 +89,3 @@ for (var _i = 0, _a = config.fileNames; _i < _a.length; _i++) { | ||
if (/\.d\.ts$/.test(fileName)) { | ||
project.versions[fileName] = 1; | ||
cache.versions[fileName] = 1; | ||
} | ||
@@ -82,3 +100,3 @@ } | ||
var diagnosticList = result.diagnostics ? | ||
formatDiagnostics(result.diagnostics, options, cwd, ts) : | ||
formatDiagnostics(result.diagnostics, ignoreWarnings, disableWarnings, cwd, ts) : | ||
[]; | ||
@@ -90,27 +108,27 @@ if (diagnosticList.length) { | ||
}; | ||
var compile = readThrough(cachedir, options, project, getOutput); | ||
var compile = readThrough(cachedir, shouldCache, getFile, fileExists, cache, getOutput); | ||
var getTypeInfo = function (fileName, position) { | ||
throw new TypeError("No type information available under \"--fast\" mode"); | ||
}; | ||
if (!options.fast) { | ||
if (!fast) { | ||
var addVersion_1 = function (fileName) { | ||
if (!project.versions.hasOwnProperty(fileName)) { | ||
project.versions[fileName] = 1; | ||
if (!cache.versions.hasOwnProperty(fileName)) { | ||
cache.versions[fileName] = 1; | ||
} | ||
}; | ||
var addCache_1 = function (code, fileName) { | ||
project.cache[fileName] = code; | ||
project.versions[fileName] += 1; | ||
cache.contents[fileName] = code; | ||
cache.versions[fileName] += 1; | ||
}; | ||
var serviceHost = { | ||
getScriptFileNames: function () { return Object.keys(project.versions); }, | ||
getScriptVersion: function (fileName) { return String(project.versions[fileName]); }, | ||
getScriptFileNames: function () { return Object.keys(cache.versions); }, | ||
getScriptVersion: function (fileName) { return String(cache.versions[fileName]); }, | ||
getScriptSnapshot: function (fileName) { | ||
if (!project.cache.hasOwnProperty(fileName)) { | ||
if (!options.fileExists(fileName)) { | ||
if (!cache.contents.hasOwnProperty(fileName)) { | ||
if (!fileExists(fileName)) { | ||
return undefined; | ||
} | ||
project.cache[fileName] = options.getFile(fileName); | ||
cache.contents[fileName] = getFile(fileName); | ||
} | ||
return ts.ScriptSnapshot.fromString(project.cache[fileName]); | ||
return ts.ScriptSnapshot.fromString(cache.contents[fileName]); | ||
}, | ||
@@ -130,3 +148,3 @@ getDirectories: getDirectories, | ||
.concat(service_1.getSemanticDiagnostics(fileName)); | ||
var diagnosticList = formatDiagnostics(diagnostics, options, cwd, ts); | ||
var diagnosticList = formatDiagnostics(diagnostics, ignoreWarnings, disableWarnings, cwd, ts); | ||
if (output.emitSkipped) { | ||
@@ -138,5 +156,11 @@ diagnosticList.push(path_1.relative(cwd, fileName) + ": Emit skipped"); | ||
} | ||
if (output.outputFiles.length === 0) { | ||
throw new TypeError('Unable to require `.d.ts` file.\n' + | ||
'This is usually the result of a faulty configuration or import. ' + | ||
'Make sure there is a `.js`, `.json` or another executable extension and ' + | ||
("loader (attached before `ts-node`) available alongside `" + fileName + "`.")); | ||
} | ||
return [output.outputFiles[1].text, output.outputFiles[0].text]; | ||
}; | ||
compile = readThrough(cachedir, options, project, function (code, fileName) { | ||
compile = readThrough(cachedir, shouldCache, getFile, fileExists, cache, function (code, fileName) { | ||
addVersion_1(fileName); | ||
@@ -159,20 +183,4 @@ addCache_1(code, fileName); | ||
} | ||
function shouldIgnore(filename) { | ||
return path_1.relative(service().cwd, filename).split(path_1.sep).indexOf('node_modules') > -1; | ||
} | ||
function registerExtension(ext) { | ||
var old = require.extensions[ext] || require.extensions['.js']; | ||
require.extensions[ext] = function (m, filename) { | ||
if (shouldIgnore(filename)) { | ||
return old(m, filename); | ||
} | ||
var _compile = m._compile; | ||
m._compile = function (code, fileName) { | ||
return _compile.call(this, service().compile(code, fileName), fileName); | ||
}; | ||
return old(m, filename); | ||
}; | ||
} | ||
registerExtension('.ts'); | ||
registerExtension('.tsx'); | ||
registerExtension('.ts', ignore, service); | ||
registerExtension('.tsx', ignore, service); | ||
if (!options.lazy) { | ||
@@ -184,8 +192,25 @@ service(); | ||
exports.register = register; | ||
function readConfig(options, cwd, ts) { | ||
var result = tsconfig_1.loadSync(cwd, options.project); | ||
function shouldIgnore(filename, service, ignore) { | ||
var relname = slash(filename); | ||
return ignore.some(function (x) { return x.test(relname); }); | ||
} | ||
function registerExtension(ext, ignore, service) { | ||
var old = require.extensions[ext] || require.extensions['.js']; | ||
require.extensions[ext] = function (m, filename) { | ||
if (shouldIgnore(filename, service, ignore)) { | ||
return old(m, filename); | ||
} | ||
var _compile = m._compile; | ||
m._compile = function (code, fileName) { | ||
return _compile.call(this, service().compile(code, fileName), fileName); | ||
}; | ||
return old(m, filename); | ||
}; | ||
} | ||
function readConfig(compilerOptions, project, cwd, ts) { | ||
var result = tsconfig_1.loadSync(cwd, typeof project === 'string' ? project : undefined); | ||
result.config.compilerOptions = extend({ | ||
target: 'es5', | ||
module: 'commonjs' | ||
}, result.config.compilerOptions, options.compilerOptions, { | ||
}, result.config.compilerOptions, compilerOptions, { | ||
sourceMap: true, | ||
@@ -205,6 +230,9 @@ inlineSourceMap: false, | ||
} | ||
return ts.parseJsonConfigFileContent(result.config, ts.sys, basePath, null, result.path); | ||
if (typeof ts.parseJsonConfigFileContent === 'function') { | ||
return ts.parseJsonConfigFileContent(result.config, ts.sys, basePath, null, result.path); | ||
} | ||
throw new TypeError('Could not find a compatible `parseConfigFile` function'); | ||
} | ||
function readThrough(cachedir, options, project, compile) { | ||
if (options.cache === false) { | ||
function readThrough(cachedir, shouldCache, getFile, fileExists, cache, compile) { | ||
if (shouldCache === false) { | ||
return function (code, fileName) { | ||
@@ -214,3 +242,3 @@ var cachePath = path_1.join(cachedir, getCacheName(code, fileName)); | ||
var out = compile(code, fileName); | ||
project.sourceMaps[fileName] = sourceMapPath; | ||
cache.sourceMaps[fileName] = sourceMapPath; | ||
var output = updateOutput(out[0], fileName, sourceMapPath); | ||
@@ -226,5 +254,5 @@ var sourceMap = updateSourceMap(out[1], fileName); | ||
var sourceMapPath = cachePath + ".js.map"; | ||
project.sourceMaps[fileName] = sourceMapPath; | ||
if (options.fileExists(outputPath)) { | ||
return options.getFile(outputPath); | ||
cache.sourceMaps[fileName] = sourceMapPath; | ||
if (fileExists(outputPath)) { | ||
return getFile(outputPath); | ||
} | ||
@@ -258,10 +286,4 @@ var out = compile(code, fileName); | ||
} | ||
function getCompilerDigest(ts, options, config) { | ||
return path_1.join(crypto.createHash('sha1') | ||
.update(ts.version, 'utf8') | ||
.update('\0', 'utf8') | ||
.update(JSON.stringify(options), 'utf8') | ||
.update('\0', 'utf8') | ||
.update(JSON.stringify(config), 'utf8') | ||
.digest('hex')); | ||
function getCompilerDigest(opts) { | ||
return crypto.createHash('sha1').update(JSON.stringify(opts), 'utf8').digest('hex'); | ||
} | ||
@@ -295,4 +317,4 @@ function fileExists(fileName) { | ||
exports.getFile = getFile; | ||
function formatDiagnostics(diagnostics, options, cwd, ts) { | ||
if (options.disableWarnings) { | ||
function formatDiagnostics(diagnostics, ignore, disable, cwd, ts) { | ||
if (disable) { | ||
return []; | ||
@@ -302,3 +324,3 @@ } | ||
.filter(function (diagnostic) { | ||
return options.ignoreWarnings.indexOf(diagnostic.code) === -1; | ||
return ignore.indexOf(diagnostic.code) === -1; | ||
}) | ||
@@ -305,0 +327,0 @@ .map(function (diagnostic) { |
@@ -11,3 +11,3 @@ "use strict"; | ||
var EXEC_PATH = path_1.join(__dirname, '../dist/bin'); | ||
var BIN_EXEC = "node " + EXEC_PATH; | ||
var BIN_EXEC = "node \"" + EXEC_PATH + "\" --project \"" + testDir + "\""; | ||
describe('ts-node', function () { | ||
@@ -62,3 +62,4 @@ this.timeout(10000); | ||
child_process_1.exec(BIN_EXEC + " -e \"import * as m from './tests/module';console.log(m.example(123))\"", function (err) { | ||
chai_1.expect(err.message).to.contain('[eval].ts (1,59): Argument of type \'number\' is not assignable to parameter of type \'string\'. (2345)'); | ||
chai_1.expect(err.message).to.match(new RegExp('\\[eval\\]\\.ts \\(1,59\\): Argument of type \'(?:number|123)\' ' + | ||
'is not assignable to parameter of type \'string\'\\. \\(2345\\)')); | ||
return done(); | ||
@@ -131,3 +132,3 @@ }); | ||
describe('register', function () { | ||
index_1.register({ project: path_1.join(testDir, '..') }); | ||
index_1.register({ project: testDir }); | ||
it('should be able to require typescript', function () { | ||
@@ -134,0 +135,0 @@ var m = require('../tests/module'); |
{ | ||
"name": "ts-node", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"preferGlobal": true, | ||
@@ -21,3 +21,3 @@ "description": "TypeScript execution environment and REPL for node", | ||
"test-spec": "mocha dist/**/*.spec.js -R spec --bail", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- dist/**/*.spec.js -R spec --bail", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- \"dist/**/*.spec.js\" -R spec --bail", | ||
"test": "npm run build && npm run lint && npm run test-cov", | ||
@@ -56,4 +56,4 @@ "prepublish": "typings install && npm run build" | ||
"tslint": "^3.13.0", | ||
"tslint-config-standard": "^1.3.0", | ||
"typescript": "^1.8.10", | ||
"tslint-config-standard": "^1.5.0", | ||
"typescript": "^2.0.3", | ||
"typings": "^1.0.4" | ||
@@ -64,3 +64,3 @@ }, | ||
"chalk": "^1.1.1", | ||
"diff": "^2.1.1", | ||
"diff": "^3.0.0", | ||
"make-error": "^1.1.1", | ||
@@ -72,4 +72,5 @@ "minimist": "^1.2.0", | ||
"tsconfig": "^5.0.2", | ||
"xtend": "^4.0.0" | ||
"xtend": "^4.0.0", | ||
"yn": "^1.2.0" | ||
} | ||
} |
@@ -86,4 +86,4 @@ # TypeScript Node | ||
* **--lazy, -L** Lazily defer TypeScript initialization until first `.ts` file | ||
* **--no-cache** Skip hitting the compiled JavaScript cache | ||
* **--cache-directory** Configure the TypeScript cache directory | ||
* **--no-cache** Skip hitting the compiled JavaScript cache (also `process.env.TS_NODE_CACHE`) | ||
* **--cache-directory** Configure the TypeScript cache directory (also `process.env.TS_NODE_CACHE_DIRECTORY`) | ||
@@ -90,0 +90,0 @@ ### Programmatic Usage |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
107891
865
11
14
+ Addedyn@^1.2.0
+ Addeddiff@3.5.0(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedyn@1.3.0(transitive)
- Removeddiff@2.2.3(transitive)
Updateddiff@^3.0.0