@tuya-sat/micro-script 3.0.26 → 3.0.27-beta.2
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/dist/apis/index.d.ts +2 -0
- package/dist/apis/index.js +204 -0
- package/dist/apis/template.ts +30 -0
- package/dist/bin/cli.js +0 -1
- package/dist/scripts/build.js +2 -0
- package/dist/scripts/start.js +2 -0
- package/dist/scripts/startInMain.js +2 -0
- package/dist/template/micro-app/index.tsx +1 -0
- package/dist/utils/promiser.d.ts +2 -2
- package/package.json +5 -4
- package/scripts/copyTemplate.js +5 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
39
|
+
const path_1 = __importDefault(require("path"));
|
|
40
|
+
const paths_1 = __importDefault(require("../config/paths"));
|
|
41
|
+
const ts = __importStar(require("typescript"));
|
|
42
|
+
const express_1 = require("express");
|
|
43
|
+
const prettier_1 = __importDefault(require("prettier"));
|
|
44
|
+
const isMonorepo = () => {
|
|
45
|
+
return (fs_extra_1.default.existsSync(path_1.default.join(process.cwd(), 'apps')) &&
|
|
46
|
+
fs_extra_1.default.existsSync(path_1.default.join(process.cwd(), 'packages')));
|
|
47
|
+
};
|
|
48
|
+
const isMainApp = () => {
|
|
49
|
+
const { debuggerConfig: { isMainApp }, } = require(paths_1.default.microConfig);
|
|
50
|
+
return isMainApp;
|
|
51
|
+
};
|
|
52
|
+
const createPropertyAssignment = (key, value) => {
|
|
53
|
+
return ts.factory.createPropertyAssignment(ts.factory.createStringLiteral(key), ts.factory.createStringLiteral(value));
|
|
54
|
+
};
|
|
55
|
+
exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
56
|
+
var _a, _b, _c, _d;
|
|
57
|
+
if (isMainApp()) {
|
|
58
|
+
}
|
|
59
|
+
else if (isMonorepo()) {
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
const microPath = path_1.default.join(process.cwd(), 'node_modules/.micro');
|
|
63
|
+
!fs_extra_1.default.existsSync(microPath) && fs_extra_1.default.mkdirSync(microPath, { recursive: true });
|
|
64
|
+
const sourceCode = fs_extra_1.default
|
|
65
|
+
.readFileSync(path_1.default.join(__dirname, './template.ts'))
|
|
66
|
+
.toString();
|
|
67
|
+
const ast = ts.createSourceFile('apis.ts', sourceCode, ts.ScriptTarget.ES2020, true);
|
|
68
|
+
const apiNode = (_d = (_c = (_b = (_a = ast.statements.find((s) => {
|
|
69
|
+
var _a, _b, _c;
|
|
70
|
+
return s.kind === ts.SyntaxKind.VariableStatement &&
|
|
71
|
+
((_c = (_b = (_a = s === null || s === void 0 ? void 0 : s.declarationList) === null || _a === void 0 ? void 0 : _a.declarations) === null || _b === void 0 ? void 0 : _b[0].name) === null || _c === void 0 ? void 0 : _c.escapedText) ===
|
|
72
|
+
'apis';
|
|
73
|
+
})) === null || _a === void 0 ? void 0 : _a.declarationList) === null || _b === void 0 ? void 0 : _b.declarations) === null || _c === void 0 ? void 0 : _c[0].initializer) === null || _d === void 0 ? void 0 : _d.properties;
|
|
74
|
+
const mf = fs_extra_1.default.readJSONSync(path_1.default.join(process.cwd(), 'manifest.json'));
|
|
75
|
+
const apis = mf.apis || [];
|
|
76
|
+
apis.forEach((api) => {
|
|
77
|
+
const paths = api.path.split('/').map((item) => {
|
|
78
|
+
return item
|
|
79
|
+
.replace('.', '')
|
|
80
|
+
.replace('-', '_')
|
|
81
|
+
.replace('{', '')
|
|
82
|
+
.replace('}', '')
|
|
83
|
+
.toUpperCase();
|
|
84
|
+
});
|
|
85
|
+
const apiName = `${api.method.toUpperCase()}${paths.join('_')}`;
|
|
86
|
+
const regex = /{(\w+)}/g;
|
|
87
|
+
const matches = [];
|
|
88
|
+
let match;
|
|
89
|
+
while ((match = regex.exec(api.path))) {
|
|
90
|
+
matches.push(match[1]);
|
|
91
|
+
}
|
|
92
|
+
const createParameterDeclarations = () => {
|
|
93
|
+
console.log('createParameterDeclarations', matches);
|
|
94
|
+
return [
|
|
95
|
+
ts.factory.createParameterDeclaration(undefined, undefined, undefined, ts.factory.createObjectBindingPattern([
|
|
96
|
+
...[
|
|
97
|
+
matches.length > 0
|
|
98
|
+
? ts.factory.createBindingElement(undefined, undefined, 'params')
|
|
99
|
+
: undefined,
|
|
100
|
+
].filter(Boolean),
|
|
101
|
+
ts.factory.createBindingElement(undefined, undefined, 'query'),
|
|
102
|
+
ts.factory.createBindingElement(undefined, undefined, 'headers'),
|
|
103
|
+
ts.factory.createBindingElement(undefined, undefined, 'data'),
|
|
104
|
+
]), undefined, ts.factory.createTypeLiteralNode([
|
|
105
|
+
...[
|
|
106
|
+
matches.length > 0
|
|
107
|
+
? ts.factory.createPropertySignature(undefined, 'params', undefined, ts.factory.createTypeLiteralNode(matches.map((match) => {
|
|
108
|
+
return ts.factory.createPropertySignature([], match, undefined, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword));
|
|
109
|
+
})))
|
|
110
|
+
: undefined,
|
|
111
|
+
].filter(Boolean),
|
|
112
|
+
ts.factory.createPropertySignature(undefined, 'query', ts.factory.createToken(ts.SyntaxKind.QuestionToken), ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)),
|
|
113
|
+
ts.factory.createPropertySignature(undefined, 'headers', ts.factory.createToken(ts.SyntaxKind.QuestionToken), ts.factory.createTypeLiteralNode([
|
|
114
|
+
ts.factory.createIndexSignature(undefined, undefined, [
|
|
115
|
+
ts.factory.createParameterDeclaration(undefined, undefined, undefined, 'key', undefined, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword), undefined),
|
|
116
|
+
], ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)),
|
|
117
|
+
])),
|
|
118
|
+
ts.factory.createPropertySignature(undefined, 'data', ts.factory.createToken(ts.SyntaxKind.QuestionToken), ts.factory.createTypeLiteralNode([
|
|
119
|
+
ts.factory.createIndexSignature(undefined, undefined, [
|
|
120
|
+
ts.factory.createParameterDeclaration(undefined, undefined, undefined, 'key', undefined, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword), undefined),
|
|
121
|
+
], ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)),
|
|
122
|
+
])),
|
|
123
|
+
])),
|
|
124
|
+
];
|
|
125
|
+
};
|
|
126
|
+
const reqNodes = () => {
|
|
127
|
+
let reqUrl = '/' +
|
|
128
|
+
`/open-api${api.path}`
|
|
129
|
+
.replace(regex, '')
|
|
130
|
+
.split('/')
|
|
131
|
+
.filter(Boolean)
|
|
132
|
+
.join('/') +
|
|
133
|
+
'/';
|
|
134
|
+
console.log('reqUrl', reqUrl);
|
|
135
|
+
const urlNode = () => {
|
|
136
|
+
if (matches.length > 0 && express_1.query) {
|
|
137
|
+
return ts.factory.createTemplateExpression(ts.factory.createTemplateHead(reqUrl), (() => {
|
|
138
|
+
if (matches.length == 1) {
|
|
139
|
+
return [
|
|
140
|
+
ts.factory.createTemplateSpan(ts.factory.createIdentifier('params.' + matches[0]), ts.factory.createTemplateMiddle('?')),
|
|
141
|
+
ts.factory.createTemplateSpan(ts.factory.createIdentifier('query'), ts.factory.createTemplateMiddle('')),
|
|
142
|
+
];
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
return [
|
|
146
|
+
...matches.slice(0, -1).map((match) => {
|
|
147
|
+
return ts.factory.createTemplateSpan(ts.factory.createIdentifier('params.' + match), ts.factory.createTemplateMiddle('/'));
|
|
148
|
+
}),
|
|
149
|
+
ts.factory.createTemplateSpan(ts.factory.createIdentifier('params.' + matches[matches.length - 1]), ts.factory.createTemplateMiddle('?')),
|
|
150
|
+
ts.factory.createTemplateSpan(ts.factory.createIdentifier('query'), ts.factory.createTemplateTail('')),
|
|
151
|
+
];
|
|
152
|
+
}
|
|
153
|
+
})());
|
|
154
|
+
}
|
|
155
|
+
if (matches.length > 0) {
|
|
156
|
+
return ts.factory.createTemplateExpression(ts.factory.createTemplateHead(reqUrl + '?'), (() => {
|
|
157
|
+
if (matches.length === 1) {
|
|
158
|
+
return [
|
|
159
|
+
ts.factory.createTemplateSpan(ts.factory.createIdentifier('params.' + matches[0]), ts.factory.createTemplateMiddle('/')),
|
|
160
|
+
];
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
return [
|
|
164
|
+
...matches.slice(0, -1).map((match) => {
|
|
165
|
+
return ts.factory.createTemplateSpan(ts.factory.createIdentifier('params.' + match), ts.factory.createTemplateMiddle('/'));
|
|
166
|
+
}),
|
|
167
|
+
ts.factory.createTemplateSpan(ts.factory.createIdentifier('params.' + matches[matches.length - 1]), ts.factory.createTemplateTail('')),
|
|
168
|
+
];
|
|
169
|
+
}
|
|
170
|
+
})());
|
|
171
|
+
}
|
|
172
|
+
if (express_1.query) {
|
|
173
|
+
return ts.factory.createTemplateExpression(ts.factory.createTemplateHead(reqUrl + '?'), [
|
|
174
|
+
ts.factory.createTemplateSpan(ts.factory.createIdentifier('query'), ts.factory.createTemplateTail('')),
|
|
175
|
+
]);
|
|
176
|
+
}
|
|
177
|
+
return ts.factory.createStringLiteral(`/open-api${api.path}`);
|
|
178
|
+
};
|
|
179
|
+
return [
|
|
180
|
+
ts.factory.createObjectLiteralExpression(ts.factory.createNodeArray([
|
|
181
|
+
ts.factory.createPropertyAssignment(ts.factory.createStringLiteral('url'), urlNode()),
|
|
182
|
+
createPropertyAssignment('method', `${api.method}`),
|
|
183
|
+
ts.factory.createPropertyAssignment('headers', ts.factory.createIdentifier('headers')),
|
|
184
|
+
ts.factory.createPropertyAssignment('data', ts.factory.createIdentifier('data')),
|
|
185
|
+
])),
|
|
186
|
+
];
|
|
187
|
+
};
|
|
188
|
+
apiNode.push(ts.factory.createPropertyAssignment(apiName, ts.factory.createArrowFunction(undefined, undefined, createParameterDeclarations(), undefined, ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), ts.factory.createBlock([
|
|
189
|
+
ts.factory.createReturnStatement(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('api'), 'request'), undefined, reqNodes()), 'then'), undefined, [
|
|
190
|
+
ts.factory.createArrowFunction(undefined, undefined, [
|
|
191
|
+
ts.factory.createParameterDeclaration(undefined, undefined, undefined, 'res', undefined, ts.factory.createTypeReferenceNode('any')),
|
|
192
|
+
], undefined, undefined, ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('res'), ts.factory.createIdentifier('data'))),
|
|
193
|
+
])),
|
|
194
|
+
]))));
|
|
195
|
+
});
|
|
196
|
+
const printer = ts.createPrinter({
|
|
197
|
+
newLine: ts.NewLineKind.LineFeed,
|
|
198
|
+
});
|
|
199
|
+
const output = printer.printNode(ts.EmitHint.Unspecified, ast, ast);
|
|
200
|
+
const apiPath = path_1.default.join(process.cwd(), 'node_modules', '.micro', 'api.ts');
|
|
201
|
+
console.log('apiPath', apiPath);
|
|
202
|
+
fs_extra_1.default.writeFileSync(apiPath, prettier_1.default.format(output), { encoding: 'utf-8' });
|
|
203
|
+
}
|
|
204
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import axios, { AxiosInstance, AxiosResponse, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { message } from 'antd';
|
|
3
|
+
|
|
4
|
+
const api = axios.create();
|
|
5
|
+
|
|
6
|
+
api.interceptors.request.use(
|
|
7
|
+
(config: AxiosRequestConfig) => {
|
|
8
|
+
config.headers = {
|
|
9
|
+
...config.headers,
|
|
10
|
+
'Content-Type': 'application/json;charset=utf-8',
|
|
11
|
+
};
|
|
12
|
+
return config;
|
|
13
|
+
},
|
|
14
|
+
(error: Error) => Promise.reject(error)
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
api.interceptors.response.use(
|
|
18
|
+
(response: AxiosResponse<any>) => {
|
|
19
|
+
const res = response.data;
|
|
20
|
+
if (!res.success) {
|
|
21
|
+
message.error(res.msg);
|
|
22
|
+
return Promise.reject(res);
|
|
23
|
+
}
|
|
24
|
+
return res;
|
|
25
|
+
},
|
|
26
|
+
(error: Error) => Promise.reject(error)
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
//根据 manifest apis生产微应用可以直接使用的
|
|
30
|
+
export const apis = {};
|
package/dist/bin/cli.js
CHANGED
package/dist/scripts/build.js
CHANGED
|
@@ -17,10 +17,12 @@ const chalk_1 = __importDefault(require("chalk"));
|
|
|
17
17
|
const ora_1 = __importDefault(require("ora"));
|
|
18
18
|
const webpack_config_1 = __importDefault(require("../config/webpack.config"));
|
|
19
19
|
const template_1 = __importDefault(require("../template"));
|
|
20
|
+
const apis_1 = __importDefault(require("../apis"));
|
|
20
21
|
const BUILD_FAIL_TIP = '糟糕:打包失败了,下面是具体信息';
|
|
21
22
|
const spinner = (0, ora_1.default)('building').start();
|
|
22
23
|
const config = (0, webpack_config_1.default)();
|
|
23
24
|
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
+
yield (0, apis_1.default)();
|
|
24
26
|
yield (0, template_1.default)();
|
|
25
27
|
(0, webpack_1.default)(config).run((err, stats) => {
|
|
26
28
|
if (err) {
|
package/dist/scripts/start.js
CHANGED
|
@@ -18,7 +18,9 @@ const mock_1 = __importDefault(require("../module/mock"));
|
|
|
18
18
|
const bundleServer_1 = require("../module/bundleServer");
|
|
19
19
|
const proxy_1 = __importDefault(require("../module/proxy"));
|
|
20
20
|
const template_1 = __importDefault(require("../template"));
|
|
21
|
+
const apis_1 = __importDefault(require("../apis"));
|
|
21
22
|
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
23
|
+
yield (0, apis_1.default)();
|
|
22
24
|
yield (0, template_1.default)();
|
|
23
25
|
let defaultPort = 9000;
|
|
24
26
|
console.log(chalk_1.default.yellow('如果是pc端的微应用调试,请勿使用该命令,考虑使用`start --main`或`start --main --proxy`'));
|
|
@@ -18,7 +18,9 @@ const mainServer_1 = __importDefault(require("../module/mainServer"));
|
|
|
18
18
|
const bundleServer_1 = require("../module/bundleServer");
|
|
19
19
|
const kill_port_1 = __importDefault(require("kill-port"));
|
|
20
20
|
const template_1 = __importDefault(require("../template"));
|
|
21
|
+
const apis_1 = __importDefault(require("../apis"));
|
|
21
22
|
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
23
|
+
yield (0, apis_1.default)();
|
|
22
24
|
yield (0, template_1.default)();
|
|
23
25
|
let defaultPort = 9000;
|
|
24
26
|
//检测端口占用情况
|
package/dist/utils/promiser.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tuya-sat/micro-script",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.27-beta.2",
|
|
4
4
|
"bin": "./dist/bin/cli.js",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"license": "MIT",
|
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
"@babel/preset-typescript": "7.16.7",
|
|
24
24
|
"@babel/traverse": "^7.20.13",
|
|
25
25
|
"@pmmmwh/react-refresh-webpack-plugin": "0.5.4",
|
|
26
|
-
"@tuya-sat/micro-dev-loader": "3.0.
|
|
27
|
-
"@tuya-sat/micro-dev-proxy": "3.0.
|
|
28
|
-
"@tuya-sat/micro-utils": "3.0.
|
|
26
|
+
"@tuya-sat/micro-dev-loader": "3.0.27-beta.2",
|
|
27
|
+
"@tuya-sat/micro-dev-proxy": "3.0.27-beta.2",
|
|
28
|
+
"@tuya-sat/micro-utils": "3.0.27-beta.2",
|
|
29
29
|
"@types/kill-port": "^2.0.0",
|
|
30
30
|
"babel-loader": "8.2.4",
|
|
31
31
|
"babel-plugin-import": "1.13.3",
|
|
@@ -52,6 +52,7 @@
|
|
|
52
52
|
"postcss": "8.4.12",
|
|
53
53
|
"postcss-loader": "6.2.1",
|
|
54
54
|
"postcss-preset-env": "7.4.3",
|
|
55
|
+
"prettier": "^2.8.8",
|
|
55
56
|
"react-refresh": "0.11.0",
|
|
56
57
|
"resolve-url-loader": "5.0.0",
|
|
57
58
|
"sass": "1.49.9",
|
package/scripts/copyTemplate.js
CHANGED