-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathmigrated-data.ts
135 lines (117 loc) · 3.32 KB
/
migrated-data.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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';
export interface MigratedData {
content: string;
filename: string;
}
interface Indent {
amount: number;
indent: string;
type?: string;
}
const prettierConfigFilenames = new Set([
'.prettierrc',
'.prettierrc.json',
'.prettierrc.yml',
'.prettierrc.yaml',
'.prettierrc.json5',
'.prettierrc.js',
'.prettierrc.cjs',
'prettier.config.js',
'prettier.config.cjs',
'.prettierrc.toml',
]);
export async function applyPrettierFormatting(
content: string,
parser: string,
indent: Indent
): Promise<string> {
const fileList = await getFileList();
let prettierExists = fileList.some((file) =>
prettierConfigFilenames.has(file)
);
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,
tabWidth: indent.amount === 0 ? 2 : indent.amount,
useTabs: indent.type === 'tab',
};
return prettier.format(content, options);
}
export class MigratedDataFactory {
// singleton
private static data: MigratedData | null;
public static async getAsync(): Promise<MigratedData | null> {
if (this.data) {
return this.data;
}
const migrated = await this.build();
if (!migrated) {
return null;
}
this.data = migrated;
return this.data;
}
public static reset(): void {
this.data = null;
}
private static async build(): Promise<MigratedData | null> {
let res: MigratedData | null = null;
try {
const rc = await detectRepoFileConfig();
const configFileParsed = rc?.configFileParsed || {};
// get migrated config
const { isMigrated, migratedConfig } = migrateConfig(configFileParsed);
if (!isMigrated) {
return null;
}
delete migratedConfig.errors;
delete migratedConfig.warnings;
const filename = rc.configFileName ?? '';
const raw = await readLocalFile(filename, 'utf8');
// indent defaults to 2 spaces
// TODO #7154
const indent = detectIndent(raw!);
const indentSpace = indent.indent ?? ' ';
let content: string;
if (filename.endsWith('.json5')) {
content = JSON5.stringify(migratedConfig, undefined, indentSpace);
} else {
content = JSON.stringify(migratedConfig, undefined, indentSpace);
}
// format if prettier is found in the user's repo
content = await applyPrettierFormatting(
content,
filename.endsWith('.json5') ? 'json5' : 'json',
indent
);
if (!content.endsWith('\n')) {
content += '\n';
}
res = { content, filename };
} catch (err) {
logger.debug(
{ err },
'MigratedDataFactory.getAsync() Error initializing renovate MigratedData'
);
}
return res;
}
}