This repository was archived by the owner on Jun 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathNode.js
More file actions
241 lines (190 loc) · 5.86 KB
/
Copy pathNode.js
File metadata and controls
241 lines (190 loc) · 5.86 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { EventEmitter2 } from 'eventemitter2';
import * as crc32 from 'buffer-crc32';
import { lsrSync, readFileSync, rimraf } from 'sander';
import { join, resolve } from 'path';
import * as requireRelative from 'require-relative';
import { grab, include, map as mapTransform, move } from '../builtins';
import { Observer, Transformer } from './index';
import config from '../config';
import GobbleError from '../utils/GobbleError';
import assign from '../utils/assign';
import warnOnce from '../utils/warnOnce';
import compareBuffers from '../utils/compareBuffers';
import serve from './serve';
import build from './build';
import watch from './watch';
import { isRegExp } from '../utils/is';
import { ABORTED } from '../utils/signals';
export default class Node extends EventEmitter2 {
constructor () {
this._gobble = true; // makes life easier for e.g. gobble-cli
// initialise event emitter
super({ wildcard: true });
this.counter = 1;
this.inspectTargets = [];
}
// This gets overwritten each time this.ready is overwritten. Until
// the first time that happens, it's a noop
_abort () {}
_findCreator () {
return this;
}
build ( options ) {
return build( this, options );
}
createWatchTask () {
const node = this;
const watchTask = new EventEmitter2({ wildcard: true });
// TODO is this the best place to handle this stuff? or is it better
// to pass off the info to e.g. gobble-cli?
let previousDetails;
node.on( 'info', details => {
if ( details === previousDetails ) return;
previousDetails = details;
watchTask.emit( 'info', details );
});
let buildScheduled;
node.on( 'invalidate', changes => {
// A node can depend on the same source twice, which will result in
// simultaneous rebuilds unless we defer it to the next tick
if ( !buildScheduled ) {
buildScheduled = true;
watchTask.emit( 'info', {
changes,
code: 'BUILD_INVALIDATED'
});
process.nextTick( build );
}
});
node.on( 'error', handleError );
function build () {
const buildStart = Date.now();
buildScheduled = false;
node.ready().then( d => {
watchTask.emit( 'info', {
code: 'BUILD_COMPLETE',
duration: Date.now() - buildStart,
watch: true
});
watchTask.emit( 'built', d );
}).catch( handleError );
}
function handleError ( e ) {
if ( e === ABORTED ) {
// these happen shortly after an invalidation,
// we can ignore them
return;
} else {
watchTask.emit( 'error', e );
}
}
watchTask.close = () => node.stop();
this.start();
build();
return watchTask;
}
exclude ( patterns ) {
if ( typeof patterns === 'string' ) { patterns = [ patterns ]; }
return new Transformer( this, include, { patterns, exclude: true });
}
getChanges ( inputdir ) {
const files = lsrSync( inputdir );
if ( !this._files ) {
this._files = files;
this._checksums = {};
files.forEach( file => {
this._checksums[ file ] = crc32( readFileSync( inputdir, file ) );
});
return files.map( file => ({ file, added: true }) );
}
const added = files.filter( file => !~this._files.indexOf( file ) ).map( file => ({ file, added: true }) );
const removed = this._files.filter( file => !~files.indexOf( file ) ).map( file => ({ file, removed: true }) );
const maybeChanged = files.filter( file => ~this._files.indexOf( file ) );
let changed = [];
maybeChanged.forEach( file => {
let checksum = crc32( readFileSync( inputdir, file ) );
if ( !compareBuffers( checksum, this._checksums[ file ] ) ) {
changed.push({ file, changed: true });
this._checksums[ file ] = checksum;
}
});
return added.concat( removed ).concat( changed );
}
grab () {
const src = join.apply( null, arguments );
return new Transformer( this, grab, { src });
}
// Built-in transformers
include ( patterns ) {
if ( typeof patterns === 'string' ) { patterns = [ patterns ]; }
return new Transformer( this, include, { patterns });
}
inspect ( target, options ) {
target = resolve( config.cwd, target );
if ( options && options.clean ) {
rimraf( target );
}
this.inspectTargets.push( target );
return this; // chainable
}
map ( fn, userOptions ) {
warnOnce( 'node.map() is deprecated. You should use node.transform() instead for both file and directory transforms' );
return this.transform( fn, userOptions );
}
moveTo () {
const dest = join.apply( null, arguments );
return new Transformer( this, move, { dest });
}
observe ( fn, userOptions ) {
if ( typeof fn === 'string' ) {
fn = tryToLoad( fn );
}
return new Observer( this, fn, userOptions );
}
observeIf ( condition, fn, userOptions ) {
return condition ? this.observe( fn, userOptions ) : this;
}
serve ( options ) {
return serve( this, options );
}
transform ( fn, userOptions ) {
if ( typeof fn === 'string' ) {
fn = tryToLoad( fn );
}
// If function takes fewer than 3 arguments, it's a file transformer
if ( fn.length < 3 ) {
const options = assign( {}, fn.defaults, userOptions, {
fn,
cache: {},
userOptions: assign( {}, userOptions )
});
if ( typeof options.accept === 'string' || isRegExp( options.accept ) ) {
options.accept = [ options.accept ];
}
return new Transformer( this, mapTransform, options, fn.id || fn.name );
}
// Otherwise it's a directory transformer
return new Transformer( this, fn, userOptions );
}
transformIf ( condition, fn, userOptions ) {
return condition ? this.transform( fn, userOptions ) : this;
}
watch ( options ) {
return watch( this, options );
}
}
function tryToLoad ( plugin ) {
try {
return requireRelative( `gobble-${plugin}`, process.cwd() );
} catch ( err ) {
if ( err.message === `Cannot find module 'gobble-${plugin}'` ) {
throw new GobbleError({
message: `Could not load gobble-${plugin} plugin`,
code: 'PLUGIN_NOT_FOUND',
plugin: plugin
});
} else {
throw err;
}
}
}