From fcbb62abd535b92bebf41615011c3ecd6dce396d Mon Sep 17 00:00:00 2001 From: Dave Date: Wed, 26 Nov 2025 14:46:22 +0000 Subject: [PATCH] Try adding shrinkwrap to published packages Need to patch the version in the shrinkwrap files to make sure they exist. Plus updated the devcontainer to ensure npm is at the latest so it can be used for caliper development Signed-off-by: Dave --- .build/publish-caliper.sh | 13 +++---- .devcontainer/devcontainer.json | 4 +- packages/caliper-publish/lib/impl/npm.js | 49 ++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/.build/publish-caliper.sh b/.build/publish-caliper.sh index 91448ceee..d0e521713 100755 --- a/.build/publish-caliper.sh +++ b/.build/publish-caliper.sh @@ -21,14 +21,11 @@ cp ./README.md ./packages/caliper-cli/README.md cp ./README.md ./packages/caliper-core/README.md cp ./README.md ./packages/caliper-fabric/README.md -# distribute root package-lock file as npm-shrinkwrap file to packages before publishing -# We can't do this because caliper-cli declares dependencies on caliper-core and caliper-fabric -# and the packaged shrinkwrap for caliper-cli will not be correct to resolve them as package-lock.json -# is mono-repo aware but the published packages won't understand it in shrinkwrap.json so will create -# invalid references. Doing this would force everyone to have to install all 3 packages manually. -#cp ./package-lock.json ./packages/caliper-cli/npm-shrinkwrap.json -#cp ./package-lock.json ./packages/caliper-core/npm-shrinkwrap.json -#cp ./package-lock.json ./packages/caliper-fabric/npm-shrinkwrap.json +# copy package-lock.json to the individual package dirs as an npm-shrinkwrap.json +# they will be patched with the correct version before publishing to npm +cp ./package-lock.json ./packages/caliper-cli/npm-shrinkwrap.json +cp ./package-lock.json ./packages/caliper-core/npm-shrinkwrap.json +cp ./package-lock.json ./packages/caliper-fabric/npm-shrinkwrap.json cd ./packages/caliper-publish/ npm ci diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 7e7be2b96..9c5721036 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -20,7 +20,7 @@ ] } }, - "postCreateCommand": "cd docs && pip install -r pip-requirements.txt && mkdocs build", + "postCreateCommand": "npm install -g npm && cd docs && pip install -r pip-requirements.txt && mkdocs build", "remoteUser": "vscode", - "forwardPorts": [8000] + "forwardPorts": [8000] } \ No newline at end of file diff --git a/packages/caliper-publish/lib/impl/npm.js b/packages/caliper-publish/lib/impl/npm.js index be29da247..57bf181a5 100644 --- a/packages/caliper-publish/lib/impl/npm.js +++ b/packages/caliper-publish/lib/impl/npm.js @@ -60,6 +60,54 @@ function injectCustomVersion(packageJsonPath, customVersion) { fs.writeFileSync(packageJsonPath, JSON.stringify(packageObject, null, 4)); } +/** + * Utility function for overwriting the common Caliper version in the package.json files. + * @param {string} shrinkwrapPath The path of the package.json file. + * @param {string} customVersion The new version to use. + */ +function injectCustomShrinkwrapVersion(shrinkwrapPath, customVersion) { + + /** + * Update dependency references starting with "@hyperledger/caliper-" + * @param {object} deps the dependency object + * @param {string} newVersion the new version + */ + function updateDependencies(deps, newVersion) { + if (!deps) { + return; + } + for (const depName in deps) { + if (depName.startsWith('@hyperledger/caliper-')) { + deps[depName] = newVersion; + } + } + } + if (!fs.existsSync(shrinkwrapPath)) { + return; + } + + const shrinkwrap = JSON.parse(fs.readFileSync(shrinkwrapPath, 'utf8')); + shrinkwrap.version = customVersion; + + // Update packages starting with "packages/caliper-" + if (shrinkwrap.packages) { + shrinkwrap.packages[''].version = customVersion; + for (const pkgName in shrinkwrap.packages) { + console.log(pkgName); + if (pkgName.startsWith('packages/caliper-')) { + if (shrinkwrap.packages[pkgName].version) { + shrinkwrap.packages[pkgName].version = customVersion; + updateDependencies(shrinkwrap.packages[pkgName].dependencies, customVersion); + } + } + } + } + + // serialize new content + fs.writeFileSync(shrinkwrapPath, JSON.stringify(shrinkwrap, null, 4)); +} + + /** * Implements the docker publish command logic. */ @@ -104,6 +152,7 @@ class NPM { if (tag === 'unstable') { injectCustomVersion(path.join(packageDir, 'package.json'), packageVersion); } + injectCustomShrinkwrapVersion(path.join(packageDir, 'npm-shrinkwrap.json'), packageVersion); try { let published = false;