From 525dbba1f01653b0ce729d4de599f24979ee3eb1 Mon Sep 17 00:00:00 2001 From: George Taveras <george.taveras1231@gmail.com> Date: Fri, 25 Nov 2016 14:06:01 -0500 Subject: [PATCH] Introduce Promises for async operations This helps some of the async code easier to read --- lib/importer.js | 14 ++++++------- lib/resolver.js | 23 +++++++------------- lib/resolver/nearest-package-root.js | 28 +++++++++++++++---------- lib/resolver/package.js | 11 ---------- package.json | 1 + test/units/nearest-package-root-test.js | 4 ++-- test/units/resolver-test.js | 14 +++++++------ 7 files changed, 42 insertions(+), 53 deletions(-) diff --git a/lib/importer.js b/lib/importer.js index e87b792..dbc27e9 100644 --- a/lib/importer.js +++ b/lib/importer.js @@ -9,14 +9,12 @@ function importer(url, file, done) { if (err || isLocal) { done({ file: url }); } else { - resolve(url, file, function (err, path) { - if(err) { - done({ file: url }); - return; - } - - done({ file: path }); - }); + resolve(url, file) + .catch(function () { return url; }) + .then(function (path) { + return { file: path }; + }) + .then(done); } }); } diff --git a/lib/resolver.js b/lib/resolver.js index 9c0bcbd..96fea54 100644 --- a/lib/resolver.js +++ b/lib/resolver.js @@ -2,23 +2,16 @@ var Package = require('./resolver/package'); var Import = require('./resolver/import'); var nearestPackageRoot = require('./resolver/nearest-package-root'); -module.exports = function resolve(importUrl, referencePath, callback) { +module.exports = function resolve(importUrl, importOriginPath) { var _import = new Import(importUrl); - Package.find(_import.packageName(), referencePath, function (err, _package) { - if(err) { - callback(err); - return; - } + return nearestPackageRoot(_import.packageName(), importOriginPath).then(function (packageRoot) { + var _package = new Package(packageRoot); - var pathToImporterFile; - - if (_import.isEntrypoint()) { - pathToImporterFile = _package.fullPathToEntrypoint(); - } else { - pathToImporterFile = _package.root(_import.specifiedFilePath()); - } - - callback(null, pathToImporterFile); + if (_import.isEntrypoint()) { + return _package.fullPathToEntrypoint(); + } else { + return _package.root(_import.specifiedFilePath()); + } }); } diff --git a/lib/resolver/nearest-package-root.js b/lib/resolver/nearest-package-root.js index ebacf6a..48529fb 100644 --- a/lib/resolver/nearest-package-root.js +++ b/lib/resolver/nearest-package-root.js @@ -1,23 +1,29 @@ var path = require('path'); var findup = require('findup'); +var Promise = require('bluebird'); /** * @param {string} importOriginPath Path of file that made @import reference */ -module.exports = function nearestPackageRoot(packageName, importOriginPath, callback) { +module.exports = function nearestPackageRoot(packageName, importOriginPath) { var pathToFind = path.join('node_modules', packageName, 'package.json'); + var dirnameOfImportOrigin = path.dirname(importOriginPath); + var promise, handleFoundPath; - findup(path.dirname(importOriginPath), pathToFind, function (err, nearestPackageParent) { - if(err) { - callback(err); - } + promise = new Promise(function (resolve, reject) { + handleFoundPath = function (err, nearestPackageParent) { + if(err) { + reject(err); + return; + } - var packageJSONLocation = path.join( - nearestPackageParent, - 'node_modules', - packageName - ); + var packageJSONLocation = path.join( nearestPackageParent, 'node_modules', packageName); - callback(null, packageJSONLocation); + resolve(packageJSONLocation); + }; }); + + findup(dirnameOfImportOrigin, pathToFind, handleFoundPath); + + return promise; }; diff --git a/lib/resolver/package.js b/lib/resolver/package.js index eb24ed4..3b695f8 100644 --- a/lib/resolver/package.js +++ b/lib/resolver/package.js @@ -8,17 +8,6 @@ function Package(rootPath) { this.JSON = require(this.root('package.json')); }; -Package.find = function find(name, referencePath, callback) { - nearestPackageRoot(name, referencePath, function (err, path) { - if(err) { - callback(err); - return; - } - - callback(null, new Package(path)); - }); -}; - Package.prototype.fullPathToEntrypoint = function fullPathToEntrypoint() { return this.root(this.entrypoint()); }; diff --git a/package.json b/package.json index 57eac83..1aaea42 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "homepage": "https://github.com/lennym/npm-sass", "dependencies": { "argh": "^0.1.4", + "bluebird": "^3.4.6", "camelify": "0.0.2", "findup": "^0.1.5", "glob": "^6.0.1", diff --git a/test/units/nearest-package-root-test.js b/test/units/nearest-package-root-test.js index 70e6805..14ead87 100644 --- a/test/units/nearest-package-root-test.js +++ b/test/units/nearest-package-root-test.js @@ -12,7 +12,7 @@ var fixturesPath = path.resolve.bind(null, describe('nearestPackageRoot', function () { it('finds the nearest module folder based on the import origin', function (done) { var sourcePath = fixturesPath('index.scss'); - nearestPackageRoot('test-module', sourcePath, function (err, result) { + nearestPackageRoot('test-module', sourcePath).then(function (result) { assert.equal(result, fixturesPath('node_modules', 'test-module')); done(); }); @@ -20,7 +20,7 @@ describe('nearestPackageRoot', function () { it('works with nested dependencies', function (done) { var sourcePath = fixturesPath('node_modules', 'test-module', 'index.scss'); - nearestPackageRoot('nested-module', sourcePath, function (err, result) { + nearestPackageRoot('nested-module', sourcePath).then(function (result) { assert.equal(result, fixturesPath('node_modules', 'test-module', 'node_modules', 'nested-module')); done(); }); diff --git a/test/units/resolver-test.js b/test/units/resolver-test.js index e42cb47..89ca17b 100644 --- a/test/units/resolver-test.js +++ b/test/units/resolver-test.js @@ -15,20 +15,22 @@ var fixturePath = path.resolve.bind( describe('resolver', function () { describe('entrypoint import', function () { it('resolves the entrypoint path', function (done) { - resolve('test-package', fixturePath('index.scss'), function (err, result) { + resolve('test-package', fixturePath('index.scss')).then(function (result) { assert.equal(result, fixturePath('node_modules', 'test-package', 'test-package-entrypoint.scss')); - done(); - }); + }) + .then(done) + .catch(done); }); }); describe('non entrypoint import', function () { it('resolves the correct path', function (done) { var importPath = path.join('test-package', 'specific-file-in-package'); - resolve(importPath, fixturePath('index.scss'), function (err, result) { + resolve(importPath, fixturePath('index.scss')).then(function (result) { assert.equal(result, fixturePath('node_modules', 'test-package', 'specific-file-in-package')); - done(); - }); + }) + .then(done) + .catch(done); }); }); });