@liuli-util/cli 3.20.2 → 3.22.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +69 -25
  3. package/README.zh-CN.md +7 -7
  4. package/package.json +40 -36
  5. package/src/commands/deploy/DeployService.ts +20 -2
  6. package/src/commands/deploy/__tests__/DeployService.test.ts +23 -4
  7. package/src/commands/esbuild/ESBuildProgram.ts +5 -9
  8. package/src/commands/esbuild/__tests__/ESBuildProgram.test.ts +2 -2
  9. package/src/commands/esbuild/util/autoExternal.ts +22 -0
  10. package/src/commands/esbuild/util/env.ts +67 -0
  11. package/src/commands/esbuild/util/index.ts +10 -0
  12. package/src/commands/esbuild/util/log.ts +20 -0
  13. package/src/commands/esbuild/util/metafile.ts +17 -0
  14. package/src/commands/esbuild/util/nativeNodeModules.ts +43 -0
  15. package/src/commands/esbuild/util/nodeExternal.ts +16 -0
  16. package/src/commands/esbuild/util/raw.ts +27 -0
  17. package/src/commands/esbuild/util/resolve.ts +22 -0
  18. package/src/commands/esbuild/util/sideEffect.ts +29 -0
  19. package/src/commands/esbuild/util/userJS.ts +38 -0
  20. package/templates/cli/package.json +35 -33
  21. package/templates/lib/package.json +24 -19
  22. package/dist/PathUtil.d.ts +0 -7
  23. package/dist/PathUtil.d.ts.map +0 -1
  24. package/dist/bin.d.ts +0 -2
  25. package/dist/bin.d.ts.map +0 -1
  26. package/dist/bin.js +0 -171
  27. package/dist/bin.js.map +0 -7
  28. package/dist/commands/deploy/DeployService.d.ts +0 -70
  29. package/dist/commands/deploy/DeployService.d.ts.map +0 -1
  30. package/dist/commands/deploy/deploy.d.ts +0 -3
  31. package/dist/commands/deploy/deploy.d.ts.map +0 -1
  32. package/dist/commands/deploy/index.d.ts +0 -3
  33. package/dist/commands/deploy/index.d.ts.map +0 -1
  34. package/dist/commands/deploy/util/PromiseUtil.d.ts +0 -14
  35. package/dist/commands/deploy/util/PromiseUtil.d.ts.map +0 -1
  36. package/dist/commands/deploy/util/createArchive.d.ts +0 -10
  37. package/dist/commands/deploy/util/createArchive.d.ts.map +0 -1
  38. package/dist/commands/deploy/util/execPromise.d.ts +0 -4
  39. package/dist/commands/deploy/util/execPromise.d.ts.map +0 -1
  40. package/dist/commands/deploy/util/validate.d.ts +0 -8
  41. package/dist/commands/deploy/util/validate.d.ts.map +0 -1
  42. package/dist/commands/deploy/util/wait.d.ts +0 -9
  43. package/dist/commands/deploy/util/wait.d.ts.map +0 -1
  44. package/dist/commands/esbuild/ESBuildProgram.d.ts +0 -83
  45. package/dist/commands/esbuild/ESBuildProgram.d.ts.map +0 -1
  46. package/dist/commands/esbuild/index.d.ts +0 -3
  47. package/dist/commands/esbuild/index.d.ts.map +0 -1
  48. package/dist/commands/esbuild/util/debounce.d.ts +0 -15
  49. package/dist/commands/esbuild/util/debounce.d.ts.map +0 -1
  50. package/dist/commands/esbuild/util/esbuildPlugins.d.ts +0 -32
  51. package/dist/commands/esbuild/util/esbuildPlugins.d.ts.map +0 -1
  52. package/dist/commands/esbuild/util/getPkgGlobalName.d.ts +0 -6
  53. package/dist/commands/esbuild/util/getPkgGlobalName.d.ts.map +0 -1
  54. package/dist/commands/generate/GenerateProgram.d.ts +0 -18
  55. package/dist/commands/generate/GenerateProgram.d.ts.map +0 -1
  56. package/dist/commands/generate/index.d.ts +0 -3
  57. package/dist/commands/generate/index.d.ts.map +0 -1
  58. package/dist/commands/sync/SyncProgram.d.ts +0 -15
  59. package/dist/commands/sync/SyncProgram.d.ts.map +0 -1
  60. package/dist/commands/sync/index.d.ts +0 -3
  61. package/dist/commands/sync/index.d.ts.map +0 -1
  62. package/dist/commands/sync/when.d.ts +0 -21
  63. package/dist/commands/sync/when.d.ts.map +0 -1
  64. package/dist/index.d.ts +0 -4
  65. package/dist/index.d.ts.map +0 -1
  66. package/dist/index.esm.js +0 -8
  67. package/dist/index.esm.js.map +0 -7
  68. package/dist/index.js +0 -8
  69. package/dist/index.js.map +0 -7
  70. package/dist/utils/AsyncArray.d.ts +0 -21
  71. package/dist/utils/AsyncArray.d.ts.map +0 -1
  72. package/dist/utils/appendScript.d.ts +0 -7
  73. package/dist/utils/appendScript.d.ts.map +0 -1
  74. package/dist/utils/arrayToMap.d.ts +0 -3
  75. package/dist/utils/arrayToMap.d.ts.map +0 -1
  76. package/dist/utils/findParent.d.ts +0 -8
  77. package/dist/utils/findParent.d.ts.map +0 -1
  78. package/dist/utils/index.d.ts +0 -5
  79. package/dist/utils/index.d.ts.map +0 -1
  80. package/dist/utils/nodeCacheDir.d.ts +0 -2
  81. package/dist/utils/nodeCacheDir.d.ts.map +0 -1
  82. package/src/commands/esbuild/__tests__/.temp/package.json +0 -1
  83. package/src/commands/esbuild/__tests__/.temp/src/index.ts +0 -1
  84. package/src/commands/esbuild/util/__tests__/.temp/index.js +0 -1
  85. package/src/commands/esbuild/util/__tests__/esbuildPlugins.test.ts +0 -52
  86. package/src/commands/esbuild/util/esbuildPlugins.ts +0 -196
  87. package/src/commands/generate/__tests__/.temp/README.md +0 -1
  88. package/src/commands/generate/__tests__/.temp/package.json +0 -3
  89. package/src/commands/generate/__tests__/.temp/test-cli/CHANGELOG.md +0 -1
  90. package/src/commands/generate/__tests__/.temp/test-cli/README.md +0 -1
  91. package/src/commands/generate/__tests__/.temp/test-cli/bin.js +0 -3
  92. package/src/commands/generate/__tests__/.temp/test-cli/package.json +0 -33
  93. package/src/commands/generate/__tests__/.temp/test-cli/src/bin.ts +0 -13
  94. package/src/commands/generate/__tests__/.temp/test-cli/src/index.ts +0 -1
  95. package/src/commands/generate/__tests__/.temp/test-cli/tsconfig.json +0 -28
  96. package/src/commands/sync/__tests__/.temp/package.json +0 -16
  97. package/wallaby.conf.js +0 -3
@@ -0,0 +1,43 @@
1
+ import { Plugin } from 'esbuild'
2
+
3
+ /**
4
+ * 处理 nodejs 原生模块
5
+ * @link https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
6
+ */
7
+ export function nativeNodeModules(): Plugin {
8
+ return {
9
+ name: 'native-node-modules',
10
+ setup(build) {
11
+ // If a ".node" file is imported within a module in the "file" namespace, resolve
12
+ // it to an absolute path and put it into the "node-file" virtual namespace.
13
+ build.onResolve({ filter: /\.node$/, namespace: 'file' }, (args) => ({
14
+ path: require.resolve(args.path, { paths: [args.resolveDir] }),
15
+ namespace: 'node-file',
16
+ }))
17
+
18
+ // Files in the "node-file" virtual namespace call "require()" on the
19
+ // path from esbuild of the ".node" file in the output directory.
20
+ build.onLoad({ filter: /.*/, namespace: 'node-file' }, (args) => ({
21
+ contents: `
22
+ import path from ${JSON.stringify(args.path)}
23
+ try { module.exports = require(path) }
24
+ catch {}
25
+ `,
26
+ }))
27
+
28
+ // If a ".node" file is imported within a module in the "node-file" namespace, put
29
+ // it in the "file" namespace where esbuild's default loading behavior will handle
30
+ // it. It is already an absolute path since we resolved it to one above.
31
+ build.onResolve({ filter: /\.node$/, namespace: 'node-file' }, (args) => ({
32
+ path: args.path,
33
+ namespace: 'file',
34
+ }))
35
+
36
+ // Tell esbuild's default loading behavior to use the "file" loader for
37
+ // these ".node" files.
38
+ const opts = build.initialOptions
39
+ opts.loader = opts.loader || {}
40
+ opts.loader['.node'] = 'file'
41
+ },
42
+ }
43
+ }
@@ -0,0 +1,16 @@
1
+ import { Plugin } from 'esbuild'
2
+
3
+ /**
4
+ * 排除和替换 node 内置模块
5
+ */
6
+ export function nodeExternal(): Plugin {
7
+ return {
8
+ name: 'nodeExternals',
9
+ setup(build) {
10
+ build.onResolve({ filter: /(^node:)/ }, (args) => ({
11
+ path: args.path.slice(5),
12
+ external: true,
13
+ }))
14
+ },
15
+ }
16
+ }
@@ -0,0 +1,27 @@
1
+ import { Plugin } from 'esbuild'
2
+ import { readFile } from 'fs-extra'
3
+ import * as path from 'path'
4
+
5
+ /**
6
+ * 通过 ?raw 将资源作为字符串打包进来
7
+ * @returns
8
+ */
9
+ export function raw(): Plugin {
10
+ return {
11
+ name: 'raw',
12
+ setup(build) {
13
+ build.onResolve({ filter: /\?raw$/ }, (args) => {
14
+ return {
15
+ path: path.isAbsolute(args.path) ? args.path : path.join(args.resolveDir, args.path),
16
+ namespace: 'raw-loader',
17
+ }
18
+ })
19
+ build.onLoad({ filter: /\?raw$/, namespace: 'raw-loader' }, async (args) => {
20
+ return {
21
+ contents: await readFile(args.path.replace(/\?raw$/, '')),
22
+ loader: 'text',
23
+ }
24
+ })
25
+ },
26
+ }
27
+ }
@@ -0,0 +1,22 @@
1
+ import { build, Plugin } from 'esbuild'
2
+ import path from 'path'
3
+
4
+ /**
5
+ * 将指定的 import 重写为另一个
6
+ * @param entries
7
+ * @returns
8
+ */
9
+ export function resolve(entries: [from: string, to: string][]): Plugin {
10
+ return {
11
+ name: 'resolve',
12
+ setup(build) {
13
+ build.onResolve({ filter: /.*/ }, async (args) => {
14
+ const findEntries = entries.find((item) => item[0] === args.path)
15
+ if (!findEntries) {
16
+ return
17
+ }
18
+ return await build.resolve(findEntries[1])
19
+ })
20
+ },
21
+ }
22
+ }
@@ -0,0 +1,29 @@
1
+ import { Plugin } from 'esbuild'
2
+
3
+ /**
4
+ * 设置指定模块为没有副作用的包,由于 webpack/esbuild 的配置不兼容,所以先使用插件来完成这件事
5
+ * @param packages
6
+ * @returns
7
+ */
8
+ export function sideEffects(packages: string[]): Plugin {
9
+ return {
10
+ name: 'sideEffects',
11
+ setup(build) {
12
+ build.onResolve({ filter: /.*/ }, async (args) => {
13
+ if (
14
+ args.pluginData || // Ignore this if we called ourselves
15
+ !packages.includes(args.path)
16
+ ) {
17
+ return
18
+ }
19
+
20
+ const { path, ...rest } = args
21
+ rest.pluginData = true // Avoid infinite recursion
22
+ const result = await build.resolve(path, rest)
23
+
24
+ result.sideEffects = false
25
+ return result
26
+ })
27
+ },
28
+ }
29
+ }
@@ -0,0 +1,38 @@
1
+ import { Plugin } from 'esbuild'
2
+ import { readJson } from 'fs-extra'
3
+ import path from 'path'
4
+
5
+ function generateBanner(meta: object) {
6
+ return (
7
+ [
8
+ '// ==UserScript==',
9
+ ...Object.entries(meta)
10
+ .map(([key, value]) => {
11
+ if (Array.isArray(value)) {
12
+ return value.map((item) => `// @${key} ${item}`)
13
+ }
14
+ return `// @${key} ${value}`
15
+ })
16
+ .flat(),
17
+ '// ==/UserScript==',
18
+ ].join('\n') + '\n'
19
+ )
20
+ }
21
+
22
+ export function userJS(): Plugin {
23
+ return {
24
+ name: 'userJS',
25
+ async setup(build) {
26
+ const json = (await readJson(path.resolve(build.initialOptions.absWorkingDir!, 'package.json'))) as {
27
+ userjs: object
28
+ }
29
+ if (!json.userjs) {
30
+ throw new Error('userjs is not supported')
31
+ }
32
+ if (!build.initialOptions.banner) {
33
+ build.initialOptions.banner = {}
34
+ }
35
+ build.initialOptions.banner!['js'] = generateBanner(json.userjs)
36
+ },
37
+ }
38
+ }
@@ -1,33 +1,35 @@
1
- {
2
- "name": "@liuli-util/cli-template",
3
- "version": "0.1.0",
4
- "main": "dist/index.js",
5
- "module": "dist/index.esm.js",
6
- "types": "dist/index.d.ts",
7
- "license": "MIT",
8
- "scripts": {
9
- "build": "liuli-cli build cli",
10
- "dev": "liuli-cli build cli -w",
11
- "start": "esno src/bin.ts"
12
- },
13
- "bin": {
14
- "cli-name": "./bin.js"
15
- },
16
- "dependencies": {
17
- "commander": "^8.2.0",
18
- "enquirer": "^2.3.6",
19
- "fs-extra": "^10.0.0"
20
- },
21
- "devDependencies": {
22
- "@liuli-util/cli": "^3.18.0",
23
- "@types/fs-extra": "^9.0.13",
24
- "@types/jest": "^27.0.2",
25
- "@types/node": "^16.9.6",
26
- "esno": "^0.9.1",
27
- "jest": "^27.4.7",
28
- "rimraf": "^3.0.2",
29
- "ts-jest": "^27.1.3",
30
- "type-fest": "^2.3.4",
31
- "typescript": "^4.5.5"
32
- }
33
- }
1
+ {
2
+ "name": "@liuli-util/cli-template",
3
+ "version": "0.2.0",
4
+ "main": "./dist/index.js",
5
+ "module": "./dist/index.esm.js",
6
+ "types": "./dist/index.d.ts",
7
+ "license": "MIT",
8
+ "scripts": {
9
+ "build": "liuli-cli build cli",
10
+ "dev": "liuli-cli build cli -w",
11
+ "start": "esno src/bin.ts"
12
+ },
13
+ "bin": {
14
+ "cli-name": "./bin.js"
15
+ },
16
+ "dependencies": {
17
+ "commander": "^9.4.0",
18
+ "enquirer": "^2.3.6",
19
+ "fs-extra": "^10.1.0"
20
+ },
21
+ "devDependencies": {
22
+ "@liuli-util/cli": "workspace:^3.21.0",
23
+ "@types/fs-extra": "^9.0.13",
24
+ "@types/node": "^18.6.4",
25
+ "esno": "^0.16.3",
26
+ "rimraf": "^3.0.2",
27
+ "type-fest": "^2.18.0",
28
+ "typescript": "^4.7.4"
29
+ },
30
+ "exports": {
31
+ "import": "./dist/index.esm.js",
32
+ "require": "./dist/index.js",
33
+ "types": "./dist/index.d.ts"
34
+ }
35
+ }
@@ -1,19 +1,24 @@
1
- {
2
- "name": "@liuli-util/lib-template",
3
- "private": true,
4
- "version": "0.1.0",
5
- "main": "dist/index.js",
6
- "module": "dist/index.esm.js",
7
- "types": "dist/index.d.ts",
8
- "license": "MIT",
9
- "scripts": {
10
- "build": "liuli-cli build lib",
11
- "dev": "liuli-cli build lib -w"
12
- },
13
- "devDependencies": {
14
- "@liuli-util/cli": "^3.18.0",
15
- "rimraf": "^3.0.2",
16
- "type-fest": "^2.3.4",
17
- "typescript": "^4.5.5"
18
- }
19
- }
1
+ {
2
+ "name": "@liuli-util/lib-template",
3
+ "private": true,
4
+ "version": "0.2.0",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.esm.js",
7
+ "types": "./dist/index.d.ts",
8
+ "license": "MIT",
9
+ "scripts": {
10
+ "build": "liuli-cli build lib",
11
+ "dev": "liuli-cli build lib -w"
12
+ },
13
+ "devDependencies": {
14
+ "@liuli-util/cli": "workspace:^3.21.0",
15
+ "rimraf": "^3.0.2",
16
+ "type-fest": "^2.18.0",
17
+ "typescript": "^4.7.4"
18
+ },
19
+ "exports": {
20
+ "import": "./dist/index.esm.js",
21
+ "require": "./dist/index.js",
22
+ "types": "./dist/index.d.ts"
23
+ }
24
+ }
@@ -1,7 +0,0 @@
1
- export declare class PathUtil {
2
- /**
3
- * 从这个项目的根目录读取,开发环境是 src/../,运行时则是 dist/../
4
- */
5
- static readonly RootPath: string;
6
- }
7
- //# sourceMappingURL=PathUtil.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"PathUtil.d.ts","sourceRoot":"","sources":["../src/PathUtil.ts"],"names":[],"mappings":"AAEA,qBAAa,QAAQ;IACnB;;OAEG;IACH,MAAM,CAAC,QAAQ,CAAC,QAAQ,SAAgC;CACzD"}
package/dist/bin.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=bin.d.ts.map
package/dist/bin.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"bin.d.ts","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":""}