-
Notifications
You must be signed in to change notification settings - Fork 1
/
replace-string.cjs
148 lines (132 loc) · 2.97 KB
/
replace-string.cjs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const { Case } = require('change-case-all')
const replace = require('replace-in-file')
const fs = require('fs')
const path = require('path')
function getVersionFromPluginPhp() {
const pluginPhpPath = path.join(__dirname, 'plugin.php')
try {
const pluginPhpContent = fs.readFileSync(pluginPhpPath, 'utf-8')
const versionLine = pluginPhpContent
.split('\n')
.find((line) => line.includes('* Version:'))
if (versionLine) {
const version = versionLine.split(':')[1].trim()
return version
}
throw new Error('Version line not found in plugin.php')
} catch (err) {
console.error(`Error reading plugin.php: ${err.message}`)
return null
}
}
const projectName = process?.argv?.[2] || ''
function replaceString(str) {
// regex example /^(AAA|BBB|CCC)$/
const capital = Case.capital(str)
const pascalName = Case.pascal(str)
const camelName = Case.camel(str)
const snakeName = Case.snake(str)
const kebabName = Case.kebab(str)
replace.sync({
files: [
'./plugin.php',
'./inc/class/admin/class-cpt.php',
'./js/src/utils/env.tsx',
],
from: /My Refine App/g,
to: capital,
})
replace.sync({
files: [
'./plugin.php',
'./inc/class/admin/class-cpt.php',
'./js/src/utils/env.tsx',
],
from: /my-refine-app/g,
to: kebabName,
})
replace.sync({
files: [
'./plugin.php',
'./inc/class/utils/class-base.php',
'./inc/templates/test.php',
'./js/src/utils/env.tsx',
],
from: /my_refine_app/g,
to: snakeName,
})
replace.sync({
files: [
'./composer.json',
'./plugin.php',
'./inc/class/class-bootstrap.php',
'./inc/class/admin/class-cpt.php',
'./inc/class/front-end/class-entry.php',
'./inc/class/utils/class-base.php',
],
from: /WpRefinePlugin/g,
to: pascalName,
})
replace.sync({
files: [
'./composer.json',
'./package.json',
'./plugin.php',
],
from: /wp-refine-plugin/g,
to: kebabName,
})
replace.sync({
files: [
'./plugin.php',
'./inc/class/admin/class-cpt.php',
],
from: /wp_refine_plugin/g,
to: snakeName,
})
const version = getVersionFromPluginPhp()
const textMap = [
{
from: version,
to: '0.0.1',
},
{
from: "'https://github.com/j7-dev/wp-refine-plugin';",
to: "''; // change to your github repo",
},
{
from: 'WP Refine Plugin (DEV)',
to: capital,
},
{
from: 'WP Refine Plugin is a boilerplate for creating a WordPress plugin with React, Tailwind, TypeScript, React Query v4, SCSS and Vite.',
to: 'your description',
},
{
from: 'vite, react, tailwind, typescript, react-query, scss, WordPress, WordPress plugin, refine',
to: 'your tags',
},
{
from: '* Author: J7',
to: '* Author: Your Name',
},
{
from: 'https://github.com/j7-dev',
to: 'Author URL',
},
]
textMap.forEach(({ from, to }) => {
const regex = new RegExp(
from.replace(/([(){}[\]\\|?*+.,^$])/g, '\\$1'),
'g',
)
replace.sync({
files: [
'./plugin.php',
],
from: regex,
to,
})
})
}
replaceString(projectName)