Repository navigation
Expand file tree
/
Copy pathfile.js
More file actions
173 lines (143 loc) · 3.28 KB
/
Copy pathfile.js
File metadata and controls
173 lines (143 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Module dependencies.
*/
var debug = require('debug')('duo-file');
var clone = require('component-clone');
var delegate = require('delegates');
var filedeps = require('file-deps');
var extend = require('extend.js');
var exists = require('co-exists');
var stat = require('fs').statSync;
var thunk = require('thunkify');
var mask = require('json-mask');
var path = require('path');
var relative = path.relative;
var extname = path.extname;
var resolve = path.resolve;
var fs = require('co-fs');
/**
* Export `File`.
*/
module.exports = File;
/**
* Fields in `duo.json`.
*/
var fields = 'id,type,mtime,src,deps,entry';
/**
* Initialize a new `File` with `attrs`.
*
* @param {Object} attrs
* @param {Duo} duo
* @return {File}
* @api public
*/
function File(attrs, duo) {
if (!(this instanceof File)) return new File(attrs, duo);
this.attrs = clone(attrs || {});
this.duo = duo;
var type = attrs.type ? '.' + attrs.type : '';
var path = attrs.path || 'source' + type;
// initial attrs
this.attrs.path = resolve(attrs.root, path);
this.attrs.type = attrs.type || extension(this.path);
this.attrs.id = relative(duo.root(), this.path);
this.attrs.mtime = attrs.mtime || this.modified(this.path);
}
/**
* Delegates.
*/
delegate(File.prototype, 'attrs')
.access('id')
.access('src')
.access('raw')
.access('type')
.access('root')
.access('path')
.access('entry')
.access('mtime');
delegate(File.prototype, 'duo')
.getter('plugins')
.method('include');
/**
* Get the dependencies of the file.
*
* @return {Array}
* @api public
*/
File.prototype.dependencies =
8000
function () {
if (this.deps) return this.deps;
if (!this.src) return [];
return this.deps = filedeps(this.src, this.type);
};
/**
* Load the files source, running any transforms before returning it.
*
* @api public
*/
File.prototype.load = function *() {
if (this.src) return this;
// read the file
this.attrs.src = this.raw = this.raw
|| (yield fs.readFile(this.path, 'utf8'));
// transform the file and update attributes
var entry = this.entry ? this : this.duo.entry();
var res = yield this.plugins.run(this, entry);
this.set(res[0].json());
return this;
};
/**
* Check if the path to the file exists, unless we already have the raw source.
*
* @return {Boolean}
* @api public
*/
File.prototype.exists = function *() {
if (this.raw) return true;
else return yield exists(this.path);
};
/**
* Set additional `attrs` on the file.
*
* @param {Object} attrs
* @return {File}
* @api public
*/
File.prototype.set = function (attrs) {
extend(this.attrs, attrs || {});
return this;
};
/**
* Get a JSON representation of the file.
*
* @return {Object}
* @api public
*/
File.prototype.json =
File.prototype.toString = function () {
var json = clone(this.attrs);
if (!json.entry) delete json.entry;
return mask(json, fields);
};
/**
* Get the mtime of a `path`, falling back to the current time.
*
* @param {String} path
* @return {Number}
* @api private
*/
File.prototype.modified = function (path) {
try {
return stat(path).mtime.getTime();
} catch (e) {
return (new Date()).getTime();
}
};
/**
* Get the extension of a `path`.
*
* @param {String} path
* @return {Strign} ext
*/
function extension(path) {
return extname(path).slice(1);
}