Gracefully handle not having a package.json at git root (#161)
* Gracefully handle not having a package.json at git root * Change files Co-authored-by: Abdelmoumen Bouabdallah <boabdelm@microsoft.com> Co-authored-by: Elizabeth Craig <elcraig@microsoft.com>
This commit is contained in:
Родитель
e74753a50e
Коммит
5577a58d53
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"type": "patch",
|
||||
"comment": "Gracefully handle not having a package.json at git root",
|
||||
"packageName": "workspace-tools",
|
||||
"email": "boabdelm@microsoft.com",
|
||||
"dependentChangeType": "patch"
|
||||
}
|
|
@ -11,7 +11,7 @@ describe("getDefaultRemote", () => {
|
|||
gitFailFast(["remote", ...args], { cwd, noExitCode: true });
|
||||
}
|
||||
|
||||
function expectConsole(n: number, message: string) {
|
||||
function expectConsole(n: number, message: string | RegExp) {
|
||||
expect(consoleMock.mock.calls.length).toBeGreaterThanOrEqual(n);
|
||||
expect(consoleMock.mock.calls[n - 1].join(" ")).toMatch(message);
|
||||
}
|
||||
|
@ -35,9 +35,11 @@ describe("getDefaultRemote", () => {
|
|||
expect(() => getDefaultRemote({ cwd: os.tmpdir(), strict: true })).toThrow("is not in a git repository");
|
||||
});
|
||||
|
||||
it("throws if package.json not found", () => {
|
||||
it("handles no package.json at git root", () => {
|
||||
cwd = setupFixture();
|
||||
expect(() => getDefaultRemote({ cwd })).toThrow(/Could not read .*package\.json/);
|
||||
expect(getDefaultRemote({ cwd, verbose: true })).toBe("origin");
|
||||
expectConsole(1, /Could not read .*package\.json/);
|
||||
|
||||
expect(() => getDefaultRemote({ cwd, strict: true })).toThrow(/Could not read .*package\.json/);
|
||||
});
|
||||
|
||||
|
|
|
@ -53,7 +53,7 @@ export function getDefaultRemote(cwdOrOptions: string | GetDefaultRemoteOptions)
|
|||
try {
|
||||
packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8").trim());
|
||||
} catch (e) {
|
||||
throw new Error(`Could not read "${packageJsonPath}"`);
|
||||
logOrThrow(`Could not read "${packageJsonPath}"`);
|
||||
}
|
||||
|
||||
const { repository } = packageJson;
|
||||
|
|
Загрузка…
Ссылка в новой задаче