-
-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite publish logic #132
Changes from 16 commits
9cda15e
5911d3f
1fd6a5b
ab1fcd5
df51c83
af47f04
1d4852d
dee2d17
b06f1a7
cf9f1ae
d3153e1
a16919e
d8ef281
9baec4c
01af708
567b247
1b9ebd2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -359,7 +359,7 @@ have published it.\ | |||||
this.spawn(gitCommand, ['commit', '-m', message], (code, stderr, stdout) => { | ||||||
stderr ??= ''; | ||||||
stdout ??= ''; | ||||||
if (code === 0) { | ||||||
if (code !== 0) { | ||||||
this.logFailure(); | ||||||
const commitOutput = `${stdout}\n${stderr}`.trim(); | ||||||
reject(`Failed to commit package.json: ${commitOutput}`); | ||||||
|
@@ -417,11 +417,24 @@ have published it.\ | |||||
|
||||||
// Run the publish command with the given options | ||||||
async run(options) { | ||||||
let pack; | ||||||
let pack, originalName; | ||||||
options = this.parseOptions(options.commandArgs); | ||||||
let {tag, rename} = options.argv; | ||||||
let [version] = options.argv._; | ||||||
|
||||||
// Normalize variables to ensure they are strings with zero length | ||||||
// rather than their default `undefined` | ||||||
// This ensures later length checks always behave as expected | ||||||
if (typeof tag !== "string") { | ||||||
tag = ""; | ||||||
} | ||||||
if (typeof version !== "string") { | ||||||
version = ""; | ||||||
} | ||||||
if (typeof rename !== "string") { | ||||||
rename = ""; | ||||||
} | ||||||
|
||||||
try { | ||||||
pack = this.loadMetadata(); | ||||||
} catch (error) { | ||||||
|
@@ -440,56 +453,71 @@ have published it.\ | |||||
return error; | ||||||
} | ||||||
|
||||||
if ((version?.length > 0) || (rename?.length > 0)) { | ||||||
let originalName; | ||||||
if (version?.length <= 0) { version = 'patch'; } | ||||||
if (rename?.length > 0) { originalName = pack.name; } | ||||||
if (version?.length === 0 && tag?.length === 0 && rename?.length === 0) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be a benefit to know this is somehow still ending up Rather than end up silently doing some tricky comparisons of
Suggested change
Sort of theoretical, since it shouldn't be possible. But also, I like to avoid more complicated syntax if it's not needed, for ease of reading. Also: Given the head-scratcher this issue was to solve, correctness and surfacing error messages in as straight-forward a way as possible is kind of my top priority, rather than avoiding errors in this case. Unless we want to try/catch and print the error and exit gracefully, but... I don't think that helps anything anyway in this case. tl;dr: Hitting a simple error message would be better than not hitting it, in this case, I think? So, optional chaining not a benefit here? Not a blocker, since it should all be hypothetical with all the normalizing done in this PR. But the goose-chase tracking down strange behavior makes me a tad paranoid, not gonna lie. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for all the nitpicking, just inclined to make this code very tidy if possible after what had to be thought through to get here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: If moving away from optional chaining for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P.S. I suppose these instances of optional chaining mirror what was there before this PR, but while we're addressing tiny subtleties of behavior that were unintuitive, I think it's worth deciding whether to move away from the optional chaining or not. |
||||||
return "A version, tag, or new package name is required"; | ||||||
} | ||||||
|
||||||
if (rename?.length > 0) { | ||||||
// A version isn't required when renaming a package (apparently) | ||||||
if (version?.length === 0) { version = "patch"; } | ||||||
originalName = pack.name; | ||||||
|
||||||
let firstTimePublishing; | ||||||
let tag; | ||||||
try { | ||||||
firstTimePublishing = await this.registerPackage(pack); | ||||||
await this.renamePackage(pack, rename); | ||||||
} catch(error) { | ||||||
return error; | ||||||
} | ||||||
} | ||||||
|
||||||
// Now we know a version has been specified, and that we have settled any | ||||||
// rename concerns. Lets get to publication | ||||||
|
||||||
if (tag?.length === 0) { | ||||||
// only create and assign a tag if the user didn't specify one. | ||||||
// if they did we assume that tag already exists and only work to publish | ||||||
try { | ||||||
tag = await this.versionPackage(version); | ||||||
await this.pushVersion(tag, pack); | ||||||
} catch (error) { | ||||||
} catch(error) { | ||||||
return error; | ||||||
} | ||||||
|
||||||
await this.waitForTagToBeAvailable(pack, tag); | ||||||
if (originalName != null) { | ||||||
// If we're renaming a package, we have to hit the API with the | ||||||
// current name, not the new one, or it will 404. | ||||||
rename = pack.name; | ||||||
pack.name = originalName; | ||||||
} | ||||||
} | ||||||
|
||||||
let doesPackageExist; | ||||||
try { | ||||||
doesPackageExist = await this.packageExists(pack.name); | ||||||
} catch(error) { | ||||||
return error; | ||||||
} | ||||||
|
||||||
if (doesPackageExist) { | ||||||
// This is an existing package we just want to publish a new version of | ||||||
try { | ||||||
await this.publishPackage(pack, tag, {rename}); | ||||||
} catch (error) { | ||||||
if (firstTimePublishing) { | ||||||
this.logFirstTimePublishMessage(pack); | ||||||
if (originalName != null) { | ||||||
// If we're renaming a package, we have to hit the API with the | ||||||
// current name, not the new one, or it will 404. | ||||||
rename = pack.name; | ||||||
pack.name = originalName; | ||||||
await this.publishPackage(pack, tag, {rename}); | ||||||
} else { | ||||||
await this.publishPackage(pack, tag); | ||||||
} | ||||||
return error; | ||||||
} | ||||||
} else if (tag?.length > 0) { | ||||||
let firstTimePublishing; | ||||||
try { | ||||||
firstTimePublishing = await this.registerPackage(pack); | ||||||
} catch (error) { | ||||||
} catch(error) { | ||||||
return error; | ||||||
} | ||||||
|
||||||
} else { | ||||||
// This is a brand new package we want to publish for the first time | ||||||
try { | ||||||
await this.publishPackage(pack, tag); | ||||||
} catch (error) { | ||||||
if (firstTimePublishing) { | ||||||
this.logFirstTimePublishMessage(pack); | ||||||
} | ||||||
await this.registerPackage(pack); | ||||||
} catch(error) { | ||||||
return error; | ||||||
} | ||||||
} else { | ||||||
return 'A version, tag, or new package name is required'; | ||||||
|
||||||
this.logFirstTimePublishMessage(pack); | ||||||
} | ||||||
|
||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just commentary, no action needed:
Kind of odd to see that this check was coded wrong from before this PR, I believe it was inadvertently changed as part of the utterly massive promisification PRs, from looking at the blame history.
This change seems legit. All other calls to
logFailure()
are whencode !== 0
, as in error code rather than clean exit code. So yeah, seems legit IMO.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was surprised to see this one broken for so long tbh