@we-debug/cli 0.0.0-stage → 0.51.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.
- package/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/README.md +116 -2
- package/bin/we-debug.js +2 -0
- package/dist/cli.d.ts +12 -0
- package/dist/cli.js +134 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.js +6 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +250 -0
- package/dist/types.d.ts +14 -0
- package/dist/types.js +2 -0
- package/package.json +34 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# [0.51.0](https://github.com/dlhandsome/we-debug/compare/v0.50.0...v0.51.0) (2026-12-01)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* **cli:** reinject newly discovered UI plugins ([740f9f3](https://github.com/dlhandsome/we-debug/commit/740f9f38d2fdd090d6d70726b139906972375d75))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
* **cli:** 新增不依赖 gulp 的 @we-debug/cli 注入工具,并补充自定义 UI 插件设计与计划 ([01a85fb](https://github.com/dlhandsome/we-debug/commit/01a85fb1d6b96d9d607272f4ec0b1ed9b13d1141))
|
|
17
|
+
* **cli:** 输出 UI 插件宿主、清单与插件文件 ([667496c](https://github.com/dlhandsome/we-debug/commit/667496c079ea962da5fd79b499c11c32fb799d50))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Sail
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,117 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @we-debug/cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
不依赖 gulp 的 we-debug 全局组件注入工具,支持 Node API 和命令行两种用法。
|
|
4
|
+
|
|
5
|
+
注入内容与 [`@we-debug/gulp-tool`](../gulp-tool) 完全一致:
|
|
6
|
+
|
|
7
|
+
- `app.json` 中声明全局组件
|
|
8
|
+
- 解析所有小程序页面(含分包),在页面 wxml 末尾追加组件 wxml 代码
|
|
9
|
+
- `app.js` 头部注入 `we-debug` 初始化文件的 `require`
|
|
10
|
+
|
|
11
|
+
配置项和插件机制也与 gulp-tool 相同,gulp-tool 的插件可以直接复用。
|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
npm install @we-debug/cli -D
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 两种工作模式
|
|
20
|
+
|
|
21
|
+
- **输出模式**(传 `outDir`):把 `baseDir` 整体输出到 `outDir` 并注入,源码不变。支持 `--watch` 增量更新。
|
|
22
|
+
- **原地模式**(不传 `outDir`):直接修改 `baseDir` 中需要注入的文件,适合在构建完成后处理产物目录。如果检测到 `app.js` 已经注入过会自动跳过,可用 `--force` 强制执行。
|
|
23
|
+
|
|
24
|
+
## 命令行
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
# 构建完成后,原地注入 dist
|
|
28
|
+
npx we-debug inject --base-dir dist --entry-file ./utils/we-debug/index
|
|
29
|
+
|
|
30
|
+
# 从 src 输出注入后的项目到 dist,并持续监听
|
|
31
|
+
npx we-debug inject -b src -o dist -w
|
|
32
|
+
|
|
33
|
+
# 使用配置文件(默认读取当前目录下的 wedebug.config.js)
|
|
34
|
+
npx we-debug inject -c wedebug.config.js
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
命令行参数会覆盖配置文件中的同名配置,完整参数见 `npx we-debug --help`。
|
|
38
|
+
|
|
39
|
+
**wedebug.config.js**
|
|
40
|
+
|
|
41
|
+
```javascript
|
|
42
|
+
module.exports = {
|
|
43
|
+
baseDir: 'dist',
|
|
44
|
+
entryFile: './utils/we-debug/index',
|
|
45
|
+
compPath: '@we-debug/miniprogram/index/index',
|
|
46
|
+
wxml: '<we-debug useCustomMask="{{true}}" />',
|
|
47
|
+
plugins: [
|
|
48
|
+
{
|
|
49
|
+
package: require('wedebug-gulp-xxx'),
|
|
50
|
+
options: {
|
|
51
|
+
// 插件配置项,由插件自行定义
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
]
|
|
55
|
+
};
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
配置文件也可以导出一个返回配置对象的函数。
|
|
59
|
+
|
|
60
|
+
## Node API
|
|
61
|
+
|
|
62
|
+
```javascript
|
|
63
|
+
const { inject, injectSync, watch } = require('@we-debug/cli');
|
|
64
|
+
|
|
65
|
+
// 返回 Promise<IInjectResult>
|
|
66
|
+
await inject({ baseDir: 'dist' });
|
|
67
|
+
|
|
68
|
+
// 同步版本
|
|
69
|
+
const result = injectSync({ baseDir: 'src', outDir: 'dist' });
|
|
70
|
+
// => { skipped, injectedPages, injectedAppJson, injectedAppScript, writtenFiles }
|
|
71
|
+
|
|
72
|
+
// 先全量注入,再监听 src 增量输出到 dist
|
|
73
|
+
const watcher = watch({ baseDir: 'src', outDir: 'dist' });
|
|
74
|
+
watcher.close();
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
如果你已有自己的构建流程,只想处理单个文件,可以直接使用 `Injector`:
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
const { Injector, VirtualFile } = require('@we-debug/cli');
|
|
81
|
+
|
|
82
|
+
const injector = new Injector({ baseDir: 'src' });
|
|
83
|
+
injector.init();
|
|
84
|
+
|
|
85
|
+
const file = new VirtualFile({ base: injector.options.baseDir, path: absPath, contents: buffer });
|
|
86
|
+
injector.transform(file);
|
|
87
|
+
// file.contents 即为注入后的内容
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## 配置项
|
|
91
|
+
|
|
92
|
+
参数 | 说明 | 类型 | 默认值
|
|
93
|
+
-|-|-|-
|
|
94
|
+
baseDir | 小程序项目目录(含 `app.json`) | `string` | `src`
|
|
95
|
+
outDir | 输出目录,不传则原地修改 `baseDir` | `string` | -
|
|
96
|
+
wxml | 追加到页面的 wxml 片段 | `string` `(props: { useCustomMask }) => string` | `<we-debug useCustomMask="{{页面是否含 page-container}}" />`
|
|
97
|
+
filter | 跳过的页面,可以是正则、字符串、函数(返回 `false` 表示跳过)或它们组成的数组 | `reg` `string` `func` `array` | -
|
|
98
|
+
exclude | 不处理也不拷贝的文件(相对 `baseDir` 的路径),可以是正则、字符串、函数(返回 `true` 表示排除)或数组;`node_modules`、`.git` 始终排除 | `reg` `string` `func` `array` | -
|
|
99
|
+
compName | 全局组件名 | `string` | `we-debug`
|
|
100
|
+
compPath | 全局组件路径,可填绝对路径或相对 `baseDir` 的路径 | `string` | `@we-debug/miniprogram/index/index`
|
|
101
|
+
entryFile | we-debug 初始化文件,可填绝对路径或相对 `baseDir` 的路径 | `string` | `we-debug/index.js`
|
|
102
|
+
plugins | 插件配置,`package` 可以是函数或包名(从当前工作目录解析) | `array<{ package, options }>` | -
|
|
103
|
+
force | 原地模式下即使已注入也强制执行 | `boolean` | `false`
|
|
104
|
+
silent | 关闭日志 | `boolean` | `false`
|
|
105
|
+
uiPlugins | 显式指定 UI 插件包名,指定后不再自动发现 | `string[]` | -
|
|
106
|
+
autoDiscoverUIPlugins | 是否从项目 `package.json` 依赖中自动发现 UI 插件 | `boolean` | `true`
|
|
107
|
+
projectDir | `package.json` 与 `node_modules` 所在目录 | `string` | 当前工作目录
|
|
108
|
+
|
|
109
|
+
## 插件
|
|
110
|
+
|
|
111
|
+
插件接口与 gulp-tool 一致,钩子收到的 `file` 是与 vinyl 兼容的对象(`path`、`base`、`relative`、`extname`、`contents` 等)。所有 `.wxml`、`.json`、`.js` 文件都会经过对应的 `wxml`、`json`、`script` 钩子;非这三类文件在输出模式下原样拷贝。
|
|
112
|
+
|
|
113
|
+
和 gulp-tool 的区别:每次调用 `inject` 都会创建独立的插件实例,不会在多次调用之间共享已注册的钩子。
|
|
114
|
+
|
|
115
|
+
## 自定义 UI 插件
|
|
116
|
+
|
|
117
|
+
依赖中声明了 `wedebug` 字段的包会被识别为 UI 插件:其 `miniprogram` 目录会被拷贝到 `miniprogram_npm/<包名>/`,并自动生成全局宿主组件 `we-debug-host`、注册插件页面。插件开发方式见 we-debug 文档「自定义 UI 插件」。
|
package/bin/we-debug.js
ADDED
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IInjectOption } from './types';
|
|
2
|
+
interface IParsedArgs {
|
|
3
|
+
options: IInjectOption;
|
|
4
|
+
config?: string;
|
|
5
|
+
watch: boolean;
|
|
6
|
+
help: boolean;
|
|
7
|
+
version: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare function parseArgs(argv: string[]): IParsedArgs;
|
|
10
|
+
export declare function loadConfig(configFile?: string): IInjectOption;
|
|
11
|
+
export declare function run(argv: string[]): void;
|
|
12
|
+
export {};
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.run = exports.loadConfig = exports.parseArgs = void 0;
|
|
18
|
+
var fs_1 = __importDefault(require("fs"));
|
|
19
|
+
var path_1 = __importDefault(require("path"));
|
|
20
|
+
var index_1 = require("./index");
|
|
21
|
+
var constants_1 = require("./constants");
|
|
22
|
+
var HELP = "\nUsage: we-debug [inject] [options]\n\n\u5C06 we-debug \u6CE8\u5165\u5C0F\u7A0B\u5E8F\u9879\u76EE\uFF1Aapp.json \u6CE8\u518C\u5168\u5C40\u7EC4\u4EF6\u3001\u9875\u9762 wxml \u8FFD\u52A0\u7EC4\u4EF6\u3001app.js \u5F15\u5165\u5165\u53E3\u6587\u4EF6\n\nOptions:\n -b, --base-dir <dir> \u5C0F\u7A0B\u5E8F\u9879\u76EE\u76EE\u5F55\uFF08\u542B app.json\uFF09\uFF0C\u9ED8\u8BA4 src\n -o, --out-dir <dir> \u8F93\u51FA\u76EE\u5F55\uFF1B\u4E0D\u4F20\u5219\u539F\u5730\u4FEE\u6539 base-dir\n -c, --config <file> \u914D\u7F6E\u6587\u4EF6\uFF0C\u9ED8\u8BA4\u8BFB\u53D6\u5F53\u524D\u76EE\u5F55\u4E0B\u7684 ".concat(constants_1.DEFAULT_CONFIG_FILE, "\uFF08\u5982\u5B58\u5728\uFF09\n --entry-file <path> we-debug \u521D\u59CB\u5316\u6587\u4EF6\uFF0C\u9ED8\u8BA4 we-debug/index.js\n --comp-name <name> \u5168\u5C40\u7EC4\u4EF6\u540D\uFF0C\u9ED8\u8BA4 we-debug\n --comp-path <path> \u5168\u5C40\u7EC4\u4EF6\u8DEF\u5F84\uFF0C\u9ED8\u8BA4 @we-debug/miniprogram/index/index\n --wxml <code> \u8FFD\u52A0\u5230\u9875\u9762\u7684 wxml \u7247\u6BB5\n --filter <str> \u8DF3\u8FC7\u8DEF\u5F84\u5305\u542B\u8BE5\u5B57\u7B26\u4E32\u7684\u9875\u9762\uFF0C\u53EF\u91CD\u590D\n --exclude <str> \u8DF3\u8FC7\u8DEF\u5F84\u5305\u542B\u8BE5\u5B57\u7B26\u4E32\u7684\u6587\u4EF6\uFF08\u4E0D\u5904\u7406\u4E5F\u4E0D\u62F7\u8D1D\uFF09\uFF0C\u53EF\u91CD\u590D\n --project-dir <dir> package.json \u4E0E node_modules \u6240\u5728\u76EE\u5F55\uFF0C\u9ED8\u8BA4\u5F53\u524D\u76EE\u5F55\n --ui-plugin <name> \u663E\u5F0F\u6307\u5B9A UI \u63D2\u4EF6\u5305\u540D\uFF08\u6307\u5B9A\u540E\u4E0D\u518D\u81EA\u52A8\u53D1\u73B0\uFF09\uFF0C\u53EF\u91CD\u590D\n --no-ui-discover \u4E0D\u4ECE\u4F9D\u8D56\u4E2D\u81EA\u52A8\u53D1\u73B0 UI \u63D2\u4EF6\n -w, --watch \u6CE8\u5165\u540E\u6301\u7EED\u76D1\u542C base-dir \u53D8\u5316\uFF08\u9700\u8981 --out-dir\uFF09\n -f, --force \u539F\u5730\u6A21\u5F0F\u4E0B\u5373\u4F7F\u5DF2\u6CE8\u5165\u4E5F\u518D\u6B21\u6CE8\u5165\n -s, --silent \u4E0D\u8F93\u51FA\u65E5\u5FD7\n -v, --version \u663E\u793A\u7248\u672C\n -h, --help \u663E\u793A\u5E2E\u52A9\n");
|
|
23
|
+
var VALUE_FLAGS = {
|
|
24
|
+
'-b': 'baseDir',
|
|
25
|
+
'--base-dir': 'baseDir',
|
|
26
|
+
'-o': 'outDir',
|
|
27
|
+
'--out-dir': 'outDir',
|
|
28
|
+
'-c': 'config',
|
|
29
|
+
'--config': 'config',
|
|
30
|
+
'--entry-file': 'entryFile',
|
|
31
|
+
'--comp-name': 'compName',
|
|
32
|
+
'--comp-path': 'compPath',
|
|
33
|
+
'--wxml': 'wxml',
|
|
34
|
+
'--filter': 'filter',
|
|
35
|
+
'--exclude': 'exclude',
|
|
36
|
+
'--project-dir': 'projectDir',
|
|
37
|
+
'--ui-plugin': 'uiPlugins'
|
|
38
|
+
};
|
|
39
|
+
var LIST_KEYS = ['filter', 'exclude', 'uiPlugins'];
|
|
40
|
+
function parseArgs(argv) {
|
|
41
|
+
var parsed = { options: {}, watch: false, help: false, version: false };
|
|
42
|
+
var options = parsed.options;
|
|
43
|
+
var args = argv.slice();
|
|
44
|
+
if (args[0] === 'inject')
|
|
45
|
+
args.shift();
|
|
46
|
+
while (args.length) {
|
|
47
|
+
var arg = args.shift();
|
|
48
|
+
var eq = arg.indexOf('=');
|
|
49
|
+
var flag = arg.indexOf('--') === 0 && eq > -1 ? arg.slice(0, eq) : arg;
|
|
50
|
+
var inlineValue = flag !== arg ? arg.slice(eq + 1) : undefined;
|
|
51
|
+
if (VALUE_FLAGS[flag]) {
|
|
52
|
+
var value = inlineValue !== undefined ? inlineValue : args.shift();
|
|
53
|
+
if (value === undefined)
|
|
54
|
+
throw new Error("missing value for ".concat(flag));
|
|
55
|
+
var key = VALUE_FLAGS[flag];
|
|
56
|
+
if (key === 'config') {
|
|
57
|
+
parsed.config = value;
|
|
58
|
+
}
|
|
59
|
+
else if (LIST_KEYS.indexOf(key) > -1) {
|
|
60
|
+
options[key] = (options[key] || []).concat(value);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
options[key] = value;
|
|
64
|
+
}
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
switch (flag) {
|
|
68
|
+
case '-w':
|
|
69
|
+
case '--watch':
|
|
70
|
+
parsed.watch = true;
|
|
71
|
+
break;
|
|
72
|
+
case '-f':
|
|
73
|
+
case '--force':
|
|
74
|
+
options.force = true;
|
|
75
|
+
break;
|
|
76
|
+
case '--no-ui-discover':
|
|
77
|
+
options.autoDiscoverUIPlugins = false;
|
|
78
|
+
break;
|
|
79
|
+
case '-s':
|
|
80
|
+
case '--silent':
|
|
81
|
+
options.silent = true;
|
|
82
|
+
break;
|
|
83
|
+
case '-h':
|
|
84
|
+
case '--help':
|
|
85
|
+
parsed.help = true;
|
|
86
|
+
break;
|
|
87
|
+
case '-v':
|
|
88
|
+
case '--version':
|
|
89
|
+
parsed.version = true;
|
|
90
|
+
break;
|
|
91
|
+
default:
|
|
92
|
+
throw new Error("unknown option: ".concat(arg));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return parsed;
|
|
96
|
+
}
|
|
97
|
+
exports.parseArgs = parseArgs;
|
|
98
|
+
function loadConfig(configFile) {
|
|
99
|
+
var file = path_1.default.resolve(configFile || constants_1.DEFAULT_CONFIG_FILE);
|
|
100
|
+
if (!fs_1.default.existsSync(file)) {
|
|
101
|
+
if (configFile)
|
|
102
|
+
throw new Error("config file not found: ".concat(file));
|
|
103
|
+
return {};
|
|
104
|
+
}
|
|
105
|
+
var mod = require(file);
|
|
106
|
+
var config = mod && mod.__esModule ? mod.default : mod;
|
|
107
|
+
return typeof config === 'function' ? config() : config || {};
|
|
108
|
+
}
|
|
109
|
+
exports.loadConfig = loadConfig;
|
|
110
|
+
function run(argv) {
|
|
111
|
+
try {
|
|
112
|
+
var parsed = parseArgs(argv);
|
|
113
|
+
if (parsed.help) {
|
|
114
|
+
console.log(HELP);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (parsed.version) {
|
|
118
|
+
console.log(require('../package.json').version);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
var options = __assign(__assign({}, loadConfig(parsed.config)), parsed.options);
|
|
122
|
+
if (parsed.watch) {
|
|
123
|
+
(0, index_1.watch)(options);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
(0, index_1.injectSync)(options);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
catch (e) {
|
|
130
|
+
console.error("[".concat(constants_1.PLUGIN_NAME, "] ").concat(e instanceof Error ? e.message : e));
|
|
131
|
+
process.exitCode = 1;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
exports.run = run;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_EXCLUDE_DIRS = exports.DEFAULT_CONFIG_FILE = exports.PLUGIN_NAME = void 0;
|
|
4
|
+
exports.PLUGIN_NAME = '@we-debug/cli';
|
|
5
|
+
exports.DEFAULT_CONFIG_FILE = 'wedebug.config.js';
|
|
6
|
+
exports.DEFAULT_EXCLUDE_DIRS = ['node_modules', '.git'];
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { IInjectOption, IInjectResult } from './types';
|
|
2
|
+
export * from '@we-debug/compiler-core';
|
|
3
|
+
export * from './types';
|
|
4
|
+
export declare function injectSync(options?: IInjectOption): IInjectResult;
|
|
5
|
+
export declare function inject(options?: IInjectOption): Promise<IInjectResult>;
|
|
6
|
+
export interface IWatcher {
|
|
7
|
+
close(): void;
|
|
8
|
+
}
|
|
9
|
+
export declare function watch(options?: IInjectOption): IWatcher;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
16
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
17
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}) : (function(o, m, k, k2) {
|
|
21
|
+
if (k2 === undefined) k2 = k;
|
|
22
|
+
o[k2] = m[k];
|
|
23
|
+
}));
|
|
24
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
25
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
26
|
+
};
|
|
27
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
28
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.watch = exports.inject = exports.injectSync = void 0;
|
|
32
|
+
var fs_1 = __importDefault(require("fs"));
|
|
33
|
+
var path_1 = __importDefault(require("path"));
|
|
34
|
+
var compiler_core_1 = require("@we-debug/compiler-core");
|
|
35
|
+
var constants_1 = require("./constants");
|
|
36
|
+
__exportStar(require("@we-debug/compiler-core"), exports);
|
|
37
|
+
__exportStar(require("./types"), exports);
|
|
38
|
+
function createContext(options) {
|
|
39
|
+
var baseDir = (0, compiler_core_1.getAbsolutePath)(options.baseDir || 'src');
|
|
40
|
+
var outDir = options.outDir ? (0, compiler_core_1.getAbsolutePath)(options.outDir) : baseDir;
|
|
41
|
+
var exclude = options.exclude ? [].concat(options.exclude) : [];
|
|
42
|
+
return {
|
|
43
|
+
baseDir: baseDir,
|
|
44
|
+
outDir: outDir,
|
|
45
|
+
inPlace: outDir === baseDir,
|
|
46
|
+
exclude: exclude,
|
|
47
|
+
log: options.silent ? function () { } : function (msg) { return console.log("[".concat(constants_1.PLUGIN_NAME, "] ").concat(msg)); }
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function isExcluded(ctx, absPath) {
|
|
51
|
+
if (!ctx.inPlace && (absPath === ctx.outDir || absPath.indexOf(ctx.outDir + path_1.default.sep) === 0))
|
|
52
|
+
return true;
|
|
53
|
+
var relative = (0, compiler_core_1.toPosix)(path_1.default.relative(ctx.baseDir, absPath));
|
|
54
|
+
if (relative.split('/').some(function (seg) { return constants_1.DEFAULT_EXCLUDE_DIRS.indexOf(seg) > -1; }))
|
|
55
|
+
return true;
|
|
56
|
+
return (0, compiler_core_1.hitExclude)(ctx.exclude, relative);
|
|
57
|
+
}
|
|
58
|
+
function walk(ctx, dir, files) {
|
|
59
|
+
if (files === void 0) { files = []; }
|
|
60
|
+
fs_1.default.readdirSync(dir, { withFileTypes: true }).forEach(function (entry) {
|
|
61
|
+
var abs = path_1.default.join(dir, entry.name);
|
|
62
|
+
if (isExcluded(ctx, abs))
|
|
63
|
+
return;
|
|
64
|
+
if (entry.isDirectory()) {
|
|
65
|
+
walk(ctx, abs, files);
|
|
66
|
+
}
|
|
67
|
+
else if (entry.isFile()) {
|
|
68
|
+
files.push(abs);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return files;
|
|
72
|
+
}
|
|
73
|
+
function processFile(ctx, injector, absPath) {
|
|
74
|
+
var dest = path_1.default.join(ctx.outDir, path_1.default.relative(ctx.baseDir, absPath));
|
|
75
|
+
if (compiler_core_1.TRANSFORM_EXTNAMES.indexOf(path_1.default.extname(absPath)) === -1) {
|
|
76
|
+
if (ctx.inPlace)
|
|
77
|
+
return false;
|
|
78
|
+
fs_1.default.mkdirSync(path_1.default.dirname(dest), { recursive: true });
|
|
79
|
+
fs_1.default.copyFileSync(absPath, dest);
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
var raw = fs_1.default.readFileSync(absPath);
|
|
83
|
+
var file = new compiler_core_1.VirtualFile({ base: ctx.baseDir, path: absPath, contents: raw });
|
|
84
|
+
injector.transform(file);
|
|
85
|
+
var contents = file.contents || raw;
|
|
86
|
+
if (ctx.inPlace && contents.equals(raw))
|
|
87
|
+
return false;
|
|
88
|
+
fs_1.default.mkdirSync(path_1.default.dirname(dest), { recursive: true });
|
|
89
|
+
fs_1.default.writeFileSync(dest, contents);
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
function createInjector(ctx, options) {
|
|
93
|
+
var injectorOptions = __assign(__assign({}, options), { baseDir: ctx.baseDir, npmDirname: compiler_core_1.DEFAULT_NPM_DIRNAME });
|
|
94
|
+
var injector = new compiler_core_1.Injector(injectorOptions);
|
|
95
|
+
injector.init();
|
|
96
|
+
return injector;
|
|
97
|
+
}
|
|
98
|
+
function writeUIFiles(ctx, injector) {
|
|
99
|
+
var files = injector.getUIFiles();
|
|
100
|
+
files.forEach(function (f) {
|
|
101
|
+
var dest = path_1.default.join(ctx.outDir, f.relative);
|
|
102
|
+
fs_1.default.mkdirSync(path_1.default.dirname(dest), { recursive: true });
|
|
103
|
+
if (f.sourcePath) {
|
|
104
|
+
fs_1.default.copyFileSync(f.sourcePath, dest);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
fs_1.default.writeFileSync(dest, f.contents);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
return files.length;
|
|
111
|
+
}
|
|
112
|
+
function isAlreadyInjected(baseDir, entryFile, needsManifest) {
|
|
113
|
+
var appJs = path_1.default.join(baseDir, 'app.js');
|
|
114
|
+
if (!fs_1.default.existsSync(appJs))
|
|
115
|
+
return false;
|
|
116
|
+
var code = fs_1.default.readFileSync(appJs, 'utf-8');
|
|
117
|
+
if (code.indexOf((0, compiler_core_1.getEntryRequire)(entryFile)) === -1)
|
|
118
|
+
return false;
|
|
119
|
+
return !needsManifest || code.indexOf("require('./".concat(compiler_core_1.HOST_DIR, "/manifest.js')")) > -1;
|
|
120
|
+
}
|
|
121
|
+
function build(ctx, options) {
|
|
122
|
+
var result = {
|
|
123
|
+
skipped: false,
|
|
124
|
+
injectedPages: [],
|
|
125
|
+
injectedAppJson: false,
|
|
126
|
+
injectedAppScript: false,
|
|
127
|
+
writtenFiles: 0
|
|
128
|
+
};
|
|
129
|
+
if (!fs_1.default.existsSync(ctx.baseDir)) {
|
|
130
|
+
throw new Error("[".concat(constants_1.PLUGIN_NAME, "] baseDir not found: ").concat(ctx.baseDir));
|
|
131
|
+
}
|
|
132
|
+
var resolved = (0, compiler_core_1.resolveCompilerOption)(options);
|
|
133
|
+
var uiPlugins = (0, compiler_core_1.discoverUIPlugins)({
|
|
134
|
+
projectDir: resolved.projectDir,
|
|
135
|
+
uiPlugins: resolved.uiPlugins,
|
|
136
|
+
autoDiscoverUIPlugins: resolved.autoDiscoverUIPlugins
|
|
137
|
+
});
|
|
138
|
+
if (ctx.inPlace && !options.force && isAlreadyInjected(ctx.baseDir, resolved.entryFile, uiPlugins.length > 0)) {
|
|
139
|
+
ctx.log("".concat(ctx.baseDir, " has already been injected, skip. use --force to inject again"));
|
|
140
|
+
return { result: __assign(__assign({}, result), { skipped: true }) };
|
|
141
|
+
}
|
|
142
|
+
var injector = createInjector(ctx, options);
|
|
143
|
+
var appJson = path_1.default.join(ctx.baseDir, 'app.json');
|
|
144
|
+
var files = walk(ctx, ctx.baseDir).sort(function (a, b) { return Number(a === appJson) - Number(b === appJson); });
|
|
145
|
+
files.forEach(function (f) {
|
|
146
|
+
if (processFile(ctx, injector, f))
|
|
147
|
+
result.writtenFiles++;
|
|
148
|
+
});
|
|
149
|
+
result.writtenFiles += writeUIFiles(ctx, injector);
|
|
150
|
+
result.injectedPages = injector.injectedPages;
|
|
151
|
+
result.injectedAppJson = injector.injectedAppJson;
|
|
152
|
+
result.injectedAppScript = injector.injectedAppScript;
|
|
153
|
+
ctx.log("injected ".concat(result.injectedPages.length, "/").concat(injector.pages.length, " pages, ") +
|
|
154
|
+
"app.json: ".concat(result.injectedAppJson, ", app.js: ").concat(result.injectedAppScript, ", ") +
|
|
155
|
+
"ui plugins: ".concat(injector.uiPlugins.length, ", ") +
|
|
156
|
+
"".concat(result.writtenFiles, " files written to ").concat(ctx.outDir));
|
|
157
|
+
return { result: result, injector: injector };
|
|
158
|
+
}
|
|
159
|
+
function injectSync(options) {
|
|
160
|
+
if (options === void 0) { options = {}; }
|
|
161
|
+
return build(createContext(options), options).result;
|
|
162
|
+
}
|
|
163
|
+
exports.injectSync = injectSync;
|
|
164
|
+
function inject(options) {
|
|
165
|
+
if (options === void 0) { options = {}; }
|
|
166
|
+
return new Promise(function (resolve) { return resolve(injectSync(options)); });
|
|
167
|
+
}
|
|
168
|
+
exports.inject = inject;
|
|
169
|
+
function watchTree(dir, onChange) {
|
|
170
|
+
try {
|
|
171
|
+
var watcher_1 = fs_1.default.watch(dir, { recursive: true }, function (_event, filename) {
|
|
172
|
+
if (filename)
|
|
173
|
+
onChange(path_1.default.join(dir, filename.toString()));
|
|
174
|
+
});
|
|
175
|
+
return { close: function () { return watcher_1.close(); } };
|
|
176
|
+
}
|
|
177
|
+
catch (e) {
|
|
178
|
+
var watchers_1 = {};
|
|
179
|
+
var add_1 = function (d) {
|
|
180
|
+
if (watchers_1[d] || path_1.default.basename(d) === 'node_modules')
|
|
181
|
+
return;
|
|
182
|
+
watchers_1[d] = fs_1.default.watch(d, function (_event, filename) {
|
|
183
|
+
if (!filename)
|
|
184
|
+
return;
|
|
185
|
+
var abs = path_1.default.join(d, filename.toString());
|
|
186
|
+
if (fs_1.default.existsSync(abs) && fs_1.default.statSync(abs).isDirectory())
|
|
187
|
+
add_1(abs);
|
|
188
|
+
onChange(abs);
|
|
189
|
+
});
|
|
190
|
+
fs_1.default.readdirSync(d, { withFileTypes: true }).forEach(function (entry) {
|
|
191
|
+
if (entry.isDirectory())
|
|
192
|
+
add_1(path_1.default.join(d, entry.name));
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
add_1(dir);
|
|
196
|
+
return { close: function () { return Object.keys(watchers_1).forEach(function (k) { return watchers_1[k].close(); }); } };
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function watch(options) {
|
|
200
|
+
if (options === void 0) { options = {}; }
|
|
201
|
+
var ctx = createContext(options);
|
|
202
|
+
if (ctx.inPlace) {
|
|
203
|
+
throw new Error("[".concat(constants_1.PLUGIN_NAME, "] watch mode requires an outDir different from baseDir"));
|
|
204
|
+
}
|
|
205
|
+
var injector = build(ctx, options).injector;
|
|
206
|
+
var appJson = path_1.default.join(ctx.baseDir, 'app.json');
|
|
207
|
+
var pending = {};
|
|
208
|
+
var timer = null;
|
|
209
|
+
var flush = function () {
|
|
210
|
+
timer = null;
|
|
211
|
+
var changed = Object.keys(pending);
|
|
212
|
+
changed.forEach(function (f) { return delete pending[f]; });
|
|
213
|
+
try {
|
|
214
|
+
if (changed.indexOf(appJson) > -1) {
|
|
215
|
+
injector = build(ctx, options).injector;
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
changed.forEach(function (abs) {
|
|
219
|
+
var dest = path_1.default.join(ctx.outDir, path_1.default.relative(ctx.baseDir, abs));
|
|
220
|
+
if (!fs_1.default.existsSync(abs)) {
|
|
221
|
+
fs_1.default.rmSync(dest, { recursive: true, force: true });
|
|
222
|
+
ctx.log("removed ".concat((0, compiler_core_1.toPosix)(path_1.default.relative(ctx.baseDir, abs))));
|
|
223
|
+
}
|
|
224
|
+
else if (fs_1.default.statSync(abs).isFile()) {
|
|
225
|
+
processFile(ctx, injector, abs);
|
|
226
|
+
ctx.log("updated ".concat((0, compiler_core_1.toPosix)(path_1.default.relative(ctx.baseDir, abs))));
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
catch (e) {
|
|
231
|
+
console.error("[".concat(constants_1.PLUGIN_NAME, "]"), e);
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
var watcher = watchTree(ctx.baseDir, function (abs) {
|
|
235
|
+
if (isExcluded(ctx, abs))
|
|
236
|
+
return;
|
|
237
|
+
pending[abs] = true;
|
|
238
|
+
if (!timer)
|
|
239
|
+
timer = setTimeout(flush, 100);
|
|
240
|
+
});
|
|
241
|
+
ctx.log("watching ".concat(ctx.baseDir));
|
|
242
|
+
return {
|
|
243
|
+
close: function () {
|
|
244
|
+
if (timer)
|
|
245
|
+
clearTimeout(timer);
|
|
246
|
+
watcher.close();
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
exports.watch = watch;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { FilterOption, IAnyObject, ICompilerOption } from '@we-debug/compiler-core';
|
|
2
|
+
export interface IInjectOption<T extends IAnyObject = IAnyObject> extends Omit<ICompilerOption<T>, 'npmDirname'> {
|
|
3
|
+
outDir?: string;
|
|
4
|
+
exclude?: FilterOption | FilterOption[];
|
|
5
|
+
force?: boolean;
|
|
6
|
+
silent?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface IInjectResult {
|
|
9
|
+
skipped: boolean;
|
|
10
|
+
injectedPages: string[];
|
|
11
|
+
injectedAppJson: boolean;
|
|
12
|
+
injectedAppScript: boolean;
|
|
13
|
+
writtenFiles: number;
|
|
14
|
+
}
|
package/dist/types.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@we-debug/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.51.0",
|
|
4
|
+
"description": "we-debug, inject we-debug into miniprogram pages via node api or cli, without gulp",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"bin": {
|
|
8
|
+
"we-debug": "bin/we-debug.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"bin",
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"test": "node test/index.test.js"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@we-debug/compiler-core": "0.51.0"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"we-debug",
|
|
25
|
+
"miniprogram",
|
|
26
|
+
"cli"
|
|
27
|
+
],
|
|
28
|
+
"author": {
|
|
29
|
+
"name": "dlhandsome"
|
|
30
|
+
},
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=14.14.0"
|
|
34
|
+
},
|
|
35
|
+
"gitHead": "22641ac4972a154619c6dc8f44b1bfdcbb92c8eb"
|
|
36
|
+
}
|