Skip to content

Commit e7881ad

Browse files
committed
Refactored to make more robust, Added anchor as default
1 parent 501388f commit e7881ad

File tree

3 files changed

+193
-169
lines changed

3 files changed

+193
-169
lines changed

lib/create-acf-block-json.js

Lines changed: 164 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,171 @@
1-
const fs = require( "fs" );
2-
const prompts = require( 'prompts' );
1+
const fs = require("fs");
2+
const prompts = require('prompts');
33

44
const DIR = '.';
55

6-
const QUESTIONS = [
7-
{
8-
type: 'text',
9-
name: 'namespace',
10-
message: 'Block Namespace',
11-
initial: 'acf'
12-
},
13-
{
14-
type: 'text',
15-
name: 'title',
16-
message: 'Block Name'
17-
},
18-
{
19-
type: 'text',
20-
name: 'description',
21-
message: 'Block description',
22-
initial: ''
23-
},
24-
{
25-
type: 'text',
26-
name: 'icon',
27-
message: 'Dashicon',
28-
initial: 'star-filled'
29-
}
30-
];
31-
32-
exports.acfBlock = function () {
33-
34-
console.clear();
35-
console.log( 'Create ACF Block JSON is running...' );
36-
console.log( '' );
37-
console.log( 'This will create a new folder in the current directory containing scaffolding for a new WordPress block using Advanced Custom Fields.' );
38-
console.log( '' );
39-
40-
(async() => {
41-
const response = await prompts( QUESTIONS );
42-
43-
const title = response.title;
44-
const slug = response.title.replace( /\s+/g, '-' ).toLowerCase();
45-
const namespace = response.namespace.replace( /\s+/g, '-' ).toLowerCase();
46-
const qualifiedName = namespace + '/' + slug;
47-
const folder = '/' + slug;
48-
const absolute = DIR + folder;
49-
50-
// Create default CSS class.
51-
const css = '.wp-block-' + namespace + '-' + slug + ' {}';
52-
53-
// Create Folder.
54-
if ( ! fs.existsSync( absolute )) {
55-
fs.mkdirSync( absolute );
56-
} else {
57-
console.log( 'Error: A directory called ' + slug + ' was already found. Aborting.' )
58-
return;
59-
}
60-
61-
// Create CSS.
62-
fs.writeFile(
63-
absolute + '/' + slug + '.css',
64-
`/* ${css} */`,
65-
function (error) {
66-
if (error) {
67-
console.log( 'Whoops there has been an error writing to the system. Aborting.' );
68-
throw error;
69-
} else {
70-
console.log( `${slug}.css created` );
71-
}
72-
}
73-
);
74-
75-
// Create SCSS.
76-
fs.writeFile(
77-
absolute + '/' + slug + '.scss',
78-
'// ' + css,
79-
function (error) {
80-
if (error) {
81-
console.log( 'Whoops there has been an error writing to the system. Aborting.' );
82-
throw error;
83-
} else {
84-
console.log( `${slug}.scss created` );
85-
}
86-
}
87-
);
88-
89-
// Creating Editor CSS.
90-
fs.writeFile(
91-
absolute + '/editor.css',
92-
`/* ${css} */`,
93-
function (error) {
94-
if (error) {
95-
console.log( 'Whoops there has been an error writing to the system. Aborting.' );
96-
throw error;
97-
} else {
98-
console.log( `editor.css created` );
99-
}
6+
const QUESTIONS = [{
7+
type: 'text',
8+
name: 'namespace',
9+
message: 'Block Namespace',
10+
initial: 'acf'
11+
},
12+
{
13+
type: 'text',
14+
name: 'title',
15+
message: 'Block Name'
16+
},
17+
{
18+
type: 'text',
19+
name: 'description',
20+
message: 'Block description',
21+
initial: ''
22+
},
23+
{
24+
type: 'text',
25+
name: 'icon',
26+
message: 'Dashicon',
27+
initial: 'star-filled'
28+
}
29+
];
30+
31+
exports.acfBlock = function() {
32+
33+
console.clear();
34+
console.log('Create ACF Block JSON is running...');
35+
console.log('');
36+
console.log('This will create a new folder in the current directory containing scaffolding for a new WordPress block using Advanced Custom Fields.');
37+
console.log('');
38+
39+
let cancelled = false;
40+
41+
// Listen for SIGINT signals and set the "cancelled" flag to true.
42+
process.on('SIGINT', () => {
43+
console.log('Cancelled.');
44+
cancelled = true;
45+
});
46+
47+
// Helper function for creating files.
48+
const createFile = async (path, content, successMessage, errorMessage) => {
49+
try {
50+
fs.writeFileSync(path, content);
51+
console.log(successMessage);
52+
} catch (error) {
53+
console.error(errorMessage, error);
54+
}
55+
};
56+
57+
(async () => {
58+
const response = await prompts(QUESTIONS, {onCancel: () => {cancelled = true}});
59+
60+
// Check if the user cancelled the prompts.
61+
if (cancelled) {
62+
console.log('Aborting...');
63+
return;
10064
}
101-
);
102-
103-
// Creating Editor SCSS.
104-
fs.writeFile(
105-
absolute + '/editor.scss',
106-
'// ' + css,
107-
function (error) {
108-
if (error) {
109-
console.log( 'Whoops there has been an error writing to the system. Aborting.' );
110-
throw error;
111-
} else {
112-
console.log( `editor.scss created` );
113-
}
114-
}
115-
);
116-
117-
// Get the PHP template and turn in to PHP.
118-
let phpTemplate = '/template-php.txt';
119-
fs.readFile(
120-
__dirname + phpTemplate,
121-
'utf8',
122-
function(err, data) {
123-
data = data.replaceAll( 'XYZ', title ).replaceAll( 'QWY', slug ).replaceAll( 'DX9S', namespace ).replaceAll( '\r\n', '\n' );
124-
let createPHP = fs.createWriteStream( absolute + '/' + slug + '.php' );
125-
createPHP.write( data );
126-
createPHP.end();
127-
console.log( `${slug}.php created` );
128-
}
129-
);
65+
66+
const title = response.title;
67+
const slug = response.title.replace(/\s+/g, '-').toLowerCase();
68+
const namespace = response.namespace.replace(/\s+/g, '-').toLowerCase();
69+
const qualifiedName = namespace + '/' + slug;
70+
const folder = '/' + slug;
71+
const absolute = DIR + folder;
72+
73+
// Create default CSS class.
74+
const css = '.wp-block-' + namespace + '-' + slug + ' {}';
75+
76+
// Create Folder.
77+
if (!fs.existsSync(absolute)) {
78+
fs.mkdirSync(absolute);
79+
} else {
80+
console.log('Error: A directory called ' + slug + ' was already found. Aborting.')
81+
return;
82+
}
83+
84+
// Handle cancellation.
85+
if (cancelled) {
86+
console.log('Aborting.');
87+
return;
88+
}
89+
90+
// Create files.
91+
await createFile(
92+
absolute + '/' + slug + '.css',
93+
`/* ${css} */`,
94+
`${slug}.css created`,
95+
'Error creating CSS file:'
96+
);
97+
await createFile(
98+
absolute + '/' + slug + '.scss',
99+
`// ${css}`,
100+
`${slug}.scss created`,
101+
'Error creating SCSS file:'
102+
);
103+
await createFile(
104+
absolute + '/editor.css',
105+
`/* ${css} */`,
106+
`editor.css created`,
107+
'Error creating editor CSS file:'
108+
);
109+
await createFile(
110+
absolute + '/editor.scss',
111+
`// ${css}`,
112+
`editor.scss created`,
113+
'Error creating editor SCSS file:'
114+
);
115+
116+
// Handle cancellation.
117+
if (cancelled) {
118+
console.log('Aborting.');
119+
return;
120+
}
121+
122+
// Get the PHP template and turn in to PHP.
123+
let phpTemplate = '/template-php.txt';
124+
try {
125+
let data = fs.readFileSync(__dirname + phpTemplate, 'utf8');
126+
data = data.replace(/XYZ/g, title)
127+
.replace(/QWY/g, slug)
128+
.replace(/DX9S/g, namespace)
129+
.replace(/\r\n/g, '\n');
130+
await createFile(
131+
absolute + '/' + slug + '.php',
132+
data,
133+
`${slug}.php created`,
134+
'Error creating PHP template:'
135+
);
136+
} catch (error) {
137+
console.error('Error creating PHP template:', error);
138+
}
139+
140+
// Handle cancellation.
141+
if (cancelled) {
142+
console.log('Aborting.');
143+
return;
144+
}
130145

131146
// Get the Block.json template.
132147
let jsonTemplate = '/template-block.json';
133-
let raw = fs.readFileSync( __dirname + jsonTemplate );
134-
let template = JSON.parse( raw );
135-
136-
// Update Block.json values.
137-
template.name = qualifiedName;
138-
template.title = title;
139-
template.description = response.description;
140-
template.icon = response.icon;
141-
template.style = 'file:./' + slug + '.css';
142-
template.editorStyle = 'file:./editor.css';
143-
template.acf.renderTemplate = slug + '.php';
144-
145-
let jsonContent = JSON.stringify( template, null, "\t" );
146-
let createJSON = fs.createWriteStream( absolute + '/block.json' );
147-
createJSON.write( jsonContent );
148-
createJSON.end();
149-
console.log( 'block.json created' );
150-
})();
151-
}
148+
try {
149+
let raw = fs.readFileSync(__dirname + jsonTemplate);
150+
let template = JSON.parse(raw);
151+
// Update Block.json values.
152+
template.name = qualifiedName;
153+
template.title = title;
154+
template.description = response.description;
155+
template.icon = response.icon;
156+
template.style = 'file:./' + slug + '.css';
157+
template.editorStyle = 'file:./editor.css';
158+
template.acf.renderTemplate = slug + '.php';
159+
let jsonContent = JSON.stringify(template, null, "\t");
160+
await createFile(
161+
absolute + '/block.json',
162+
jsonContent,
163+
'block.json created',
164+
'Error creating JSON template:'
165+
);
166+
} catch (error) {
167+
console.error('Error creating JSON template:', error);
168+
}
169+
170+
})();
171+
}

lib/template-block.json

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
11
{
2-
"$schema": "https://schemas.wp.org/trunk/block.json",
2+
"$schema": "https://schemas.wp.org/trunk/block.json",
33
"apiVersion": 2,
44
"name": "",
55
"title": "",
66
"description": "",
77
"style": "",
8-
"editorStyle": "",
9-
"editorScript" : [],
8+
"editorStyle": "",
9+
"editorScript": [],
1010
"category": "common",
1111
"icon": "awards",
1212
"keywords": [
1313
"",
1414
"",
1515
""
1616
],
17-
"acf": {
17+
"acf": {
1818
"mode": "preview",
1919
"renderTemplate": ""
2020
},
2121
"supports": {
22-
"mode": false,
23-
"align": ["wide", "full", "left", "center", "right"],
24-
"alignWide": true,
22+
"align": [
23+
"wide",
24+
"full",
25+
"left",
26+
"center",
27+
"right"
28+
],
29+
"alignWide": true,
2530
"alignContent": false,
26-
"alignText": false,
31+
"alignText": false,
2732
"anchor": true,
2833
"color": {
29-
"background": false,
30-
"gradients": false,
31-
"link": false,
32-
"text": false
33-
},
34+
"background": false,
35+
"gradients": false,
36+
"link": false,
37+
"text": false
38+
},
39+
"fullHeight": false,
40+
"html": false,
3441
"spacing": {
35-
"margin": false,
36-
"padding": false
37-
},
42+
"margin": false,
43+
"padding": false
44+
},
3845
"typography": {
39-
"fontSize": false,
40-
"lineHeight": false
41-
},
42-
"fullHeight": false
46+
"fontSize": false,
47+
"lineHeight": false
48+
}
4349
},
4450
"attributes": {
4551
"variable": {
@@ -48,4 +54,4 @@
4854
}
4955
},
5056
"example": {}
51-
}
57+
}

0 commit comments

Comments
 (0)