From 173bfb7c03470d9e6fa0293de4e3bcd4c6525ed2 Mon Sep 17 00:00:00 2001 From: Slava Kim Date: Thu, 23 Jul 2015 12:31:55 -0700 Subject: [PATCH] Expose and use wrapped fs/path moduldes in Plugins --- packages/caching-compiler/caching-compiler.js | 6 +++--- .../multi-file-caching-compiler.js | 3 +-- packages/jshint/plugin/lint-jshint.js | 1 - packages/less/plugin/compile-less.js | 5 ----- packages/stylus/plugin/compile-stylus.js | 20 ++++++++----------- tools/isobuild/isopack.js | 16 ++++++++++++++- .../packages/local-plugin/plugin.js | 8 ++++---- 7 files changed, 31 insertions(+), 28 deletions(-) diff --git a/packages/caching-compiler/caching-compiler.js b/packages/caching-compiler/caching-compiler.js index da1bb60027..e29b5ffb15 100644 --- a/packages/caching-compiler/caching-compiler.js +++ b/packages/caching-compiler/caching-compiler.js @@ -1,5 +1,5 @@ -const fs = Npm.require('fs'); -const path = Npm.require('path'); +const fs = Plugin.fs; +const path = Plugin.path; const createHash = Npm.require('crypto').createHash; const assert = Npm.require('assert'); const Future = Npm.require('fibers/future'); @@ -178,7 +178,7 @@ CachingCompilerBase = class CachingCompilerBase { // doesn't exist. _readFileOrNull(filename) { try { - return fs.readFileSync(filename, 'utf8'); + return fs.readFile(filename, 'utf8'); } catch (e) { if (e && e.code === 'ENOENT') return null; diff --git a/packages/caching-compiler/multi-file-caching-compiler.js b/packages/caching-compiler/multi-file-caching-compiler.js index 3668adf3e3..3cbee6e573 100644 --- a/packages/caching-compiler/multi-file-caching-compiler.js +++ b/packages/caching-compiler/multi-file-caching-compiler.js @@ -1,5 +1,4 @@ -const fs = Npm.require('path'); -const path = Npm.require('path'); +const path = Plugin.path; const Future = Npm.require('fibers/future'); const LRU = Npm.require('lru-cache'); const async = Npm.require('async'); diff --git a/packages/jshint/plugin/lint-jshint.js b/packages/jshint/plugin/lint-jshint.js index 665b05d21e..a7b7d80833 100644 --- a/packages/jshint/plugin/lint-jshint.js +++ b/packages/jshint/plugin/lint-jshint.js @@ -1,6 +1,5 @@ var util = Npm.require('util'); var Future = Npm.require('fibers/future'); -var path = Npm.require('path'); var jshint = Npm.require('jshint').JSHINT; Plugin.registerLinter({ diff --git a/packages/less/plugin/compile-less.js b/packages/less/plugin/compile-less.js index fa802f4379..cfc26794ab 100644 --- a/packages/less/plugin/compile-less.js +++ b/packages/less/plugin/compile-less.js @@ -138,11 +138,6 @@ class MeteorImportLessFileManager extends less.AbstractFileManager { resolvedFilename = path.join(currentDirectory, filename); } - if (process.platform === 'win32') { - // convert the path back to standard path (backslashes to forward slashes) - resolvedFilename = resolvedFilename.replace(/\\/g, '/'); - } - if (!this.allFiles.has(resolvedFilename)) { cb({type: 'File', message: 'Unknown import: ' + filename}); return; diff --git a/packages/stylus/plugin/compile-stylus.js b/packages/stylus/plugin/compile-stylus.js index ef721a915d..cca6c0d47f 100644 --- a/packages/stylus/plugin/compile-stylus.js +++ b/packages/stylus/plugin/compile-stylus.js @@ -1,8 +1,8 @@ const stylus = Npm.require('stylus'); const nib = Npm.require('nib'); const Future = Npm.require('fibers/future'); -const fs = Npm.require('fs'); -const path = Npm.require('path'); +const fs = Plugin.fs; +const path = Plugin.path; Plugin.registerCompiler({ extensions: ['styl'], @@ -87,9 +87,8 @@ class StylusCompiler extends MultiFileCachingCompiler { if (importPath[0] !== '{') { // if it is not a custom syntax path, it could be a lookup in a folder for (let i = paths.length - 1; i >= 0; i--) { - const joined = path.join(paths[i], importPath) - .replace(/\\/g, '/'); // XXX turn Windows paths back into standard path - if (fs.existsSync(joined)) + const joined = path.join(paths[i], importPath); + if (fs.exists(joined)) return [joined]; } } @@ -103,18 +102,15 @@ class StylusCompiler extends MultiFileCachingCompiler { return [absolutePath]; }, readFile(filePath) { - const isAbsolute = (process.platform === 'win32') ? - filePath[0].match(/^[A-Za-z]:\\/) : filePath[0] === '/'; - const normalizedPath = (process.platform === 'win32') ? - filePath.replace(/\\/g, '/') : filePath; + const isAbsolute = filePath[0] === '/'; const isNib = - normalizedPath.indexOf('/node_modules/nib/lib/nib/') !== -1; + filePath.indexOf('/node_modules/nib/lib/nib/') !== -1; const isStylusBuiltIn = - normalizedPath.indexOf('/node_modules/stylus/lib/') !== -1; + filePath.indexOf('/node_modules/stylus/lib/') !== -1; if (isAbsolute || isNib || isStylusBuiltIn) { // absolute path? let the default implementation handle this - return fs.readFileSync(filePath, 'utf8'); + return fs.readFile(filePath, 'utf8'); } const parsed = parseImportPath(filePath); diff --git a/tools/isobuild/isopack.js b/tools/isobuild/isopack.js index 4cb1c291e2..20f3efb7e1 100644 --- a/tools/isobuild/isopack.js +++ b/tools/isobuild/isopack.js @@ -814,7 +814,21 @@ _.extend(Isopack.prototype, { nudge: function () { Console.nudge(true); - } + }, + + convertToOSPath: files.convertToOSPath, + convertToStandardPath: files.convertToStandardPath, + path: { + join: files.pathJoin, + normalize: files.pathNormalize, + relative: files.pathRelative, + resolve: files.pathResolve, + dirname: files.pathDirname, + basename: files.pathBasename, + extname: files.pathExtname, + sep: files.pathSep + }, + fs: files }; return Plugin; }, diff --git a/tools/tests/apps/local-compiler-plugin/packages/local-plugin/plugin.js b/tools/tests/apps/local-compiler-plugin/packages/local-plugin/plugin.js index 0a591d36cf..9758047a1d 100644 --- a/tools/tests/apps/local-compiler-plugin/packages/local-plugin/plugin.js +++ b/tools/tests/apps/local-compiler-plugin/packages/local-plugin/plugin.js @@ -1,5 +1,5 @@ -var fs = Npm.require('fs'); -var path = Npm.require('path'); +var fs = Plugin.fs; +var path = Plugin.path; Plugin.registerCompiler({ extensions: ['printme'], @@ -25,14 +25,14 @@ PrintmeCompiler.prototype.processFilesForTarget = function (inputFiles) { }); console.log("PrintmeCompiler invocation", ++self.runCount); if (self.diskCache) { - fs.writeFileSync(self.diskCache, self.runCount + '\n'); + fs.writeFile(self.diskCache, self.runCount + '\n'); } }; PrintmeCompiler.prototype.setDiskCacheDirectory = function (diskCacheDir) { var self = this; self.diskCache = path.join(diskCacheDir, 'cache'); try { - var data = fs.readFileSync(self.diskCache, 'utf8'); + var data = fs.readFile(self.diskCache, 'utf8'); } catch (e) { if (e.code !== 'ENOENT') throw e;