create-blocklet 0.2.16 → 0.3.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 (81) hide show
  1. package/README.md +8 -3
  2. package/common/.github/workflows/main.yml +5 -5
  3. package/common/.husky/pre-commit +4 -0
  4. package/common/_eslintignore +10 -0
  5. package/common/_eslintrc.js +1 -0
  6. package/common/_gitignore +15 -9
  7. package/common/logo.png +0 -0
  8. package/common/public/favicon.ico +0 -0
  9. package/index.js +177 -105
  10. package/lib/arcblock.js +3 -1
  11. package/lib/git.js +25 -0
  12. package/lib/index.js +17 -5
  13. package/lib/npm.js +7 -9
  14. package/lib/server.js +26 -20
  15. package/package.json +18 -16
  16. package/template-dapp/react/README.md +5 -5
  17. package/template-dapp/react/{server → api}/hooks/pre-start.js +0 -0
  18. package/template-dapp/react/{server → api}/index.js +0 -0
  19. package/template-dapp/react/{server → api}/libs/auth.js +0 -0
  20. package/template-dapp/react/{server → api}/libs/env.js +0 -0
  21. package/template-dapp/react/{server → api}/libs/logger.js +0 -0
  22. package/template-dapp/{vue2/server → react/api}/routes/index.js +1 -1
  23. package/template-dapp/react/blocklet.yml +6 -6
  24. package/template-dapp/react/package.json +35 -23
  25. package/template-dapp/react/public/index.html +1 -3
  26. package/template-dapp/react/src/app.js +8 -8
  27. package/template-dapp/vue/.browserslistrc +4 -0
  28. package/template-dapp/vue/.eslintrc.js +25 -0
  29. package/template-dapp/vue/{server → api}/hooks/pre-start.js +0 -0
  30. package/template-dapp/vue/{server → api}/index.js +0 -0
  31. package/template-dapp/vue/{server → api}/libs/auth.js +0 -0
  32. package/template-dapp/vue/{server → api}/libs/env.js +0 -0
  33. package/template-dapp/vue/{server → api}/libs/logger.js +0 -0
  34. package/template-dapp/{react/server → vue/api}/routes/index.js +1 -1
  35. package/template-dapp/vue/blocklet.yml +6 -5
  36. package/template-dapp/vue/index.html +2 -3
  37. package/template-dapp/vue/package.json +38 -21
  38. package/template-dapp/vue/src/App.vue +4 -2
  39. package/template-dapp/vue/vite.config.js +8 -6
  40. package/template-dapp/vue2/.browserslistrc +4 -0
  41. package/template-dapp/vue2/.eslintrc.js +14 -0
  42. package/template-dapp/vue2/{server → api}/hooks/pre-start.js +0 -0
  43. package/template-dapp/vue2/{server → api}/index.js +0 -0
  44. package/template-dapp/vue2/{server → api}/libs/auth.js +0 -0
  45. package/template-dapp/vue2/{server → api}/libs/env.js +0 -0
  46. package/template-dapp/vue2/{server → api}/libs/logger.js +0 -0
  47. package/template-dapp/{vue/server → vue2/api}/routes/index.js +1 -1
  48. package/template-dapp/vue2/blocklet.yml +7 -5
  49. package/template-dapp/vue2/jsconfig.json +19 -0
  50. package/template-dapp/vue2/package.json +38 -43
  51. package/template-dapp/vue2/public/index.html +2 -9
  52. package/template-dapp/vue2/src/App.vue +17 -1
  53. package/template-dapp/vue2/src/main.js +0 -2
  54. package/template-dapp/vue2/vue.config.js +0 -1
  55. package/template-static/blocklet-page/package.json +5 -5
  56. package/template-static/react/blocklet.yml +3 -3
  57. package/template-static/react/package.json +21 -9
  58. package/template-static/react/public/index.html +1 -3
  59. package/template-static/react/src/app.js +8 -8
  60. package/template-static/vue/.browserslistrc +4 -0
  61. package/template-static/vue/.eslintrc.js +25 -0
  62. package/template-static/vue/blocklet.yml +3 -3
  63. package/template-static/vue/index.html +2 -3
  64. package/template-static/vue/package.json +25 -8
  65. package/template-static/vue/vite.config.js +8 -6
  66. package/template-static/vue2/.browserslistrc +4 -0
  67. package/template-static/vue2/.eslintrc.js +14 -0
  68. package/template-static/vue2/blocklet.yml +3 -3
  69. package/template-static/vue2/jsconfig.json +19 -0
  70. package/template-static/vue2/package.json +25 -30
  71. package/template-static/vue2/public/index.html +2 -9
  72. package/template-static/vue2/src/App.vue +7 -1
  73. package/template-static/vue2/src/main.js +0 -2
  74. package/template-static/vue2/vue.config.js +0 -3
  75. package/common/public/favicon.svg +0 -1
  76. package/template-dapp/vue2/src/pages/About.vue +0 -13
  77. package/template-dapp/vue2/src/pages/Home.vue +0 -27
  78. package/template-dapp/vue2/src/router.js +0 -24
  79. package/template-static/vue2/src/pages/About.vue +0 -13
  80. package/template-static/vue2/src/pages/Home.vue +0 -20
  81. package/template-static/vue2/src/router.js +0 -24
package/README.md CHANGED
@@ -17,16 +17,16 @@ With NPM:
17
17
  npm init blocklet@latest [blocklet-name]
18
18
  ```
19
19
 
20
- With Yarn:
20
+ With Yarn: (recommend)
21
21
 
22
22
  ```bash
23
23
  yarn create blocklet [blocklet-name]
24
24
  ```
25
25
 
26
- With PNPM:
26
+ With PNPM: (recommend)
27
27
 
28
28
  ```bash
29
- pnpx create-blocklet [blocklet-name]
29
+ pnpm create blocklet [blocklet-name]
30
30
  ```
31
31
 
32
32
  Currently supported template presets include:
@@ -34,10 +34,15 @@ Currently supported template presets include:
34
34
  **dapp**
35
35
 
36
36
  - `react`
37
+ - `vue3 + vite`
38
+ - `vue2 + @vue/cli`
37
39
 
38
40
  **static**
39
41
 
40
42
  - `react`
43
+ - `vue3 + vite`
44
+ - `vue2 + @vue/cli`
45
+ - `blocklet page`
41
46
 
42
47
  ## Community Templates
43
48
 
@@ -27,10 +27,10 @@ jobs:
27
27
  skip-upload: false
28
28
  skip-deploy: false
29
29
  bundle-command: yarn bundle
30
- store-endpoint: ${{ secrets.STORE_ENDPOINT_DEV }}
31
- store-access-token: ${{ secrets.STORE_ACCESS_TOKEN_DEV }}
32
- server-endpoint: ${{ secrets.STAGING_NODE_ENDPOINT }}
33
- server-access-key: ${{ secrets.STAGING_NODE_ACCESS_KEY }}
34
- server-access-secret: ${{ secrets.STAGING_NODE_ACCESS_SECRET }}
30
+ store-endpoint: ${{ secrets.STORE_ENDPOINT }}
31
+ store-access-token: ${{ secrets.STORE_ACCESS_TOKEN }}
32
+ server-endpoint: ${{ secrets.SERVER_ENDPOINT }}
33
+ server-access-key: ${{ secrets.SERVER_ACCESS_KEY }}
34
+ server-access-secret: ${{ secrets.SERVER_ACCESS_SECRET }}
35
35
  slack-webhook: ${{ secrets.SLACK_WEBHOOK }}
36
36
  github-token: ${{ secrets.GITHUB_TOKEN }}
@@ -0,0 +1,4 @@
1
+ #!/bin/sh
2
+ . "$(dirname "$0")/_/husky.sh"
3
+
4
+ npx lint-staged
@@ -0,0 +1,10 @@
1
+ public
2
+ dist
3
+ build
4
+
5
+ .blocklet
6
+ node_modules
7
+ .github
8
+ .husky
9
+ *.d.ts
10
+ package.json
@@ -1,4 +1,5 @@
1
1
  module.exports = {
2
+ root: true,
2
3
  parser: 'babel-eslint',
3
4
  extends: '@arcblock/eslint-config',
4
5
  env: {
package/common/_gitignore CHANGED
@@ -1,9 +1,10 @@
1
1
  # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2
2
 
3
3
  # dependencies
4
- /node_modules
5
4
  /.pnp
6
5
  .pnp.js
6
+ .DS_Store
7
+ node_modules
7
8
 
8
9
  # testing
9
10
  /coverage
@@ -11,19 +12,24 @@
11
12
  # production
12
13
  /build
13
14
  /dist
15
+ .blocklet
14
16
 
15
- # misc
16
- .DS_Store
17
+ # local env files
17
18
  .env.local
18
- .env.development.local
19
- .env.test.local
20
- .env.production.local
19
+ .env.*.local
21
20
 
21
+ # Log files
22
22
  npm-debug.log*
23
23
  yarn-debug.log*
24
24
  yarn-error.log*
25
+ pnpm-debug.log*
25
26
 
26
- .blocklet
27
- blocklet.js
28
- .DS_Store
27
+ # Editor directories and files
28
+ .idea
29
29
  .vscode
30
+ *.suo
31
+ *.ntvs*
32
+ *.njsproj
33
+ *.sln
34
+ *.sw?
35
+
package/common/logo.png CHANGED
Binary file
Binary file
package/index.js CHANGED
@@ -1,21 +1,22 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import fs from 'fs';
4
3
  import ejs from 'ejs';
5
- import path from 'path';
6
- import YAML from 'yaml';
4
+ import boxen from 'boxen';
7
5
  import { fileURLToPath } from 'url';
8
- import minimist from 'minimist';
6
+ import { execSync } from 'child_process';
7
+ import { cd, argv, fs, YAML, chalk, path } from 'zx';
8
+ import { startSpinner } from 'zx/experimental';
9
9
  import prompts from 'prompts';
10
- import { yellow, red, green, cyan, blue, lightYellow } from 'kolorist';
11
10
  import * as envfile from 'envfile';
12
11
 
13
12
  import { echoBrand, echoDocument } from './lib/arcblock.js';
14
- import { getAuthor } from './lib/npm.js';
13
+ import { getUser } from './lib/index.js';
15
14
  import { checkServerInstalled, checkServerRunning, checkSatisfiedVersion, getServerDirectory } from './lib/server.js';
16
- import { toBlockletDid, toDidIcon } from './lib/did.js';
15
+ import { toBlockletDid } from './lib/did.js';
16
+ import { initGitRepo } from './lib/git.js';
17
+
18
+ const { yellow, red, green, cyan, blue, bold, dim } = chalk;
17
19
 
18
- const argv = minimist(process.argv.slice(2));
19
20
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
20
21
 
21
22
  const cwd = process.cwd();
@@ -63,33 +64,38 @@ const TYPES = [
63
64
  },
64
65
  {
65
66
  name: 'blocklet-page',
66
- display: 'Blocklet Page',
67
+ display: 'blocklet page',
67
68
  color: blue,
68
69
  },
69
70
  ],
70
71
  },
71
72
  ];
72
73
 
73
- const SERVICES = [
74
- {
75
- name: 'auth',
76
- display: 'Auth Service',
77
- color: yellow,
78
- },
79
- ];
80
-
81
74
  const renameFiles = {
82
75
  _gitignore: '.gitignore',
83
76
  '_eslintrc.js': '.eslintrc.js',
77
+ _eslintignore: '.eslintignore',
84
78
  _npmrc: '.npmrc',
85
79
  };
86
80
 
87
81
  async function init() {
88
- let targetDir = argv._[0];
82
+ const { version } = await fs.readJSONSync(path.resolve(__dirname, 'package.json'));
83
+
84
+ console.log(
85
+ boxen(`${bold('Blocklet') + dim(' Starter')} ${blue(`v${version}`)}`, {
86
+ padding: 1,
87
+ margin: 1,
88
+ float: 'center',
89
+ borderStyle: 'double',
90
+ })
91
+ );
92
+
93
+ let targetDir = argv._[0] ? String(argv._[0]) : undefined;
89
94
 
90
95
  const defaultProjectName = !targetDir ? 'blocklet-project' : targetDir;
91
96
 
92
97
  let result = {};
98
+ const authorInfo = await getUser();
93
99
 
94
100
  try {
95
101
  result = await prompts(
@@ -162,17 +168,16 @@ async function init() {
162
168
  },
163
169
  },
164
170
  {
165
- type: 'multiselect',
166
- name: 'services',
167
- message: 'Choose blocklet services:',
168
- initial: 0,
169
- choices: SERVICES.map((service) => {
170
- const serviceColor = service.color;
171
- return {
172
- title: serviceColor(service.display),
173
- value: service.name,
174
- };
175
- }),
171
+ type: 'text',
172
+ name: 'authorName',
173
+ message: 'Author name:',
174
+ initial: authorInfo?.name || '',
175
+ },
176
+ {
177
+ type: 'text',
178
+ name: 'authorEmail',
179
+ message: 'Author email:',
180
+ initial: authorInfo?.email || '',
176
181
  },
177
182
  ],
178
183
  {
@@ -182,12 +187,14 @@ async function init() {
182
187
  }
183
188
  );
184
189
  } catch (cancelled) {
185
- console.log(cancelled.message);
190
+ console.error(cancelled.message);
186
191
  return;
187
192
  }
188
193
 
189
194
  // user choice associated with prompts
190
- const { type, framework, overwrite, packageName, services } = result;
195
+ const { type, framework, overwrite, packageName, authorName, authorEmail } = result;
196
+
197
+ const stopSpinner = startSpinner();
191
198
 
192
199
  const root = path.join(cwd, targetDir);
193
200
 
@@ -202,42 +209,9 @@ async function init() {
202
209
 
203
210
  console.log('Checking blocklet server runtime environment...', '\n');
204
211
 
205
- const isServerInstalled = checkServerInstalled();
206
- const isSatisfiedVersion = checkSatisfiedVersion();
207
- const isServerRunning = checkServerRunning();
208
-
209
- if (!isServerInstalled) {
210
- // 未安装 blocklet server
211
- console.log(red('To run the blocklet, you need a running blocklet server instance on local machine.'), '\n');
212
- console.log(`Checkout ${green('README.md')} for more usage instructions.`);
213
- console.log('Now you should run:', '\n');
214
- console.log(cyan('npm install -g @blocklet/cli'));
215
- console.log(cyan('blocklet server start -a'));
216
- } else if (!isSatisfiedVersion) {
217
- // 已安装 blocklet server,但版本不满足
218
- console.log(red('Your blocklet server version is outdate, please update it to the latest version.'));
219
- console.log('Now you should run:', '\n');
220
- if (isServerRunning) {
221
- // blocklet server 已经启动
222
- const serverPath = getServerDirectory();
223
- console.log(cyan(`cd ${serverPath}`));
224
- console.log(cyan('blocklet server stop'));
225
- console.log(cyan('npm install -g @blocklet/cli'));
226
- console.log(cyan('blocklet server start'));
227
- console.log(cyan(`cd ${cwd}`));
228
- } else {
229
- // blocklet server 未启动
230
- // TODO: 如何获取未启动的 blocklet server 实例目录?
231
- console.log(cyan('npm install -g @blocklet/cli'));
232
- console.log(cyan('blocklet server start -a'));
233
- }
234
- } else if (!isServerRunning) {
235
- // 已经安装 blocklet server,且版本满足,并且 blocklet server 未启动
236
- console.log(red('You need to start your blocklet server before develop this blocklet.'));
237
- console.log('Now you should run:', '\n');
238
- // TODO: 如何获取未启动的 blocklet server 实例目录?
239
- console.log(cyan('blocklet server start -a'));
240
- }
212
+ const isServerInstalled = await checkServerInstalled();
213
+ const isSatisfiedVersion = await checkSatisfiedVersion();
214
+ const isServerRunning = await checkServerRunning();
241
215
 
242
216
  console.log(`\n\nScaffolding project in ${root}...`);
243
217
 
@@ -253,6 +227,10 @@ async function init() {
253
227
  // eslint-disable-next-line no-continue
254
228
  continue;
255
229
  }
230
+ if (framework === 'blocklet-page' && ['_eslintignore', '.husky'].includes(file)) {
231
+ // eslint-disable-next-line no-continue
232
+ continue;
233
+ }
256
234
  const targetPath = renameFiles[file] ? path.join(root, renameFiles[file]) : path.join(root, file);
257
235
  copy(path.join(commonDir, file), targetPath);
258
236
  }
@@ -277,34 +255,23 @@ async function init() {
277
255
  modifyBlockletMd((md) => {
278
256
  return md.replace(/# template-react/g, `# ${name}`);
279
257
  });
280
- modifyEnv((env) => {
281
- if (framework === 'react') {
282
- env.REACT_APP_TITLE = name;
283
- } else {
284
- env.APP_TITLE = name;
285
- }
286
- return env;
287
- });
258
+ if (!['blocklet-page'].includes(framework)) {
259
+ modifyEnv((env) => {
260
+ if (['react'].includes(framework)) {
261
+ env.REACT_APP_TITLE = name;
262
+ } else if (['vue', 'blocklet-page'].includes(framework)) {
263
+ env.VITE_APP_TITLE = name;
264
+ } else {
265
+ env.APP_TITLE = name;
266
+ }
267
+ return env;
268
+ });
269
+ }
288
270
 
289
- // patch blocklet services
290
- modifyBlockletYaml((yamlConfig) => {
291
- if (services.includes('auth')) {
292
- yamlConfig.interfaces[0].services = [
293
- {
294
- name: '@abtnode/auth-service',
295
- config: {
296
- blockUnauthenticated: true,
297
- },
298
- },
299
- ];
300
- }
301
- });
302
271
  // patch blocklet author
303
- modifyBlockletYaml((yamlConfig) => {
304
- // eslint-disable-next-line no-shadow
305
- const { name, email } = getAuthor();
306
- if (name) yamlConfig.author.name = name;
307
- if (email) yamlConfig.author.email = email;
272
+ modifyBlockletYaml(async (yamlConfig) => {
273
+ yamlConfig.author.name = authorName;
274
+ yamlConfig.author.email = authorEmail;
308
275
  });
309
276
 
310
277
  // patch did
@@ -320,23 +287,128 @@ async function init() {
320
287
  pkg.scripts.bundle = ejs.render(pkg.scripts.bundle, { did });
321
288
  }
322
289
  });
323
- const pngIcon = toDidIcon(did, undefined, true);
324
- fs.writeFileSync(path.join(root, 'logo.png'), pngIcon);
290
+ // disabled random logo
291
+ // const pngIcon = toDidIcon(did, undefined, true);
292
+ // fs.writeFileSync(path.join(root, 'logo.png'), pngIcon);
325
293
  })();
326
294
 
327
- const pkgManager = /yarn/.test(process.env.npm_execpath) ? 'yarn' : 'npm';
328
-
295
+ const related = path.relative(cwd, root);
329
296
  console.log('\n\n✨ Done. Now run:\n');
297
+ stopSpinner();
298
+
299
+ // const pkgManager =
300
+ // // eslint-disable-next-line no-nested-ternary
301
+ // /pnpm/.test(process.env.npm_execpath) || /pnpm/.test(process.env.npm_config_user_agent)
302
+ // ? 'pnpm'
303
+ // : /yarn/.test(process.env.npm_execpath)
304
+ // ? 'yarn'
305
+ // : 'npm';
306
+ try {
307
+ const { yes } = await prompts(
308
+ {
309
+ type: 'confirm',
310
+ name: 'yes',
311
+ initial: 'Y',
312
+ message: 'Install and start it now?',
313
+ },
314
+ {
315
+ onCancel: () => {
316
+ throw new Error(`${red('✖')} Operation cancelled`);
317
+ },
318
+ }
319
+ );
320
+ let hasStart = false;
321
+
322
+ await initGitRepo(root);
323
+
324
+ let defaultAgent = 'npm';
325
+ let agentList = ['npm', 'yarn', 'pnpm'];
326
+ switch (framework) {
327
+ case 'react':
328
+ case 'blocklet-page':
329
+ agentList = ['npm', 'yarn'];
330
+ break;
331
+ default:
332
+ break;
333
+ }
334
+ if (yes) {
335
+ const { agent } = await prompts({
336
+ name: 'agent',
337
+ type: 'select',
338
+ message: 'Choose the agent',
339
+ choices: agentList.map((i) => ({ value: i, title: i })),
340
+ });
341
+
342
+ if (!agent) {
343
+ return;
344
+ }
345
+ defaultAgent = agent;
346
+
347
+ await cd(root);
348
+ execSync(`${agent} install`, { stdio: 'inherit' });
349
+ if (isServerInstalled && isServerRunning && isSatisfiedVersion) {
350
+ console.log(
351
+ boxen(bold('blocklet dev'), {
352
+ padding: 1,
353
+ margin: 1,
354
+ float: 'center',
355
+ })
356
+ );
357
+ hasStart = true;
358
+ execSync('blocklet dev', { stdio: 'inherit' });
359
+ } else {
360
+ console.log();
361
+ console.log();
362
+ }
363
+ } else {
364
+ console.log();
365
+ console.log();
366
+ }
330
367
 
331
- if (root !== cwd) {
332
- console.log(` ${cyan('cd')} ${path.relative(cwd, root)}`);
333
- }
368
+ if (!isServerInstalled) {
369
+ // 未安装 blocklet server
370
+ console.log(red('To run the blocklet, you need a running blocklet server instance on local machine.'), '\n');
371
+ console.log(`Checkout ${green('README.md')} for more usage instructions.`);
372
+ console.log('Now you should run:', '\n');
373
+ console.log(cyan(`${defaultAgent} install -g @blocklet/cli`));
374
+ console.log(cyan('blocklet server start -a'));
375
+ } else if (!isSatisfiedVersion) {
376
+ // 已安装 blocklet server,但版本不满足
377
+ console.log(red('Your blocklet server version is outdate, please update it to the latest version.'));
378
+ console.log('Now you should run:', '\n');
379
+ if (isServerRunning) {
380
+ // blocklet server 已经启动
381
+ const serverPath = await getServerDirectory();
382
+ console.log(cyan(`cd ${serverPath}`));
383
+ console.log(cyan('blocklet server stop'));
384
+ console.log(cyan(`${defaultAgent} install -g @blocklet/cli`));
385
+ console.log(cyan('blocklet server start'));
386
+ } else {
387
+ // blocklet server 未启动
388
+ // TODO: 如何获取未启动的 blocklet server 实例目录?
389
+ console.log(cyan(`${defaultAgent} install -g @blocklet/cli`));
390
+ console.log(cyan('blocklet server start -a'));
391
+ }
392
+ } else if (!isServerRunning) {
393
+ // 已经安装 blocklet server,且版本满足,并且 blocklet server 未启动
394
+ console.log(red('You need to start your blocklet server before develop this blocklet.'));
395
+ console.log('Now you should run:', '\n');
396
+ // TODO: 如何获取未启动的 blocklet server 实例目录?
397
+ console.log(cyan('blocklet server start -a'));
398
+ }
334
399
 
335
- console.log(cyan(` ${pkgManager === 'yarn' ? 'yarn' : 'npm install'}`));
336
- console.log(cyan('blocklet dev'));
337
- console.log();
400
+ if (!hasStart) {
401
+ console.log(dim('\n start it later by:\n'));
402
+ if (root !== cwd) console.log(blue(` cd ${bold(related)}`));
338
403
 
339
- console.log(`Find more usage in ${green('README.md')}`, '\n');
404
+ console.log(blue(` ${defaultAgent === 'yarn' ? 'yarn' : `${defaultAgent} install`}`));
405
+ console.log(blue(` ${defaultAgent === 'yarn' ? 'yarn dev' : `${defaultAgent} run dev`}`));
406
+ console.log(cyan('blocklet dev'));
407
+ console.log('\n', `Find more usage in ${green('README.md')}`, '\n');
408
+ }
409
+ } catch (cancelled) {
410
+ console.error(cancelled.message);
411
+ }
340
412
 
341
413
  // inside functions
342
414
  function write(file, content) {
@@ -375,7 +447,7 @@ async function init() {
375
447
  modifyFn(env);
376
448
  write('.env', envfile.stringify(env));
377
449
  } catch {
378
- console.warn(`\n${lightYellow('No .env file found, please add one.')}`);
450
+ console.warn(`\n${yellow('No .env file found, please add one.')}`);
379
451
  }
380
452
  }
381
453
  }
package/lib/arcblock.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import figlet from 'figlet';
2
2
  import terminalLink from 'terminal-link';
3
- import { green } from 'kolorist';
4
3
  import gradient from 'gradient-string';
4
+ import { chalk } from 'zx';
5
+
6
+ const { green } = chalk;
5
7
 
6
8
  export function echoBrand() {
7
9
  let msg;
package/lib/git.js ADDED
@@ -0,0 +1,25 @@
1
+ import { $, which, cd } from 'zx';
2
+
3
+ $.verbose = false;
4
+
5
+ export async function isGitInstalled() {
6
+ try {
7
+ await which('git');
8
+ return true;
9
+ } catch (e) {
10
+ return false;
11
+ }
12
+ }
13
+
14
+ export async function initGitRepo(root) {
15
+ await cd(root);
16
+ await $`git init`;
17
+ await $`git add .`;
18
+ await $`git commit -m 'init'`;
19
+ }
20
+
21
+ export async function getUserInfo() {
22
+ const { stdout: name } = await $`git config user.name`;
23
+ const { stdout: email } = await $`git config user.email`;
24
+ return { name: name.trim(), email: email.trim() };
25
+ }
package/lib/index.js CHANGED
@@ -1,7 +1,19 @@
1
- /* eslint-disable import/prefer-default-export */
2
- import shell from 'shelljs';
3
- import { stripColors } from 'kolorist';
1
+ import { $ } from 'zx';
2
+ import { getAuthor } from './npm.js';
3
+ import { getUserInfo } from './git.js';
4
4
 
5
- export function getOutput(cmd) {
6
- return stripColors(shell.exec(cmd, { silent: true }).stdout);
5
+ $.verbose = false;
6
+
7
+ export async function getOutput(cmd) {
8
+ const { stdout: output } = await $`${cmd}`;
9
+ return output.trim();
10
+ }
11
+
12
+ export async function getUser() {
13
+ const npmAuthor = await getAuthor();
14
+ const gitUser = await getUserInfo();
15
+ return {
16
+ name: npmAuthor.name || gitUser.name,
17
+ email: npmAuthor.email || gitUser.email,
18
+ };
7
19
  }
package/lib/npm.js CHANGED
@@ -1,13 +1,11 @@
1
- import rc from 'rc';
1
+ /* eslint-disable import/prefer-default-export */
2
+ import { $ } from 'zx';
2
3
 
3
- export function getConfig(key) {
4
- const conf = rc('npm');
5
- return conf?.[key] || '';
6
- }
4
+ $.verbose = false;
7
5
 
8
- export function getAuthor() {
9
- const name = getConfig('init.author.name');
10
- const email = getConfig('init.author.email');
6
+ export async function getAuthor() {
7
+ const { stdout: name } = await $`npm config get init.author.name`;
8
+ const { stdout: email } = await $`npm config get init.author.email`;
11
9
 
12
- return { name, email };
10
+ return { name: name.trim(), email: email.trim() };
13
11
  }
package/lib/server.js CHANGED
@@ -1,38 +1,44 @@
1
+ import { $, which } from 'zx';
1
2
  import semver from 'semver';
2
- import { getOutput } from './index.js';
3
3
 
4
- export function checkServerInstalled() {
5
- const output = getOutput('which blocklet');
6
- return !!output;
4
+ $.verbose = false;
5
+
6
+ export async function checkServerInstalled() {
7
+ try {
8
+ await which('blocklet');
9
+ return true;
10
+ } catch (e) {
11
+ return false;
12
+ }
7
13
  }
8
14
 
9
- export function getServerVersion() {
10
- const output = getOutput('blocklet --version');
15
+ export async function getServerVersion() {
16
+ const { stdout: output } = await $`blocklet --version`;
11
17
  return output.trim();
12
18
  }
13
19
 
14
- export function getServerStatus() {
15
- const output = getOutput('blocklet server status');
16
- const [matchStr] = output.match(/Blocklet Server status:[\s\S]*?\n/g) || [];
17
- if (!matchStr) {
20
+ export async function getServerStatus() {
21
+ try {
22
+ const { stdout: output } = await $`blocklet server status`;
23
+ const [matchStr] = output.match(/Blocklet Server status:[\s\S]*?\n/g) || [];
24
+ const status = matchStr.replace(/Blocklet Server status:\s*(\S+)\s*\S*\n*[\s\S]*/gm, '$1');
25
+ return status.toLowerCase();
26
+ } catch (e) {
18
27
  return 'stop';
19
28
  }
20
- const status = matchStr.replace(/Blocklet Server status:\s*(\S+)\s*\S*\n*[\s\S]*/gm, '$1');
21
- return status.toLowerCase();
22
29
  }
23
- export function checkServerRunning() {
24
- const status = getServerStatus();
30
+ export async function checkServerRunning() {
31
+ const status = await getServerStatus();
25
32
  return status === 'running';
26
33
  }
27
34
 
28
- export function checkSatisfiedVersion() {
29
- const output = getOutput('blocklet --version');
30
- const version = output.trim();
31
- return semver.satisfies(version, '>= 1.6.1');
35
+ export async function checkSatisfiedVersion() {
36
+ const version = await getServerVersion();
37
+ return semver.satisfies(version, '>= 1.7.0');
32
38
  }
33
39
 
34
- export function getServerDirectory() {
35
- const output = getOutput('blocklet server status');
40
+ export async function getServerDirectory() {
41
+ const { stdout: output } = await $`blocklet server status`;
36
42
  const [matchStr] = output.match(/Blocklet Server Data Directory:[\s\S]*?\n/gm) || [];
37
43
  if (!matchStr) return null;
38
44