6
6
7
7
const shelljs = require ( 'shelljs' ) ;
8
8
const path = require ( 'path' ) ;
9
+ const fs = require ( 'fs' ) ;
10
+
11
+ /** Path to the project directory. */
12
+ const projectDir = path . join ( __dirname , '../..' ) ;
9
13
10
14
shelljs . set ( '-e' ) ;
11
- shelljs . cd ( path . join ( __dirname , '../..' ) ) ;
15
+ shelljs . cd ( projectDir ) ;
12
16
13
17
// Do not apply postinstall patches when running "postinstall" outside. The
14
18
// "generate_build_file.js" file indicates that we run in Bazel managed node modules.
@@ -22,68 +26,91 @@ shelljs.exec('ngc -p angular-tsconfig.json');
22
26
// Workaround for https://github.com/angular/angular/issues/30586. It's not possible to
23
27
// enable tsickle decorator processing without enabling import rewriting to closure.
24
28
// This replacement allows us to enable decorator processing without rewriting imports.
25
- shelljs . sed (
26
- '-i' , / ( t h i s \. t r a n s f o r m T y p e s T o C l o s u r e ) = b a z e l O p t s \. t s i c k l e ; / , '$1 = false;' ,
29
+ searchAndReplace (
30
+ / ( t h i s \. t r a n s f o r m T y p e s T o C l o s u r e ) = b a z e l O p t s \. t s i c k l e ; / , '$1 = false;' ,
27
31
'node_modules/@bazel/typescript/internal/tsc_wrapped/compiler_host.js' ) ;
28
- shelljs . sed (
29
- '-i' , ' bazelOpts\.tsickleExternsPath', 'null' ,
32
+ searchAndReplace (
33
+ 'bazelOpts\.tsickleExternsPath' , 'null' ,
30
34
'node_modules/@bazel/typescript/internal/tsc_wrapped/tsc_wrapped.js' ) ;
31
35
32
36
// Workaround for https://github.com/angular/angular/issues/32389. We need to ensure
33
37
// that tsickle is available for esm5 output re-compilations.
34
- shelljs . sed (
35
- '-i' , ' @npm//@bazel/typescript/bin:tsc_wrapped',
38
+ searchAndReplace (
39
+ '@npm//@bazel/typescript/bin:tsc_wrapped' ,
36
40
'@angular_material//tools/bazel:tsc_wrapped_with_tsickle' ,
37
41
'node_modules/@angular/bazel/src/esm5.bzl' ) ;
38
42
39
43
// Workaround for: https://github.com/angular/angular/issues/32651. We just do not
40
44
// generate re-exports for secondary entry-points. Similar to what "ng-packagr" does.
41
- shelljs . sed ( '-i' , / (? ! f u n c t i o n \s + ) c r e a t e M e t a d a t a R e e x p o r t F i l e \( [ ^ ) ] + \) ; / ,
42
- '' , 'node_modules/@angular/bazel/src/ng_package/packager.js' ) ;
43
- shelljs . sed ( '-i' , / (? ! f u n c t i o n \s + ) c r e a t e T y p i n g s R e e x p o r t F i l e \( [ ^ ) ] + \) ; / ,
44
- '' , 'node_modules/@angular/bazel/src/ng_package/packager.js' ) ;
45
+ searchAndReplace (
46
+ / (? ! f u n c t i o n \s + ) c r e a t e M e t a d a t a R e e x p o r t F i l e \( [ ^ ) ] + \) ; / , '' ,
47
+ 'node_modules/@angular/bazel/src/ng_package/packager.js' ) ;
48
+ searchAndReplace (
49
+ / (? ! f u n c t i o n \s + ) c r e a t e T y p i n g s R e e x p o r t F i l e \( [ ^ ) ] + \) ; / , '' ,
50
+ 'node_modules/@angular/bazel/src/ng_package/packager.js' ) ;
45
51
46
52
// Workaround for: https://github.com/angular/angular/pull/32650
47
- shelljs . sed ( '-i' , 'var indexFile;' , `
53
+ searchAndReplace (
54
+ 'var indexFile;' , `
48
55
var publicApiFile = files.find(f => f.endsWith('/public-api.ts'));
49
56
var moduleFile = files.find(f => f.endsWith('/module.ts'));
50
57
var indexFile = publicApiFile || moduleFile;
51
- ` , 'node_modules/@angular/compiler-cli/src/metadata/bundle_index_host.js' ) ;
52
- shelljs . sed ( '-i' , 'var resolvedEntryPoint = null;' , `
58
+ ` ,
59
+ 'node_modules/@angular/compiler-cli/src/metadata/bundle_index_host.js' ) ;
60
+ searchAndReplace (
61
+ 'var resolvedEntryPoint = null;' , `
53
62
var publicApiFile = tsFiles.find(f => f.endsWith('/public-api.ts'));
54
63
var moduleFile = tsFiles.find(f => f.endsWith('/module.ts'));
55
64
var resolvedEntryPoint = publicApiFile || moduleFile || null;
56
- ` , 'node_modules/@angular/compiler-cli/src/ngtsc/entry_point/src/logic.js' ) ;
65
+ ` ,
66
+ 'node_modules/@angular/compiler-cli/src/ngtsc/entry_point/src/logic.js' ) ;
57
67
58
- // Workaround for https://github.com/angular/angular/issues/32603. Note that we don't
59
- // want to apply the patch if it has been applied already.
60
- if ( ! shelljs . test ( '-f' , 'node_modules/@angular/bazel/src/ng_package/rollup_bin.js' ) ) {
61
- shelljs . cat ( path . join ( __dirname , './rollup_windows_arguments.patch' ) ) . exec ( 'patch -p0' ) ;
62
- }
68
+ // Workaround for https://github.com/angular/angular/issues/32603.
69
+ shelljs . cat ( path . join ( __dirname , './rollup_windows_arguments.patch' ) ) . exec ( 'patch -p0' ) ;
63
70
64
71
// Workaround for: https://hackmd.io/MlqFp-yrSx-0mw4rD7dnQQ?both. We only want to discard
65
72
// the metadata of files in the bazel managed node modules. That way we keep the default
66
73
// behavior of ngc-wrapped except for dependencies between sources of the library. This makes
67
74
// the "generateCodeForLibraries" flag more accurate in the Bazel environment where previous
68
75
// compilations should not be treated as external libraries. Read more about this in the document.
69
- shelljs . sed ( '-i' , / i f \( ( t h i s \. o p t i o n s \. g e n e r a t e C o d e F o r L i b r a r i e s = = = f a l s e ) / , `
76
+ searchAndReplace (
77
+ / i f \( ( t h i s \. o p t i o n s \. g e n e r a t e C o d e F o r L i b r a r i e s = = = f a l s e ) / , `
70
78
const fs = require('fs');
71
79
const hasFlatModuleBundle = fs.existsSync(filePath.replace('.d.ts', '.metadata.json'));
72
80
if ((filePath.includes('node_modules/') || !hasFlatModuleBundle) && $1` ,
73
- 'node_modules/@angular/compiler-cli/src/transformers/compiler_host.js' ) ;
81
+ 'node_modules/@angular/compiler-cli/src/transformers/compiler_host.js' ) ;
74
82
shelljs . cat ( path . join ( __dirname , './flat_module_factory_resolution.patch' ) ) . exec ( 'patch -p0' ) ;
75
83
// The three replacements below ensure that metadata files can be read by NGC and
76
84
// that metadata files are collected as Bazel action inputs.
77
- shelljs . sed ( '-i' , / ( c o n s t N G C _ A S S E T S = \/ [ ^ ( ] + \( ) ( [ ^ ) ] * ) ( \) .* \/ ; ) / , '$1$2|metadata.json$3' ,
78
- 'node_modules/@angular/bazel/src/ngc-wrapped/index.js' ) ;
79
- shelljs . sed ( '-i' , / ^ ( ( \s * ) r e s u l t s = d e p s e t \( d e p .a n g u l a r .s u m m a r i e s , t r a n s i t i v e = \[ r e s u l t s ] \) ) $ / ,
80
- `$1#\n$2results = depset(dep.angular.metadata, transitive = [results])` ,
81
- 'node_modules/@angular/bazel/src/ng_module.bzl' ) ;
82
- shelljs . sed ( '-i' ,
83
- / ^ ( ( \s * ) r e s u l t s = d e p s e t \( t a r g e t .a n g u l a r .s u m m a r i e s i f h a s a t t r \( t a r g e t , " a n g u l a r " \) e l s e \[ ] \) ) $ / ,
84
- `$1#\n$2results = depset(target.angular.metadata if hasattr(target, "angular") else [], transitive = [results])` ,
85
- 'node_modules/@angular/bazel/src/ng_module.bzl' ) ;
85
+ searchAndReplace (
86
+ / ( c o n s t N G C _ A S S E T S = \/ [ ^ ( ] + \( ) ( [ ^ ) ] * ) ( \) .* \/ ; ) / , '$1$2|metadata.json$3' ,
87
+ 'node_modules/@angular/bazel/src/ngc-wrapped/index.js' ) ;
88
+ searchAndReplace (
89
+ / ^ ( ( \s * ) r e s u l t s = d e p s e t \( d e p .a n g u l a r .s u m m a r i e s , t r a n s i t i v e = \[ r e s u l t s ] \) ) $ / m,
90
+ `$1#\n$2results = depset(dep.angular.metadata, transitive = [results])` ,
91
+ 'node_modules/@angular/bazel/src/ng_module.bzl' ) ;
92
+ searchAndReplace (
93
+ / ^ ( ( \s * ) r e s u l t s = d e p s e t \( t a r g e t .a n g u l a r .s u m m a r i e s i f h a s a t t r \( t a r g e t , " a n g u l a r " \) e l s e \[ ] \) ) $ / m,
94
+ `$1#\n$2results = depset(target.angular.metadata if hasattr(target, "angular") else [], transitive = [results])` ,
95
+ 'node_modules/@angular/bazel/src/ng_module.bzl' ) ;
86
96
// Ensure that "metadata" of transitive dependencies can be collected.
87
- shelljs . sed ( '-i' , / ( " m e t a d a t a " : o u t s .m e t a d a t a ) , / ,
88
- `$1 + [m for dep in ctx.attr.deps if hasattr(dep, "angular") for m in dep.angular.metadata],` ,
89
- 'node_modules/@angular/bazel/src/ng_module.bzl' ) ;
97
+ searchAndReplace (
98
+ / ( " m e t a d a t a " : o u t s .m e t a d a t a ) , / ,
99
+ `$1 + [m for dep in ctx.attr.deps if hasattr(dep, "angular") for m in dep.angular.metadata],` ,
100
+ 'node_modules/@angular/bazel/src/ng_module.bzl' ) ;
101
+
102
+ /**
103
+ * Reads the specified file and replaces matches of the search expression
104
+ * with the given replacement. Throws if no changes were made.
105
+ */
106
+ function searchAndReplace ( search , replacement , relativeFilePath ) {
107
+ const filePath = path . join ( projectDir , relativeFilePath ) ;
108
+ const originalContent = fs . readFileSync ( filePath , 'utf8' ) ;
109
+ const newFileContent = originalContent . replace ( search , replacement ) ;
110
+
111
+ if ( originalContent === newFileContent ) {
112
+ throw Error ( `Could not perform replacement in: ${ filePath } .` ) ;
113
+ }
114
+
115
+ fs . writeFileSync ( filePath , newFileContent , 'utf8' ) ;
116
+ }
0 commit comments