Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using an alternate fs for lookup of sources #68

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Run mocha",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"stopOnEntry": false,
"args": [
"test/test.js",
"--no-timeouts"
],
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"runtimeExecutable": null,
"env": {
"NODE_ENV": "testing"
}
}
]
}
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const defaultLookups = {
* @param {String} [options.webpackConfig] Path to the webpack config
* @param {Object} [options.ast] A preparsed AST for the file identified by filename.
* @param {Object} [options.tsconfig] Path to a typescript config file
* @param {Object} [options.fileSystem] An alternative filesystem / fs implementation to use for locating files.
dazinator marked this conversation as resolved.
Show resolved Hide resolved
*/
module.exports = function cabinet(options) {
const {
Expand Down Expand Up @@ -173,7 +174,7 @@ function jsLookup({dependency, filename, directory, config, webpackConfig, confi
}
}

function tsLookup({dependency, filename, tsConfig}) {
function tsLookup({dependency, filename, tsConfig, fileSystem}) {
debug('performing a typescript lookup');

const defaultTsConfig = {
Expand All @@ -186,15 +187,16 @@ function tsLookup({dependency, filename, tsConfig}) {

debug('given typescript config: ', tsConfig);

var fileSystem = fileSystem || fs;

if (!tsConfig) {
tsConfig = defaultTsConfig;
debug('no tsconfig given, defaulting');

} else if (typeof tsConfig === 'string') {
debug('string tsconfig given, parsing');

try {
tsConfig = JSON.parse(fs.readFileSync(tsConfig, 'utf8'));
tsConfig = JSON.parse(fileSystem.readFileSync(tsConfig, 'utf8'));
debug('successfully parsed tsconfig');
} catch (e) {
debug('could not parse tsconfig');
Expand All @@ -212,7 +214,17 @@ function tsLookup({dependency, filename, tsConfig}) {
options.module = ts.ModuleKind.AMD;
}

const host = ts.createCompilerHost({});
var host = ts.createCompilerHost({});
dazinator marked this conversation as resolved.
Show resolved Hide resolved

// Override host methods, to support finding files in provided fs.
const isDirectory = dirPath => fileSystem.lstatSync(dirPath).isDirectory();
const getDirectories = dirPath => fileSystem.readdirSync(dirPath).map(name => path.join(dirPath, name)).filter(isDirectory);
const pathExists = filePath => fileSystem.existsSync(filePath);

host.getDirectories = getDirectories;
dazinator marked this conversation as resolved.
Show resolved Hide resolved
host.fileExists = pathExists;
host.directoryExists = pathExists;

debug('with options: ', options);
const resolvedModule = ts.resolveModuleName(dependency, filename, options, host).resolvedModule;
debug('ts resolved module: ', resolvedModule);
Expand Down
39 changes: 39 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"mocha": "^5.0.0",
"mock-fs": "^4.4.2",
"rewire": "^4.0.1",
"sinon": "^4.1.6"
"sinon": "^4.1.6",
"unionfs": "^3.0.2",
"memfs": "^2.14.1"
},
"dependencies": {
"app-module-path": "^2.2.0",
Expand Down
2 changes: 1 addition & 1 deletion test/mockedJSFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
'ts': {
'index.ts': 'import foo from "./foo";',
'foo.ts': 'export default 1;',
'.tsconfig': '{ "version": "1.0.0", "compilerOptions": { "module": "commonjs" } }'
'.tsconfig': '{ "version": "1.0.0", "compilerOptions": { "module": "commonjs" } }',
},
'amd': {
'foo.js': 'define(["./bar"], function(bar){ return bar; });',
Expand Down
37 changes: 34 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,31 @@ describe('filing-cabinet', function() {
);
});

it('resolves an import using an alternative fs', function() {
mock.restore();
const volumeDir = 'app';
const filename = volumeDir + '/index.ts';

const unionfs = require('unionfs');
const memfs = require('memfs');

// mount files specified by "mockedFiles.js.ts" to "app" base directory.
var vol = memfs.Volume.fromJSON(mockedFiles.js.ts, `${volumeDir}`);
var ufs = unionfs.ufs.use(vol);

const result = cabinet({
partial: './foo',
filename,
directory,
fileSystem: ufs
});
//directory
assert.equal(
result,
path.join(path.resolve('app'), 'foo.ts')
);
});

describe('when a partial does not exist', function() {
it('returns an empty result', function() {
const filename = directory + '/index.ts';
Expand All @@ -390,7 +415,9 @@ describe('filing-cabinet', function() {
ModuleKind: {
AMD: 'amd'
},
createCompilerHost: sinon.stub(),
createCompilerHost: sinon.stub().returns({
getDirectories: null
}),
resolveModuleName: sinon.stub().returns({
resolvedModule: ''
}),
Expand Down Expand Up @@ -424,7 +451,9 @@ describe('filing-cabinet', function() {
ModuleKind: {
AMD: 'amd'
},
createCompilerHost: sinon.stub(),
createCompilerHost: sinon.stub().returns({
getDirectories: null
}),
resolveModuleName: sinon.stub().returns({
resolvedModule: ''
}),
Expand Down Expand Up @@ -456,7 +485,9 @@ describe('filing-cabinet', function() {
ModuleKind: {
AMD: 'amd'
},
createCompilerHost: sinon.stub(),
createCompilerHost: sinon.stub().returns({
getDirectories: null
}),
resolveModuleName: sinon.stub().returns({
resolvedModule: ''
}),
Expand Down