Skip to content
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

feat: run prettier on migratedJSON #16471

Merged
merged 31 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d5fad16
feat: format migratedJSON
RahulGautamSingh Jul 7, 2022
bb906ee
Update package.json
RahulGautamSingh Jul 7, 2022
18d8cc2
Update yarn.lock
RahulGautamSingh Jul 7, 2022
e4ede92
Update yarn.lock
RahulGautamSingh Jul 7, 2022
77f7199
Merge branch 'main' into feat/run-prettier
RahulGautamSingh Jul 7, 2022
28d5a9f
add @types/prettier to dependencies list
RahulGautamSingh Jul 7, 2022
6fb990b
feat: skip formatting if options is null
RahulGautamSingh Jul 7, 2022
230b8dc
Update package.json
RahulGautamSingh Jul 7, 2022
3b63205
Update package.json
RahulGautamSingh Jul 7, 2022
14b52ff
revert changes to yarn.lock
RahulGautamSingh Jul 7, 2022
7dbac1b
Merge branch 'feat/run-prettier' of https://github.com/RahulGautamSin…
RahulGautamSingh Jul 7, 2022
321e23c
move prettier formatting
RahulGautamSingh Jul 7, 2022
a52633f
refactor: update tests
RahulGautamSingh Jul 7, 2022
7a4ce4d
refactor: rename var
RahulGautamSingh Jul 7, 2022
0b49cd9
refactor: add test + apply suggestions
RahulGautamSingh Jul 8, 2022
c78fae5
Merge branch 'main' into feat/run-prettier
RahulGautamSingh Jul 8, 2022
51c7150
refactor: add test to check working for invalid content
RahulGautamSingh Jul 8, 2022
afe9b3b
refactor: update test
RahulGautamSingh Jul 8, 2022
c26764f
update options
RahulGautamSingh Jul 9, 2022
859630d
apply suggestions
RahulGautamSingh Jul 10, 2022
e488ec7
add ident to prettier options
RahulGautamSingh Jul 11, 2022
ca8482d
update options
RahulGautamSingh Jul 11, 2022
1bcbb66
Update .editorconfig
RahulGautamSingh Jul 11, 2022
3b04d5b
revert format-change
RahulGautamSingh Jul 11, 2022
d93dd02
Merge branch 'feat/run-prettier' of https://github.com/RahulGautamSin…
RahulGautamSingh Jul 11, 2022
e23bb0d
Merge branch 'main' into feat/run-prettier
RahulGautamSingh Jul 11, 2022
397d077
Merge branch 'main' into feat/run-prettier
RahulGautamSingh Jul 13, 2022
393d21d
refactor: apply suggestions
RahulGautamSingh Jul 13, 2022
37e7341
add comment
RahulGautamSingh Jul 13, 2022
4e2d02a
Merge branch 'main' into feat/run-prettier
rarkins Jul 13, 2022
800d078
Merge branch 'main' into feat/run-prettier
rarkins Jul 14, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"filename": "renovate.json",
"content": "{\n \"extends\": [\n \":separateMajorReleases\",\n \":prImmediately\",\n \":renovatePrefix\",\n \":semanticPrefixFixDepsChoreOthers\",\n \":updateNotScheduled\",\n \":automergeDisabled\",\n \":maintainLockFilesDisabled\",\n \":autodetectPinVersions\",\n \"group:monorepos\"\n ],\n \"onboarding\": false,\n \"rangeStrategy\": \"replace\",\n \"semanticCommits\": \"enabled\",\n \"timezone\": \"US/Central\",\n \"baseBranches\": [\"main\"]\n}\n"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { mockedFunction } from '../../../../../test/util';

import { migrateConfig } from '../../../../config/migration';
import { readLocalFile } from '../../../../util/fs';
import { getFileList } from '../../../../util/git';
import { detectRepoFileConfig } from '../../init/merge';
import { MigratedDataFactory } from './migrated-data';
import { MigratedDataFactory, applyPrettierFormatting } from './migrated-data';

jest.mock('../../../../config/migration');
jest.mock('../../../../util/git');
jest.mock('../../../../util/fs');
jest.mock('../../init/merge');
jest.mock('detect-indent');
Expand All @@ -17,6 +19,9 @@ const rawNonMigratedJson5 = Fixtures.get('./renovate.json5');
const migratedData = Fixtures.getJson('./migrated-data.json');
const migratedDataJson5 = Fixtures.getJson('./migrated-data.json5');
const migratedConfigObj = Fixtures.getJson('./migrated.json');
const formattedMigratedData = Fixtures.getJson(
'./migrated-data-formatted.json'
);

describe('workers/repository/config-migration/branch/migrated-data', () => {
describe('MigratedDataFactory.getAsync', () => {
Expand All @@ -35,6 +40,7 @@ describe('workers/repository/config-migration/branch/migrated-data', () => {
isMigrated: true,
migratedConfig: migratedConfigObj,
});
mockedFunction(getFileList).mockResolvedValue([]);
});

it('Calls getAsync a first when migration not needed', async () => {
Expand Down Expand Up @@ -110,5 +116,38 @@ describe('workers/repository/config-migration/branch/migrated-data', () => {
MigratedDataFactory.reset();
await expect(MigratedDataFactory.getAsync()).resolves.toBeNull();
});

it('format and migrate a JSON config file', async () => {
mockedFunction(detectRepoFileConfig).mockResolvedValueOnce({
configFileName: 'renovate.json',
});
mockedFunction(readLocalFile).mockResolvedValueOnce(rawNonMigrated);
mockedFunction(getFileList).mockResolvedValue(['.prettierrc']);
MigratedDataFactory.reset();
await expect(MigratedDataFactory.getAsync()).resolves.toEqual(
formattedMigratedData
);
});

it('should not stop run for invalid package.json', async () => {
mockedFunction(detectRepoFileConfig).mockResolvedValueOnce({
configFileName: 'renovate.json',
});
mockedFunction(readLocalFile).mockResolvedValueOnce(rawNonMigrated);
mockedFunction(readLocalFile).mockResolvedValue('abci');
MigratedDataFactory.reset();
await expect(MigratedDataFactory.getAsync()).resolves.toEqual(
migratedData
);
});

it('return original content if its invalid', async () => {
await expect(
applyPrettierFormatting(`{"name":"Rahul"`, 'json', {
indent: ' ',
amount: 2,
})
).resolves.toBe(`{"name":"Rahul"`);
});
});
});
67 changes: 64 additions & 3 deletions lib/workers/repository/config-migration/branch/migrated-data.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,62 @@
import detectIndent from 'detect-indent';
import JSON5 from 'json5';
import prettier from 'prettier';
import { migrateConfig } from '../../../../config/migration';
import { logger } from '../../../../logger';
import { readLocalFile } from '../../../../util/fs';
import { getFileList } from '../../../../util/git';
import { detectRepoFileConfig } from '../../init/merge';
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved

export interface MigratedData {
content: string;
filename: string;
}
interface Indent {
amount: number;
indent: string;
type?: string;
}
export async function applyPrettierFormatting(
content: string,
parser: string,
indent: Indent
): Promise<string> {
const prettierConfigFilenames = [
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
'.prettierrc',
'.prettierrc.json',
'.prettierrc.yml',
'.prettierrc.yaml',
'.prettierrc.json5',
'.prettierrc.js',
'.prettierrc.cjs',
'prettier.config.js',
'prettier.config.cjs',
'.prettierrc.toml',
];
let prettierExists = (await getFileList()).some((file) =>
prettierConfigFilenames.includes(file)
);
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
if (!prettierExists) {
try {
const packageJsonContent = await readLocalFile('package.json', 'utf8');
prettierExists ||=
packageJsonContent && JSON.parse(packageJsonContent).prettier;
} catch {
logger.warn('Invalid JSON found in package.json');
}
}

if (!prettierExists) {
return content;
}
const options = {
parser: parser,
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
tabWidth: indent.amount,
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
useTabs: indent.type === 'tab',
};

return prettier.format(content, options);
}

export class MigratedDataFactory {
// singleton
Expand Down Expand Up @@ -52,15 +100,28 @@ export class MigratedDataFactory {

// indent defaults to 2 spaces
// TODO #7154
const indent = detectIndent(raw!).indent ?? ' ';
const indent = detectIndent(raw!);
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
let content: string;

if (filename.endsWith('.json5')) {
content = JSON5.stringify(migratedConfig, undefined, indent);
content = JSON5.stringify(
migratedConfig,
undefined,
indent.indent ?? ' '
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
);
} else {
content = JSON.stringify(migratedConfig, undefined, indent);
content = JSON.stringify(
migratedConfig,
undefined,
indent.indent ?? ' '
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
);
}

content = await applyPrettierFormatting(
content,
filename.endsWith('.json5') ? 'json5' : 'json',
indent
);
if (!content.endsWith('\n')) {
content += '\n';
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
"p-map": "4.0.0",
"p-queue": "6.6.2",
"parse-link-header": "2.0.0",
"prettier": "2.7.1",
viceice marked this conversation as resolved.
Show resolved Hide resolved
"redis": "4.1.1",
"remark": "13.0.0",
"remark-github": "10.1.0",
Expand Down Expand Up @@ -291,7 +292,6 @@
"mockdate": "3.0.5",
"nock": "13.2.8",
"npm-run-all": "4.1.5",
"prettier": "2.7.1",
"pretty-quick": "3.1.3",
"rimraf": "3.0.2",
"semantic-release": "19.0.3",
Expand Down