Skip to content

Commit

Permalink
Introduce Promises for async operations
Browse files Browse the repository at this point in the history
This helps some of the async code easier to read
  • Loading branch information
GeorgeTaveras1231 committed Nov 25, 2016
1 parent f74e5bf commit 525dbba
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 53 deletions.
14 changes: 6 additions & 8 deletions lib/importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
Expand Down
23 changes: 8 additions & 15 deletions lib/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});
}
28 changes: 17 additions & 11 deletions lib/resolver/nearest-package-root.js
Original file line number Diff line number Diff line change
@@ -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;
};
11 changes: 0 additions & 11 deletions lib/resolver/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions test/units/nearest-package-root-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ 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();
});
});

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();
});
Expand Down
14 changes: 8 additions & 6 deletions test/units/resolver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit 525dbba

Please # to comment.