Skip to content

Commit

Permalink
chore: add optional OTP option to release script
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Sep 19, 2023
1 parent da67b4c commit 6a505a2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
5 changes: 4 additions & 1 deletion scripts/npm-utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { exec } from './child-process.mjs';
* @param {{ cwd: String, dryRun: Boolean}} options
* @returns {Promise<any>}
*/
export function publishPackage(publishTagName, { cwd, dryRun }) {
export function publishPackage(publishTagName, { cwd, otp, dryRun }) {
const execArgs = ['publish'];
if (publishTagName) {
execArgs.push('--tag', publishTagName);
}
if (otp) {
execArgs.push('--otp', otp);
}
if (dryRun) {
execArgs.push('--dry-run');
}
Expand Down
22 changes: 18 additions & 4 deletions scripts/release.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ const pkg = loadJsonFileSync(path.join(__dirname, '../', 'package.json'));
} else if (whichBumpType.includes('beta')) {
publishTagName = 'beta';
}
await publishPackage(publishTagName, { cwd, dryRun: argv.dryRun });
let otp = '';
if (await promptConfirmation(`${c.bgMagenta(dryRunPrefix)} Do you have OTP (One-Time-Password) enabled?`, undefined, 1)) {
otp = await promptOtp();
}
await publishPackage(publishTagName, { cwd, otp, dryRun: argv.dryRun });
console.log(`${c.bgMagenta(dryRunPrefix)} 📦 Published to NPM - 🔗 https://www.npmjs.com/package/${pkg.name}`.trim())
}

Expand Down Expand Up @@ -264,7 +268,9 @@ async function promptConfirmation(message, choices, defaultIndex) {
{ key: 'y', name: 'Yes', value: true },
{ key: 'n', name: 'No', value: false },
];
defaultIndex = 0;
if (defaultIndex === undefined) {
defaultIndex = 0;
}
}

// display propmpt message and choices
Expand All @@ -274,10 +280,18 @@ async function promptConfirmation(message, choices, defaultIndex) {
}

// get and process input
const input = await getConsoleInput("Enter value " + " (default " + (defaultIndex + 1) + ') ');
const input = await getConsoleInput(`Enter value (default ${(defaultIndex + 1)}) `);
var index = !isNaN(input) && !isNaN(parseFloat(input)) ? +input - 1 : defaultIndex;
if (index < 0 || index >= choices.length) {
throw Error('The input ' + input + ' could not be matched to a selection');
throw Error(`The input ${input} could not be matched to a selection`);
}
return choices[index].value;
}

async function promptOtp() {
const otp = await getConsoleInput(`Enter OTP value:`);
if (otp.length < 6) {
throw new Error('OTP must be 6 digits.');
}
return otp;
}

0 comments on commit 6a505a2

Please sign in to comment.