You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

ts-node

Package Overview
Dependencies
Maintainers
1
Versions
128
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-node - npm Package Compare versions

Comparing version

to
1.0.0

39

dist/_bin.js

@@ -12,6 +12,7 @@ "use strict";

var strings = ['eval', 'print', 'compiler', 'project', 'ignoreWarnings'];
var booleans = ['help', 'fast', 'version', 'disableWarnings', 'noProject'];
var booleans = ['help', 'fast', 'lazy', 'version', 'disableWarnings', 'cache'];
var aliases = {
help: ['h'],
fast: ['f'],
fast: ['F'],
lazy: ['L'],
version: ['v'],

@@ -21,7 +22,6 @@ eval: ['e'],

project: ['P'],
compiler: ['c'],
ignoreWarnings: ['i', 'ignore-warnings'],
disableWarnings: ['d', 'disable-warnings'],
noProject: ['n', 'no-project'],
compilerOptions: ['o', 'compiler-options']
compiler: ['C'],
ignoreWarnings: ['I', 'ignore-warnings'],
disableWarnings: ['D', 'disable-warnings'],
compilerOptions: ['O', 'compiler-options']
};

@@ -39,3 +39,3 @@ var stop = process.argv.length;

}
var alias = aliases[name];
var alias = aliases[bool];
if (alias) {

@@ -70,10 +70,14 @@ for (var _a = 0, alias_1 = alias; _a < alias_1.length; _a++) {

boolean: booleans,
alias: aliases
alias: aliases,
default: {
cache: true
}
});
if (argv.version) {
console.log(index_1.VERSION);
console.log("ts-node v" + index_1.VERSION);
console.log("node " + process.version);
process.exit(0);
}
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 -c, --compiler [name] Specify a custom TypeScript compiler\n -i, --ignoreWarnings [codes] Ignore TypeScript warnings by diagnostic code\n -d, --disableWarnings Ignore every TypeScript warning\n -n, --noProject Ignore the \"tsconfig.json\" project file\n -P, --project [path] Specify the path to the TypeScript project\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 -C, --compiler [name] Specify a custom TypeScript compiler\n -I, --ignoreWarnings [codes] 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");
process.exit(0);

@@ -96,4 +100,6 @@ }

getVersion: isEval ? getVersionEval : index_1.getVersion,
getFileExists: isEval ? getFileExistsEval : index_1.getFileExists,
fileExists: isEval ? fileExistsEval : index_1.fileExists,
fast: argv.fast,
lazy: argv.lazy,
cache: argv.cache,
compiler: argv.compiler,

@@ -103,3 +109,2 @@ ignoreWarnings: list(argv.ignoreWarnings),

disableWarnings: argv.disableWarnings,
noProject: argv.noProject,
compilerOptions: argv.compilerOptions

@@ -170,3 +175,3 @@ });

try {
output = service.compile(EVAL_PATH);
output = service().compile(EVAL_PATH);
}

@@ -218,3 +223,3 @@ catch (error) {

evalFile.version++;
var _a = service.getTypeInfo(EVAL_PATH, evalFile.input.length), name = _a.name, comment = _a.comment;
var _a = service().getTypeInfo(EVAL_PATH, evalFile.input.length), name = _a.name, comment = _a.comment;
repl.outputStream.write(chalk.bold(name) + "\n" + (comment ? comment + "\n" : ''));

@@ -255,5 +260,5 @@ repl.displayPrompt();

}
function getFileExistsEval(fileName) {
return fileName === EVAL_PATH ? true : index_1.getFileExists(fileName);
function fileExistsEval(fileName) {
return fileName === EVAL_PATH ? true : index_1.fileExists(fileName);
}
//# sourceMappingURL=_bin.js.map
import { BaseError } from 'make-error';
import * as TS from 'typescript';
export interface TSCommon {
version: typeof TS.version;
sys: typeof TS.sys;

@@ -23,4 +24,5 @@ ScriptSnapshot: typeof TS.ScriptSnapshot;

fast?: boolean;
lazy?: boolean;
cache?: boolean;
compiler?: string;
noProject?: boolean;
project?: string;

@@ -31,3 +33,3 @@ ignoreWarnings?: Array<number | string>;

getVersion?: (fileName: string) => string;
getFileExists?: (fileName: string) => boolean;
fileExists?: (fileName: string) => boolean;
compilerOptions?: any;

@@ -39,8 +41,10 @@ }

}
export declare function register(opts?: Options): {
compile: (fileName: string) => string;
getTypeInfo: (fileName: string, position: number) => TypeInfo;
};
export interface Register {
cwd: string;
compile(fileName: string): string;
getTypeInfo(fileName: string, position: number): TypeInfo;
}
export declare function register(opts?: Options): () => Register;
export declare function getVersion(fileName: string): string;
export declare function getFileExists(fileName: string): boolean;
export declare function fileExists(fileName: string): boolean;
export declare function getFile(fileName: string): string;

@@ -47,0 +51,0 @@ export declare class TSError extends BaseError {

@@ -13,46 +13,17 @@ "use strict";

var arrify = require('arrify');
var mkdirp = require('mkdirp');
var crypto = require('crypto');
var make_error_1 = require('make-error');
var tsconfig = require('tsconfig');
var TMP_DIR = path_1.join(os_1.tmpdir(), 'ts-node');
var pkg = require('../package.json');
var oldHandlers = {};
exports.VERSION = pkg.version;
function readConfig(options, cwd, ts) {
var project = options.project, noProject = options.noProject;
var fileName = noProject ? undefined : tsconfig.resolveSync(project || cwd);
var result = fileName ? ts.readConfigFile(fileName, ts.sys.readFile) : {
config: {
files: [],
compilerOptions: {}
}
};
if (result.error) {
throw new TSError([formatDiagnostic(result.error, ts, cwd)]);
}
result.config.compilerOptions = extend({
target: 'es5',
module: 'commonjs'
}, result.config.compilerOptions, options.compilerOptions, {
sourceMap: false,
inlineSourceMap: true,
inlineSources: true,
declaration: false,
noEmit: false,
outDir: "tmp" + Math.random().toString(36).substr(2)
});
delete result.config.compilerOptions.out;
delete result.config.compilerOptions.outFile;
var basePath = fileName ? path_1.dirname(path_1.resolve(fileName)) : cwd;
if (typeof ts.parseConfigFile === 'function') {
return ts.parseConfigFile(result.config, ts.sys, basePath);
}
return ts.parseJsonConfigFileContent(result.config, ts.sys, basePath, null, fileName);
}
var DEFAULT_OPTIONS = {
getFile: getFile,
getVersion: getVersion,
getFileExists: getFileExists,
fileExists: fileExists,
cache: process.env.TS_NODE_CACHE,
disableWarnings: process.env.TS_NODE_DISABLE_WARNINGS,
compiler: process.env.TS_NODE_COMPILER,
project: process.env.TS_NODE_PROJECT || process.cwd(),
noProject: process.env.TS_NODE_NO_PROJECT,
project: process.env.TS_NODE_PROJECT,
ignoreWarnings: process.env.TS_NODE_IGNORE_WARNINGS,

@@ -62,5 +33,4 @@ fast: process.env.TS_NODE_FAST

function register(opts) {
var cwd = process.cwd();
var options = extend(DEFAULT_OPTIONS, opts);
var project = { version: 0, files: {}, versions: {} };
var result;
options.compiler = options.compiler || 'typescript';

@@ -71,101 +41,116 @@ options.ignoreWarnings = arrify(options.ignoreWarnings).map(Number);

options.compilerOptions;
sourceMapSupport.install({
environment: 'node',
retrieveFile: function (fileName) {
if (project.files[fileName]) {
return getOutput(fileName);
function load() {
var project = { version: 0, files: {}, versions: {}, maps: {} };
sourceMapSupport.install({
environment: 'node',
retrieveSourceMap: function (fileName) {
if (project.maps[fileName]) {
return {
url: project.maps[fileName],
map: options.getFile(project.maps[fileName])
};
}
}
});
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(TMP_DIR, getCompilerDigest(ts, options, config));
mkdirp.sync(cachedir);
if (configDiagnostics.length) {
throw new TSError(configDiagnostics);
}
});
var ts = require(options.compiler);
var config = readConfig(options, cwd, ts);
var diagnostics = formatDiagnostics(config.errors, cwd, ts, options);
if (diagnostics.length) {
throw new TSError(diagnostics);
}
for (var _i = 0, _a = config.fileNames; _i < _a.length; _i++) {
var fileName = _a[_i];
project.files[fileName] = true;
}
var getOutput = function (fileName) {
var contents = options.getFile(fileName);
var _a = ts.transpileModule(contents, {
compilerOptions: config.options,
fileName: fileName,
reportDiagnostics: true
}), outputText = _a.outputText, diagnostics = _a.diagnostics;
var diagnosticList = diagnostics ? formatDiagnostics(diagnostics, cwd, ts, options) : [];
if (diagnosticList.length) {
throw new TSError(diagnosticList);
if (config.options.allowJs) {
registerExtension('.js');
}
return outputText;
};
var compile = function (fileName) {
return getOutput(fileName);
};
var getTypeInfo = function (fileName, position) {
throw new TypeError("No type information available under \"--fast\" mode");
};
if (!options.fast) {
var serviceHost = {
getScriptFileNames: function () { return Object.keys(project.files); },
getProjectVersion: function () { return String(project.version); },
getScriptVersion: function (fileName) { return versionFile_1(fileName); },
getScriptSnapshot: function (fileName) {
if (!options.getFileExists(fileName)) {
return undefined;
}
return ts.ScriptSnapshot.fromString(options.getFile(fileName));
},
getNewLine: function () { return os_1.EOL; },
getCurrentDirectory: function () { return cwd; },
getCompilationSettings: function () { return config.options; },
getDefaultLibFileName: function (options) { return ts.getDefaultLibFilePath(config.options); }
};
var service_1 = ts.createLanguageService(serviceHost);
var addAndVersionFile_1 = function (fileName) {
for (var _i = 0, _a = config.fileNames; _i < _a.length; _i++) {
var fileName = _a[_i];
project.files[fileName] = true;
var currentVersion = project.versions[fileName];
var newVersion = versionFile_1(fileName);
if (currentVersion !== newVersion) {
project.version++;
}
return newVersion;
};
var versionFile_1 = function (fileName) {
var version = options.getVersion(fileName);
project.versions[fileName] = version;
return version;
};
getOutput = function (fileName) {
var output = service_1.getEmitOutput(fileName);
var diagnostics = service_1.getCompilerOptionsDiagnostics()
.concat(service_1.getSyntacticDiagnostics(fileName))
.concat(service_1.getSemanticDiagnostics(fileName));
var diagnosticList = formatDiagnostics(diagnostics, cwd, ts, options);
if (output.emitSkipped) {
diagnosticList.push(path_1.relative(cwd, fileName) + ": Emit skipped");
}
}
var getOutput = function (fileName, contents) {
var result = ts.transpileModule(contents, {
fileName: fileName,
compilerOptions: config.options,
reportDiagnostics: true
});
var diagnosticList = result.diagnostics ?
formatDiagnostics(result.diagnostics, options, cwd, ts) :
[];
if (diagnosticList.length) {
throw new TSError(diagnosticList);
}
return output.outputFiles[0].text;
return [result.outputText, result.sourceMapText];
};
compile = function (fileName) {
addAndVersionFile_1(fileName);
return getOutput(fileName);
var compile = readThrough(cachedir, options, project, getOutput);
var getTypeInfo = function (fileName, position) {
throw new TypeError("No type information available under \"--fast\" mode");
};
getTypeInfo = function (fileName, position) {
addAndVersionFile_1(fileName);
var info = service_1.getQuickInfoAtPosition(fileName, position);
var name = ts.displayPartsToString(info ? info.displayParts : []);
var comment = ts.displayPartsToString(info ? info.documentation : []);
return { name: name, comment: comment };
};
if (!options.fast) {
var serviceHost = {
getScriptFileNames: function () { return Object.keys(project.files); },
getProjectVersion: function () { return String(project.version); },
getScriptVersion: function (fileName) { return versionFile_1(fileName); },
getScriptSnapshot: function (fileName) {
if (!options.fileExists(fileName)) {
return undefined;
}
return ts.ScriptSnapshot.fromString(options.getFile(fileName));
},
getNewLine: function () { return os_1.EOL; },
getCurrentDirectory: function () { return cwd; },
getCompilationSettings: function () { return config.options; },
getDefaultLibFileName: function (options) { return ts.getDefaultLibFilePath(config.options); }
};
var service_1 = ts.createLanguageService(serviceHost);
var addAndVersionFile_1 = function (fileName) {
project.files[fileName] = true;
var currentVersion = project.versions[fileName];
var newVersion = versionFile_1(fileName);
if (currentVersion !== newVersion) {
project.version++;
}
return newVersion;
};
var versionFile_1 = function (fileName) {
var version = options.getVersion(fileName);
project.versions[fileName] = version;
return version;
};
getOutput = function (fileName) {
var output = service_1.getEmitOutput(fileName);
var diagnostics = service_1.getCompilerOptionsDiagnostics()
.concat(service_1.getSyntacticDiagnostics(fileName))
.concat(service_1.getSemanticDiagnostics(fileName));
var diagnosticList = formatDiagnostics(diagnostics, options, cwd, ts);
if (output.emitSkipped) {
diagnosticList.push(path_1.relative(cwd, fileName) + ": Emit skipped");
}
if (diagnosticList.length) {
throw new TSError(diagnosticList);
}
return [output.outputFiles[1].text, output.outputFiles[0].text];
};
compile = readThrough(cachedir, options, project, function (fileName, contents) {
addAndVersionFile_1(fileName);
return getOutput(fileName, contents);
});
getTypeInfo = function (fileName, position) {
addAndVersionFile_1(fileName);
var info = service_1.getQuickInfoAtPosition(fileName, position);
var name = ts.displayPartsToString(info ? info.displayParts : []);
var comment = ts.displayPartsToString(info ? info.documentation : []);
return { name: name, comment: comment };
};
}
return { cwd: cwd, compile: compile, getOutput: getOutput, getTypeInfo: getTypeInfo };
}
function service() {
return result || (result = load());
}
function loader(m, fileName) {
return m._compile(compile(fileName), fileName);
return m._compile(service().compile(fileName), fileName);
}
function shouldIgnore(filename) {
return path_1.relative(cwd, filename).split(path_1.sep).indexOf('node_modules') > -1;
return path_1.relative(service().cwd, filename).split(path_1.sep).indexOf('node_modules') > -1;
}

@@ -184,8 +169,110 @@ function registerExtension(ext) {

registerExtension('.tsx');
if (config.options.allowJs) {
registerExtension('.js');
if (!options.lazy) {
service();
}
return { compile: compile, getTypeInfo: getTypeInfo };
return service;
}
exports.register = register;
function readConfig(options, cwd, ts) {
var project;
if (options.project == null) {
var path = ts.findConfigFile(cwd, options.fileExists);
project = path ? path_1.resolve(path) : undefined;
}
else if (typeof options.project === 'string') {
var path = path_1.resolve(options.project);
if (options.fileExists(path)) {
project = path;
}
else {
project = path_1.join(path, 'tsconfig.json');
}
}
var result = project ? ts.readConfigFile(project, options.getFile) : {
config: {
files: [],
compilerOptions: {}
}
};
if (result.error) {
throw new TSError([formatDiagnostic(result.error, cwd, ts)]);
}
result.config.compilerOptions = extend({
target: 'es5',
module: 'commonjs'
}, result.config.compilerOptions, options.compilerOptions, {
sourceMap: true,
inlineSourceMap: false,
inlineSources: true,
declaration: false,
noEmit: false,
outDir: '$$ts-node$$'
});
delete result.config.compilerOptions.out;
delete result.config.compilerOptions.outFile;
var basePath = project ? path_1.dirname(project) : cwd;
if (typeof ts.parseConfigFile === 'function') {
return ts.parseConfigFile(result.config, ts.sys, basePath);
}
return ts.parseJsonConfigFileContent(result.config, ts.sys, basePath, null, project);
}
function readThrough(cachedir, options, project, compile) {
if (options.cache === false) {
return function (fileName) {
var contents = options.getFile(fileName);
var cachePath = path_1.join(cachedir, getCacheName(contents, fileName));
var sourceMapPath = cachePath + ".js.map";
var out = compile(fileName, contents);
project.maps[fileName] = sourceMapPath;
var output = updateOutput(out[0], fileName, sourceMapPath);
var sourceMap = updateSourceMap(out[1], fileName);
fs_1.writeFileSync(sourceMapPath, sourceMap);
return output;
};
}
return function (fileName) {
var contents = options.getFile(fileName);
var cachePath = path_1.join(cachedir, getCacheName(contents, fileName));
var outputPath = cachePath + ".js";
var sourceMapPath = cachePath + ".js.map";
project.maps[fileName] = sourceMapPath;
if (options.fileExists(outputPath)) {
return options.getFile(outputPath);
}
var out = compile(fileName, contents);
var output = updateOutput(out[0], fileName, sourceMapPath);
var sourceMap = updateSourceMap(out[1], fileName);
fs_1.writeFileSync(outputPath, output);
fs_1.writeFileSync(sourceMapPath, sourceMap);
return output;
};
}
function updateOutput(outputText, fileName, sourceMapPath) {
var ext = path_1.extname(fileName);
var originalPath = path_1.basename(fileName).slice(0, -ext.length) + '.js.map';
return outputText.slice(0, -originalPath.length) + sourceMapPath.replace(/\\/g, '/');
}
function updateSourceMap(sourceMapText, fileName) {
var sourceMap = JSON.parse(sourceMapText);
sourceMap.file = fileName;
sourceMap.sources = [fileName];
delete sourceMap.sourceRoot;
return JSON.stringify(sourceMap);
}
function getCacheName(sourceCode, fileName) {
return crypto.createHash('sha1')
.update(path_1.extname(fileName), 'utf8')
.update('\0', 'utf8')
.update(sourceCode, 'utf8')
.digest('hex');
}
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 getVersion(fileName) {

@@ -195,3 +282,3 @@ return String(fs_1.statSync(fileName).mtime.getTime());

exports.getVersion = getVersion;
function getFileExists(fileName) {
function fileExists(fileName) {
try {

@@ -205,3 +292,3 @@ var stats = fs_1.statSync(fileName);

}
exports.getFileExists = getFileExists;
exports.fileExists = fileExists;
function getFile(fileName) {

@@ -211,3 +298,3 @@ return fs_1.readFileSync(fileName, 'utf8');

exports.getFile = getFile;
function formatDiagnostics(diagnostics, cwd, ts, options) {
function formatDiagnostics(diagnostics, options, cwd, ts) {
if (options.disableWarnings) {

@@ -221,6 +308,6 @@ return [];

.map(function (diagnostic) {
return formatDiagnostic(diagnostic, ts, cwd);
return formatDiagnostic(diagnostic, cwd, ts);
});
}
function formatDiagnostic(diagnostic, ts, cwd) {
function formatDiagnostic(diagnostic, cwd, ts) {
var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');

@@ -227,0 +314,0 @@ if (diagnostic.file) {

@@ -9,5 +9,5 @@ "use strict";

var index_1 = require('./index');
var cwd = path_1.join(__dirname, '../tests');
var testDir = path_1.join(__dirname, '../tests');
var EXEC_PATH = path_1.join(__dirname, '../dist/bin');
var BIN_EXEC = "node " + EXEC_PATH + " --project \"" + cwd + "\"";
var BIN_EXEC = "node " + EXEC_PATH;
describe('ts-node', function () {

@@ -27,3 +27,3 @@ this.timeout(10000);

it('should execute cli with absolute path', function (done) {
child_process_1.exec(BIN_EXEC + " \"" + path_1.join(cwd, '../tests/hello-world') + "\"", function (err, stdout) {
child_process_1.exec(BIN_EXEC + " \"" + path_1.join(testDir, 'hello-world') + "\"", function (err, stdout) {
chai_1.expect(err).to.not.exist;

@@ -45,3 +45,3 @@ chai_1.expect(stdout).to.equal('Hello, world!\n');

BIN_EXEC,
'-o "{\\\"allowJs\\\":true}"',
'-O "{\\\"allowJs\\\":true}"',
'-p "import { main } from \'./tests/allow-js/run\';main()"'

@@ -77,3 +77,3 @@ ].join(' '), function (err, stdout) {

chai_1.expect(err.message).to.contain([
(path_1.normalize('../../tests/throw.ts') + ":3"),
(path_1.join(__dirname, '../tests/throw.ts') + ":3"),
' bar () { throw new Error(\'this is a demo\') }',

@@ -89,3 +89,3 @@ ' ^',

chai_1.expect(err.message).to.contain([
(path_1.normalize('../../tests/throw.ts') + ":3"),
(path_1.join(__dirname, '../tests/throw.ts') + ":3"),
' bar () { throw new Error(\'this is a demo\') }',

@@ -99,3 +99,3 @@ ' ^',

it('should ignore all warnings', function (done) {
child_process_1.exec(BIN_EXEC + " -d -p \"x\"", function (err) {
child_process_1.exec(BIN_EXEC + " -D -p \"x\"", function (err) {
chai_1.expect(err.message).to.contain('ReferenceError: x is not defined');

@@ -128,3 +128,3 @@ return done();

describe('register', function () {
index_1.register({ project: cwd });
index_1.register({ project: path_1.join(testDir, '..') });
it('should be able to require typescript', function () {

@@ -151,3 +151,3 @@ var m = require('../tests/module');

'Error: this is a demo',
(" at Foo.bar (" + path_1.normalize('../../tests/throw.ts') + ":3:18)")
(" at Foo.bar (" + path_1.join(__dirname, '../tests/throw.ts') + ":3:18)")
].join('\n'));

@@ -154,0 +154,0 @@ done();

{
"name": "ts-node",
"version": "0.9.3",
"version": "1.0.0",
"preferGlobal": true,

@@ -65,6 +65,6 @@ "description": "TypeScript execution environment and REPL for node",

"minimist": "^1.2.0",
"mkdirp": "^0.5.1",
"source-map-support": "^0.4.0",
"tsconfig": "^3.0.0",
"xtend": "^4.0.0"
}
}

@@ -1,1 +0,3 @@

require('./').register()
require('./').register({
lazy: true
})

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