|
1 |
| -const fs = require( "fs" ); |
2 |
| -const prompts = require( 'prompts' ); |
| 1 | +const fs = require("fs"); |
| 2 | +const prompts = require('prompts'); |
3 | 3 |
|
4 | 4 | const DIR = '.';
|
5 | 5 |
|
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; |
100 | 64 | }
|
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 | + } |
130 | 145 |
|
131 | 146 | // Get the Block.json template.
|
132 | 147 | 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 | +} |
0 commit comments