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)