Skip to content

Commit

Permalink
fix(format): skip .repository when its .directory is present
Browse files Browse the repository at this point in the history
Closes #91
Closes #93
Refs #100
  • Loading branch information
jodyheavener authored and JamieMason committed Oct 28, 2022
1 parent c6db878 commit 688bc0c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"Aparajita Fishman (https://github.com/aparajita)",
"Artur Wierzbicki (https://github.com/ArturWierzbicki)",
"Jamie Mason (https://github.com/JamieMason)",
"Jody Heavener (https://github.com/jodyheavener)",
"Luis Vieira (https://github.com/luisvieiragmr)",
"Marais Rossouw (https://github.com/maraisr)",
"Matt Sprague (https://github.com/uforic)",
Expand Down
21 changes: 21 additions & 0 deletions src/bin-format/format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ describe('format', () => {
expect.stringContaining(normalize('some/package.json')),
);
});
it('retains long form format for "repository" when directory property used', () => {
const disk = mockDisk();
const before = {
repository: {
url: 'git://gitlab.com/User/repo',
type: 'git',
directory: 'packages/foo',
},
};
const input = {
...DEFAULT_CONFIG,
wrappers: [createWrapper(before)],
} as ProgramInput;
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
format(input, disk);
expect(disk.writeFileSync).not.toHaveBeenCalledWith();
expect(log).toHaveBeenCalledWith(
expect.stringMatching(/-/),
expect.stringContaining(normalize('some/package.json')),
);
});
it('uses github shorthand format for "repository"', () => {
const disk = mockDisk();
const before = {
Expand Down
14 changes: 8 additions & 6 deletions src/bin-format/format.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray } from 'expect-more';
import { isArray, isNonEmptyString } from 'expect-more';
import type { Disk } from '../lib/disk';
import type { ProgramInput } from '../lib/get-input';
import type { Source } from '../lib/get-input/get-wrappers';
Expand All @@ -10,18 +10,20 @@ export function format(input: ProgramInput, disk: Disk): void {
wrappers.forEach(({ contents, filePath, json }) => {
const sortedKeys = Object.keys(contents).sort();
const keys = new Set<string>(sortFirst.concat(sortedKeys));

const optionalChaining: any = contents;
const bugsUrl = optionalChaining?.bugs?.url;
const repositoryUrl = optionalChaining?.repository?.url;
const repoUrl = optionalChaining?.repository?.url;
const repoDir = optionalChaining?.repository?.directory;

if (bugsUrl) {
contents.bugs = bugsUrl;
}

if (repositoryUrl) {
contents.repository = repositoryUrl.includes('github.com')
? repositoryUrl.replace(/^.+github\.com\//, '')
: repositoryUrl;
if (isNonEmptyString(repoUrl) && !isNonEmptyString(repoDir)) {
contents.repository = repoUrl.includes('github.com')
? repoUrl.replace(/^.+github\.com\//, '')
: repoUrl;
}

sortAz.forEach((key) => sortAlphabetically(contents[key]));
Expand Down
2 changes: 1 addition & 1 deletion src/lib/get-input/get-wrappers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface Source {
pnpm?: {
overrides?: Record<string, string>;
};
repository?: { type: string; url: string } | string;
repository?: { directory?: string; type: string; url: string } | string;
resolutions?: Record<string, string>;
scripts?: Record<string, string>;
version?: string;
Expand Down

0 comments on commit 688bc0c

Please sign in to comment.