UNPKG

2.5 kBJavaScriptView Raw
1
2var
3 _ = require('underscore'),
4 when = require('when'),
5 packageInfo = require('./package.json'),
6 fs = require('final-fs'),
7 child = require('child_process'),
8 CoffeeScript = require('coffee-script'),
9 File = require('./lib/file'),
10 Directory = require('./lib/directory');
11
12require('colors');
13
14// Recursive directory scraping / loading / callbacks / watching / callbacks
15// options to compile
16var loaddir = function(options) {
17
18 var directory;
19
20 options = options ? _.clone(options) : {};
21
22 if (options.recursive == null)
23 options.recursive = true;
24
25 // normalize extensions -- no dot ( i.e. 'html', 'js')
26 if (options.extension && options.extension[0] === '.')
27 options.extension = options.extension.substring(1);
28
29 // normalize path -- no ending slash
30 if ('/' === _.last(options.path))
31 options.path = options.path.slice(0, -1);
32
33 // HACK: first run through uses black list and other features
34 options.top = true;
35
36 // This has all of the files added to it
37 options.output = {};
38
39 return when().then(function(){
40 if (options.manifest) {
41 if (options.manifest.substring(options.manifest.length - 5) !== '.json')
42 options.manifest += '.json';
43 return fs.exists(options.manifest);
44 }
45 }).then(function(exists){
46 if (exists)
47 return fs.readFile(options.manifest);
48 }).then(function(manifest){
49
50 if (manifest) {
51 try { manifest = JSON.parse(manifest) }
52 catch(er) { manifest = undefined; }
53 }
54
55 options.existingManifest = manifest;
56
57 // returns Promise
58 var dir = new Directory(options)
59 return dir.load().then(function() {
60 // Top directory doesn't get stats loaded
61 return fs.lstat(dir.path);
62 }).then(function(stats){
63 dir.stats = stats
64
65 // Build manifest file
66 if (options.manifest) {
67
68 if (options.require) throw new Error('Cannot build a manifest for directly required files');
69
70 fs.writeFile(options.manifest, JSON.stringify(dir.buildManifest(), null, ' '));
71
72 }
73 return options.output;
74
75 }).otherwise(function(er){
76 console.log("loaddir error: ".blue, (er + '').red, er.stack);
77 });
78 });
79
80};
81
82module.exports.File = File;
83module.exports.Directory = Directory;
84
85// Save a reference to loaddir for testing loaddir options
86File.prototype.loaddir = Directory.prototype.loaddir = loaddir;
87
88loaddir.package = packageInfo;
89loaddir.version = packageInfo.version;
90module.exports = loaddir;