Skip to content

Instantly share code, notes, and snippets.

@yllieth
Last active May 13, 2019 20:18
Show Gist options
  • Save yllieth/fcf2bc7f278739e7e46840cea529c539 to your computer and use it in GitHub Desktop.
Save yllieth/fcf2bc7f278739e7e46840cea529c539 to your computer and use it in GitHub Desktop.
Cleaning generated files from ts/less compilation
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');
});
@yllieth
Copy link
Author

yllieth commented May 13, 2019

Situation when this snippet applies

  • you are compiling typescript, less, scss files into js, css files in the same folder
  • and your compilation is creating map files
  • and you want to clean generated files without deleting other files

Important notes:

  • When additional extensions are given, the source file (.ts, .less) has to be there in order to delete the file with this additional extension. For example, let's say you had a gulpfile.ts which generated gulpfile.js and gulpfile.js.map. Then you decide to abandon the ts and only keep a js file, but you didn't remove the .js.map. Without this condiction, the task would have removed the js file, event if it's now your original source file.

Receipe for typescript compilation:

  • look for .js.map files
  • when you find one having in the same folder a file with the same name but the .ts and .js enxtensions, you can consider .js.map and .js files as generated by the compilation and remove them

Example:

// content of a folder
--- filename ---               --- description ---              --- will be deleted ? ---
a.component.ts            // original source file                      no
a.component.js            // generated file                            yes
a.component.js.map        // generated file                            yes
a.component.less          // original source file                      no
a.component.css           // generated file                            yes
a.component.css.map       // generated file                            yes
a.module.js               // another file                              no

Outputs

  • when debug mode is set to true, each map file will outputs something like:
Mapfile considered: registrars.js.map (folder: ., filename: registrars)
> Potential source file ./registrars.ts: found
> Files to delete: 2
  - ./registrars.js.map
  - ./registrars.js

... and files won't be really removed

  • when debug mode is set to false, we only prints a summary once all the files are removed
[16:17:59] Using gulpfile D:\code\portal\api\static\public\gulpfile.js
[16:17:59] Starting 'typescript:clean'...
[16:17:59] Finished 'typescript:clean' after 3.3 ms
1900 generated files from typescript compilation deleted in 568 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment