-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme.js
81 lines (74 loc) · 2.04 KB
/
theme.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
const path = require('path')
const fs = require('fs')
const project = require('./theme.json')
const glob = require('glob')
require('pretty-error').start()
/**
* This script is ran to execute all custom variable and textdomain
* replacements of the child theme in once. Overwrites project files
* based on ./theme.json configuration.
*
* @example npm run setup
* @return {Promise<void>}
*/
glob(
/*
* {String} Pattern to be matched (target all files)
* https://github.com/isaacs/node-glob#glob-primer
*/
'**/*.*',
/*
* {Object} Options (exclude matches)
* https://github.com/isaacs/node-glob#options
*/
{
ignore: [
path.basename(__filename), // This script
'project.json', // The project's data
'assets/**',
'README.md',
// 'composer.json',
'package.json',
'LICENSE',
'node_modules/**',
'vendor/**',
'screenshot.png',
'composer.lock',
'package-lock.json',
'yarn.lock',
],
},
/**
* Callback function.
*
* @param {Array<String>} filesArray filenames found matching the pattern.
*/
function (err, filesArray) {
if (err) throw err
// console.log(filesArray)
filesArray.forEach((fileName, index, array) => {
/*
* Read every file and return the content of each.
*/
fs.readFile(fileName, 'utf8', (err, fileContent) => {
if (err) throw err
/*
* Replace matched search values on each file.
* @example replace(searchValue, newValue)
*/
const replace = fileContent
.replace(/THEME_NAME_/g, project.THEME_NAME_)
.replace(/ThemeName/g, project.ThemeName)
.replace(/Theme_Name/g, project.Theme_Name)
.replace(/parent_theme_name/g, project.parent_theme_name)
.replace(/TextDomain/g, project.TextDomain)
/*
* Rewrite values and save the new files.
*/
fs.writeFile(fileName, replace, 'utf8', function (err) {
if (err) throw err
})
})
})
}
)