1 |
|
2 | var
|
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 |
|
12 | require('colors');
|
13 |
|
14 |
|
15 |
|
16 | var 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 |
|
26 | if (options.extension && options.extension[0] === '.')
|
27 | options.extension = options.extension.substring(1);
|
28 |
|
29 |
|
30 | if ('/' === _.last(options.path))
|
31 | options.path = options.path.slice(0, -1);
|
32 |
|
33 |
|
34 | options.top = true;
|
35 |
|
36 |
|
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 |
|
58 | var dir = new Directory(options)
|
59 | return dir.load().then(function() {
|
60 |
|
61 | return fs.lstat(dir.path);
|
62 | }).then(function(stats){
|
63 | dir.stats = stats
|
64 |
|
65 |
|
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 |
|
82 | module.exports.File = File;
|
83 | module.exports.Directory = Directory;
|
84 |
|
85 |
|
86 | File.prototype.loaddir = Directory.prototype.loaddir = loaddir;
|
87 |
|
88 | loaddir.package = packageInfo;
|
89 | loaddir.version = packageInfo.version;
|
90 | module.exports = loaddir;
|