Last active
May 13, 2019 20:18
-
-
Save yllieth/fcf2bc7f278739e7e46840cea529c539 to your computer and use it in GitHub Desktop.
Cleaning generated files from ts/less compilation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cleanGeneratedFilesBasedOn = (mapFileExtension, sourceFileExtension, otherExtensionsToDelete) => { | |
const debug = true; | |
const startTime = new Date().getTime(); | |
if (!Array.isArray(otherExtensionsToDelete)) { | |
otherExtensionsToDelete = []; | |
} | |
const pattern = '{,!(node_modules)/**/}*' + mapFileExtension; | |
return glob(pattern, (err, files) => { | |
let toDelete = []; | |
for (let file of files) { | |
const siblingsToDelete = []; | |
const folder = path.dirname(file); | |
const filename = path.basename(file).slice(0, -mapFileExtension.length); | |
const srcFilePath = `${folder}/${filename}${sourceFileExtension}` | |
const hasSiblingSrcFile = fs.existsSync(srcFilePath); | |
if (hasSiblingSrcFile) { | |
siblingsToDelete.push(`${folder}/${filename}${mapFileExtension}`); | |
} | |
for (let otherExtensionToDelete of otherExtensionsToDelete) { | |
const otherPath = `${folder}/${filename}${otherExtensionToDelete}` | |
const hasSiblingFileToDelete = fs.existsSync(otherPath); | |
if (hasSiblingSrcFile && hasSiblingFileToDelete) { | |
siblingsToDelete.push(otherPath); | |
} | |
} | |
if (debug) { | |
console.debug(`Mapfile considered: ${file} (folder: ${folder}, filename: ${filename})`); | |
console.debug(`> Potential source file ${srcFilePath}: ${hasSiblingSrcFile ? 'found' : 'not found'}`); | |
console.debug(`> Files to delete: ${siblingsToDelete.length}`); | |
for (let pathToDelete of siblingsToDelete) { | |
console.debug(` - ${pathToDelete}`); | |
} | |
} | |
toDelete = toDelete.concat(siblingsToDelete); | |
} | |
return del(toDelete, {dryRun: debug}) | |
.then(deletedPaths => console.debug(`${deletedPaths.length} generated files from typescript compilation deleted in ${new Date().getTime() - startTime} ms`)); | |
}); | |
} | |
gulp.task('typescript:clean', () => { | |
return cleanGeneratedFilesBasedOn('.js.map', '.ts', ['.js']); | |
}); | |
gulp.task('less:clean', () => { | |
return cleanGeneratedFilesBasedOn('.css.map', '.less'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Situation when this snippet applies
Important notes:
gulpfile.ts
which generatedgulpfile.js
andgulpfile.js.map
. Then you decide to abandon thets
and only keep ajs
file, but you didn't remove the.js.map
. Without this condiction, the task would have removed thejs
file, event if it's now your original source file.Receipe for typescript compilation:
.js.map
files.ts
and.js
enxtensions, you can consider.js.map
and.js
files as generated by the compilation and remove themExample:
Outputs
true
, each map file will outputs something like:... and files won't be really removed
false
, we only prints a summary once all the files are removed