Browse Source

follow symlinks

gh-1132
Rich-Harris 9 years ago
parent
commit
1a81585a92
  1. 19
      src/utils/defaults.js
  2. 11
      src/utils/fs.js
  3. 3
      test/function/symlink/_config.js
  4. 1
      test/function/symlink/foo.js
  5. 2
      test/function/symlink/main.js
  6. 2
      test/function/symlink/symlinked/bar.js
  7. 3
      test/function/symlink/symlinked/baz.js

19
src/utils/defaults.js

@ -1,4 +1,4 @@
import { isFile, readFileSync } from './fs.js';
import { lstatSync, readFileSync, realpathSync } from './fs.js';
import { dirname, isAbsolute, resolve } from './path.js';
import { blank } from './object.js';
@ -6,15 +6,22 @@ export function load ( id ) {
return readFileSync( id, 'utf-8' );
}
function addJsExtensionIfNecessary ( file ) {
if ( isFile( file ) ) return file;
file += '.js';
if ( isFile( file ) ) return file;
function findFile ( file ) {
try {
const stats = lstatSync( file );
if ( stats.isSymbolicLink() ) return findFile( realpathSync( file ) );
if ( stats.isFile() ) return file;
} catch ( err ) {
// suppress
}
return null;
}
function addJsExtensionIfNecessary ( file ) {
return findFile( file ) || findFile( file + '.js' );
}
export function resolveId ( importee, importer ) {
// absolute paths are left untouched
if ( isAbsolute( importee ) ) return addJsExtensionIfNecessary( importee );

11
src/utils/fs.js

@ -12,15 +12,6 @@ function mkdirpath ( path ) {
}
}
export function isFile ( file ) {
try {
const stats = fs.statSync( file );
return stats.isFile();
} catch ( err ) {
return false;
}
}
export function writeFile ( dest, data ) {
return new Promise( ( fulfil, reject ) => {
mkdirpath( dest );
@ -35,5 +26,7 @@ export function writeFile ( dest, data ) {
});
}
export const lstatSync = fs.lstatSync;
export const readdirSync = fs.readdirSync;
export const readFileSync = fs.readFileSync;
export const realpathSync = fs.realpathSync;

3
test/function/symlink/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'follows symlinks'
};

1
test/function/symlink/foo.js

@ -0,0 +1 @@
symlinked/bar.js

2
test/function/symlink/main.js

@ -0,0 +1,2 @@
import foo from './foo.js';
assert.equal( foo, 'BAZ' );

2
test/function/symlink/symlinked/bar.js

@ -0,0 +1,2 @@
import baz from './baz.js';
export default baz();

3
test/function/symlink/symlinked/baz.js

@ -0,0 +1,3 @@
export default function () {
return 'BAZ';
}
Loading…
Cancel
Save