5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import { JsonParseMode , parseJsonAst , strings , tags } from '@angular-devkit/core' ;
8
+ import { JsonParseMode , join , normalize , parseJsonAst , strings , tags } from '@angular-devkit/core' ;
9
9
import {
10
10
Rule , SchematicContext , SchematicsException , Tree ,
11
11
apply , applyTemplates , chain , mergeWith , move , noop , url ,
@@ -21,54 +21,16 @@ function addConfig(options: WebWorkerOptions, root: string, tsConfigPath: string
21
21
return ( host : Tree , context : SchematicContext ) => {
22
22
context . logger . debug ( 'updating project configuration.' ) ;
23
23
24
- const tsConfigRules = [ ] ;
25
-
26
- // Add tsconfig.worker.json.
27
- const relativePathToWorkspaceRoot = root . split ( '/' ) . map ( x => '..' ) . join ( '/' ) ;
28
- tsConfigRules . push ( mergeWith ( apply ( url ( './files/worker-tsconfig' ) , [
29
- applyTemplates ( { ...options , relativePathToWorkspaceRoot } ) ,
30
- move ( root ) ,
31
- ] ) ) ) ;
32
-
33
- // Add project tsconfig.json.
34
- // The project level tsconfig.json with webworker lib is for editor support since
35
- // the dom and webworker libs are mutually exclusive.
36
- // Note: this schematic does not change other tsconfigs to use the project-level tsconfig.
37
- const projectTsConfigPath = `${ root } /tsconfig.json` ;
38
- if ( host . exists ( projectTsConfigPath ) ) {
39
- // If the file already exists, alter it.
40
- const buffer = host . read ( projectTsConfigPath ) ;
41
- if ( buffer ) {
42
- const tsCfgAst = parseJsonAst ( buffer . toString ( ) , JsonParseMode . Loose ) ;
43
- if ( tsCfgAst . kind != 'object' ) {
44
- throw new SchematicsException ( 'Invalid tsconfig. Was expecting an object' ) ;
45
- }
46
- const optsAstNode = findPropertyInAstObject ( tsCfgAst , 'compilerOptions' ) ;
47
- if ( optsAstNode && optsAstNode . kind != 'object' ) {
48
- throw new SchematicsException (
49
- 'Invalid tsconfig "compilerOptions" property; Was expecting an object.' ) ;
50
- }
51
- const libAstNode = findPropertyInAstObject ( tsCfgAst , 'lib' ) ;
52
- if ( libAstNode && libAstNode . kind != 'array' ) {
53
- throw new SchematicsException ( 'Invalid tsconfig "lib" property; expected an array.' ) ;
54
- }
55
- const newLibProp = 'webworker' ;
56
- if ( libAstNode && ! libAstNode . value . includes ( newLibProp ) ) {
57
- const recorder = host . beginUpdate ( projectTsConfigPath ) ;
58
- appendValueInAstArray ( recorder , libAstNode , newLibProp ) ;
59
- host . commitUpdate ( recorder ) ;
60
- }
61
- }
62
- } else {
63
- // Otherwise create it.
64
- tsConfigRules . push ( mergeWith ( apply ( url ( './files/project-tsconfig' ) , [
65
- applyTemplates ( { ...options , relativePathToWorkspaceRoot } ) ,
66
- move ( root ) ,
67
- ] ) ) ) ;
68
- }
24
+ // todo: replace with the new helper method in a seperate PR
25
+ // https://github.com/angular/angular-cli/pull/14207
26
+ const rootNormalized = root . endsWith ( '/' ) ? root . slice ( 0 , - 1 ) : root ;
27
+ const relativePathToWorkspaceRoot =
28
+ rootNormalized
29
+ ? rootNormalized . split ( '/' ) . map ( x => '..' ) . join ( '/' )
30
+ : '.' ;
69
31
70
32
// Add worker glob exclusion to tsconfig.app.json.
71
- const workerGlob = '**/*.worker.ts' ;
33
+ const workerGlob = 'src/ **/*.worker.ts' ;
72
34
const buffer = host . read ( tsConfigPath ) ;
73
35
if ( buffer ) {
74
36
const tsCfgAst = parseJsonAst ( buffer . toString ( ) , JsonParseMode . Loose ) ;
@@ -80,17 +42,19 @@ function addConfig(options: WebWorkerOptions, root: string, tsConfigPath: string
80
42
throw new SchematicsException ( 'Invalid tsconfig "exclude" property; expected an array.' ) ;
81
43
}
82
44
83
- if ( filesAstNode && filesAstNode . value . indexOf ( workerGlob ) == - 1 ) {
45
+ if ( filesAstNode && ! filesAstNode . value . includes ( workerGlob ) ) {
84
46
const recorder = host . beginUpdate ( tsConfigPath ) ;
85
47
appendValueInAstArray ( recorder , filesAstNode , workerGlob ) ;
86
48
host . commitUpdate ( recorder ) ;
87
49
}
88
50
}
89
51
90
- return chain ( [
91
- // Add tsconfigs.
92
- ...tsConfigRules ,
93
- ] ) ;
52
+ return mergeWith (
53
+ apply ( url ( './files/worker-tsconfig' ) , [
54
+ applyTemplates ( { ...options , relativePathToWorkspaceRoot } ) ,
55
+ move ( root ) ,
56
+ ] ) ,
57
+ ) ;
94
58
} ;
95
59
}
96
60
@@ -145,7 +109,7 @@ export default function (options: WebWorkerOptions): Rule {
145
109
throw new SchematicsException ( 'Option "project" is required.' ) ;
146
110
}
147
111
if ( ! options . target ) {
148
- throw new SchematicsException ( 'Option ( target) is required.' ) ;
112
+ throw new SchematicsException ( 'Option " target" is required.' ) ;
149
113
}
150
114
151
115
const project = workspace . projects . get ( options . project ) ;
@@ -169,11 +133,11 @@ export default function (options: WebWorkerOptions): Rule {
169
133
const parsedPath = parseName ( options . path , options . name ) ;
170
134
options . name = parsedPath . name ;
171
135
options . path = parsedPath . path ;
172
- const root = project . root || project . sourceRoot || '' ;
136
+ const root = project . root || '' ;
173
137
174
138
const needWebWorkerConfig = ! projectTargetOptions . webWorkerTsConfig ;
175
139
if ( needWebWorkerConfig ) {
176
- const workerConfigPath = ` ${ root . endsWith ( '/' ) ? root : root + '/' } tsconfig.worker.json` ;
140
+ const workerConfigPath = join ( normalize ( root ) , ' tsconfig.worker.json' ) ;
177
141
projectTargetOptions . webWorkerTsConfig = workerConfigPath ;
178
142
179
143
// add worker tsconfig to lint architect target
0 commit comments