@@ -8,18 +8,18 @@ const shelljs = require('shelljs');
88const path = require ( 'path' ) ;
99const fs = require ( 'fs' ) ;
1010
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-
1711/** Path to the project directory. */
1812const projectDir = path . join ( __dirname , '../..' ) ;
1913
2014shelljs . set ( '-e' ) ;
2115shelljs . cd ( projectDir ) ;
2216
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+
2323// Workaround for https://github.com/angular/angular/issues/18810.
2424shelljs . exec ( 'ngc -p angular-tsconfig.json' ) ;
2525
@@ -69,7 +69,7 @@ searchAndReplace(
6969 const hasFlatModuleBundle = fs.existsSync(filePath.replace('.d.ts', '.metadata.json'));
7070 if ((filePath.includes('node_modules/') || !hasFlatModuleBundle) && $1` ,
7171 'node_modules/@angular/compiler-cli/src/transformers/compiler_host.js' ) ;
72- applyPatch ( path . join ( __dirname , './flat_module_factory_resolution.patch' ) ) ;
72+ shelljs . cat ( path . join ( __dirname , './flat_module_factory_resolution.patch' ) ) . exec ( 'patch -p0' ) ;
7373// The three replacements below ensure that metadata files can be read by NGC and
7474// that metadata files are collected as Bazel action inputs.
7575searchAndReplace (
@@ -90,7 +90,7 @@ searchAndReplace(
9090 'node_modules/@angular/bazel/src/ng_module.bzl' ) ;
9191
9292// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1208.
93- applyPatch ( path . join ( __dirname , './manifest_externs_hermeticity.patch' ) ) ;
93+ shelljs . cat ( path . join ( __dirname , './manifest_externs_hermeticity.patch' ) ) . exec ( 'patch -p0' ) ;
9494
9595// Workaround for using Ngcc with "--create-ivy-entry-points". This is a special
9696// issue for our repository since we want to run Ivy by default in the module resolution,
@@ -136,53 +136,18 @@ shelljs.rm('-rf', [
136136 'node_modules/rxjs/Subscription.*' ,
137137] ) ;
138138
139- /**
140- * Applies the given patch if not done already. Throws if the patch does
141- * not apply cleanly.
142- */
143- function applyPatch ( patchFile ) {
144- const patchMarkerFileName = `${ path . basename ( patchFile ) } .patch_marker` ;
145- const patchMarkerPath = path . join ( projectDir , 'node_modules/' , patchMarkerFileName ) ;
146-
147- if ( hasFileBeenPatched ( patchMarkerPath ) ) {
148- return ;
149- }
150-
151- writePatchMarker ( patchMarkerPath ) ;
152- shelljs . cat ( patchFile ) . exec ( 'patch -p0' ) ;
153- }
154-
155139/**
156140 * Reads the specified file and replaces matches of the search expression
157- * with the given replacement. Throws if no changes were made and the
158- * patch has not been applied yet.
141+ * with the given replacement. Throws if no changes were made.
159142 */
160143function searchAndReplace ( search , replacement , relativeFilePath ) {
161144 const filePath = path . join ( projectDir , relativeFilePath ) ;
162-
163- if ( hasFileBeenPatched ( filePath ) ) {
164- return ;
165- }
166-
167145 const originalContent = fs . readFileSync ( filePath , 'utf8' ) ;
168146 const newFileContent = originalContent . replace ( search , replacement ) ;
169147
170148 if ( originalContent === newFileContent ) {
171149 throw Error ( `Could not perform replacement in: ${ filePath } .` ) ;
172150 }
173151
174- writePatchMarker ( filePath ) ;
175152 fs . writeFileSync ( filePath , newFileContent , 'utf8' ) ;
176153}
177-
178- /** Marks the specified file as patched. */
179- function writePatchMarker ( filePath ) {
180- shelljs . echo ( PATCH_VERSION ) . to ( `${ filePath } .patch_marker` ) ;
181- }
182-
183- /** Checks if the given file has been patched. */
184- function hasFileBeenPatched ( filePath ) {
185- const markerFilePath = `${ filePath } .patch_marker` ;
186- return shelljs . test ( '-e' , markerFilePath ) &&
187- shelljs . cat ( markerFilePath ) . toString ( ) . trim ( ) === `${ PATCH_VERSION } ` ;
188- }
0 commit comments