From 1a81585a9281cbcb7cf503f3114425fbbc7f6ead Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 17 Jan 2016 17:25:15 -0500 Subject: [PATCH 1/6] follow symlinks --- src/utils/defaults.js | 19 +++++++++++++------ src/utils/fs.js | 11 ++--------- test/function/symlink/_config.js | 3 +++ test/function/symlink/foo.js | 1 + test/function/symlink/main.js | 2 ++ test/function/symlink/symlinked/bar.js | 2 ++ test/function/symlink/symlinked/baz.js | 3 +++ 7 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 test/function/symlink/_config.js create mode 120000 test/function/symlink/foo.js create mode 100644 test/function/symlink/main.js create mode 100644 test/function/symlink/symlinked/bar.js create mode 100644 test/function/symlink/symlinked/baz.js diff --git a/src/utils/defaults.js b/src/utils/defaults.js index 4753d1b..8848c49 100644 --- a/src/utils/defaults.js +++ b/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 ); diff --git a/src/utils/fs.js b/src/utils/fs.js index d153099..ff04ead 100644 --- a/src/utils/fs.js +++ b/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; diff --git a/test/function/symlink/_config.js b/test/function/symlink/_config.js new file mode 100644 index 0000000..b3895a5 --- /dev/null +++ b/test/function/symlink/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'follows symlinks' +}; diff --git a/test/function/symlink/foo.js b/test/function/symlink/foo.js new file mode 120000 index 0000000..a4af385 --- /dev/null +++ b/test/function/symlink/foo.js @@ -0,0 +1 @@ +symlinked/bar.js \ No newline at end of file diff --git a/test/function/symlink/main.js b/test/function/symlink/main.js new file mode 100644 index 0000000..292b3b5 --- /dev/null +++ b/test/function/symlink/main.js @@ -0,0 +1,2 @@ +import foo from './foo.js'; +assert.equal( foo, 'BAZ' ); diff --git a/test/function/symlink/symlinked/bar.js b/test/function/symlink/symlinked/bar.js new file mode 100644 index 0000000..5d6afaa --- /dev/null +++ b/test/function/symlink/symlinked/bar.js @@ -0,0 +1,2 @@ +import baz from './baz.js'; +export default baz(); diff --git a/test/function/symlink/symlinked/baz.js b/test/function/symlink/symlinked/baz.js new file mode 100644 index 0000000..d08083e --- /dev/null +++ b/test/function/symlink/symlinked/baz.js @@ -0,0 +1,3 @@ +export default function () { + return 'BAZ'; +} From d41060182175a5bd899875861c6b962c04024eed Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 17 Jan 2016 17:28:57 -0500 Subject: [PATCH 2/6] fix browser version --- browser/fs.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/browser/fs.js b/browser/fs.js index be97954..dbb1f11 100644 --- a/browser/fs.js +++ b/browser/fs.js @@ -1,6 +1,7 @@ const nope = method => `Cannot use fs.${method} inside browser`; -export const isFile = () => false; +export const lstatSync = nope( 'lstatSync' ); export const readdirSync = nope( 'readdirSync' ); export const readFileSync = nope( 'readFileSync' ); +export const realpathSync = nope( 'realpathSync' ); export const writeFile = nope( 'writeFile' ); From d266c8aab9e9feefca02a326bcda7b85f5c7cd6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Tue, 19 Jan 2016 10:36:00 +0100 Subject: [PATCH 3/6] Reexport fs functions directly --- src/utils/fs.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/utils/fs.js b/src/utils/fs.js index ff04ead..74dcd54 100644 --- a/src/utils/fs.js +++ b/src/utils/fs.js @@ -2,6 +2,13 @@ import Promise from 'es6-promise/lib/es6-promise/promise.js'; import * as fs from 'fs'; import { dirname } from './path.js'; +export { + lstatSync, + readdirSync, + readFileSync, + realpathSync +} from 'fs'; + function mkdirpath ( path ) { const dir = dirname( path ); try { @@ -25,8 +32,3 @@ 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; From d8c73f707666989460b59200331a6bc64110953d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Wed, 20 Jan 2016 14:52:42 +0100 Subject: [PATCH 4/6] Just re-export them all --- src/utils/fs.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/utils/fs.js b/src/utils/fs.js index 74dcd54..2afe00b 100644 --- a/src/utils/fs.js +++ b/src/utils/fs.js @@ -2,12 +2,7 @@ import Promise from 'es6-promise/lib/es6-promise/promise.js'; import * as fs from 'fs'; import { dirname } from './path.js'; -export { - lstatSync, - readdirSync, - readFileSync, - realpathSync -} from 'fs'; +export * from 'fs'; function mkdirpath ( path ) { const dir = dirname( path ); From 70261d03fab31f4fdfd3eb51f7cabc5cb65a0ec2 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Tue, 20 Dec 2016 16:57:14 -0500 Subject: [PATCH 5/6] linting --- src/utils/defaults.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/defaults.js b/src/utils/defaults.js index 2dc0b74..8db3c0b 100644 --- a/src/utils/defaults.js +++ b/src/utils/defaults.js @@ -1,4 +1,4 @@ -import { lstatSync, readdirSync, readFileSync, realpathSync } from './fs.js'; +import { lstatSync, readdirSync, readFileSync, realpathSync } from './fs.js'; // eslint-disable-line import { basename, dirname, isAbsolute, resolve } from './path.js'; import { blank } from './object.js'; From 490d4281806cc698e5554c700315847fb5e941da Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Tue, 20 Dec 2016 17:49:51 -0500 Subject: [PATCH 6/6] skip symlink test on windows --- test/function/symlink/_config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/function/symlink/_config.js b/test/function/symlink/_config.js index b3895a5..561b738 100644 --- a/test/function/symlink/_config.js +++ b/test/function/symlink/_config.js @@ -1,3 +1,4 @@ module.exports = { + skip: process.platform === 'win32', description: 'follows symlinks' };