Skip to content

Commit e3e14ca

Browse files
committed
Script to clean up folders generated by CLI
1 parent ed8b92f commit e3e14ca

File tree

4 files changed

+97
-25
lines changed

4 files changed

+97
-25
lines changed

cleanup.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

dev/cleanup.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { readdirSync, rmdirSync } from 'fs';
2+
import { repo } from './developer';
3+
const { cwd, chdir } = process;
4+
5+
// -------------------------------------------------------------------------------------------------
6+
// TODO 1 (after CLI upgrade): Configure cleanup to run using node-mongo command i.e. "node-mongo cleanup" not "npm run cleanup"
7+
// TODO 2 (if possible): (I don't think you will need this TODO when this is done using the "node-mongo cleanup" command - or it might need slight modification)
8+
// update package.json cleanup script dynamically with the cleanup file path.
9+
// This means you will have to get the cleanup file name/path dynamically too...
10+
// -------------------------------------------------------------------------------------------------
11+
12+
// cd into repo root
13+
chdir('../');
14+
15+
// use repo root
16+
const rootDir = cwd();
17+
18+
// return all files and folders in repo root i.e. original + node-mongo generated
19+
const repoContent = readdirSync(rootDir, (err, filesAndFolders) => {
20+
if (err) {
21+
throw err;
22+
}
23+
24+
return filesAndFolders;
25+
});
26+
27+
console.log(repoContent);
28+
29+
const filterContentToGetTheOnesGeneratedByCLI = repoContent.reduce((acc, curr) => {
30+
if (!repo.originalContentList.includes(curr)) {
31+
acc.push(curr);
32+
}
33+
return acc;
34+
}, []);
35+
36+
console.log(filterContentToGetTheOnesGeneratedByCLI);
37+
38+
// delete folders generated by CLI
39+
try {
40+
filterContentToGetTheOnesGeneratedByCLI.map(folder => {
41+
console.log(`${folder} folder deleted successfully`);
42+
return rmdirSync(folder, { recursive: true, force: true });
43+
});
44+
} catch (err) {
45+
console.log(err);
46+
}
47+
48+
console.log(repoContent);
49+
50+
// ------------------------------------
51+
// TODO: Write another script at the end of this file
52+
// that updates this repoContent list back when done.
53+
// Is this TODO even needed? - We'll see. For now the
54+
// solution ending here works fine.
55+
// ------------------------------------

dev/developer.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { basename, resolve, dirname } from 'path';
2+
// file to get and save if user still has the
3+
// Initial settings...
4+
5+
const { chdir } = process;
6+
7+
// Dynamically get current the name of this file you are in, and its parent folder
8+
const developerJsFileName = basename(__filename/*, extname(__filename)*/);
9+
const cleanupFolderPath = dirname(new URL(import.meta.url).pathname);
10+
11+
// Change directory into the dynamically gotten parent folder, then extract folder name from path
12+
chdir(cleanupFolderPath);
13+
const cleanupFolderName = basename(resolve());
14+
15+
// CLI repo's original content config object
16+
export const repo = {
17+
hasOriginalContentOnly: true,
18+
originalContentList: [
19+
cleanupFolderName,
20+
'.all-contributorsrc',
21+
'.babelrc',
22+
'.editorconfig',
23+
'.eslintrc.json',
24+
'.git',
25+
'.github',
26+
'.gitignore',
27+
'.npmrc',
28+
'LICENSE',
29+
'README.md',
30+
'bin',
31+
'cleanup.js',
32+
'node_modules',
33+
'package-lock.json',
34+
'package.json',
35+
'spec',
36+
'src',
37+
'templates'
38+
],
39+
developerJsFileName,
40+
cleanupFolderName,
41+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"dev-test": "nodemon --exec babel-node spec/run.js",
1717
"CI-test": "babel-node spec/run.js",
1818
"test-message": "node -r esm src/customize/testMessage",
19-
"cleanup": "node cleanUp.js"
19+
"cleanup": "node -r esm dev/cleanup.js"
2020
},
2121
"repository": {
2222
"type": "git",

0 commit comments

Comments
 (0)