From 757dfd8f917173aaac4c5d85bb33de79bad4e532 Mon Sep 17 00:00:00 2001 From: Maurizio Casimirri Date: Tue, 15 Sep 2020 12:37:27 +0200 Subject: [PATCH] chore: check user permissions to publish (#327) --- scripts/publish-npm.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/scripts/publish-npm.js b/scripts/publish-npm.js index 24814b6ac..78fa954fa 100644 --- a/scripts/publish-npm.js +++ b/scripts/publish-npm.js @@ -67,8 +67,32 @@ function markTaskAsDone(task, releaseDirPath) { fs.writeFileSync(taskDoneFilePath(task, releaseDirPath), ''); } +function checkUserPermissionsToPublish() { + const lerna = path.resolve(rootPath, 'node_modules', '.bin', 'lerna'); + + const userPackagesAccess = JSON.parse(execSync(`npm access ls-packages`).toString().trim()) + const lernaPackages = JSON.parse(execFileSync(lerna, + ['list', '--json', '--no-private', '--loglevel=error']).toString().trim()); + + const missingAccess = []; + + for (const repo of lernaPackages) { + if (userPackagesAccess[repo.name] !== 'read-write') { + missingAccess.push(repo.name); + } + } + + if (missingAccess.length) { + throw new Error( + `Required write access missing for the following packages: ${missingAccess.join(', ')}` + ); + } +} + async function publish() { const segmentApiKey = requireSegmentApiKey(); + checkUserPermissionsToPublish(); + const remoteUrl = getGitRemoteUrl(); const remoteHeadSha = getGitRemoteHeadSHA(remoteUrl); const releaseDirPath = path.resolve(rootPath, 'tmp', 'releases', remoteHeadSha);