@blocklet/cli 1.16.33 → 1.16.34-beta-20241120-080738-bbbe036c

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 (87) hide show
  1. package/README.md +32 -25
  2. package/bin/blocklet.js +292 -1
  3. package/config.example.yml +33 -0
  4. package/lib/arcblock.js +53 -0
  5. package/lib/commands/blocklet/add.js +124 -0
  6. package/lib/commands/blocklet/assets/git-ignore +28 -0
  7. package/lib/commands/blocklet/assets/index.html +9 -0
  8. package/lib/commands/blocklet/assets/index.js +14 -0
  9. package/lib/commands/blocklet/assets/logo.png +0 -0
  10. package/lib/commands/blocklet/bundle/bundle.js +184 -0
  11. package/lib/commands/blocklet/bundle/bundlers/blocklet.js +138 -0
  12. package/lib/commands/blocklet/bundle/bundlers/changelog.js +100 -0
  13. package/lib/commands/blocklet/bundle/bundlers/logo.js +56 -0
  14. package/lib/commands/blocklet/bundle/bundlers/markdown.js +241 -0
  15. package/lib/commands/blocklet/bundle/bundlers/preference.js +50 -0
  16. package/lib/commands/blocklet/bundle/bundlers/readme.js +43 -0
  17. package/lib/commands/blocklet/bundle/bundlers/screenshots.js +94 -0
  18. package/lib/commands/blocklet/bundle/bundlers/simple.js +70 -0
  19. package/lib/commands/blocklet/bundle/compact/bundle-compact-file.js +48 -0
  20. package/lib/commands/blocklet/bundle/compact/bundle-merge-extra.js +66 -0
  21. package/lib/commands/blocklet/bundle/compact/default-external.js +5 -0
  22. package/lib/commands/blocklet/bundle/compact/index.js +88 -0
  23. package/lib/commands/blocklet/bundle/index.js +139 -0
  24. package/lib/commands/blocklet/bundle/pack.js +8 -0
  25. package/lib/commands/blocklet/bundle/parse-external-dependencies.js +97 -0
  26. package/lib/commands/blocklet/bundle/simple/index.js +62 -0
  27. package/lib/commands/blocklet/bundle/zip/archive.js +35 -0
  28. package/lib/commands/blocklet/bundle/zip/dependencies.js +333 -0
  29. package/lib/commands/blocklet/bundle/zip/index.js +165 -0
  30. package/lib/commands/blocklet/bundle/zip/main.js +124 -0
  31. package/lib/commands/blocklet/bundle/zip/node.js +59 -0
  32. package/lib/commands/blocklet/bundle/zip/resolve.js +93 -0
  33. package/lib/commands/blocklet/cleanup.js +52 -0
  34. package/lib/commands/blocklet/config.js +108 -0
  35. package/lib/commands/blocklet/connect.js +87 -0
  36. package/lib/commands/blocklet/create.js +38 -0
  37. package/lib/commands/blocklet/deploy.js +435 -0
  38. package/lib/commands/blocklet/dev.js +1000 -0
  39. package/lib/commands/blocklet/document.js +39 -0
  40. package/lib/commands/blocklet/exec.js +106 -0
  41. package/lib/commands/blocklet/init.js +300 -0
  42. package/lib/commands/blocklet/meta.js +22 -0
  43. package/lib/commands/blocklet/remove.js +35 -0
  44. package/lib/commands/blocklet/test.js +201 -0
  45. package/lib/commands/blocklet/upload.js +105 -0
  46. package/lib/commands/blocklet/version.js +81 -0
  47. package/lib/commands/server/cleanup.js +32 -0
  48. package/lib/commands/server/command.js +131 -0
  49. package/lib/commands/server/info.js +92 -0
  50. package/lib/commands/server/init.js +433 -0
  51. package/lib/commands/server/logs.js +99 -0
  52. package/lib/commands/server/rescue.js +71 -0
  53. package/lib/commands/server/start.js +821 -0
  54. package/lib/commands/server/status.js +107 -0
  55. package/lib/commands/server/stop.js +163 -0
  56. package/lib/commands/server/upgrade.js +123 -0
  57. package/lib/constant.js +21 -2
  58. package/lib/debug.js +20 -0
  59. package/lib/manager/config.js +122 -0
  60. package/lib/manager/deploy.js +75 -0
  61. package/lib/manager/index.js +23 -0
  62. package/lib/manager/process.js +47 -0
  63. package/lib/node.js +214 -0
  64. package/lib/port.js +19 -0
  65. package/lib/postinstall.js +3 -0
  66. package/lib/process/daemon.js +196 -0
  67. package/lib/process/service.js +86 -0
  68. package/lib/ui.js +137 -0
  69. package/lib/util/blocklet/config.js +78 -0
  70. package/lib/util/blocklet/env.js +172 -0
  71. package/lib/util/blocklet/meta.js +36 -0
  72. package/lib/util/blocklet/payment.js +88 -0
  73. package/lib/util/blocklet/sign.js +21 -0
  74. package/lib/util/blocklet/tar.js +119 -0
  75. package/lib/util/convert-to-nosources-sourcemap.js +37 -0
  76. package/lib/util/docker-status-log.js +17 -0
  77. package/lib/util/exit-when-server-stopped.js +44 -0
  78. package/lib/util/get-cli-binary-name.js +8 -0
  79. package/lib/util/get-download-bundle-step.js +36 -0
  80. package/lib/util/get-service-instance-number.js +12 -0
  81. package/lib/util/index.js +626 -0
  82. package/lib/util/print-error.js +11 -0
  83. package/lib/util/print.js +9 -0
  84. package/lib/util/what-uri.js +40 -0
  85. package/package.json +123 -27
  86. package/lib/run.d.ts +0 -2
  87. package/lib/run.js +0 -73
@@ -0,0 +1,88 @@
1
+ /* eslint-disable no-await-in-loop */
2
+ const fs = require('fs-extra');
3
+ const path = require('path');
4
+ const chalk = require('chalk');
5
+ const { BLOCKLET_BUNDLE_FOLDER, BLOCKLET_ENTRY_FILE } = require('@blocklet/constant');
6
+ const parseBlockletMeta = require('@blocklet/meta/lib/parse');
7
+
8
+ const { print, printSuccess } = require('../../../../util');
9
+ const { wrapSpinner } = require('../../../../ui');
10
+ const pack = require('../pack');
11
+ const { createBlockletBundle, createBlockletEntry, getExtraFiles } = require('../bundle');
12
+ const { logTar, getContents } = require('../../../../util/blocklet/tar');
13
+ const { bundleMergeExtra } = require('./bundle-merge-extra');
14
+ const { defaultExternals } = require('./default-external');
15
+
16
+ exports.run = async ({
17
+ meta,
18
+ blockletDir,
19
+ createRelease = false,
20
+ inMonoRepo = false,
21
+ withChangeLog = true,
22
+ sourceMap,
23
+ nosourcesSourceMap,
24
+ externalManager,
25
+ minify,
26
+ externals: inputExternals = [],
27
+ }) => {
28
+ const externals = Array.from(new Set([...defaultExternals, ...inputExternals]));
29
+
30
+ // eslint-disable-next-line no-param-reassign
31
+ meta = meta || parseBlockletMeta(blockletDir);
32
+
33
+ const buildOutputDir = path.join(blockletDir, BLOCKLET_BUNDLE_FOLDER);
34
+
35
+ if (fs.existsSync(buildOutputDir)) {
36
+ fs.removeSync(buildOutputDir);
37
+ }
38
+ fs.mkdirSync(buildOutputDir, { recursive: true });
39
+
40
+ await wrapSpinner(`Creating blocklet bundle in ${chalk.cyan(BLOCKLET_BUNDLE_FOLDER)}...`, async () => {
41
+ if (meta.main && meta.main !== BLOCKLET_ENTRY_FILE) {
42
+ fs.writeFileSync(path.join(buildOutputDir, BLOCKLET_ENTRY_FILE), createBlockletEntry(blockletDir, meta.main));
43
+ }
44
+
45
+ await createBlockletBundle({
46
+ blockletDir,
47
+ meta,
48
+ updates: meta.group === 'dapp' ? { main: BLOCKLET_ENTRY_FILE } : {},
49
+ inMonoRepo,
50
+ withChangeLog,
51
+ });
52
+ });
53
+
54
+ await wrapSpinner(`Bundle single file in ${chalk.cyan(BLOCKLET_BUNDLE_FOLDER)}...`, async () => {
55
+ fs.rmSync(path.dirname(path.join(buildOutputDir, meta.main)), { force: true, recursive: true });
56
+ const extraFiles = getExtraFiles(blockletDir, meta).filter((file) => file.endsWith('.js') || file.endsWith('.ts'));
57
+
58
+ // clean history builded files
59
+ for (const file of extraFiles) {
60
+ fs.rmSync(path.dirname(path.join(buildOutputDir, file)), { force: true, recursive: true });
61
+ }
62
+
63
+ await bundleMergeExtra({
64
+ extraFiles,
65
+ srcPath: blockletDir,
66
+ distDir: buildOutputDir,
67
+ sourceMap,
68
+ nosourcesSourceMap,
69
+ enterFile: meta.main || BLOCKLET_ENTRY_FILE,
70
+ externalManager,
71
+ externals,
72
+ minify,
73
+ });
74
+ });
75
+
76
+ // print total stats
77
+ printSuccess(`Blocklet ${chalk.cyan(`${meta.name}@${meta.version}`)} was successfully bundled!`);
78
+
79
+ if (createRelease) {
80
+ print('');
81
+ // eslint-disable-next-line no-shadow
82
+ const { tarball, meta } = await pack(blockletDir);
83
+ const pkgContents = await getContents(meta, tarball);
84
+ logTar(pkgContents);
85
+ }
86
+
87
+ process.exit(0);
88
+ };
@@ -0,0 +1,139 @@
1
+ const path = require('path');
2
+ const chalk = require('chalk');
3
+ const formatError = require('@abtnode/util/lib/format-error');
4
+ const parseBlockletMeta = require('@blocklet/meta/lib/parse');
5
+ const hasReservedKey = require('@blocklet/meta/lib/has-reserved-key');
6
+ const zipBundle = require('./zip');
7
+ const simpleBundle = require('./simple');
8
+ const compactBundle = require('./compact');
9
+ const {
10
+ printError,
11
+ printInfo,
12
+ printSuccess,
13
+ wrapDefaultStoreUrl,
14
+ checkEntryFileForStaticBlocklet,
15
+ } = require('../../../util');
16
+ const { checkCircularDependencies } = require('../../../util/blocklet/meta');
17
+
18
+ const getMode = (meta, { useZip, useSimple, useCompact } = {}) => {
19
+ if (useCompact) {
20
+ return 'compact';
21
+ }
22
+
23
+ if (useSimple) {
24
+ return 'simple';
25
+ }
26
+
27
+ let mode = 'simple';
28
+ if (meta.group === 'static' || useZip) {
29
+ mode = 'zip';
30
+ }
31
+
32
+ if (meta.group === 'gateway') {
33
+ mode = 'simple';
34
+ }
35
+
36
+ if (!meta.group) {
37
+ mode = 'zip';
38
+ }
39
+
40
+ return mode;
41
+ };
42
+
43
+ exports.getMode = getMode;
44
+
45
+ exports.run = async ({
46
+ zip: useZip = false,
47
+ simple: useSimple = false,
48
+ changelog: withChangeLog = true,
49
+ createRelease = false,
50
+ monorepo: inMonoRepo = false,
51
+ compact: useCompact = false,
52
+ sourceMap = false,
53
+ nosourcesSourceMap = false,
54
+ external = [],
55
+ externalManager = 'npm',
56
+ storeUrl,
57
+ minify = true,
58
+ }) => {
59
+ const defaultStoreUrl = storeUrl || process.env.COMPONENT_STORE_URL;
60
+ const blockletDir = process.cwd();
61
+ let meta = {};
62
+
63
+ try {
64
+ meta = parseBlockletMeta(blockletDir, {
65
+ ensureFiles: true,
66
+ schemaOptions: {
67
+ stripUnknown: false,
68
+ },
69
+ defaultStoreUrl: defaultStoreUrl ? wrapDefaultStoreUrl(defaultStoreUrl) : null,
70
+ });
71
+ printSuccess(`Parsing blocklet meta from: ${chalk.cyan(blockletDir)}`);
72
+ } catch (err) {
73
+ if (process.env.DEBUG) {
74
+ console.error(err);
75
+ }
76
+ printError(`Blocklet bundle failed from ${chalk.cyan(blockletDir)}`);
77
+ printError(`Abort due to error: ${chalk.red(formatError(err))}`);
78
+ printInfo(`Please check your ${chalk.cyan(path.join(blockletDir, 'blocklet.yml'))}`);
79
+ printInfo(`See ${chalk.cyan('https://developer.blocklet.io/docs/reference/blocklet-spec')}`);
80
+ process.exit(1);
81
+ }
82
+
83
+ if (hasReservedKey(meta.environments)) {
84
+ printError('Blocklet key of environments can not start with `ABT_NODE_` or `BLOCKLET_`');
85
+ process.exit(1);
86
+ }
87
+
88
+ if (meta.group === 'static') {
89
+ try {
90
+ checkEntryFileForStaticBlocklet(meta, blockletDir);
91
+ } catch (error) {
92
+ printError(error.message);
93
+ process.exit(1);
94
+ }
95
+ }
96
+
97
+ // Try parse components to avoid circular dependencies
98
+ await checkCircularDependencies(meta);
99
+
100
+ const mode = getMode(meta, { useZip, useSimple, useCompact });
101
+
102
+ printInfo(`Bundling in ${chalk.cyan(mode)} mode for blocklet ${chalk.cyan(meta.name)}...`);
103
+
104
+ let externals = [];
105
+ if (external) {
106
+ externals = Array.isArray(external) ? external : [external];
107
+ externals = externals.reduce((acc, cur) => {
108
+ if (cur.indexOf(',') > -1) {
109
+ acc.push(...cur.split(','));
110
+ } else {
111
+ acc.push(cur);
112
+ }
113
+ return acc;
114
+ }, []);
115
+ }
116
+
117
+ if (mode === 'compact') {
118
+ await compactBundle.run({
119
+ meta,
120
+ blockletDir,
121
+ createRelease,
122
+ inMonoRepo,
123
+ withChangeLog,
124
+ sourceMap,
125
+ nosourcesSourceMap,
126
+ externals,
127
+ externalManager,
128
+ minify,
129
+ });
130
+ } else if (mode === 'zip') {
131
+ await zipBundle.run({ meta, blockletDir, createRelease, inMonoRepo, withChangeLog, externals, externalManager });
132
+ } else if (mode === 'simple') {
133
+ await simpleBundle.run({ meta, blockletDir, createRelease, inMonoRepo, withChangeLog, externals, externalManager });
134
+ } else {
135
+ // should not reach here
136
+ printError(`Unsupported bundle mode ${mode}`);
137
+ process.exit(1);
138
+ }
139
+ };
@@ -0,0 +1,8 @@
1
+ const path = require('path');
2
+ const { createRelease } = require('@abtnode/util/lib/create-blocklet-release');
3
+
4
+ const { printError, printInfo } = require('../../../util');
5
+
6
+ const pack = (dir) => createRelease(path.join(dir, '.blocklet'), { printError, printInfo });
7
+
8
+ module.exports = pack;
@@ -0,0 +1,97 @@
1
+ const fs = require('fs-extra');
2
+ const path = require('path');
3
+
4
+ const repoPrefixes = [
5
+ 'http:',
6
+ 'https:',
7
+ 'github:',
8
+ 'git+',
9
+ 'file:',
10
+ 'ssh:',
11
+ 'git@',
12
+ 'bitbucket:',
13
+ 'gitlab:',
14
+ 'hg+',
15
+ 'svn+',
16
+ 'bzr+',
17
+ ];
18
+
19
+ // eslint-disable-next-line consistent-return
20
+ function parseExternalDependencies({ externals = [], distDir, externalManager, persist = true }) {
21
+ if (['npm', 'pnpm', 'yarn'].includes(externalManager) === false) {
22
+ throw new Error(`Unsupported external manager: ${externalManager}`);
23
+ }
24
+
25
+ if (externals.length === 0) {
26
+ return { dependencies: {}, externalManager, blockletExternalDependencies: [] };
27
+ }
28
+
29
+ const packageJsonPath = 'package.json';
30
+ const dependencies = {};
31
+
32
+ const manifest = fs.readJSONSync(packageJsonPath);
33
+ if (externals.find((x) => x === '*')) {
34
+ return {
35
+ dependencies: manifest.dependencies,
36
+ externalManager,
37
+ blockletExternalDependencies: Object.keys(manifest.dependencies),
38
+ };
39
+ }
40
+
41
+ externals.forEach((external) => {
42
+ const version =
43
+ manifest.dependencies?.[external] ||
44
+ manifest.devDependencies?.[external] ||
45
+ manifest.peerDependencies?.[external] ||
46
+ manifest.optionalDependencies?.[external];
47
+ if (version && repoPrefixes.some((prefix) => version.startsWith(prefix))) {
48
+ dependencies[external] = version;
49
+ return;
50
+ }
51
+ for (let i = 0; i < 9; i++) {
52
+ const upDir = Array(i).fill('..');
53
+ const tmp = path.resolve(...upDir, 'node_modules', external, 'package.json');
54
+ if (fs.existsSync(tmp)) {
55
+ const packageJson = fs.readJSONSync(tmp);
56
+ dependencies[external] = packageJson.version;
57
+ break;
58
+ }
59
+ }
60
+ });
61
+
62
+ if (Object.keys(dependencies).length === 0) {
63
+ return { dependencies: {}, externalManager, blockletExternalDependencies: [] };
64
+ }
65
+
66
+ if (persist === false) {
67
+ return { dependencies, externalManager, blockletExternalDependencies: Object.keys(dependencies) };
68
+ }
69
+
70
+ let packageJson = {};
71
+ if (fs.existsSync(packageJsonPath)) {
72
+ packageJson = fs.readJSONSync(packageJsonPath);
73
+ delete packageJson.devDependencies;
74
+ delete packageJson.peerDependencies;
75
+ delete packageJson.optionalDependencies;
76
+ delete packageJson.bundledDependencies;
77
+ delete packageJson.scripts;
78
+ }
79
+
80
+ fs.writeFileSync(
81
+ path.resolve(distDir, 'package.json'),
82
+ JSON.stringify(
83
+ {
84
+ ...packageJson,
85
+ dependencies,
86
+ externalManager,
87
+ blockletExternalDependencies: Object.keys(dependencies),
88
+ },
89
+ null,
90
+ 2
91
+ )
92
+ );
93
+ }
94
+
95
+ module.exports = {
96
+ parseExternalDependencies,
97
+ };
@@ -0,0 +1,62 @@
1
+ /* eslint-disable no-await-in-loop */
2
+ const fs = require('fs-extra');
3
+ const path = require('path');
4
+ const chalk = require('chalk');
5
+ const { BLOCKLET_BUNDLE_FOLDER, BLOCKLET_ENTRY_FILE } = require('@blocklet/constant');
6
+ const parseBlockletMeta = require('@blocklet/meta/lib/parse');
7
+
8
+ const { print, printSuccess } = require('../../../../util');
9
+ const { wrapSpinner } = require('../../../../ui');
10
+ const pack = require('../pack');
11
+ const { createBlockletBundle, createBlockletEntry } = require('../bundle');
12
+ const { logTar, getContents } = require('../../../../util/blocklet/tar');
13
+
14
+ exports.run = async ({
15
+ meta,
16
+ blockletDir,
17
+ createRelease = false,
18
+ inMonoRepo = false,
19
+ withChangeLog = true,
20
+ externals = [],
21
+ externalManager = 'npm',
22
+ }) => {
23
+ // eslint-disable-next-line no-param-reassign
24
+ meta = meta || parseBlockletMeta(blockletDir);
25
+
26
+ const distDir = path.join(blockletDir, BLOCKLET_BUNDLE_FOLDER);
27
+
28
+ if (fs.existsSync(distDir)) {
29
+ fs.removeSync(distDir);
30
+ } else {
31
+ fs.mkdirSync(distDir, { recursive: true });
32
+ }
33
+
34
+ await wrapSpinner(`Creating blocklet bundle in ${chalk.cyan(BLOCKLET_BUNDLE_FOLDER)}...`, async () => {
35
+ if (meta.main && meta.main !== BLOCKLET_ENTRY_FILE) {
36
+ fs.writeFileSync(path.join(distDir, BLOCKLET_ENTRY_FILE), createBlockletEntry(blockletDir, meta.main));
37
+ }
38
+
39
+ await createBlockletBundle({
40
+ blockletDir,
41
+ meta,
42
+ updates: meta.group === 'dapp' ? { main: BLOCKLET_ENTRY_FILE } : {},
43
+ inMonoRepo,
44
+ withChangeLog,
45
+ externals,
46
+ externalManager,
47
+ });
48
+ });
49
+
50
+ // print total stats
51
+ printSuccess(`Blocklet ${chalk.cyan(`${meta.name}@${meta.version}`)} was successfully bundled!`);
52
+
53
+ if (createRelease) {
54
+ print('');
55
+ // eslint-disable-next-line no-shadow
56
+ const { tarball, meta } = await pack(blockletDir);
57
+ const pkgContents = await getContents(meta, tarball);
58
+ logTar(pkgContents);
59
+ }
60
+
61
+ process.exit(0);
62
+ };
@@ -0,0 +1,35 @@
1
+ /* eslint-disable func-names */
2
+ const { createWriteStream } = require('fs');
3
+
4
+ const archiver = require('archiver');
5
+ const endOfStream = require('end-of-stream');
6
+ const { promisify } = require('util');
7
+
8
+ const pEndOfStream = promisify(endOfStream);
9
+
10
+ // Start zipping files
11
+ const startZip = function (destPath) {
12
+ const output = createWriteStream(destPath);
13
+ const archive = archiver('zip', { level: 9 });
14
+ archive.pipe(output);
15
+ return { archive, output };
16
+ };
17
+
18
+ // Add new file to zip
19
+ const addZipFile = function (archive, file, name, stat) {
20
+ // Ensure sha256 stability regardless of mtime
21
+ archive.file(file, { name, mode: stat.mode, date: new Date(0), stats: stat });
22
+ };
23
+
24
+ // Add new file content to zip
25
+ const addZipContent = function (archive, content, name) {
26
+ archive.append(content, { name, date: new Date(0) });
27
+ };
28
+
29
+ // End zipping files
30
+ const endZip = async function (archive, output) {
31
+ archive.finalize();
32
+ await pEndOfStream(output);
33
+ };
34
+
35
+ module.exports = { startZip, addZipFile, addZipContent, endZip };