create-blocklet 0.5.21 → 0.6.1

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.
package/index.js CHANGED
@@ -7,12 +7,13 @@ import { execSync } from 'child_process';
7
7
  import { cd, argv, fs, YAML, chalk, path } from 'zx';
8
8
  import ora from 'ora';
9
9
  import prompts from 'prompts';
10
+ import { isValid, toTypeInfo, types } from '@arcblock/did';
10
11
  import * as envfile from 'envfile';
11
12
 
12
13
  import { echoBrand, echoDocument } from './lib/arcblock.js';
13
14
  import { getUser } from './lib/index.js';
14
15
  import { checkServerInstalled, checkServerRunning, checkSatisfiedVersion, getServerDirectory } from './lib/server.js';
15
- import { toBlockletDid } from './lib/did.js';
16
+ import { getBlockletDidList } from './lib/did.js';
16
17
  import { initGitRepo } from './lib/git.js';
17
18
  import {
18
19
  copy,
@@ -125,12 +126,35 @@ const renameFiles = {
125
126
  _npmrc: '.npmrc',
126
127
  };
127
128
 
129
+ function checkDid(did = '') {
130
+ if (did) {
131
+ if (!isValid(did)) {
132
+ return false;
133
+ }
134
+ const typeInfo = toTypeInfo(did);
135
+ if (typeInfo.role !== types.RoleType.ROLE_BLOCKLET) {
136
+ return `The DID must be an blocklet DID: ${did}`;
137
+ }
138
+ }
139
+ return true;
140
+ }
141
+
128
142
  async function init() {
129
143
  const { version } = await fs.readJSONSync(path.resolve(__dirname, 'package.json'));
130
144
  await echoBrand({ version });
131
145
 
132
146
  let targetDir = argv._[0] ? String(argv._[0]) : undefined;
133
147
  const inputTemplateName = argv.template;
148
+ const connectUrl = argv.connectUrl;
149
+ const inputDid = argv.did;
150
+ const checkRes = checkDid(inputDid);
151
+ if (typeof checkRes === 'string') {
152
+ console.error(checkRes);
153
+ return;
154
+ } else if (checkRes !== true) {
155
+ console.error(`Invalid blocklet did: ${inputDid}`);
156
+ return;
157
+ }
134
158
  if (inputTemplateName && !templates.find((item) => item.name === inputTemplateName)) {
135
159
  console.error(`${red('✖')} The template ${inputTemplateName} is invalid.`);
136
160
  return;
@@ -285,7 +309,19 @@ async function init() {
285
309
  }
286
310
  }
287
311
 
312
+ let didList = [];
313
+ if (inputDid && templateNames.length === 1) {
314
+ didList = [inputDid];
315
+ } else {
316
+ try {
317
+ didList = await getBlockletDidList(templateNames, connectUrl);
318
+ } catch {
319
+ process.exit(1);
320
+ }
321
+ }
322
+
288
323
  for (const templateName of templateNames) {
324
+ const index = templateNames.indexOf(templateName);
289
325
  const templateDir = path.join(__dirname, `templates/${templateName}`);
290
326
  const finalTemplateName = `${name}-${templateName}`;
291
327
  // TODO: 需要把 common file copy 的逻辑移除,不同的 template 之间的差异越来越多,就会需要越来越多特殊处理的代码,违背了初衷,移除这部分逻辑可能是更好的选择
@@ -350,8 +386,8 @@ async function init() {
350
386
  );
351
387
 
352
388
  // patch did
353
- (() => {
354
- const did = toBlockletDid(mainBlocklet ? finalTemplateName : name);
389
+ async function patchDid() {
390
+ const did = didList[index];
355
391
  modifyBlockletYaml(
356
392
  (yamlConfig) => {
357
393
  yamlConfig.did = did;
@@ -372,7 +408,7 @@ async function init() {
372
408
  }
373
409
  // 如果用户选了多个模板,为其他应用配置好 dev:child 和 deploy:child
374
410
  if (mainBlocklet && templateName !== mainBlocklet) {
375
- const mainBlockletDid = toBlockletDid(`${name}-${mainBlocklet}`);
411
+ const mainBlockletDid = didList[templateNames.indexOf(mainBlocklet)];
376
412
  pkg.scripts['dev:child'] = ejs.render(pkg.scripts['dev:child'], { did: mainBlockletDid });
377
413
  pkg.scripts['deploy:child'] = ejs.render(pkg.scripts['deploy:child'], { did: mainBlockletDid });
378
414
  }
@@ -390,7 +426,8 @@ async function init() {
390
426
  // disabled random logo
391
427
  // const pngIcon = toDidIcon(did, undefined, true);
392
428
  // fs.writeFileSync(path.join(root, 'logo.png'), pngIcon);
393
- })();
429
+ }
430
+ await patchDid();
394
431
  }
395
432
 
396
433
  scaffoldSpinner.succeed('✨ Done. Now run:\n');
@@ -0,0 +1 @@
1
+ export const BLOCKLET_COMMAND = 'blocklet';
package/lib/did.js CHANGED
@@ -3,6 +3,9 @@ import Mcrypto from '@ocap/mcrypto';
3
3
  import * as jdenticon from 'jdenticon';
4
4
  import { toHex } from '@ocap/util';
5
5
  import { fromPublicKey } from '@arcblock/did';
6
+ import { execSync } from 'child_process';
7
+ import { trimServerOutputVersion } from './server.js';
8
+ import { BLOCKLET_COMMAND } from './constant.js';
6
9
 
7
10
  const { types } = Mcrypto;
8
11
 
@@ -14,3 +17,27 @@ export function toBlockletDid(name) {
14
17
  export function toDidIcon(did, size = 200, isPng = false) {
15
18
  return isPng ? jdenticon.toPng(did, size) : jdenticon.toSvg(did, size);
16
19
  }
20
+
21
+ export async function getBlockletDidList(monikerList = [], connectUrl) {
22
+ try {
23
+ let command = `${BLOCKLET_COMMAND} init`;
24
+ if (monikerList.length > 0) {
25
+ command += ` --monikers=${monikerList.join(',')}`;
26
+ } else {
27
+ return [];
28
+ }
29
+
30
+ if (connectUrl) {
31
+ command += ` --connectUrl=${connectUrl}`;
32
+ }
33
+ const output = execSync(command);
34
+ const pureOutput = await trimServerOutputVersion(output.toString('utf8'));
35
+ const didStrList = pureOutput.split('\n').pop();
36
+ return didStrList
37
+ .trim()
38
+ .split(',')
39
+ .filter((x) => x !== '');
40
+ } catch {
41
+ throw new Error('Failed to generate blocklet did');
42
+ }
43
+ }
package/lib/server.js CHANGED
@@ -1,21 +1,22 @@
1
1
  import { $, which } from 'zx';
2
2
  import semver from 'semver';
3
+ import { BLOCKLET_COMMAND } from './constant.js';
3
4
 
4
5
  $.verbose = false;
5
6
 
6
- async function trimServerOutputVersion(output = '', command) {
7
+ export async function trimServerOutputVersion(output = '', command) {
7
8
  // 调用 blocklet 命令时,都会在第一行先打印一个 blocklet [command] [version] 的信息,需要把这个信息 trim 掉
8
9
  const version = await getServerVersion();
9
10
  if (command) {
10
- return output?.replace(`blocklet ${command} v${version}\n`, '');
11
+ return output?.replace(`${BLOCKLET_COMMAND} ${command} v${version}\n`, '');
11
12
  }
12
- const reg = new RegExp(`blocklet \\S+ v${version}\\n+`, 'g');
13
- return output?.replace(reg, '');
13
+ const reg = new RegExp(`${BLOCKLET_COMMAND} \\S+ v${version}\\n+`, 'g');
14
+ return output?.replace(reg, '').trim();
14
15
  }
15
16
 
16
17
  export async function checkServerInstalled() {
17
18
  try {
18
- await which('blocklet');
19
+ await which(BLOCKLET_COMMAND);
19
20
  return true;
20
21
  } catch (e) {
21
22
  return false;
@@ -24,7 +25,7 @@ export async function checkServerInstalled() {
24
25
 
25
26
  export async function getServerVersion() {
26
27
  try {
27
- const { stdout: output } = await $`blocklet --version`;
28
+ const { stdout: output } = await $`${BLOCKLET_COMMAND} --version`;
28
29
  return output.trim();
29
30
  } catch (e) {
30
31
  return '0.0.0';
@@ -33,7 +34,7 @@ export async function getServerVersion() {
33
34
 
34
35
  export async function getServerStatus() {
35
36
  try {
36
- const { stdout: output } = await $`blocklet server status`;
37
+ const { stdout: output } = await $`${BLOCKLET_COMMAND} server status`;
37
38
  const [matchStr] = output.match(/Blocklet Server status:[\s\S]*?\n/g) || [];
38
39
  const status = matchStr.replace(/Blocklet Server status:\s*(\S+)\s*\S*\n*[\s\S]*/gm, '$1');
39
40
  return status.toLowerCase();
@@ -54,7 +55,7 @@ export async function checkSatisfiedVersion() {
54
55
 
55
56
  export async function getServerDirectory() {
56
57
  try {
57
- const { stdout: output } = await $`blocklet server status`;
58
+ const { stdout: output } = await $`${BLOCKLET_COMMAND} server status`;
58
59
  const [matchStr] = output.match(/Blocklet Server Data Directory:[\s\S]*?\n/gm) || [];
59
60
  if (!matchStr) return null;
60
61
 
@@ -67,8 +68,8 @@ export async function getServerDirectory() {
67
68
 
68
69
  export async function getUserInfo() {
69
70
  try {
70
- const { stdout: name } = await $`blocklet config get name`;
71
- const { stdout: email } = await $`blocklet config get email`;
71
+ const { stdout: name } = await $`${BLOCKLET_COMMAND} config get name`;
72
+ const { stdout: email } = await $`${BLOCKLET_COMMAND} config get email`;
72
73
  return {
73
74
  name: await trimServerOutputVersion(name?.trim()),
74
75
  email: await trimServerOutputVersion(email?.trim()),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-blocklet",
3
- "version": "0.5.21",
3
+ "version": "0.6.1",
4
4
  "exports": "./index.js",
5
5
  "type": "module",
6
6
  "repository": "git@github.com:blocklet/create-blocklet.git",
@@ -71,7 +71,7 @@
71
71
  "npm-run-all": "^4.1.5",
72
72
  "prettier": "^2.8.7",
73
73
  "vite": "^3.2.5",
74
- "vite-plugin-blocklet": "^0.5.21",
74
+ "vite-plugin-blocklet": "^0.6.1",
75
75
  "vite-plugin-node-polyfills": "^0.7.0",
76
76
  "vite-plugin-svgr": "^2.4.0",
77
77
  "zx": "^7.2.1"
@@ -82,7 +82,7 @@
82
82
  "ts-node": "^10.9.1",
83
83
  "typescript": "^4.9.5",
84
84
  "vite": "^3.2.5",
85
- "vite-plugin-blocklet": "^0.5.21",
85
+ "vite-plugin-blocklet": "^0.6.1",
86
86
  "vite-plugin-node-polyfills": "^0.7.0",
87
87
  "vite-plugin-svgr": "^2.4.0",
88
88
  "zx": "^7.2.1"
@@ -72,7 +72,7 @@
72
72
  "prettier": "^2.8.7",
73
73
  "rimraf": "^3.0.2",
74
74
  "vite": "^3.2.5",
75
- "vite-plugin-blocklet": "^0.5.21",
75
+ "vite-plugin-blocklet": "^0.6.1",
76
76
  "vite-plugin-node-polyfills": "^0.7.0",
77
77
  "vite-plugin-svgr": "^2.4.0",
78
78
  "zx": "^7.2.1"
@@ -53,7 +53,7 @@
53
53
  "prettier": "^2.8.7",
54
54
  "rimraf": "^3.0.2",
55
55
  "vite": "^3.2.5",
56
- "vite-plugin-blocklet": "^0.5.21",
56
+ "vite-plugin-blocklet": "^0.6.1",
57
57
  "vite-plugin-node-polyfills": "^0.7.0",
58
58
  "vite-plugin-svgr": "^2.4.0",
59
59
  "zx": "^7.2.1"
@@ -33,7 +33,7 @@
33
33
  "prettier": "^2.8.7",
34
34
  "rimraf": "^3.0.2",
35
35
  "vite": "^3.2.5",
36
- "vite-plugin-blocklet": "^0.5.21",
36
+ "vite-plugin-blocklet": "^0.6.1",
37
37
  "vite-plugin-node-polyfills": "^0.7.0",
38
38
  "vite-plugin-solid": "^2.6.1",
39
39
  "zx": "^7.2.1"
@@ -29,7 +29,7 @@
29
29
  "prettier": "^2.8.7",
30
30
  "rimraf": "^3.0.2",
31
31
  "vite": "^3.2.5",
32
- "vite-plugin-blocklet": "^0.5.21",
32
+ "vite-plugin-blocklet": "^0.6.1",
33
33
  "vite-plugin-node-polyfills": "^0.7.0",
34
34
  "vite-plugin-solid": "^2.6.1",
35
35
  "zx": "^7.2.1"
@@ -47,7 +47,7 @@
47
47
  "rimraf": "^3.0.2",
48
48
  "svelte": "^3.58.0",
49
49
  "vite": "^3.2.5",
50
- "vite-plugin-blocklet": "^0.5.21",
50
+ "vite-plugin-blocklet": "^0.6.1",
51
51
  "vite-plugin-html": "^3.2.0",
52
52
  "vite-plugin-node-polyfills": "^0.7.0",
53
53
  "zx": "^7.2.1"
@@ -30,7 +30,7 @@
30
30
  "rimraf": "^3.0.2",
31
31
  "svelte": "^3.58.0",
32
32
  "vite": "^3.2.5",
33
- "vite-plugin-blocklet": "^0.5.21",
33
+ "vite-plugin-blocklet": "^0.6.1",
34
34
  "vite-plugin-node-polyfills": "^0.7.0",
35
35
  "zx": "^7.2.1"
36
36
  },
@@ -49,7 +49,7 @@
49
49
  "prettier": "^2.8.7",
50
50
  "rimraf": "^3.0.2",
51
51
  "vite": "^3.2.5",
52
- "vite-plugin-blocklet": "^0.5.21",
52
+ "vite-plugin-blocklet": "^0.6.1",
53
53
  "vite-plugin-node-polyfills": "^0.7.0",
54
54
  "zx": "^7.2.1"
55
55
  },
@@ -31,7 +31,7 @@
31
31
  "prettier": "^2.8.7",
32
32
  "rimraf": "^3.0.2",
33
33
  "vite": "^3.2.5",
34
- "vite-plugin-blocklet": "^0.5.21",
34
+ "vite-plugin-blocklet": "^0.6.1",
35
35
  "vite-plugin-node-polyfills": "^0.7.0",
36
36
  "zx": "^7.2.1"
37
37
  },
@@ -49,7 +49,7 @@
49
49
  "prettier": "^2.8.7",
50
50
  "rimraf": "^3.0.2",
51
51
  "vite": "^3.2.5",
52
- "vite-plugin-blocklet": "^0.5.21",
52
+ "vite-plugin-blocklet": "^0.6.1",
53
53
  "vite-plugin-node-polyfills": "^0.7.0",
54
54
  "zx": "^7.2.1"
55
55
  },
@@ -31,7 +31,7 @@
31
31
  "prettier": "^2.8.7",
32
32
  "rimraf": "^3.0.2",
33
33
  "vite": "^3.2.5",
34
- "vite-plugin-blocklet": "^0.5.21",
34
+ "vite-plugin-blocklet": "^0.6.1",
35
35
  "vite-plugin-node-polyfills": "^0.7.0",
36
36
  "zx": "^7.2.1"
37
37
  },