Resolve module paths as relative to appRootDir
This commit is contained in:
parent
cb7ca7d9ad
commit
4913b3ffb9
|
@ -150,7 +150,7 @@ function findScripts(dir) {
|
||||||
var results = [];
|
var results = [];
|
||||||
files.forEach(function(filename) {
|
files.forEach(function(filename) {
|
||||||
// ignore index.js and files prefixed with underscore
|
// ignore index.js and files prefixed with underscore
|
||||||
if ((filename === 'index.js') || (filename[0] === '_')) {
|
if (filename === 'index.js' || filename[0] === '_') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,11 +165,7 @@ function findScripts(dir) {
|
||||||
else
|
else
|
||||||
debug('Skipping file %s - unknown extension', filepath);
|
debug('Skipping file %s - unknown extension', filepath);
|
||||||
} else {
|
} else {
|
||||||
try {
|
debug('Skipping directory %s', filepath);
|
||||||
path.join(require.resolve(filepath));
|
|
||||||
} catch (err) {
|
|
||||||
debug('Skipping directory %s - %s', filepath, err.code || err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -270,7 +266,7 @@ function findModelDefinitions(rootDir, sources) {
|
||||||
var registry = {};
|
var registry = {};
|
||||||
|
|
||||||
sources.forEach(function(src) {
|
sources.forEach(function(src) {
|
||||||
var srcDir = resolveSourceDir(rootDir, src);
|
var srcDir = resolveAppPath(rootDir, src);
|
||||||
if (!srcDir) {
|
if (!srcDir) {
|
||||||
debug('Skipping unknown module source dir %j', src);
|
debug('Skipping unknown module source dir %j', src);
|
||||||
return;
|
return;
|
||||||
|
@ -298,14 +294,15 @@ function findModelDefinitions(rootDir, sources) {
|
||||||
return registry;
|
return registry;
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveSourceDir(rootDir, sourceDir) {
|
function resolveAppPath(rootDir, relativePath) {
|
||||||
var srcDir = path.resolve(rootDir, sourceDir);
|
var fullPath = path.resolve(rootDir, relativePath);
|
||||||
if (fs.existsSync(srcDir))
|
if (fs.existsSync(fullPath))
|
||||||
return srcDir;
|
return fullPath;
|
||||||
|
|
||||||
// Handle module-relative path, e.g. `loopback/common/models`
|
var start = relativePath.substring(0, 2);
|
||||||
var start = sourceDir.substring(0, 2);
|
|
||||||
if (start !== './' && start !== '..') {
|
if (start !== './' && start !== '..') {
|
||||||
|
// Handle module-relative path, e.g. `loopback/common/models`
|
||||||
|
|
||||||
// Module.globalPaths is a list of globally configured paths like
|
// Module.globalPaths is a list of globally configured paths like
|
||||||
// [ env.NODE_PATH values, $HOME/.node_modules, etc. ]
|
// [ env.NODE_PATH values, $HOME/.node_modules, etc. ]
|
||||||
// Module._nodeModulePaths(rootDir) returns a list of paths like
|
// Module._nodeModulePaths(rootDir) returns a list of paths like
|
||||||
|
@ -313,16 +310,24 @@ function resolveSourceDir(rootDir, sourceDir) {
|
||||||
var modulePaths = Module.globalPaths
|
var modulePaths = Module.globalPaths
|
||||||
.concat(Module._nodeModulePaths(rootDir));
|
.concat(Module._nodeModulePaths(rootDir));
|
||||||
|
|
||||||
srcDir = modulePaths
|
fullPath = modulePaths
|
||||||
.map(function(candidateDir) {
|
.map(function(candidateDir) {
|
||||||
return path.join(candidateDir, sourceDir);
|
return path.join(candidateDir, relativePath);
|
||||||
})
|
})
|
||||||
.filter(function(candidate) {
|
.filter(function(candidate) {
|
||||||
return fs.existsSync(candidate);
|
return fs.existsSync(candidate);
|
||||||
})
|
})
|
||||||
[0];
|
[0];
|
||||||
if (srcDir)
|
if (fullPath)
|
||||||
return srcDir;
|
return fullPath;
|
||||||
|
} else {
|
||||||
|
// Handle relative path, e.g. `./common/models`
|
||||||
|
try {
|
||||||
|
fullPath = require.resolve(fullPath);
|
||||||
|
return fullPath;
|
||||||
|
} catch (err) {
|
||||||
|
debug ('Skipping %s - %s', fullPath, err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
|
@ -503,9 +508,11 @@ function buildComponentInstructions(rootDir, componentConfig) {
|
||||||
|
|
||||||
function resolveRelativePaths(relativePaths, appRootDir) {
|
function resolveRelativePaths(relativePaths, appRootDir) {
|
||||||
relativePaths.forEach(function(relativePath, k) {
|
relativePaths.forEach(function(relativePath, k) {
|
||||||
var start = relativePath.substring(0, 2);
|
var resolvedPath = resolveAppPath(appRootDir, relativePath);
|
||||||
if (start === './' || start === '..') {
|
if (resolvedPath !== undefined) {
|
||||||
relativePaths[k] = path.resolve(appRootDir, relativePath);
|
relativePaths[k] = resolvedPath;
|
||||||
|
} else {
|
||||||
|
debug ('skipping boot script %s - unknown file', relativePath);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -439,6 +439,69 @@ describe('compiler', function() {
|
||||||
expect(instructions.files.boot).to.eql([initJs]);
|
expect(instructions.files.boot).to.eql([initJs]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should resolve non-relative path in `bootScripts`', function() {
|
||||||
|
appdir.createConfigFilesSync();
|
||||||
|
var initJs = appdir.writeFileSync('custom-boot/init.js', '');
|
||||||
|
var instructions = boot.compile({
|
||||||
|
appRootDir: appdir.PATH,
|
||||||
|
bootScripts: ['custom-boot/init.js']
|
||||||
|
});
|
||||||
|
expect(instructions.files.boot).to.eql([initJs]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should resolve non-relative path in `bootDirs`', function() {
|
||||||
|
appdir.createConfigFilesSync();
|
||||||
|
var initJs = appdir.writeFileSync('custom-boot/init.js', '');
|
||||||
|
var instructions = boot.compile({
|
||||||
|
appRootDir: appdir.PATH,
|
||||||
|
bootDirs:['custom-boot']
|
||||||
|
});
|
||||||
|
expect(instructions.files.boot).to.eql([initJs]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('resolves missing extensions in `bootScripts`', function() {
|
||||||
|
appdir.createConfigFilesSync();
|
||||||
|
var initJs = appdir.writeFileSync('custom-boot/init.js', '');
|
||||||
|
var instructions = boot.compile({
|
||||||
|
appRootDir: appdir.PATH,
|
||||||
|
bootScripts:['./custom-boot/init']
|
||||||
|
});
|
||||||
|
expect(instructions.files.boot).to.eql([initJs]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('ignores index.js in `bootDirs`', function() {
|
||||||
|
appdir.createConfigFilesSync();
|
||||||
|
appdir.writeFileSync('custom-boot/index.js', '');
|
||||||
|
var instructions = boot.compile({
|
||||||
|
appRootDir: appdir.PATH,
|
||||||
|
bootDirs:['./custom-boot']
|
||||||
|
});
|
||||||
|
expect(instructions.files.boot).to.have.length(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('resolves module relative path for `bootScripts`', function() {
|
||||||
|
appdir.createConfigFilesSync();
|
||||||
|
var initJs = appdir.writeFileSync('node_modules/custom-boot/init.js', '');
|
||||||
|
var instructions = boot.compile({
|
||||||
|
appRootDir: appdir.PATH,
|
||||||
|
bootScripts: ['custom-boot/init.js']
|
||||||
|
});
|
||||||
|
expect(instructions.files.boot).to.eql([initJs]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('explores `bootScripts` in app relative path', function() {
|
||||||
|
appdir.createConfigFilesSync();
|
||||||
|
var appJs = appdir.writeFileSync('./custom-boot/init.js', '');
|
||||||
|
|
||||||
|
appdir.writeFileSync('node_modules/custom-boot/init.js', '');
|
||||||
|
|
||||||
|
var instructions = boot.compile({
|
||||||
|
appRootDir: appdir.PATH,
|
||||||
|
bootScripts: ['custom-boot/init.js']
|
||||||
|
});
|
||||||
|
expect(instructions.files.boot).to.eql([appJs]);
|
||||||
|
});
|
||||||
|
|
||||||
it('ignores models/ subdirectory', function() {
|
it('ignores models/ subdirectory', function() {
|
||||||
appdir.createConfigFilesSync();
|
appdir.createConfigFilesSync();
|
||||||
appdir.writeFileSync('models/my-model.js', '');
|
appdir.writeFileSync('models/my-model.js', '');
|
||||||
|
|
Loading…
Reference in New Issue