-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
file-utils.ts
180 lines (160 loc) · 4.91 KB
/
file-utils.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { toOldFormatOrNull, Workspaces } from '../config/workspaces';
import { execSync } from 'child_process';
import { existsSync, readFileSync } from 'fs';
import { extname, join, relative, sep } from 'path';
import { readNxJson } from '../config/configuration';
import { NxJsonConfiguration } from '../config/nx-json';
import { FileData } from '../config/project-graph';
import { ProjectsConfigurations } from '../config/workspace-json-project-json';
import type { NxArgs } from '../utils/command-line-utils';
import { workspaceRoot } from '../utils/workspace-root';
import { fileExists } from '../utils/fileutils';
import { jsonDiff } from '../utils/json-diff';
import ignore from 'ignore';
import { readJsonFile } from '../utils/fileutils';
import {
readCachedProjectGraph,
readProjectsConfigurationFromProjectGraph,
} from './project-graph';
export interface Change {
type: string;
}
export interface FileChange<T extends Change = Change> extends FileData {
getChanges: () => T[];
}
export class WholeFileChange implements Change {
type = 'WholeFileChange';
}
export class DeletedFileChange implements Change {
type = 'WholeFileDeleted';
}
export function isWholeFileChange(change: Change): change is WholeFileChange {
return change.type === 'WholeFileChange';
}
export function isDeletedFileChange(
change: Change
): change is DeletedFileChange {
return change.type === 'WholeFileDeleted';
}
export function readFileIfExisting(path: string) {
return existsSync(path) ? readFileSync(path, 'utf-8') : '';
}
function getIgnoredGlobs() {
const ig = ignore();
ig.add(readFileIfExisting(`${workspaceRoot}/.gitignore`));
ig.add(readFileIfExisting(`${workspaceRoot}/.nxignore`));
return ig;
}
export function calculateFileChanges(
files: string[],
allWorkspaceFiles: FileData[],
nxArgs?: NxArgs,
readFileAtRevision: (
f: string,
r: void | string
) => string = defaultReadFileAtRevision,
ignore = getIgnoredGlobs()
): FileChange[] {
files = files.filter((f) => !ignore.ignores(f));
return files.map((f) => {
const ext = extname(f);
const file = allWorkspaceFiles.find((fileData) => fileData.file == f);
const hash = file?.hash;
return {
file: f,
ext,
hash,
getChanges: (): Change[] => {
if (!existsSync(join(workspaceRoot, f))) {
return [new DeletedFileChange()];
}
if (!nxArgs) {
return [new WholeFileChange()];
}
if (nxArgs.files && nxArgs.files.includes(f)) {
return [new WholeFileChange()];
}
switch (ext) {
case '.json':
try {
const atBase = readFileAtRevision(f, nxArgs.base);
const atHead = readFileAtRevision(f, nxArgs.head);
return jsonDiff(JSON.parse(atBase), JSON.parse(atHead));
} catch (e) {
return [new WholeFileChange()];
}
default:
return [new WholeFileChange()];
}
},
};
});
}
export const TEN_MEGABYTES = 1024 * 10000;
function defaultReadFileAtRevision(
file: string,
revision: void | string
): string {
try {
const fileFullPath = `${workspaceRoot}${sep}${file}`;
const gitRepositoryPath = execSync('git rev-parse --show-toplevel')
.toString()
.trim();
const filePathInGitRepository = relative(gitRepositoryPath, fileFullPath)
.split(sep)
.join('/');
return !revision
? readFileSync(file, 'utf-8')
: execSync(`git show ${revision}:${filePathInGitRepository}`, {
maxBuffer: TEN_MEGABYTES,
stdio: ['pipe', 'pipe', 'ignore'],
})
.toString()
.trim();
} catch {
return '';
}
}
export function readWorkspaceConfig(opts: {
format: 'angularCli' | 'nx';
path?: string;
}): ProjectsConfigurations & NxJsonConfiguration {
let configuration: (ProjectsConfigurations & NxJsonConfiguration) | null;
try {
const projectGraph = readCachedProjectGraph();
configuration = {
...readNxJson(),
...readProjectsConfigurationFromProjectGraph(projectGraph),
};
} catch {
const ws = new Workspaces(opts.path || process.cwd());
configuration = ws.readWorkspaceConfiguration();
}
if (opts.format === 'angularCli') {
const formatted = toOldFormatOrNull(configuration);
return formatted ?? configuration;
} else {
return configuration;
}
}
export function workspaceFileName() {
if (fileExists(`${workspaceRoot}/angular.json`)) {
return 'angular.json';
} else {
return 'workspace.json';
}
}
export function defaultFileRead(filePath: string): string | null {
return readFileSync(join(workspaceRoot, filePath), 'utf-8');
}
export function readPackageJson(): any {
return readJsonFile(`${workspaceRoot}/package.json`);
}
// Original Exports
export { FileData };
// TODO(v16): Remove these exports
export {
readNxJson,
readAllWorkspaceConfiguration,
workspaceLayout,
} from '../config/configuration';