From 3a1915a3ae109d058addeb70ddc26e520726583d Mon Sep 17 00:00:00 2001 From: xingfujiang <18224518307@163.com> Date: Wed, 13 Sep 2023 11:40:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=9C=A8=E7=BA=BF?= =?UTF-8?q?=E7=AD=BE=E5=90=8D=E5=8A=9F=E8=83=BD=20(#51)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 72161674 --- .../src/plugins/sign-online-plugin.js | 83 +++++++++++++++++++ .../hap-packager/src/subpackages/service.js | 2 +- packages/hap-packager/src/webpack.post.js | 17 ++++ .../src/compilation-config.js | 1 + packages/hap-shared-utils/src/config.js | 5 +- packages/hap-toolkit/src/commands/compile.js | 18 +++- 6 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 packages/hap-packager/src/plugins/sign-online-plugin.js diff --git a/packages/hap-packager/src/plugins/sign-online-plugin.js b/packages/hap-packager/src/plugins/sign-online-plugin.js new file mode 100644 index 00000000..cf9325b2 --- /dev/null +++ b/packages/hap-packager/src/plugins/sign-online-plugin.js @@ -0,0 +1,83 @@ +import http from 'http' +import fs from 'fs' +import path from 'path' +import { URL } from 'url' +import { colorconsole } from '@hap-toolkit/shared-utils' + +function SignOnlinePlugin(options) { + this.options = options +} + +SignOnlinePlugin.prototype.apply = function (compiler) { + const options = this.options + + compiler.hooks.done.tapAsync('SignOnlinePlugin', function (stats, cb) { + const requestPath = options.request + if (!requestPath || typeof requestPath !== 'string') { + colorconsole.error(`### App Server ### 请求线上接口无效,请检查`) + cb() + return + } + const ext = options.signOnlineRpks ? 'rpks' : 'rpk' + const pkgName = options.name + const versionName = options.versionName + const noSignFile = `${pkgName}.nosign.${versionName}.${ext}` + + const noSignFilePath = path.join(options.output, noSignFile) + + const distFileName = `${pkgName}.${options.sign}.${versionName}.${ext}` + const distFilePath = path.join(options.output, distFileName) + const distFileStream = fs.createWriteStream(distFilePath) + + let form = null + + const file = fs.createReadStream(noSignFilePath) + + if (options.formData && options.formData(file)) { + form = options.formData(file) + } + + form && Object.assign(options.headers(), form.getHeaders()) + + const url = new URL(requestPath) + const param = { + host: url.hostname, + port: url.port, + path: url.pathname, + method: 'POST', + timeout: 10000, + headers: options.headers + } + const req = http + .request(param, (res) => { + colorconsole.log(`### App Loader ### 请求接口的状态码:${res.statusCode}`) + res.pipe(distFileStream, { end: false }) + res.on('end', (data) => { + if (res.statusCode === 200) { + distFileStream.end() + colorconsole.log( + `### App Loader ### 请求线上签名成功,dist目录生成文件:${distFileName}` + ) + cb() + } + }) + }) + .on('error', (err) => { + colorconsole.error(`### App Server ### 请求线上签名错误,错误信息: ${err.message} $`) + cb(err) + }) + .on('timeout', function () { + colorconsole.warn( + `### App Server ### 请求线上签名网络超时,请检查网络与接口地址 ${ + form + ? '' + : '缺少FormData对象,请在配置文件quickapp.config.js里面定义的params函数返回FormData对象' + }` + ) + req.abort() + }) + form && form.pipe(req) + }) +} + +export default SignOnlinePlugin diff --git a/packages/hap-packager/src/subpackages/service.js b/packages/hap-packager/src/subpackages/service.js index fe8093b7..e066465c 100644 --- a/packages/hap-packager/src/subpackages/service.js +++ b/packages/hap-packager/src/subpackages/service.js @@ -77,7 +77,7 @@ function allocateResourceToPackages(files, base, fullPackage, subPackages, build files.forEach((fileBuildPath) => { const fileAbsPath = path.join(base, fileBuildPath) - const fileContentBuffer = fs.readFileSync(fileAbsPath.replace(/\\/g, '/')) + const fileContentBuffer = fs.readFileSync(fileAbsPath) const fileContentDigest = calcDataDigest(fileContentBuffer) // 资源基本信息 const resourceInfo = [fileBuildPath, fileContentBuffer, fileContentDigest] diff --git a/packages/hap-packager/src/webpack.post.js b/packages/hap-packager/src/webpack.post.js index 0226c478..3f9a6625 100644 --- a/packages/hap-packager/src/webpack.post.js +++ b/packages/hap-packager/src/webpack.post.js @@ -19,6 +19,7 @@ import { } from './plugins' import { genPriorities, getBabelConfigJsPath } from './common/utils' import { getSkeletonConfig } from './common/info' +import SignOnlinePlugin from './plugins/sign-online-plugin' /** * 配置关联 @@ -166,6 +167,22 @@ function postHook(webpackConf, defaultsOptions, quickappConfig = {}) { }) ) + if (compileOptionsObject['signOnline']) { + // 发送接口进行线上签名 + webpackConf.plugins.push( + new SignOnlinePlugin({ + signOnlineRpks: compileOptionsObject.signOnlineRpks, + sign: webpackConf.mode === 'development' ? 'debug' : 'release', + name: appPackageName, + versionName, + output: pathDist, + request: globalConfig.signOnLineConfig.signOnLine, + headers: globalConfig.signOnLineConfig.headers, + formData: globalConfig.signOnLineConfig.params + }) + ) + } + // 解决错误信息定位问题 if (compileOptionsObject.matchSourcemap) { webpackConf.plugins.push(new SourcemapFixPlugin()) diff --git a/packages/hap-shared-utils/src/compilation-config.js b/packages/hap-shared-utils/src/compilation-config.js index 8ccca06e..7035ab52 100644 --- a/packages/hap-shared-utils/src/compilation-config.js +++ b/packages/hap-shared-utils/src/compilation-config.js @@ -104,6 +104,7 @@ const compileOptionsObject = { */ function mergeCompileOptionsObject(argopts) { // TODO release memeory, use optimize-prop only + compileOptionsObject.devtool = argopts.devtool ? argopts.devtool : false Object.assign(compileOptionsObject, argopts) } diff --git a/packages/hap-shared-utils/src/config.js b/packages/hap-shared-utils/src/config.js index 9672d4e7..8805eff7 100644 --- a/packages/hap-shared-utils/src/config.js +++ b/packages/hap-shared-utils/src/config.js @@ -22,5 +22,8 @@ export default { // 代码风格规则 isSmartMode: false, // 记录 watch 模式下哪些 .ux 文件对应的 .js 文件改变了 - changedJS: {} + changedJS: {}, + signOnLineConfig: { + signOnLine: '' + } } diff --git a/packages/hap-toolkit/src/commands/compile.js b/packages/hap-toolkit/src/commands/compile.js index b4913093..fed1da56 100644 --- a/packages/hap-toolkit/src/commands/compile.js +++ b/packages/hap-toolkit/src/commands/compile.js @@ -5,7 +5,12 @@ import webpack from 'webpack' import adbCommander from 'adb-commander' -import { setCustomConfig, colorconsole } from '@hap-toolkit/shared-utils' +import { + setCustomConfig, + colorconsole, + globalConfig, + compileOptionsMeta +} from '@hap-toolkit/shared-utils' import genWebpackConf from '../gen-webpack-conf' import { summaryErrors, summaryWarnings } from './utils' @@ -70,6 +75,17 @@ export function compile(platform, mode, watch, options = {}) { const webpackMode = mode === 'prod' ? 'production' : 'development' + if (options.disableSignOnline) { + options['signOnline'] = false + } else if ( + globalConfig.signOnLineConfig.signOnLine.match( + /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/ + ) + ) { + options['signOnline'] = true + options['signMode'] = compileOptionsMeta.signModeEnum.NULL + } + try { const webpackConfig = await genWebpackConf(options, webpackMode)