Skip to content

Commit 16f7c8d

Browse files
committed
refactor: cleanup.js
Replace rimraf with fs.rmSync, move path.join outside the rmIfExists function, put cleanup start in a separate function.
1 parent 2cc5baa commit 16f7c8d

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

cleanup.js

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
1-
var _ = require('lodash');
2-
var exec = require('child_process').execSync;
3-
var fs = require('fs');
4-
var commandExists = require('command-exists');
5-
const path = require('path');
6-
const rimraf = require('rimraf');
1+
const fs = require("fs");
2+
const path = require("path");
73

8-
function rmIfExists(base, name) {
9-
let dir = path.join(base, name);
10-
if(fs.existsSync(dir)) {
11-
console.log("Clean ",dir);
12-
rimraf.sync(dir);
13-
}
4+
/**
5+
* @param {string} dir
6+
*/
7+
function rmIfExists(dir) {
8+
if (fs.existsSync(dir)) {
9+
console.log("Cleaning", dir);
10+
fs.rmSync(dir, { recursive: true });
11+
}
1412
}
1513

16-
for (let keyedType of ['keyed', 'non-keyed']) {
17-
let dir = path.resolve('frameworks', keyedType);
18-
let directories = fs.readdirSync(dir);
14+
/**
15+
* @param {string} basePath
16+
* @param {string} keyedTypes
17+
*/
18+
function cleanFrameworkDirectories(basePath, keyedTypes) {
19+
for (const keyedType of keyedTypes) {
20+
const frameworkDir = path.resolve(basePath, keyedType);
21+
const directories = fs.readdirSync(frameworkDir);
1922

20-
for (let name of directories) {
21-
let fd = path.resolve(dir, name);
22-
console.log('cleaning ', fd);
23-
if(fs.existsSync(fd+"/node_modules")) {
24-
rimraf.sync(fd+"/node_modules");
25-
}
26-
rmIfExists(fd, "package-lock.json");
27-
rmIfExists(fd, "yarn.lock");
28-
rmIfExists(fd, "dist");
29-
rmIfExists(fd, "elm-stuff");
30-
rmIfExists(fd, "bower_components");
31-
rmIfExists(fd, "node_modules");
32-
}
23+
for (const directory of directories) {
24+
const frameworkPath = path.resolve(frameworkDir, directory);
25+
console.log("cleaning ", frameworkPath);
26+
27+
rmIfExists(path.join(frameworkPath, "package-lock.json"));
28+
rmIfExists(path.join(frameworkPath, "yarn.lock"));
29+
rmIfExists(path.join(frameworkPath, "node_modules"));
30+
rmIfExists(path.join(frameworkPath, "dist"));
31+
rmIfExists(path.join(frameworkPath, "elm-stuff"));
32+
rmIfExists(path.join(frameworkPath, "bower_components"));
33+
}
34+
}
3335
}
36+
37+
const keyedTypes = ["keyed", "non-keyed"];
38+
const frameworksPath = path.resolve("frameworks");
39+
40+
cleanFrameworkDirectories(frameworksPath, keyedTypes);

0 commit comments

Comments
 (0)