-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed an issue with parsing
--json
output when publishing (#676)
* Fixed an issue with parsing `--json` output when publishing * Be more defensive about the `getLastJsonObjectFromString` return value
- Loading branch information
Showing
5 changed files
with
129 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@changesets/cli": patch | ||
--- | ||
|
||
Improved compatibility with npm 7+ since they've started to print errors to the `stderr` (where previously they were printed to `stdout`) when using `npm publish --json`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@changesets/cli": patch | ||
--- | ||
|
||
Fixed an internal issue that prevented `npm publish --json`'s output to be handled properly. This makes sure that unrelated JSONs printed by lifecycle scripts don't interfere with our logic. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/cli/src/utils/getLastJsonObjectFromString.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { getLastJsonObjectFromString } from "./getLastJsonObjectFromString"; | ||
|
||
describe("getLastJsonObjectFromString", () => { | ||
it("should handle stringified object", () => { | ||
expect( | ||
getLastJsonObjectFromString(JSON.stringify({ test: "foo" })) | ||
).toEqual({ test: "foo" }); | ||
}); | ||
|
||
it("should handle stringified deep object", () => { | ||
expect( | ||
getLastJsonObjectFromString( | ||
JSON.stringify({ | ||
test: "foo", | ||
bar: { baz: { qwe: "rty" }, arr: [1, 2, 3, 4] } | ||
}) | ||
) | ||
).toEqual({ | ||
test: "foo", | ||
bar: { baz: { qwe: "rty" }, arr: [1, 2, 3, 4] } | ||
}); | ||
}); | ||
|
||
it("should handle leading whitespace", () => { | ||
expect( | ||
getLastJsonObjectFromString( | ||
` \n\n ${JSON.stringify({ test: "foo", baz: { qwe: "rty" } })}` | ||
) | ||
).toEqual({ test: "foo", baz: { qwe: "rty" } }); | ||
}); | ||
|
||
it("should handle trailing whitespace", () => { | ||
expect( | ||
getLastJsonObjectFromString( | ||
`${JSON.stringify({ test: "foo", baz: { qwe: "rty" } })} \n\n ` | ||
) | ||
).toEqual({ test: "foo", baz: { qwe: "rty" } }); | ||
}); | ||
|
||
it("should handle trailing text", () => { | ||
expect( | ||
getLastJsonObjectFromString( | ||
`${JSON.stringify({ test: "foo", baz: { qwe: "rty" } })} \n\n test` | ||
) | ||
).toEqual({ test: "foo", baz: { qwe: "rty" } }); | ||
}); | ||
|
||
it("should handle string with multiple objects", () => { | ||
expect( | ||
getLastJsonObjectFromString( | ||
`${JSON.stringify({ | ||
test: "foo", | ||
baz: { qwe: "rty" } | ||
})} \n\n ${JSON.stringify({ much: "awesome" })}` | ||
) | ||
).toEqual({ much: "awesome" }); | ||
}); | ||
|
||
it("should return `null` for an empty string", () => { | ||
expect(getLastJsonObjectFromString("")).toEqual(null); | ||
}); | ||
|
||
it("should return `null` for a string with a broken object", () => { | ||
expect(getLastJsonObjectFromString(`{"bar:"`)).toEqual(null); | ||
}); | ||
|
||
it("should return `null` for a string without an object", () => { | ||
expect(getLastJsonObjectFromString(`qwerty`)).toEqual(null); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const getLastJsonObjectFromString = (str: string) => { | ||
str = str.replace(/[^}]*$/, ""); | ||
|
||
while (str) { | ||
str = str.replace(/[^{]*/, ""); | ||
|
||
try { | ||
return JSON.parse(str); | ||
} catch (err) { | ||
// move past the potentially leading `{` so the regexp in the loop can try to match for the next `{` | ||
str = str.slice(1); | ||
} | ||
} | ||
return null; | ||
}; |