Skip to content

Commit 69e8b7f

Browse files
committed
Fix oc-cli breaking on Windows when modifying files
1 parent 31d641b commit 69e8b7f

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/cli/domain/watch.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = function(dirs, baseDir, changed) {
1919
(fileName, currentStat, previousStat) => {
2020
if (!!currentStat || !!previousStat) {
2121
const componentDir = dirs.find(dir =>
22-
Boolean(fileName.match(dir + path.sep))
22+
Boolean(fileName.match(escapeRegularExpression(dir + path.sep)))
2323
);
2424
changed(null, fileName, componentDir);
2525
}
@@ -29,3 +29,7 @@ module.exports = function(dirs, baseDir, changed) {
2929
changed(err);
3030
}
3131
};
32+
33+
function escapeRegularExpression(text) {
34+
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
35+
}

test/unit/cli-domain-watch.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const injectr = require('injectr');
55
const sinon = require('sinon');
66

77
describe('cli : domain : watch', () => {
8-
const execute = (fileChanged, cb) => {
8+
const execute = (fileChanged, separator, cb) => {
99
class Stats {
1010
constructor(settings) {
1111
this.stuff = settings.stuff;
@@ -15,7 +15,7 @@ describe('cli : domain : watch', () => {
1515
const watch = injectr('../../src/cli/domain/watch.js', {
1616
path: {
1717
resolve: x => x,
18-
sep: '/'
18+
sep: separator
1919
},
2020
watch: {
2121
watchTree: sinon
@@ -30,6 +30,7 @@ describe('cli : domain : watch', () => {
3030

3131
watch(
3232
[
33+
'C:\\Windows-like\\path\\to\\yet-another-component',
3334
'/path/to/my-component',
3435
'/path/to/my-component2',
3536
'/path/to/some-other-component'
@@ -44,6 +45,7 @@ describe('cli : domain : watch', () => {
4445
before(done => {
4546
execute(
4647
'/path/to/my-component/server.js',
48+
'/',
4749
(error, fileName, componentDir) => {
4850
result = { error, fileName, componentDir };
4951
done();
@@ -69,6 +71,7 @@ describe('cli : domain : watch', () => {
6971
before(done => {
7072
execute(
7173
'/path/to/my-component2/server.js',
74+
'/',
7275
(error, fileName, componentDir) => {
7376
result = { error, fileName, componentDir };
7477
done();
@@ -88,4 +91,34 @@ describe('cli : domain : watch', () => {
8891
expect(result.componentDir).to.equal('/path/to/my-component2');
8992
});
9093
});
94+
95+
describe('when a file from a component on Windows-like path changes', () => {
96+
let result;
97+
before(done => {
98+
execute(
99+
'C:\\Windows-like\\path\\to\\yet-another-component\\server.js',
100+
'\\',
101+
(error, fileName, componentDir) => {
102+
result = { error, fileName, componentDir };
103+
done();
104+
}
105+
);
106+
});
107+
108+
it('should return no error', () => {
109+
expect(result.error).to.be.null;
110+
});
111+
112+
it('should return the fileName', () => {
113+
expect(result.fileName).to.equal(
114+
'C:\\Windows-like\\path\\to\\yet-another-component\\server.js'
115+
);
116+
});
117+
118+
it('should return the component folder', () => {
119+
expect(result.componentDir).to.equal(
120+
'C:\\Windows-like\\path\\to\\yet-another-component'
121+
);
122+
});
123+
});
91124
});

0 commit comments

Comments
 (0)