new-s-block 1.0.0 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/bin/cli.js +27 -19
  2. package/package.json +1 -1
package/bin/cli.js CHANGED
@@ -29,6 +29,11 @@ function splitKeywords(blockName) {
29
29
  return blockName.split('-').filter(Boolean);
30
30
  }
31
31
 
32
+ // "new-block" -> "st_new_block_inner"
33
+ function toInnerClass(blockName) {
34
+ return 'st_' + blockName.replace(/-/g, '_') + '_inner';
35
+ }
36
+
32
37
  function ensureDir(dirPath) {
33
38
  if (!fs.existsSync(dirPath)) {
34
39
  fs.mkdirSync(dirPath, { recursive: true });
@@ -71,9 +76,9 @@ async function main() {
71
76
  }
72
77
  }
73
78
 
74
- // 2. Block label
79
+ // 2. Block title
75
80
  const defaultLabel = toTitleCase(blockName);
76
- const labelRaw = await prompt(rl, 'Block label [' + defaultLabel + ']: ');
81
+ const labelRaw = await prompt(rl, 'Block title [' + defaultLabel + ']: ');
77
82
  const blockLabel = labelRaw.trim() || defaultLabel;
78
83
 
79
84
  // 3. Description
@@ -88,21 +93,25 @@ async function main() {
88
93
 
89
94
  // 5. Blocks folder
90
95
  const folderRaw = await prompt(rl, 'Blocks folder [blocks]: ');
91
- const blocksDir = 'blocks/' + (folderRaw.trim() || '');
92
96
 
93
97
  rl.close();
94
98
 
95
99
  // ── Resolve paths ────────────────────────────────────────────────────────
96
100
 
97
- // Normalise: strip any trailing slash and duplicate 'blocks/' the user may have typed
98
- const subFolder = folderRaw.trim().replace(/^blocks[\/\\]?/i, '').replace(/[\/\\]+$/, '');
99
- const blockDir = subFolder
101
+ // Strip any trailing slash and leading 'blocks/' the user may have typed
102
+ const subFolder = folderRaw.trim().replace(/^blocks[\/\\]?/i, '').replace(/[\/\\]+$/, '');
103
+ const blockDir = subFolder
100
104
  ? path.join(cwd, 'blocks', subFolder, blockName)
101
105
  : path.join(cwd, 'blocks', blockName);
102
106
 
107
+ // Relative block path from 'blocks/' used in end messages e.g. "cards/new-block"
108
+ const blockRelPath = subFolder ? subFolder + '/' + blockName : blockName;
109
+
103
110
  const renderFile = path.join(blockDir, blockName + '.php');
104
111
  const blockJsonFile = path.join(blockDir, 'block.json');
105
- const scssFile = path.join(cwd, 'assets', 'sass', 'blocks', blockName + '.scss');
112
+ const scssFile = subFolder
113
+ ? path.join(cwd, 'assets', 'sass', 'blocks', subFolder, blockName + '.scss')
114
+ : path.join(cwd, 'assets', 'sass', 'blocks', blockName + '.scss');
106
115
 
107
116
  if (fs.existsSync(blockDir)) {
108
117
  console.error('\n\u274C Block folder already exists: ' + path.relative(cwd, blockDir) + '\n');
@@ -119,12 +128,10 @@ async function main() {
119
128
  var autoKeywords = splitKeywords(blockName);
120
129
  autoKeywords.push('stier');
121
130
  var allKeywords = autoKeywords.concat(extraKeywords);
122
- // Deduplicate
123
131
  allKeywords = allKeywords.filter(function (v, i, a) { return a.indexOf(v) === i; });
124
132
 
125
133
  var keywordsJson = '["' + allKeywords.join('", "') + '"]';
126
134
 
127
- // Build block.json as a string to keep keywords on one line
128
135
  const blockJsonStr = '{\n' +
129
136
  ' "name": "acf/' + blockName + '",\n' +
130
137
  ' "title": "' + blockLabel + '",\n' +
@@ -147,12 +154,12 @@ async function main() {
147
154
 
148
155
  // ── render.php ───────────────────────────────────────────────────────────
149
156
 
150
- const innerClass = 'st' + blockName.replace(/-/g, '') + '_inner';
157
+ const innerClass = toInnerClass(blockName);
151
158
 
152
159
  const renderPhp = '<?php\n' +
153
160
  "$padding = get_field('padding');\n" +
154
161
  '\n' +
155
- "$class = 'st_block st_" + blockName + "';\n" +
162
+ "$class = 'st_block st_" + blockName.replace(/-/g, '_') + "';\n" +
156
163
  "if ( ! empty( $block['className'] ) ) {\n" +
157
164
  " $class .= ' ' . $block['className'];\n" +
158
165
  '}\n' +
@@ -179,28 +186,29 @@ async function main() {
179
186
 
180
187
  // ── block-name.scss ──────────────────────────────────────────────────────
181
188
 
182
- const scssContent = '.st_' + blockName + ' {\n\n}\n';
189
+ const scssContent = '.st_' + blockName.replace(/-/g, '_') + ' {\n\n}\n';
183
190
 
184
191
  writeFile(scssFile, scssContent);
185
192
 
186
193
  // ── Summary ──────────────────────────────────────────────────────────────
187
194
 
195
+ var cyan = '\x1b[36m';
196
+ var reset = '\x1b[0m';
197
+
188
198
  console.log('\n\u2705 Block "' + blockLabel + '" created successfully!\n');
189
199
  console.log(' \uD83D\uDCC1 ' + path.relative(cwd, blockDir) + '/');
190
200
  console.log(' \u251C\u2500\u2500 block.json');
191
201
  console.log(' \u2514\u2500\u2500 ' + blockName + '.php');
192
- console.log(' \uD83D\uDCC1 assets/sass/blocks/');
202
+ var sassDisplay = subFolder ? 'assets/sass/blocks/' + subFolder + '/' : 'assets/sass/blocks/';
203
+ console.log(' \uD83D\uDCC1 ' + sassDisplay);
193
204
  console.log(' \u2514\u2500\u2500 ' + blockName + '.scss');
194
205
  console.log('');
195
- var cyan = '\x1b[36m';
196
- var reset = '\x1b[0m';
197
- console.log('');
198
- console.log(" Don't forget to add " + cyan + "@import 'blocks/" + blockName + "';" + reset + " in 'assets/sass/theme.scss'");
199
- console.log(" Don't forget to add " + cyan + "register_block_type( __DIR__ . '/../blocks/" + blockName + "' );" + reset + " in 'includes/template-functions.php'");
206
+ console.log(" Don't forget to add " + cyan + "@import 'blocks/" + (subFolder ? subFolder + '/' : '') + blockName + "';" + reset + " in 'assets/sass/theme.scss'");
207
+ console.log(" Don't forget to add " + cyan + "register_block_type( __DIR__ . '/../blocks/" + blockRelPath + "' );" + reset + " in 'includes/template-functions.php'");
200
208
  console.log('');
201
209
  }
202
210
 
203
211
  main().catch(function (err) {
204
212
  console.error('\n\u274C Error: ' + err.message);
205
213
  process.exit(1);
206
- });
214
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "new-s-block",
3
- "version": "1.0.0",
3
+ "version": "1.0.5",
4
4
  "description": "CLI tool to scaffold ACF Gutenberg blocks",
5
5
  "bin": {
6
6
  "new-s-block": "./bin/cli.js"