-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
141 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
var fs = require("fs") | ||
var path = require("path") | ||
var resolveModule = require("resolve") | ||
|
||
var moduleDirectories = [ | ||
"web_modules", | ||
"node_modules", | ||
] | ||
|
||
function isFile(path) { | ||
return new Promise(function(resolve, reject) { | ||
fs.stat(path, function(err, stat) { | ||
if (err) { | ||
if (err.code === "ENOENT") { | ||
return resolve(false) | ||
} | ||
return reject(err) | ||
} | ||
resolve(stat.isFile() || stat.isFIFO()) | ||
}) | ||
}) | ||
} | ||
|
||
function getFirstExistingFile(files) { | ||
return files.reduce(function(promise, file) { | ||
return promise.then(function(resolvedFile) { | ||
if (resolvedFile) { | ||
return resolvedFile | ||
} | ||
return isFile(file).then(function(is) { | ||
if (is) { | ||
return file | ||
} | ||
}) | ||
}) | ||
}, Promise.resolve()) | ||
} | ||
|
||
module.exports = function(id, base, paths) { | ||
var files = [ base ].concat(paths).reduce(function(files, p) { | ||
files.push(path.resolve(p, id)) | ||
files.push(path.resolve(p, id + ".css")) | ||
files.push(path.resolve(p, id, "index.css")) | ||
return files | ||
}, []) | ||
|
||
return getFirstExistingFile(files).then(function(existing) { | ||
if (existing) { | ||
return existing | ||
} | ||
var opts = { | ||
basedir: path.resolve(base), | ||
moduleDirectory: moduleDirectories, | ||
extensions: [ ".css" ], | ||
packageFilter: function(pkg) { | ||
if (!/\.css$/.test(pkg.main)) { | ||
pkg.main = pkg.style || "index.css" | ||
} | ||
return pkg | ||
}, | ||
} | ||
|
||
return new Promise(function(resolve) { | ||
resolveModule(id, opts, function(err, existing) { | ||
if (err) { | ||
return resolve() | ||
} | ||
resolve(existing) | ||
}) | ||
}) | ||
}).then(function(existing) { | ||
if (existing) { | ||
return existing | ||
} | ||
throw new Error([ | ||
"Failed to find '" + id + "'", | ||
"in [ ", | ||
" " + paths.join(",\n "), | ||
"]", | ||
].join("\n ")) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters