@@ -12,15 +12,15 @@ import {
1212 BuilderConfiguration ,
1313 BuilderContext ,
1414} from '@angular-devkit/architect' ;
15- import { getSystemPath , resolve } from '@angular-devkit/core' ;
15+ import { getSystemPath } from '@angular-devkit/core' ;
1616import { readFileSync } from 'fs' ;
1717import * as glob from 'glob' ;
1818import { Minimatch } from 'minimatch' ;
1919import * as path from 'path' ;
20- import { Observable } from 'rxjs' ;
20+ import { Observable , from } from 'rxjs' ;
21+ import { concatMap } from 'rxjs/operators' ;
2122import * as tslint from 'tslint' ; // tslint:disable-line:no-implicit-dependencies
2223import * as ts from 'typescript' ; // tslint:disable-line:no-implicit-dependencies
23- import { requireProjectModule } from '../angular-cli-files/utilities/require-project-module' ;
2424import { stripBom } from '../angular-cli-files/utilities/strip-bom' ;
2525
2626
@@ -36,24 +36,37 @@ export interface TslintBuilderOptions {
3636 files : string [ ] ;
3737}
3838
39- export class TslintBuilder implements Builder < TslintBuilderOptions > {
39+ export default class TslintBuilder implements Builder < TslintBuilderOptions > {
4040
4141 constructor ( public context : BuilderContext ) { }
4242
43+ private async loadTslint ( ) {
44+ let tslint ;
45+ try {
46+ tslint = await import ( 'tslint' ) ; // tslint:disable-line:no-implicit-dependencies
47+ } catch {
48+ throw new Error ( 'Unable to find TSLint. Ensure TSLint is installed.' ) ;
49+ }
50+
51+ const version = tslint . Linter . VERSION && tslint . Linter . VERSION . split ( '.' ) ;
52+ if ( ! version || version . length < 2 || Number ( version [ 0 ] ) < 5 || Number ( version [ 1 ] ) < 5 ) {
53+ throw new Error ( 'TSLint must be version 5.5 or higher.' ) ;
54+ }
55+
56+ return tslint ;
57+ }
58+
4359 run ( builderConfig : BuilderConfiguration < TslintBuilderOptions > ) : Observable < BuildEvent > {
4460
4561 const root = this . context . workspace . root ;
4662 const systemRoot = getSystemPath ( root ) ;
47- const projectRoot = resolve ( root , builderConfig . root ) ;
4863 const options = builderConfig . options ;
4964
5065 if ( ! options . tsConfig && options . typeCheck ) {
5166 throw new Error ( 'A "project" must be specified to enable type checking.' ) ;
5267 }
5368
54- return new Observable ( obs => {
55- const projectTslint = requireProjectModule (
56- getSystemPath ( projectRoot ) , 'tslint' ) as typeof tslint ;
69+ return from ( this . loadTslint ( ) ) . pipe ( concatMap ( projectTslint => new Observable ( obs => {
5770 const tslintConfigPath = options . tslintConfig
5871 ? path . resolve ( systemRoot , options . tslintConfig )
5972 : null ;
@@ -119,7 +132,7 @@ export class TslintBuilder implements Builder<TslintBuilderOptions> {
119132 obs . next ( { success } ) ;
120133
121134 return obs . complete ( ) ;
122- } ) ;
135+ } ) ) ) ;
123136 }
124137}
125138
@@ -215,5 +228,3 @@ function getFileContents(
215228 throw new Error ( `Could not read file '${ file } '.` ) ;
216229 }
217230}
218-
219- export default TslintBuilder ;
0 commit comments