@next-core/yo 1.3.0 → 1.4.0

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/package.json +3 -3
  2. package/src/plopfile.js +69 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next-core/yo",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Generator for next v3",
5
5
  "homepage": "https://github.com/easyops-cn/next-core/tree/v3/packages/yo",
6
6
  "license": "GPL-3.0",
@@ -24,7 +24,7 @@
24
24
  "plop": "^3.1.2"
25
25
  },
26
26
  "devDependencies": {
27
- "@next-core/build-next-bricks": "^1.12.0",
27
+ "@next-core/build-next-bricks": "^1.13.0",
28
28
  "@next-core/build-next-libs": "^1.0.5",
29
29
  "@next-core/element": "^1.1.0",
30
30
  "@next-core/i18n": "^1.0.21",
@@ -33,5 +33,5 @@
33
33
  "@next-core/theme": "^1.1.1",
34
34
  "react": "0.0.0-experimental-ee8509801-20230117"
35
35
  },
36
- "gitHead": "fbd30ceaf3abd9c99c5dc2085a81413c68e367e0"
36
+ "gitHead": "06fca79a3a3b63a995d92729cfbed8e312cfdccd"
37
37
  }
package/src/plopfile.js CHANGED
@@ -1,13 +1,12 @@
1
1
  import path from "node:path";
2
2
  import { existsSync } from "node:fs";
3
- import { readFile, readdir } from "node:fs/promises";
3
+ import { readFile, readdir, writeFile } from "node:fs/promises";
4
4
  import { fileURLToPath } from "node:url";
5
5
 
6
6
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
7
 
8
8
  const validPkgName = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/;
9
9
  const validBrickName = /^[a-z][a-z0-9]*(-[a-z0-9]+)+$/;
10
- const validPartialBrickName = validPkgName;
11
10
 
12
11
  const rootDir = process.cwd();
13
12
  const bricksDir = path.join(rootDir, "bricks");
@@ -198,28 +197,40 @@ export default function (
198
197
  when(data) {
199
198
  return data.type === "brick";
200
199
  },
201
- validate(value, data) {
202
- if (
203
- !(
204
- data.brickType === "common"
205
- ? validPartialBrickName
206
- : validBrickName
207
- ).test(value)
208
- ) {
200
+ async validate(value, data) {
201
+ const realBrickName =
202
+ data.brickType === "common" ? `eo-${value}` : value;
203
+ if (!validBrickName.test(realBrickName)) {
209
204
  return "Please enter a lower-kebab-case brick name.";
210
205
  }
211
206
 
212
207
  if (existsSync(path.join(bricksDir, data.pkgName, "src", value))) {
213
- return `Brick "${value}" seems to be existed, please enter another name.`;
208
+ return `Brick "${realBrickName}" seems to be existed, please enter another name.`;
209
+ }
210
+
211
+ const commonBricksJsonFile = path.join(
212
+ rootDir,
213
+ "shared/common-bricks/common-bricks.json"
214
+ );
215
+ if (data.brickType === "common" && existsSync(commonBricksJsonFile)) {
216
+ const commonBricksJson = JSON.parse(
217
+ await readFile(commonBricksJsonFile, "utf-8")
218
+ );
219
+ for (const [pkg, commonBricks] of Object.entries(
220
+ commonBricksJson
221
+ )) {
222
+ if (commonBricks.includes(realBrickName)) {
223
+ return `Brick "${realBrickName}" existed in package "${pkg}", please enter another name.`;
224
+ }
225
+ }
214
226
  }
215
227
 
216
228
  return true;
217
229
  },
218
230
  transformer(input, data) {
219
- if (data.brickType === "common") {
220
- return `eo-${input}`;
221
- }
222
- return input;
231
+ return data.brickType === "common"
232
+ ? `eo-${input}`
233
+ : `${data.pkgName}.${input}`;
223
234
  },
224
235
  },
225
236
  {
@@ -249,6 +260,9 @@ export default function (
249
260
 
250
261
  return true;
251
262
  },
263
+ transformer(input, data) {
264
+ return `${data.pkgName}.${input}`;
265
+ },
252
266
  },
253
267
  ],
254
268
  actions(data) {
@@ -271,6 +285,46 @@ export default function (
271
285
  path: "bricks/{{pkgName}}/docs/{{>lastTagName}}.md",
272
286
  templateFile: "templates/brick.md.hbs",
273
287
  },
288
+ async function modifyCommonBricksJson(answers) {
289
+ if (answers.brickType === "common") {
290
+ const realBrickName = `eo-${answers.brickName}`;
291
+ const commonBricksJsonFile = path.join(
292
+ rootDir,
293
+ "shared/common-bricks/common-bricks.json"
294
+ );
295
+ /** @type {Record<string, string[]>} */
296
+ let commonBricksJson;
297
+ /** @type {string[]} */
298
+ let commonBricks;
299
+ if (existsSync(commonBricksJsonFile)) {
300
+ commonBricksJson = JSON.parse(
301
+ await readFile(commonBricksJsonFile, "utf-8")
302
+ );
303
+ if (
304
+ Object.prototype.hasOwnProperty.call(
305
+ commonBricksJson,
306
+ answers.pkgName
307
+ )
308
+ ) {
309
+ commonBricks = commonBricksJson[answers.pkgName];
310
+ } else {
311
+ commonBricks = commonBricksJson[answers.pkgName] = [];
312
+ }
313
+ } else {
314
+ commonBricksJson = {};
315
+ commonBricks = commonBricksJson[answers.pkgName] = [];
316
+ }
317
+ commonBricks.push(realBrickName);
318
+ await writeFile(
319
+ commonBricksJsonFile,
320
+ JSON.stringify(commonBricksJson, null, 2)
321
+ );
322
+ return plop.renderString(
323
+ `added {{pkgName}}: ${realBrickName} in /shared/common-bricks/common-bricks.json`,
324
+ answers
325
+ );
326
+ }
327
+ },
274
328
  ];
275
329
  } else if (data.type === "provider") {
276
330
  return [