progeny
Recursively finds dependencies of style and template source files.
Or configure it to do the same kind of thing with any other type of code file
that has an import
-type syntax.
Usage
progeny ([config]) (path, [sourceContents], callback)
progeny.Sync ([config]) (path, [sourceContents])
Call progeny with an optional configuration object, it returns a reusable
function. Call that function with a path to a source file (and its
source code if you already have it handy), and it will figure out all of that
file's dependencies and sub-dependencies, passing an array of them to your
callback. Or use the Sync
API to get the results as a return value.
Result array has non-enumerable patterns
property with an array of
glob patterns found in source files' import
statements.
Examples using path
assume you already have var path = require('path');
.
You could just use strings like '/path/to/project'
, but you may run into
cross-compatibility issues.
Quick and Simple
You can skip the config object and the source code, letting Progeny read
the source from the file itself and apply a built-in configuration based on the file extension.
var progeny = require('progeny');
var filePath = path.join('path', 'to', 'project', 'style-or-template.pug');
progeny()(filePath, function (err, dependencies) {
});
var dependencies = progeny()(filePath);
Configuration
There are
built-in configurations
already for css
, sass
/scss
, less
, stylus
, pug
/jade
, slm
, and proto
.
Configuration must be specified for any other formats. Feel free to submit Pull
Requests to add default types, or improve the settings for the existing ones.
var progenyConfig = {
extension: 'styl',
extensionsList: ['scss', 'sass'],
regexp: /^\s*@import\s+['"]?([^'"]+)['"]?/,
prefix: '_',
exclusion: /^compass/,
rootPath: path.join('path', 'to', 'project'),
altPaths: [
path.join('path', 'to', 'shared'),
path.join('path', 'to', 'common')
],
multipass: [
/@import[^;]+;/g,
/\s*['"][^'"]+['"]\s*,?/g,
/(?:['"])([^'"]+)/
],
potentialDeps: true,
skipComments: true;
resolver: function (depFilename, parentDir, parentFilename) {
if (depFilename.startsWith('~')) {
var absPath = path.resolve(path.join('.', 'node_modules', depFilename.substr(1)));
return path.relative(parentDir, absPath);
}
},
debug: true
};
More Examples
Process a list of files:
var progeny = require('progeny');
var getDependencies = progeny(progenyConfig);
myFiles.forEach(function (file) {
getDependencies(file.path, file.source, function (err, deps) {
if (err) throw new Error(err);
file.dependencies = deps;
});
});
Multiple configs:
var getDefaultDependencies = progeny();
var getCustomDependencies = progeny({
extension: 'foo',
regexp: /([^\s,]+)/
});
Process source code from a string without its file path:
var mySourceString;
progeny({
extension: 'less',
rootPath: path.join('path', 'to', 'project')
})(null, mySourceString, function (err, deps) {});
Change Log
See release notes page on GitHub
License
MIT