-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename.js
110 lines (90 loc) · 2.91 KB
/
rename.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import prompts from "prompts";
import { exec } from 'node:child_process';
import fs from "fs";
import { fileURLToPath } from 'url';
import path from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const questions = [
{
type: "text",
name: "name",
message: "Plugin name:",
initial: "My App",
validate: (text) =>
text.match(/[^A-Za-z0-9_-\s]/g)
? "Name allows alphanumeric English characters, spaces, underscores and dashes."
: true,
},
{
type: "text",
name: "path",
message: "Your application path:",
initial: "/my-app",
validate: (text) =>
text.match(/[^A-Za-z0-9\/-\s]/g)
? "Path allows alphanumeric English characters, forward slashes, underscores and dashes."
: true,
},
];
(async () => {
const response = await prompts(questions);
if (!response.name) {
console.error("You must provide a plugin name.");
process.exit(1);
}
if (!response.path) {
console.error("You must provide a plugin path.");
process.exit(1);
}
const replacements = {
"{plugin-name}": response.name,
'{plugin}': response.name.toLowerCase().replace(/[-\s]/g, "_"),
'{PLUGIN}': response.name.toUpperCase().replace(/[-\s]/g, "_"),
'{plugin-shortcode}': response.name.toLowerCase().replace(/[_\s]/g, "-"),
'{plugin-path}': response.path.toLowerCase().replace(/[_\s]/g, "-").replace(/^\//g, "").replace(/\/$/g, ""),
};
console.log("Replacing in file names...");
renameFiles(replacements);
console.log("Replacing in file contents...");
searchReplaceContents(replacements);
await removeRenameFile();
console.log("Done.");
})();
const renameFiles = (replacements) => {
const dir = path.resolve(__dirname);
const files = walkSync(dir);
files.forEach((file) => {
let newPath = Object.keys(replacements).reduce(
(acc, key) => acc.replace(new RegExp(key, "g"), replacements[key]),
file
);
fs.renameSync(file, newPath);
});
}
const searchReplaceContents = (replacements) => {
const dir = path.resolve(__dirname);
const files = walkSync(dir);
files.forEach((file) => {
const oldContent = fs.readFileSync(file, "utf8");
let newFileContent = Object.keys(replacements).reduce(
(acc, key) => acc.replace(new RegExp(key, "g"), replacements[key]),
oldContent
);
fs.writeFileSync(file, newFileContent, "utf8");
});
}
const walkSync = (dir, filelist = []) => {
fs.readdirSync(dir).forEach((file) => {
filelist = fs.statSync(path.join(dir, file)).isDirectory()
? walkSync(path.join(dir, file), filelist)
: filelist.concat(path.join(dir, file));
});
return filelist.filter(
(path) => !path.match(/node_modules|\.git|package-lock|rename\.js/)
);
};
const removeRenameFile = async () => {
fs.unlinkSync('rename.js');
await new Promise(resolve => exec('npm uninstall prompts', () => resolve()));
}