-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateNewChildBlock2.js
More file actions
142 lines (126 loc) Β· 3.98 KB
/
createNewChildBlock2.js
File metadata and controls
142 lines (126 loc) Β· 3.98 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
require('dotenv').config();
const fs = require('fs');
const {globSync} = require('glob');
const {exec} = require('child_process');
const acfTemplate = require('./new-block-templates/template-acf-pattern');
const acfTemplateScss = require('@total_onion/onion-library/new-block-templates/template-scss-blank');
const acfTemplateJs = require('@total_onion/onion-library/new-block-templates/template-js-blank');
const yaml = require('js-yaml');
const axios = require('axios');
const themePath =
process.env.THEME_PATH || 'web/wp-content/themes/global-theme';
const yamlData = yaml.load(fs.readFileSync('../../../../.lando.yml', 'utf8'));
const siteName = yamlData.config.site;
const parentURL = process.env.DESIGN_MULTIDEV
? `${process.env.DESIGN_MULTIDEV}/wp-admin/admin-ajax.php`
: `http://${siteName}.lndo.site/wp-admin/admin-ajax.php`;
const srcPathJs = `${__dirname}/components`;
const srcPathScss = `${__dirname}/components`;
let projectName = 'Global Theme';
const projectJson = JSON.parse(fs.readFileSync('./package.json'));
if (projectJson) {
projectName = projectJson.name;
if (projectName.slice(0, 3) === 'the') {
const prefix =
projectName.slice(0, 3).charAt(0).toUpperCase() +
projectName.slice(0, 3).slice(1);
projectName = `${prefix} ${
projectName.slice(3).charAt(0).toUpperCase() +
projectName.slice(3).slice(1)
}`;
} else {
projectName = `${
projectName.charAt(0).toUpperCase() + projectName.slice(1)
}`;
}
}
const dynamicEntryPoints = globSync(`${themePath}/assets/js/blocks/*.js`).map(
(path) => {
const assetKey = path
.replace('assets/js/blocks/', '')
.replace('.js', '');
return assetKey;
}
);
const newBlockName = process.argv[2]?.toLowerCase();
const patternID = process.argv[3];
if (!newBlockName) {
return console.log('Did you forget to give the new block a name?');
}
if (!patternID) {
return console.log('Did you forget to supply the pattern ID?');
}
fs.writeFileSync(
`${themePath}/inc/acf-blocks/${newBlockName}.php`,
acfTemplate(newBlockName, projectName)
);
const blockName = 'group-container-v3';
const fullPath = `${srcPathJs}/block-${blockName}/${blockName}.js`;
const jsdir = `Assets/js/blocks/`;
// if (!fs.existsSync(jsdir)) {
// fs.mkdirSync(jsdir, 0o744);
// }
const scssdir = `Assets/scss/blocks/`;
// if (!fs.existsSync(scssdir)) {
// fs.mkdirSync(scssdir, 0o744);
// }
if (!fs.existsSync(`${jsdir}/${newBlockName}.js`)) {
fs.writeFileSync(
`${jsdir}/${newBlockName}.js`,
acfTemplateJs(newBlockName)
);
console.log(`ππ\x1b[32m Successfully created the js file! ππ`);
}
if (!fs.existsSync(`${scssdir}/${newBlockName}.scss`)) {
fs.writeFileSync(
`${scssdir}/${newBlockName}.scss`,
acfTemplateScss(newBlockName)
);
console.log(`ππ\x1b[32m Successfully created the scss file! ππ`);
}
let data = new FormData();
data.append('action', 'get_pattern_block');
data.append('postID', patternID);
const headers = {
headers: {
'user-agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36'
}
};
axios.post(parentURL, data, headers).then(function (response) {
fs.writeFileSync(
`${themePath}/views/blocks/${newBlockName}.twig`,
response.data.html
);
fs.readFile(
`${themePath}/views/blocks/${newBlockName}.twig`,
'utf-8',
(err, contents) => {
if (err) throw err;
const regEx = RegExp(
String.raw`(${blockName.replaceAll(/( |-)/g, '')})`,
'gi'
);
const replaced = contents
.replaceAll(
regEx,
`${newBlockName.toLowerCase().replaceAll(/( |-)/g, '')}`
)
.replace(/(?<!sub-)group-container-v3/g, `${newBlockName}`);
fs.writeFile(
`${themePath}/views/blocks/${newBlockName}.twig`,
replaced,
'utf-8',
function (err) {
if (err) throw err;
console.log(
`ππ\x1b[32m Successfully did searcha and replace on the twig file! ππ`
);
console.log(
`π π π Hurrah! You made a new child block called ${newBlockName} π π π`
);
}
);
}
);
});