@opentapd/tplugin-cli 0.69.0 → 0.69.1-alpha.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/lib/deploy.js CHANGED
@@ -18,6 +18,7 @@ const SimpleGit = require('simple-git');
18
18
  const archiver = require('archiver');
19
19
  const crypto = require('crypto');
20
20
  const path = require('path');
21
+ const { execSync } = require('child_process');
21
22
  const { get } = require('lodash');
22
23
  const getFolderHash = require('../util/getFolderHash');
23
24
  // CLI部署
@@ -39,6 +40,9 @@ module.exports = async () => {
39
40
  if (tpluginConf.tapd.pluginEnv === 'oa') {
40
41
  await oaEnvDeploy(docObj);
41
42
  } else {
43
+ if (tpluginConf.tapd.pluginEnv === 'cloud') {
44
+ await cloudPreDeal();
45
+ }
42
46
  // 生成插件的文件hash;
43
47
  const projectHash = (await getFolderHash(process.cwd(), new SimpleGit())).slice(0, 8);
44
48
 
@@ -183,13 +187,16 @@ function zipCurrentWorkspace(code, callback) {
183
187
  });
184
188
 
185
189
  archive.pipe(output);
186
-
190
+ let ignore = ['node_modules/**', '.git/**'];
191
+ if (tpluginConf.tapd.pluginEnv === 'cloud') {
192
+ ignore = ['.git/**'];
193
+ }
187
194
  archive.glob(
188
195
  '**/*',
189
196
  {
190
197
  cwd: `${process.cwd()}/`,
191
198
  dot: true,
192
- ignore: ['node_modules/**', '.git/**'],
199
+ ignore,
193
200
  },
194
201
  {
195
202
  prefix: `${code}/`,
@@ -199,11 +206,18 @@ function zipCurrentWorkspace(code, callback) {
199
206
  archive.finalize();
200
207
  }
201
208
 
202
- function uploadZipFile(code, filePath) {
209
+ async function readConfig() {
210
+ let content = '';
211
+ content = await fs.readFile(path.join(process.cwd(), 'plugin.yaml'));
212
+ return content.toString();
213
+ }
214
+
215
+ async function uploadZipFile(code, filePath) {
203
216
  spinner.start('开始上传压缩包');
204
217
  return uploadToCos({
205
218
  client_id: code,
206
219
  file: fs.createReadStream(filePath),
220
+ yaml_content: await readConfig(),
207
221
  })
208
222
  .then(() => {
209
223
  spinner.succeed('压缩包上传成功!');
@@ -235,13 +249,16 @@ async function deploy(branch, commit, code, { nick } = {}) {
235
249
  spinner.start('启动部署任务');
236
250
 
237
251
  // 触发部署流水线
238
- const { data } = await tapdsdk.request({ url: 'open_user_app/bk_pipeline_start', params: {
239
- branch,
240
- commit_id: commit,
241
- client_id: code,
242
- plugin_env: pluginEnv,
243
- nick,
244
- }, method: 'POST' });
252
+ const { data } = await tapdsdk.request({
253
+ url: 'open_user_app/bk_pipeline_start', params: {
254
+ branch,
255
+ commit_id: commit,
256
+ client_id: code,
257
+ plugin_env: pluginEnv,
258
+ nick,
259
+ is_local_build: true,
260
+ }, method: 'POST'
261
+ });
245
262
 
246
263
  // 流水线启动失败
247
264
  if (!data.status || !data.data || !data.data.bk_build_id) {
@@ -264,10 +281,12 @@ async function deploy(branch, commit, code, { nick } = {}) {
264
281
  while (i++ < 120) {
265
282
  let buildStatus = '';
266
283
  try {
267
- const { data } = await tapdsdk.request({ url: 'open_user_app/get_build_status', params: {
268
- build_id: buildId,
269
- client_id: code,
270
- }, method: 'GET' });
284
+ const { data } = await tapdsdk.request({
285
+ url: 'open_user_app/get_build_status', params: {
286
+ build_id: buildId,
287
+ client_id: code,
288
+ }, method: 'GET'
289
+ });
271
290
  // 判断部署流水线状态
272
291
  buildStatus = get(data, 'data.build_status', '');
273
292
  } catch (err) {
@@ -323,29 +342,132 @@ async function switchTestVersionTips({ code, version }) {
323
342
 
324
343
  // 获取当前公司的测试版本
325
344
  async function getAppTestVersion({ code }) {
326
- const { data } = await tapdsdk.request({ url: 'open_user_app/get_app_current_test_version', params: {
327
- code,
328
- } });
345
+ const { data } = await tapdsdk.request({
346
+ url: 'open_user_app/get_app_current_test_version', params: {
347
+ code,
348
+ }
349
+ });
329
350
  return get(data, 'data.test_deploy_info');
330
351
  }
331
352
 
332
353
  // 切换当前公司的测试版本
333
354
  async function switchTestVersion({ code, version }) {
334
- const { data } = await tapdsdk.request({ url: 'open_user_app/switch_test_app_version', params: {
335
- code,
336
- version,
337
- } });
355
+ const { data } = await tapdsdk.request({
356
+ url: 'open_user_app/switch_test_app_version', params: {
357
+ code,
358
+ version,
359
+ }
360
+ });
338
361
  return data;
339
362
  }
340
363
 
341
364
  async function checkDeployVersion({ version, code }) {
342
- const { data } = await tapdsdk.request({ url: 'open_user_app/check_app_version', params: {
343
- code,
344
- version,
345
- } });
365
+ const { data } = await tapdsdk.request({
366
+ url: 'open_user_app/check_app_version', params: {
367
+ code,
368
+ version,
369
+ }
370
+ });
346
371
 
347
372
  if (data.data.exist !== false) {
348
373
  throw new Error('该代码已经部署过了');
349
374
  }
350
375
  return true;
351
376
  }
377
+
378
+ async function checkAndInstallJsDeps(skipClean) {
379
+ spinner.start('正在安装 JS 依赖 (npm install)...');
380
+ try {
381
+ execSync('npm install --unsafe-perm', { stdio: 'inherit' });
382
+ spinner.succeed('JS 依赖安装完成');
383
+ } catch (e) {
384
+ spinner.fail('JS 依赖安装失败,请手动执行 npm install');
385
+ process.exit(1);
386
+ }
387
+ }
388
+
389
+ async function cloudPreDeal() {
390
+ const pluginRunEnv = getPluginRunEnv();
391
+ if (pluginRunEnv === 'js') {
392
+ await checkAndInstallJsDeps();
393
+ await runJsBuild();
394
+ } else if (pluginRunEnv === 'python') {
395
+ await checkAndInstallPythonDeps();
396
+ } else {
397
+ spinner.fail('未识别插件类型,请确认根目录下有 index.js 或 index.py');
398
+ process.exit(1);
399
+ }
400
+ }
401
+
402
+ async function runJsBuild() {
403
+ const distPath = 'resources/dist';
404
+ spinner.start('开始执行本地构建...');
405
+ // 清空 dist 目录
406
+ try {
407
+ if (fs.existsSync(distPath)) {
408
+ fs.rmSync(distPath, { recursive: true, force: true });
409
+ spinner.info('已清空 resources/dist 目录');
410
+ }
411
+ } catch (err) {
412
+ spinner.fail('清理 resources/dist 目录失败,请检查权限');
413
+ process.exit(1);
414
+ }
415
+
416
+ // 执行构建
417
+ try {
418
+ execSync('npm run build', { stdio: 'inherit' });
419
+ spinner.succeed('构建成功!');
420
+ } catch (e) {
421
+ spinner.fail('构建失败,请检查错误日志');
422
+ process.exit(1);
423
+ }
424
+
425
+ // 清除 node modules
426
+ try {
427
+ if (fs.existsSync('node_modules')) {
428
+ fs.rmSync('node_modules', { recursive: true, force: true });
429
+ spinner.info('已清空 node_modules 目录');
430
+ }
431
+ } catch (err) {
432
+ spinner.fail('清理 node_modules 目录失败,请检查权限');
433
+ process.exit(1);
434
+ }
435
+
436
+ // 安装后端Handler依赖包
437
+ try {
438
+ execSync('npm install --unsafe-perm --production', { stdio: 'inherit' });
439
+ spinner.succeed('Handler依赖包安装成功!');
440
+ } catch (e) {
441
+ spinner.fail('Handler依赖包安装失败,请手动执行 npm install --unsafe-perm --production');
442
+ process.exit(1);
443
+ }
444
+ }
445
+
446
+ function getPluginRunEnv() {
447
+ const cwd = process.cwd();
448
+ const jsPath = path.join(cwd, 'index.js');
449
+ const pyPath = path.join(cwd, 'index.py');
450
+ if (fs.existsSync(jsPath)) {
451
+ return 'js';
452
+ }
453
+ if (fs.existsSync(pyPath)) {
454
+ return 'python';
455
+ }
456
+ return 'unknown';
457
+ }
458
+
459
+ async function checkAndInstallPythonDeps() {
460
+ if (!fs.existsSync('requirements.txt')) {
461
+ spinner.start('未找到 requirements.txt, 跳过依赖安装。');
462
+ spinner.succeed('跳过 Python 依赖安装。');
463
+ return;
464
+ }
465
+ spinner.start('检测到 Python 插件,正在安装依赖...');
466
+ try {
467
+ execSync('pip3 install -r requirements.txt -t .', { stdio: 'inherit' });
468
+ spinner.succeed('Python 依赖安装完成');
469
+ } catch (e) {
470
+ spinner.fail('Python依赖安装失败,请手动执行 pip install -r requirements.txt -t .');
471
+ process.exit(1);
472
+ }
473
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentapd/tplugin-cli",
3
- "version": "0.69.0",
3
+ "version": "0.69.1-alpha.0",
4
4
  "description": "tplugin-cli",
5
5
  "bin": {
6
6
  "tplugin-cli": "index.js"
@@ -26,7 +26,7 @@
26
26
  "author": "",
27
27
  "license": "ISC",
28
28
  "dependencies": {
29
- "@opentapd/tplugin-core": "^1.44.0",
29
+ "@opentapd/tplugin-core": "^1.44.1-alpha.0",
30
30
  "address": "^1.2.2",
31
31
  "archiver": "^5.3.1",
32
32
  "axios": "^0.21.1",
@@ -79,5 +79,5 @@
79
79
  "node": ">=14.13.0"
80
80
  },
81
81
  "main": "index.js",
82
- "gitHead": "f8ce70780573e282ea13a194c754ed505dd1bed4"
82
+ "gitHead": "040453b0bd43c1bef40f627e78dbfa823e9599ff"
83
83
  }
@@ -7,6 +7,17 @@
7
7
 
8
8
  const semver = require('semver');
9
9
  const { engines } = require('../package.json');
10
+ const tpluginConf = require('../config');
11
+
12
+ if (tpluginConf.tapd.pluginEnv === 'cloud') {
13
+ const cloudRequiredNodeVersion = '16.13.x';
14
+ // 检查当前 Node 版本是否saas符合要求
15
+ if (!semver.satisfies(process.version, cloudRequiredNodeVersion)) {
16
+ console.log(`This project require node version: ${cloudRequiredNodeVersion}`);
17
+ process.exit(-1);
18
+ }
19
+ }
20
+
10
21
  const requiredNodeVersion = engines.node;
11
22
 
12
23
  // 判断Node版本范围