-
Notifications
You must be signed in to change notification settings - Fork 6
/
replace-string.cjs
181 lines (160 loc) · 3.53 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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/classes/Admin/CPT.php',
'./js/src/utils/env.tsx',
],
from: /My App/g,
to: capital,
})
replace.sync({
files: [
'./plugin.php',
'./inc/classes/Admin/CPT.php',
'./js/src/utils/env.tsx',
],
from: /my-app/g,
to: kebabName,
})
replace.sync({
files: [
'./plugin.php',
'./inc/classes/Utils/Base.php',
'./inc/classes/Admin/CPT.php',
'./inc/classes/FrontEnd/Entry.php',
'./inc/templates/test.php',
'./js/src/utils/env.tsx',
],
from: /my_app/g,
to: snakeName,
})
replace.sync({
files: [
'./composer.json',
'./plugin.php',
'./inc/classes/Bootstrap.php',
'./inc/classes/Admin/CPT.php',
'./inc/classes/FrontEnd/Entry.php',
'./inc/classes/Utils/Base.php',
],
from: /WpReactPlugin/g,
to: pascalName,
})
replace.sync({
files: [
'./composer.json',
'./package.json',
'./plugin.php',
'./release/.release-it.cjs',
],
from: /wp-react-plugin/g,
to: kebabName,
})
replace.sync({
files: [
'./package.json',
],
from: /create-wp-react-plugin/g,
to: kebabName,
})
replace.sync({
files: [
'./package.json',
],
from: "wp i18n make-pot . languages/wp_react_plugin.pot",
to: `wp i18n make-pot . languages/${snakeName}.pot`,
})
replace.sync({
files: [
'./package.json',
],
from: /"version": "[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}"/,
to: '"version": "0.0.1"',
})
replace.sync({
files: [
'./plugin.php',
'./inc/classes/Admin/CPT.php',
],
from: /wp_react_plugin/g,
to: snakeName,
})
const version = getVersionFromPluginPhp()
const textMap = [
{
from: version,
to: '0.0.1',
},
{
from: "'https://github.com/j7-dev/wp-react-plugin',",
to: "'', // change to your github repo url",
},
{
from: 'WP React Plugin (DEV)',
to: capital,
},
{
from: 'WP React 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',
to: 'your tags',
},
{
from: '* Author: J7',
to: '* Author: Your Name',
},
{
from: "'app_name' => 'Wp React Plugin'",
to: `'app_name' => '${str}'`,
},
{
from: 'https://github.com/j7-dev',
to: '[YOUR GITHUB URL]',
},
]
textMap.forEach(({ from, to }) => {
const regex = new RegExp(
from.replace(/([(){}[\]\\|?*+.,^$])/g, '\\$1'),
'g',
)
replace.sync({
files: [
'./plugin.php',
],
from: regex,
to,
})
})
}
replaceString(projectName)