Skip to content

Commit 7a78d1f

Browse files
committed
feat: remove opt
1 parent 015bc8b commit 7a78d1f

File tree

4 files changed

+14
-90
lines changed

4 files changed

+14
-90
lines changed

src/cli.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ Options
4545
--no-publish Skips publishing
4646
--build Run build script before publishing (You can also use "prepublishOnly")
4747
--no-tag-merge When publishing multiple packages, each package has its own independent tag and commit
48-
--otp This is a one-time password from a two-factor authenticator
4948
--no-release-draft Skips opening a GitHub release draft
5049
--dry-run Don't actually release, just simulate the process
5150
--only-publish Only publish the current package
@@ -125,9 +124,6 @@ Options
125124
releaseDraft: {
126125
type: 'boolean',
127126
},
128-
otp: {
129-
type: 'string',
130-
},
131127
dryRun: {
132128
type: 'boolean',
133129
},
@@ -214,13 +210,15 @@ else {
214210
if (e.code === ReleaseErrorCode.WARNING || e.code === ReleaseErrorCode.EXIT) {
215211
logger.warning(msg);
216212
}
213+
else if (e.code === ReleaseErrorCode.ROLLBACK) {
214+
logger.error(msg);
215+
await resetGitSubmit(getOptions() as any);
216+
}
217217
else {
218218
logger.error(msg);
219219
}
220220
}
221221

222-
await resetGitSubmit(getOptions() as any);
223-
224222
if (cliOpts.verbose) {
225223
console.log();
226224
console.log();

src/git.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -312,24 +312,12 @@ export async function resetGitSubmit(opts: ReleaseOptions) {
312312

313313
// remote git tag
314314
for (const pkg of pkgs) {
315-
const tag = getGitTagVersion(pkg.tagName, pkg.version, opts);
315+
const tag = getGitTagVersion(pkg.tagName, pkg.newVersion, opts);
316316
const res = await run(`git tag -l ${tag}`);
317317
if (res) {
318318
await run(`git tag -d ${tag}`);
319319
}
320320
}
321321

322-
// reset git commit
323-
const res = await run(`git --no-pager log -n ${pkgs.length} --pretty="format:%h"`);
324-
const shaList = res
325-
.split('\n')
326-
.map(s => s.trim())
327-
.filter(s => s);
328-
329-
for (const sha of shaList) {
330-
if (sha === gitSHA) {
331-
return;
332-
}
333-
await run(`git reset --hard ${sha}`);
334-
}
322+
await run(`git reset --hard HEAD~1`);
335323
}

src/npm.ts

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { PackageManager } from '@tomjs/pkg';
2-
import type { NpmInfo, PackageInfo, ReleaseOptions } from './types';
2+
import type { NpmInfo, PackageInfo } from './types';
33
import fs from 'node:fs';
44
import path from 'node:path';
55
import inquirer from 'inquirer';
@@ -106,46 +106,6 @@ export function updatePackageVersion(pkg: PackageInfo) {
106106
fs.writeFileSync(jsonPath, `${JSON.stringify(json, null, 2)}\n`);
107107
}
108108

109-
async function getTokenIsRequired() {
110-
try {
111-
const result = await run('npm profile get --json', {
112-
env: getNpmEnv(),
113-
});
114-
115-
const json = JSON.parse(result);
116-
if (json.error || !json.tfa || !json.tfa.mode) {
117-
return false;
118-
}
119-
return json.tfa.mode === 'auth-and-writes';
120-
}
121-
catch (e: any) {
122-
logger.error('Error while checking if token is required', e.message);
123-
return false;
124-
}
125-
}
126-
127-
export function getTwoFactorState(opts: ReleaseOptions): TwoFactorState {
128-
const { otp, pkgs } = opts;
129-
if (otp) {
130-
return {
131-
token: otp,
132-
isRequired: Promise.resolve(true),
133-
};
134-
}
135-
136-
if (pkgs.some(pkg => pkg.registry !== NPM_REGISTRY)) {
137-
return {
138-
token: null,
139-
isRequired: Promise.resolve(false),
140-
};
141-
}
142-
143-
return {
144-
token: null,
145-
isRequired: getTokenIsRequired(),
146-
};
147-
}
148-
149109
// const otpAskLimit = pLimit(1);
150110
async function askForOtpCode(twoFactorState: TwoFactorState) {
151111
if (twoFactorState.token !== null)

src/publish.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { TwoFactorState } from './npm';
21
import type { PackageInfo, ReleaseOptions } from './types';
32
import chalk from 'chalk';
43
import { execa } from 'execa';
54
import open from 'open';
65
import semver from 'semver';
6+
import { ReleaseError } from './error';
77
import {
88
getGitTagVersion,
99
getGitTagVersionRelease,
@@ -12,7 +12,7 @@ import {
1212
releaseNotes,
1313
} from './git';
1414
import { logger } from './logger';
15-
import { getNpmEnv, getOtpCode, getTwoFactorState, updatePackageVersion } from './npm';
15+
import { getNpmEnv, updatePackageVersion } from './npm';
1616
import { run } from './utils';
1717

1818
export async function runPublish(opts: ReleaseOptions) {
@@ -56,14 +56,13 @@ async function bumpVersionAndTag(opts: ReleaseOptions) {
5656

5757
async function runPublishPackages(opts: ReleaseOptions) {
5858
const { pkgs, dryRun, packageManager: pm } = opts;
59-
const twoFactorState = getTwoFactorState(opts);
6059

6160
if (opts.publish) {
6261
for (const pkg of pkgs) {
6362
if (!dryRun && opts.build && pkg.packageJson?.scripts?.build) {
6463
await run(`${pm.cli} run build`, { cwd: pkg.dir });
6564
}
66-
await publishOnePackage(pkg, opts, twoFactorState);
65+
await publishOnePackage(pkg, opts);
6766
const tag = `${pkg.name}@${pkg.newVersion}`;
6867
logger.success(`Publish ${chalk.green(tag)} successfully 🎉`);
6968
}
@@ -85,7 +84,6 @@ async function runPublishPackages(opts: ReleaseOptions) {
8584
async function publishOnePackage(
8685
pkg: PackageInfo,
8786
opts: ReleaseOptions,
88-
twoFactorState: TwoFactorState,
8987
) {
9088
const { packageManager: pm, dryRun } = opts;
9189
const args: string[] = ['publish', '--access', pkg.access, '--tag', pkg.tag];
@@ -98,10 +96,6 @@ async function publishOnePackage(
9896
args.push(`---new-version`, pkg.newVersion);
9997
}
10098

101-
if (await twoFactorState.isRequired) {
102-
await getOtpCode(twoFactorState);
103-
}
104-
10599
if (dryRun && pm.cli !== 'yarn') {
106100
args.push('--dry-run');
107101
}
@@ -111,15 +105,7 @@ async function publishOnePackage(
111105
cli = 'yarn npm';
112106
}
113107

114-
const getArgs = () => {
115-
const otp = twoFactorState.token;
116-
if (otp) {
117-
return [...args, '--otp', otp];
118-
}
119-
return [...args];
120-
};
121-
122-
const cmd = [cli].concat(getArgs()).join(' ');
108+
const cmd = [cli].concat(args).join(' ');
123109
logger.debug(cmd);
124110

125111
if (dryRun && pm.cli === 'yarn') {
@@ -128,21 +114,13 @@ async function publishOnePackage(
128114
}
129115

130116
try {
131-
await runNpmPublish(cli, getArgs(), pkg);
132-
twoFactorState.tryAgain = false;
117+
await runNpmPublish(cli, args, pkg);
133118
}
134119
catch (e: any) {
135120
if (e && needOtp(e.message)) {
136-
if (twoFactorState.token !== null) {
137-
// the current otp code must be invalid since it errored
138-
twoFactorState.token = null;
139-
}
140-
twoFactorState.tryAgain = true;
141-
await publishOnePackage(pkg, opts, twoFactorState);
142-
}
143-
else {
144-
throw e;
121+
logger.error(chalk.red('Please use "Access Tokens -> Security settings" to bypass 2FA"'));
145122
}
123+
ReleaseError.rollback(e.message);
146124
}
147125
}
148126

0 commit comments

Comments
 (0)