This repository has been archived by the owner on Aug 8, 2019. It is now read-only.
forked from zertosh/dep-case-verify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdep-case-verify.js
98 lines (85 loc) · 2.42 KB
/
dep-case-verify.js
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
'use strict';
var fs = require('fs');
var path = require('path');
var AsyncCache = require('async-cache');
var through = require('through2');
// turns "/a/b/c.js" into ["/a", "/a/b", "/a/b/c.js"]
function pathSteps(pathString) {
return pathString
.split('/')
.map(function(part, i, parts) {
return parts.slice(0, i + 1).join('/');
})
.filter(Boolean);
}
function error(stream, row, step) {
var id = path.relative(process.cwd(), row.id);
Object.keys(row.deps).some(function(key) {
if (row.deps[key].indexOf(step) !== -1) {
var err = new Error('Unmatched case in "' + id + '" for "' + key + '"');
stream.emit('error', err);
return true;
}
});
}
module.exports = function apply(b, opts) {
// nothing to do if this isn't a mac
if (process.platform !== 'darwin') {
return;
}
// AsyncCache has a default "max" of "Infinity".
// since this closure only lives for the duration of any
// one "bundle()", it's safe to assume that the source file
// directory listings won't change during a build
var readdir = new AsyncCache({
load: function(key, cb) {
fs.readdir(key, cb);
}
});
// assume that everything up to the cwd is valid
var skipSteps = pathSteps(process.cwd());
b.pipeline.get('deps').push(through.obj(function(row, enc, next) {
var stream = this;
var steps = [];
Object.keys(row.deps).forEach(function(key) {
// external modules have a value of "false"
if (typeof row.deps[key] === 'string') {
pathSteps(row.deps[key]).forEach(function(step) {
if (skipSteps.indexOf(step) === -1) {
steps.push(step);
}
});
}
});
// nothing to do
if (steps.length === 0) {
stream.push(row);
next();
return;
}
steps.forEach(function(step) {
var basename = path.basename(step);
var dirname = path.dirname(step);
readdir.get(dirname, function(err, files) {
if (err) {
stream.emit('error', err);
return;
}
if (files.indexOf(basename) === -1) {
// don't emit the broken row because that'll
// trip up watchify
error(stream, row, step);
return;
}
steps.splice(steps.indexOf(step), 1);
if (steps.length === 0) {
stream.push(row);
next();
}
});
});
}));
b.once('reset', function() {
apply(b, opts);
});
};