Skip to content

Commit a96b518

Browse files
committed
fix(overwrite): Truncate old contents before appending again
1 parent 3f43b29 commit a96b518

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

index.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ Promise.resolve()
7979

8080
input = i
8181

82+
truncateDestinationFile()
83+
8284
return files(input)
8385
})
8486
.then(results => {
@@ -97,11 +99,15 @@ Promise.resolve()
9799
.on('change', file => {
98100
let recompile = []
99101

100-
if (~input.indexOf(file)) recompile.push(file)
102+
if (!shouldAppendCSS()) {
103+
if (~input.indexOf(file)) recompile.push(file)
101104

102-
recompile = recompile.concat(
103-
depGraph.dependantsOf(file).filter(file => ~input.indexOf(file))
104-
)
105+
recompile = recompile.concat(
106+
depGraph.dependantsOf(file).filter(file => ~input.indexOf(file)),
107+
)
108+
} else {
109+
truncateDestinationFile()
110+
}
105111

106112
if (!recompile.length) recompile = input
107113

@@ -307,3 +313,19 @@ function shouldAppendCSS() {
307313
return hasMultipleFiles && hasOutputFile
308314
}
309315

316+
function truncateDestinationFile() {
317+
Promise.resolve()
318+
.then(() => {
319+
return fs.pathExists(output)
320+
})
321+
.then((exists) => {
322+
if (exists) {
323+
fs.truncate(output, err => {
324+
if (err) return error('Output Error : Cannot truncate output file.')
325+
})
326+
}
327+
})
328+
.catch(ex => {
329+
return error(ex);
330+
})
331+
}

0 commit comments

Comments
 (0)