Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["teppeis/node-v6", "teppeis/+prettier", "teppeis/+mocha"]
}
18 changes: 4 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,12 @@ node_js:
- "10"
sudo: false
script:
- npm install --no-save typescript@2.2
- npm install -D typescript@2.4
- npm test
- npm install --no-save typescript@2.3
- npm install -D typescript@2.8
- npm test
- npm install --no-save typescript@2.4
- npm test
- npm install --no-save typescript@2.5
- npm test
- npm install --no-save typescript@2.6
- npm test
- npm install --no-save typescript@2.7
- npm test
- npm install --no-save typescript@2.8
- npm test
- npm install --no-save typescript@latest
- npm install -D typescript@2.9
- npm test
cache:
directories:
- node_modules
- $HOME/.npm
50 changes: 14 additions & 36 deletions guess.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,21 @@
var fs = require('fs');
var path = require('path');
'use strict';

var ts = require('typescript');
const path = require('path');

var pattern = 'test/**/*.@(ts|tsx)';
var cwd = process.cwd();
var packageData = require(path.join(cwd, 'package.json'));
let pattern = 'test/**/*.@(ts|tsx)';
const cwd = process.cwd();
const packageData = require(path.join(cwd, 'package.json'));

if (packageData &&
typeof packageData.directories === 'object' &&
typeof packageData.directories.test === 'string') {
var testDir = packageData.directories.test;
pattern = testDir + ((testDir.lastIndexOf('/', 0) === 0) ? '' : '/') + '**/*.@(ts|tsx)';
}

var tsconfigPath = ts.findConfigFile(cwd, fs.existsSync);
var tsconfigBasepath = null;
var compilerOptions = null;
if (tsconfigPath) {
compilerOptions = parseTsConfig(tsconfigPath);
tsconfigBasepath = path.dirname(tsconfigPath);
if (
packageData &&
typeof packageData.directories === 'object' &&
typeof packageData.directories.test === 'string'
) {
const testDir = packageData.directories.test;
pattern = `${testDir + (testDir.lastIndexOf('/', 0) === 0 ? '' : '/')}**/*.@(ts|tsx)`;
}

require('./index')({
cwd: cwd,
pattern: pattern,
compilerOptions: compilerOptions,
basepath: tsconfigBasepath
cwd: cwd,
pattern: pattern,
});

function parseTsConfig(tsconfigPath) {
var parsed = ts.parseConfigFileTextToJson(tsconfigPath, fs.readFileSync(tsconfigPath, 'utf8'));
if (parsed.error) {
throw new Error(parsed.error.messageText);
}

if (!parsed.config || !parsed.config.compilerOptions) {
return null;
}

return parsed.config.compilerOptions;
}
56 changes: 23 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,33 @@
'use strict';
/* eslint node/no-deprecated-api: [error, {ignoreGlobalItems: ["require.extensions"]}] */

var fs = require('fs');
var path = require('path');
'use strict';

var espowerSource = require('espower-source');
var minimatch = require('minimatch');
var ts = require('typescript');
var TypeScriptSimple = require('typescript-simple').TypeScriptSimple;
const espowerSource = require('espower-source');
const minimatch = require('minimatch');
const tsNodeRegister = require('ts-node').register;

function espowerTypeScript(options) {
var cwd = options.cwd || process.cwd();
var separator = (options.pattern.lastIndexOf('/', 0) === 0) ? '' : '/';
var pattern = cwd + separator + options.pattern;
var compilerOptions = convertCompilerOptions(options.compilerOptions, options.basepath || cwd);
var tss = new TypeScriptSimple(compilerOptions, false);

function loadTypeScript(localModule, filepath) {
var result = tss.compile(fs.readFileSync(filepath, 'utf-8'), path.relative(cwd, filepath));
if (minimatch(filepath, pattern)) {
result = espowerSource(result, filepath, options);
}
localModule._compile(result, filepath);
};

require.extensions['.ts'] = loadTypeScript;
require.extensions['.tsx'] = loadTypeScript;
tsNodeRegister(options.tsNode);
espowerTsRegister('.ts', options);
espowerTsRegister('.tsx', options);
}

function convertCompilerOptions(compilerOptions, basepath) {
if (!compilerOptions) {
return null;
}
function espowerTsRegister(ext, options) {
const cwd = options.cwd || process.cwd();
const separator = options.pattern.lastIndexOf('/', 0) === 0 ? '' : '/';
const pattern = cwd + separator + options.pattern;

var converted = ts.convertCompilerOptionsFromJson(compilerOptions, basepath);
if (converted.errors && converted.errors.length > 0) {
var msg = converted.errors.map(function(e) {return e.messageText}).join(', ');
throw new Error(msg);
}
return converted.options;
const originalExtension = require.extensions[ext];
require.extensions[ext] = (module, filepath) => {
if (!minimatch(filepath, pattern)) {
return originalExtension(module, filepath);
}
const originalCompile = module._compile;
module._compile = function(code, filepath) {
return originalCompile.call(this, espowerSource(code, filepath, options), filepath);
};
return originalExtension(module, filepath);
};
}

module.exports = espowerTypeScript;
Loading