-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcleanDist.mjs
58 lines (46 loc) · 1.6 KB
/
cleanDist.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Loop through the input dir looking for markdown files and removes those.
*
* By default, any README.md file is always included in the npm package.
* We preserve the README.md file at package level excluding the ones at component level.
*/
import { existsSync, readdirSync, statSync, unlinkSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import pc from 'picocolors';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const checkDirectory = (path) => existsSync(join(__dirname, path));
function getAllFilesByName(dirPath, name, arrayOfFiles = []) {
let input = join(__dirname, dirPath);
let files = readdirSync(input);
files.forEach(function (file) {
if (statSync(input + '/' + file).isDirectory()) {
arrayOfFiles = getAllFilesByName(dirPath + '/' + file, name, arrayOfFiles);
} else {
if (file === name) {
arrayOfFiles.push(join(__dirname, dirPath, '/', file));
}
}
});
return arrayOfFiles;
}
function cleanUpDistFolder() {
const args = process.argv;
if (args.length != 3) {
console.error(pc.red('ERROR: Expected one argument!'));
process.exit(1);
}
const inputDir = args[2];
if (!checkDirectory(inputDir)) {
console.error(pc.red(`ERROR: ${inputDir} not found! Check it.`));
process.exit(1);
}
const fileToGet = 'README.md';
let readmes = getAllFilesByName(inputDir, fileToGet);
if (readmes.length != 0) {
console.log(pc.magenta(pc.bold(`\n-> Deleting ${fileToGet} files within ${inputDir}`)));
Array.from(readmes).map((f) => unlinkSync(f));
}
}
cleanUpDistFolder();