Skip to content

Commit

Permalink
chore(publish): fix timeout problem without lerna
Browse files Browse the repository at this point in the history
  • Loading branch information
sedghi committed Sep 26, 2023
1 parent 1f90696 commit 7d92738
Showing 1 changed file with 55 additions and 16 deletions.
71 changes: 55 additions & 16 deletions publish-package.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
import { execa } from 'execa';
import fs from 'fs/promises';
import glob from 'glob';
import path from 'path';

const MAX_RETRIES = 3;
const RETRY_DELAY = 10000; // 10 seconds

async function run() {
const { stdout: branchName } = await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD']);

// using the environment variable NPM_TOKEN, create a .npmrc file
// and set the token to the value of the environment variable
// Publishing each package, if on master/main branch publish beta versions
// otherwise publish latest
if (branchName === 'release') {
await execa('npx', ['lerna', 'publish', 'from-package', '--no-verify-access', '--yes']);
} else {
await execa('npx', [
'lerna',
'publish',
'from-package',
'--no-verify-access',
'--yes',
'--dist-tag',
'beta',
]);
const lernaJson = JSON.parse(await fs.readFile('lerna.json', 'utf8'));

const packages = lernaJson.packages;

const rootDir = process.cwd();

for (const packagePathPattern of packages) {
const matchingDirectories = glob.sync(packagePathPattern);

for (const packageDirectory of matchingDirectories) {
// change back to the root directory
process.chdir(rootDir);

const packageJsonPath = path.join(packageDirectory, 'package.json');

try {
const packageJsonContent = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));

if (packageJsonContent.private) {
console.log(`Skipping private package at ${packageDirectory}`);
continue;
}

process.chdir(packageDirectory);

let retries = 0;
while (retries < MAX_RETRIES) {
try {
const publishArgs = ['publish'];

if (branchName === 'master') {
publishArgs.push('--tag', 'beta');
}

await execa('npm', publishArgs);
console.log(`Successfully published package at ${packageDirectory}`);
break;
} catch (error) {
retries++;
console.error(
`Failed to publish package at ${packageDirectory} with error ${error}, retrying... (${retries}/${MAX_RETRIES})`
);
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));
}
}
} catch (error) {
console.error(`An error occurred while processing ${packageDirectory}: ${error}`);
}
}
}

console.log('Finished');
Expand Down

0 comments on commit 7d92738

Please sign in to comment.