@@ -8,18 +8,18 @@ const shelljs = require('shelljs');
8
8
const path = require ( 'path' ) ;
9
9
const fs = require ( 'fs' ) ;
10
10
11
+ /**
12
+ * Version of the post install patch. Needs to be incremented when patches
13
+ * have been added or removed.
14
+ */
15
+ const PATCH_VERSION = 1 ;
16
+
11
17
/** Path to the project directory. */
12
18
const projectDir = path . join ( __dirname , '../..' ) ;
13
19
14
20
shelljs . set ( '-e' ) ;
15
21
shelljs . cd ( projectDir ) ;
16
22
17
- // Do not apply postinstall patches when running "postinstall" outside. The
18
- // "generate_build_file.js" file indicates that we run in Bazel managed node modules.
19
- if ( ! shelljs . test ( '-e' , 'generate_build_file.js' ) ) {
20
- return ;
21
- }
22
-
23
23
// Workaround for https://github.com/angular/angular/issues/18810.
24
24
shelljs . exec ( 'ngc -p angular-tsconfig.json' ) ;
25
25
@@ -69,7 +69,7 @@ searchAndReplace(
69
69
const hasFlatModuleBundle = fs.existsSync(filePath.replace('.d.ts', '.metadata.json'));
70
70
if ((filePath.includes('node_modules/') || !hasFlatModuleBundle) && $1` ,
71
71
'node_modules/@angular/compiler-cli/src/transformers/compiler_host.js' ) ;
72
- shelljs . cat ( path . join ( __dirname , './flat_module_factory_resolution.patch' ) ) . exec ( 'patch -p0' ) ;
72
+ applyPatch ( path . join ( __dirname , './flat_module_factory_resolution.patch' ) ) ;
73
73
// The three replacements below ensure that metadata files can be read by NGC and
74
74
// that metadata files are collected as Bazel action inputs.
75
75
searchAndReplace (
@@ -90,7 +90,7 @@ searchAndReplace(
90
90
'node_modules/@angular/bazel/src/ng_module.bzl' ) ;
91
91
92
92
// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1208.
93
- shelljs . cat ( path . join ( __dirname , './manifest_externs_hermeticity.patch' ) ) . exec ( 'patch -p0' ) ;
93
+ applyPatch ( path . join ( __dirname , './manifest_externs_hermeticity.patch' ) ) ;
94
94
95
95
// Workaround for using Ngcc with "--create-ivy-entry-points". This is a special
96
96
// issue for our repository since we want to run Ivy by default in the module resolution,
@@ -105,18 +105,53 @@ searchAndReplace(/angular_compiler_options = {/, `$&
105
105
"strictAttributeTypes": False,
106
106
"strictDomEventTypes": False,` , 'node_modules/@angular/bazel/src/ng_module.bzl' ) ;
107
107
108
+ /**
109
+ * Applies the given patch if not done already. Throws if the patch does
110
+ * not apply cleanly.
111
+ */
112
+ function applyPatch ( patchFile ) {
113
+ const patchMarkerFileName = `${ path . basename ( patchFile ) } .patch_marker` ;
114
+ const patchMarkerPath = path . join ( projectDir , 'node_modules/' , patchMarkerFileName ) ;
115
+
116
+ if ( hasFileBeenPatched ( patchMarkerPath ) ) {
117
+ return ;
118
+ }
119
+
120
+ writePatchMarker ( patchMarkerPath ) ;
121
+ shelljs . cat ( patchFile ) . exec ( 'patch -p0' ) ;
122
+ }
123
+
108
124
/**
109
125
* Reads the specified file and replaces matches of the search expression
110
- * with the given replacement. Throws if no changes were made.
126
+ * with the given replacement. Throws if no changes were made and the
127
+ * patch has not been applied yet.
111
128
*/
112
129
function searchAndReplace ( search , replacement , relativeFilePath ) {
113
130
const filePath = path . join ( projectDir , relativeFilePath ) ;
131
+
132
+ if ( hasFileBeenPatched ( filePath ) ) {
133
+ return ;
134
+ }
135
+
114
136
const originalContent = fs . readFileSync ( filePath , 'utf8' ) ;
115
137
const newFileContent = originalContent . replace ( search , replacement ) ;
116
138
117
139
if ( originalContent === newFileContent ) {
118
140
throw Error ( `Could not perform replacement in: ${ filePath } .` ) ;
119
141
}
120
142
143
+ writePatchMarker ( filePath ) ;
121
144
fs . writeFileSync ( filePath , newFileContent , 'utf8' ) ;
122
145
}
146
+
147
+ /** Marks the specified file as patched. */
148
+ function writePatchMarker ( filePath ) {
149
+ shelljs . echo ( PATCH_VERSION ) . to ( `${ filePath } .patch_marker` ) ;
150
+ }
151
+
152
+ /** Checks if the given file has been patched. */
153
+ function hasFileBeenPatched ( filePath ) {
154
+ const markerFilePath = `${ filePath } .patch_marker` ;
155
+ return shelljs . test ( '-e' , markerFilePath ) &&
156
+ shelljs . cat ( markerFilePath ) . toString ( ) . trim ( ) === `${ PATCH_VERSION } ` ;
157
+ }
0 commit comments