forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 1
/
in_place_file_writer.ts
77 lines (70 loc) · 3.06 KB
/
in_place_file_writer.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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {absoluteFrom, AbsoluteFsPath, dirname, FileSystem} from '../../../src/ngtsc/file_system';
import {Logger} from '../logging/logger';
import {EntryPoint, EntryPointJsonProperty} from '../packages/entry_point';
import {EntryPointBundle} from '../packages/entry_point_bundle';
import {FileToWrite} from '../rendering/utils';
import {FileWriter} from './file_writer';
export const NGCC_BACKUP_EXTENSION = '.__ivy_ngcc_bak';
/**
* This FileWriter overwrites the transformed file, in-place, while creating
* a back-up of the original file with an extra `.__ivy_ngcc_bak` extension.
*/
export class InPlaceFileWriter implements FileWriter {
constructor(
protected fs: FileSystem, protected logger: Logger,
protected errorOnFailedEntryPoint: boolean) {}
writeBundle(
_bundle: EntryPointBundle, transformedFiles: FileToWrite[],
_formatProperties?: EntryPointJsonProperty[]) {
transformedFiles.forEach(file => this.writeFileAndBackup(file));
}
revertBundle(
_entryPoint: EntryPoint, transformedFilePaths: AbsoluteFsPath[],
_formatProperties: EntryPointJsonProperty[]): void {
for (const filePath of transformedFilePaths) {
this.revertFileAndBackup(filePath);
}
}
protected writeFileAndBackup(file: FileToWrite): void {
this.fs.ensureDir(dirname(file.path));
const backPath = absoluteFrom(`${file.path}${NGCC_BACKUP_EXTENSION}`);
if (this.fs.exists(backPath)) {
if (this.errorOnFailedEntryPoint) {
throw new Error(
`Tried to overwrite ${backPath} with an ngcc back up file, which is disallowed.`);
} else {
this.logger.error(
`Tried to write ${
backPath} with an ngcc back up file but it already exists so not writing, nor backing up, ${
file.path}.\n` +
`This error may be caused by one of the following:\n` +
`* two or more entry-points overlap and ngcc has been asked to process some files more than once.\n` +
` In this case, you should check other entry-points in this package\n` +
` and set up a config to ignore any that you are not using.\n` +
`* a previous run of ngcc was killed in the middle of processing, in a way that cannot be recovered.\n` +
` In this case, you should try cleaning the node_modules directory and any dist directories that contain local libraries. Then try again.`);
}
} else {
if (this.fs.exists(file.path)) {
this.fs.moveFile(file.path, backPath);
}
this.fs.writeFile(file.path, file.contents);
}
}
protected revertFileAndBackup(filePath: AbsoluteFsPath): void {
if (this.fs.exists(filePath)) {
this.fs.removeFile(filePath);
const backPath = absoluteFrom(`${filePath}${NGCC_BACKUP_EXTENSION}`);
if (this.fs.exists(backPath)) {
this.fs.moveFile(backPath, filePath);
}
}
}
}