From 4701e4c85e73f3aeb88f9cd0b8e8d89e1d42cc95 Mon Sep 17 00:00:00 2001 From: weareoutman Date: Mon, 26 Aug 2024 10:10:11 +0800 Subject: [PATCH] fix(): extract source of contract in contract.yaml --- .../src/generateBrickContracts.js | 14 +++++++++++--- .../bricks/ScanUseProviderHookContractsPlugin.js | 4 +++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/build-config-factory/src/generateBrickContracts.js b/packages/build-config-factory/src/generateBrickContracts.js index 509eee5dae..2c8aa2bc73 100644 --- a/packages/build-config-factory/src/generateBrickContracts.js +++ b/packages/build-config-factory/src/generateBrickContracts.js @@ -8,6 +8,10 @@ const { bricks: { webpackContractsFactory }, } = require("@next-core/webpack-config-factory"); +const globalContractRegExp = + /(?<=@contract\s+)(?:]+)>\s+)?(?:[\w.]+)(?:@[\d.]+)?(?=\s+)/g; +const singleContractRegExp = /^(?:]+)>\s+)?([\w.]+)(?:@([\d.]+))?$/; + module.exports = function generateBrickContracts(dir, isProviderBricks) { console.log("Analyzing brick contracts..."); const startTime = Date.now(); @@ -43,7 +47,7 @@ module.exports = function generateBrickContracts(dir, isProviderBricks) { const contractFiles = await globby( path.posix.join(dir, "contracts.log/*/*.contracts") ); - const contractRegExp = /(?<=@contract\s+)(?:[\w.]+)(?:@[\d.]+)?(?=\s+)/g; + const contractRegExp = globalContractRegExp; await Promise.all( contractFiles.map(async (filePath) => { const source = await fs.readFile(filePath, "utf-8"); @@ -51,7 +55,8 @@ module.exports = function generateBrickContracts(dir, isProviderBricks) { if (isProviderBricks) { if (contracts) { for (const item of contracts) { - const [contract, version] = item.split("@"); + const [_full, source, contract, version] = + item.match(singleContractRegExp); depsMap.set( `${pkgLastName}.${contract .split(".") @@ -60,6 +65,7 @@ module.exports = function generateBrickContracts(dir, isProviderBricks) { .join("-api-")}`, { type: "contract", + source: source || "sdk", contract, version: version || "*", } @@ -72,9 +78,11 @@ module.exports = function generateBrickContracts(dir, isProviderBricks) { depsMap.set( brick, contracts.map((item) => { - const [contract, version] = item.split("@"); + const [_full, source, contract, version] = + item.match(singleContractRegExp); return { type: "contract", + source: source || "sdk", contract, version: version || "*", }; diff --git a/packages/webpack-config-factory/src/bricks/ScanUseProviderHookContractsPlugin.js b/packages/webpack-config-factory/src/bricks/ScanUseProviderHookContractsPlugin.js index 9e283f7488..33d7d7200f 100644 --- a/packages/webpack-config-factory/src/bricks/ScanUseProviderHookContractsPlugin.js +++ b/packages/webpack-config-factory/src/bricks/ScanUseProviderHookContractsPlugin.js @@ -8,10 +8,12 @@ const validFlowApiProviderName = function gatherContractComments({ provider, brickName, contractsEntries }) { let contract = ""; + let source = "provider"; // Flow Api Provider if (provider.includes("@")) { contract = provider.replace("@", ".").replace(":", "@"); + source = "contract"; } else { // SDK API Provider const [namespace, apiFullName] = provider.split("."); @@ -22,7 +24,7 @@ function gatherContractComments({ provider, brickName, contractsEntries }) { service )}.${changeCase.snakeCase(model)}.${changeCase.pascalCase(apiName)}`; } - const comment = `/**! @contract ${contract} */`; + const comment = `/**! @contract ${contract} */`; const fileName = `${brickName}${contractFileExtName}`; const contracts = contractsEntries.get(fileName) || [];