Skip to content

Commit

Permalink
Add some path normalization
Browse files Browse the repository at this point in the history
Fixes zont#4 by mentioning the quote requirement in the readme
  • Loading branch information
HoldYourWaffle committed Feb 25, 2019
1 parent 7993e8d commit 4632231
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ options:
--clean - clean target folder on start
```

Please note that you need **quote your globs**. Without quote marks the glob will be expanded by the shell instead of `copy-and-watch` which can create unexpected behavior.

### In your `package.json`

You may have some build script in your package.json involving mirroring folders (let's say, static assets), that's a good use-case for `copy-and-watch`:
Expand All @@ -28,8 +30,8 @@ You may have some build script in your package.json involving mirroring folders
"copy-and-watch": "latest"
},
"scripts": {
"build": "copy-and-watch src/**/*.{html,json} src/**/fonts/* dist",
"watch": "copy-and-watch --watch src/**/*.{html,json} src/**/{fonts,images}/* dist"
"build": "copy-and-watch \"src/**/*.{html,json}\" \"src/**/fonts/*\" dist",
"watch": "copy-and-watch --watch \"src/**/*.{html,json}\" \"src/**/{fonts,images}/*\" dist"
}
}
```
Expand Down
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ for (let arg of args) {
} else if (arg === '--clean') {
clean = true;
} else {
sourceGlobs.push(arg);
sourceGlobs.push(path.normalize(arg));
}
}

Expand All @@ -34,7 +34,7 @@ if (sourceGlobs.length === 0 || destination === undefined) {
process.exit(1);
}

parents = [...new Set(sourceGlobs.map(globParent))];
parents = [...new Set(sourceGlobs.map(globParent).map(path.normalize))];

if (clean) {
console.log('Cleaning...');
Expand Down Expand Up @@ -67,7 +67,7 @@ if (watch) {
/* FUNCTIONS */
function findTarget(from) {
const parent = parents
.filter(p => from.indexOf(p) >= 0)
.filter(parent => from.indexOf(parent) >= 0)
.sort()
.reverse()[0];
return path.join(destination, path.relative(parent, from));
Expand All @@ -92,16 +92,17 @@ function createDirIfNotExist(to) {
}

function copy(from) {
const to = findTarget(from);
const pathFrom = path.normalize(from);
const to = findTarget(pathFrom);
createDirIfNotExist(to);

const stats = fs.statSync(from);
const stats = fs.statSync(pathFrom);

if (stats.isDirectory()) {
fs.readdirSync(from).map(fileName => from+fileName).forEach(copy); //recursively copy directory contents
fs.readdirSync(pathFrom).map(fileName => path.join(pathFrom, fileName)).forEach(copy); //recursively copy directory contents
} else {
fs.writeFileSync(to, fs.readFileSync(from));
console.log('[COPY]'.yellow, from, 'to'.yellow, to);
fs.writeFileSync(to, fs.readFileSync(pathFrom));
console.log('[COPY]'.yellow, pathFrom, 'to'.yellow, to);
}
}

Expand Down

0 comments on commit 4632231

Please sign in to comment.