-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathreplace.config.js
More file actions
156 lines (136 loc) · 5.57 KB
/
replace.config.js
File metadata and controls
156 lines (136 loc) · 5.57 KB
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
const replace = require('replace-in-file')
const fs = require('fs')
const prompts = require('prompts')
const jsonContent = fs.readFileSync('./theme.config.json')
const themeConfig = JSON.parse(jsonContent)
const args = require('minimist')(process.argv)
const dry = args['dry'] //dry mode run wont change anything
// let packageName = new RegExp(`@package ${fromName}`, 'g')
// let lowercaseDash = new RegExp(`${fromSlug}-`, 'g')
// let inAString = new RegExp(`'${fromSlug}'`, 'g');
// let lowerCaseUnderscore = new RegExp(`${fromSlug}_`, 'g')
// let internationalize = new RegExp(`__\\( '${fromName}'`, 'g');
// module.exports = {
// files: './**/*',
// ignore: ['./*.md', './*.json', './webpack.config.js','./replace.config.js', './theme.config.json', './node_modules/**', './acf-json/**'],
// dry: args['dry'] || false,
// from: [packageName, lowercaseDash, inAString, lowerCaseUnderscore, internationalize, `Theme Name: ${fromName}`, `Theme URI: https://ignition.press/` ],
// to: [`@package ${themeConfig.name}`, `${themeConfig.slug}-`, `'${themeConfig.slug}'`, `${themeConfig.slug}_`, `__( '${themeConfig.name}'`, `Theme Name: ${themeConfig.name}`],
// verbose: true
// }
const questions = [
{
type: 'text',
name: 'name',
message: 'Theme Name'
},
{
type: 'text',
name: 'slug',
message: 'Theme-Slug (no-spaces)'
},
{
type: 'text',
name: 'uri',
message: 'Theme URI',
initial: 'https://ignition.press/'
},
{
type: 'text',
name: 'author',
message: 'Author',
initial: 'Eric Greenfield'
},
{
type: 'text',
name: 'authorUri',
message: 'Author URI',
initial: 'https://saltnpixels.com/'
},
{
type: 'text',
name: 'description',
message: 'Description',
initial: 'An amazing theme built with ignition'
},
{
type: 'text',
name: 'server',
message: 'Development or Remote Url',
initial: 'ignition.local'
},
{
type: 'confirm',
name: 'ssl',
message: 'Does your local server have an SSL?',
}
];
(async () => {
let allQuestions = true
const onCancel = prompt => {
console.log('Theme Setup Aborted')
allQuestions = false
}
const response = await prompts(questions, { onCancel })
//name and slug and all questions answered must be true
if (response.name && response.slug && allQuestions) {
//fixing responses
response.slug = response.slug.replace(' ', '-').toLowerCase()
let underscoreSlug = response.slug.replace('-', '_')
let underscoreOldSlug = themeConfig.slug.replace('-', '_')
//all the regex patterns for replacement
let packageName = new RegExp(`@package ${themeConfig.name}`, 'g')
let lowercaseDash = new RegExp(`${themeConfig.slug}-`, 'g')
let inAString = new RegExp(`'${themeConfig.slug}'`, 'g')
let lowerCaseUnderscore = new RegExp(`${underscoreOldSlug}_`, 'g')
let internationalize = new RegExp(`__\\( '${themeConfig.slug}'`, 'g')
//replacement options. setup files to replace
const options = {
files: './**/*',
ignore: ['./*.md', './*.json', './webpack.config.js', './replace.config.js', './theme.config.json', './node_modules/**', './acf-json/**'],
dry: args['dry'] || false,
from: [packageName, lowercaseDash, inAString, lowerCaseUnderscore, internationalize, /Theme Name: .*/g, /Theme URI: .*/g, /Author: .*/g, /Author URI: .*/g, /Description: .*/g, /Text Domain: .*/g],
to: [`@package ${response.name}`, `${response.slug}-`, `'${response.slug}'`, `${underscoreSlug}_`, `__( '${response.name}'`, `Theme Name: ${response.name}`, `Theme URI: ${response.uri}`, `Author: ${response.author}`, `Author URI: ${response.authorUri}`, `Description: ${response.description}`, `Text Domain: ${response.slug}`],
verbose: true
}
try {
const results = await replace(options)
const changedFiles = results
.filter(result => result.hasChanged)
.map(result => result.file + '\n')
console.log(`${changedFiles.length} have changed. \n The following files have changed: \n ${changedFiles.join(' ')}`)
} catch (error) {
console.error('Error occurred:', error)
}
//second replace. change themes config options
const optionsConfig = {
files: './theme.config.json',
dry: args['dry'] || false,
verbose: true,
from: [
/"name": ".*/g,
/"slug": ".*/g,
/"server": ".*/g,
/"ssl": .*/g,
]
,
to: [
`"name": "${response.name}",`,
`"slug": "${response.slug}",`,
`"server": "${response.server}",`,
`"ssl": ${response.ssl},`
]
}
try {
const results = await replace(optionsConfig)
const changedFiles = results
.filter(result => result.hasChanged)
.map(result => result.file + '\n')
console.log('Updated Theme Config')
console.log('All Done! \n Now you can start creating your theme! \n Check out the theme.config file as well as functions.php file, and the variables.scss files. \n There is where you can set things up a bit further.')
} catch (error) {
console.error('Error occurred:', error)
}
}
}
)()