mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-21 05:52:41 +08:00
refactor: use standard node resolution and use async/await instead of rxjs
This commit is contained in:
parent
7ef64dbe46
commit
bebd8b6c09
@ -5,25 +5,16 @@
|
|||||||
* Use of this source code is governed by an MIT-style license that can be
|
* Use of this source code is governed by an MIT-style license that can be
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
import { resolve } from '@angular-devkit/core/node';
|
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import { Observable } from 'rxjs';
|
import * as tsLint from 'tslint'; // tslint:disable-line:no-implicit-dependencies
|
||||||
import {
|
|
||||||
Configuration as ConfigurationNS,
|
|
||||||
Linter as LinterNS,
|
|
||||||
} from 'tslint'; // tslint:disable-line:no-implicit-dependencies
|
|
||||||
import * as ts from 'typescript'; // tslint:disable-line:no-implicit-dependencies
|
import * as ts from 'typescript'; // tslint:disable-line:no-implicit-dependencies
|
||||||
import { SchematicContext, TaskExecutor } from '../../src';
|
import { SchematicContext, TaskExecutor } from '../../src';
|
||||||
import { TslintFixTaskOptions } from './options';
|
import { TslintFixTaskOptions } from './options';
|
||||||
|
|
||||||
|
|
||||||
type ConfigurationT = typeof ConfigurationNS;
|
|
||||||
type LinterT = typeof LinterNS;
|
|
||||||
|
|
||||||
|
|
||||||
function _loadConfiguration(
|
function _loadConfiguration(
|
||||||
Configuration: ConfigurationT,
|
Configuration: typeof tsLint.Configuration,
|
||||||
options: TslintFixTaskOptions,
|
options: TslintFixTaskOptions,
|
||||||
root: string,
|
root: string,
|
||||||
file?: string,
|
file?: string,
|
||||||
@ -89,114 +80,105 @@ function _listAllFiles(root: string): string[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export default function(): TaskExecutor<TslintFixTaskOptions> {
|
export default function (): TaskExecutor<TslintFixTaskOptions> {
|
||||||
return (options: TslintFixTaskOptions, context: SchematicContext) => {
|
return async (options: TslintFixTaskOptions, context: SchematicContext) => {
|
||||||
return new Observable(obs => {
|
const root = process.cwd();
|
||||||
const root = process.cwd();
|
const tslint = await import('tslint'); // tslint:disable-line:no-implicit-dependencies
|
||||||
const tslint = require(resolve('tslint', {
|
|
||||||
basedir: root,
|
const includes = (
|
||||||
checkGlobal: true,
|
Array.isArray(options.includes)
|
||||||
checkLocal: true,
|
? options.includes
|
||||||
}));
|
: (options.includes ? [options.includes] : [])
|
||||||
const includes = (
|
);
|
||||||
Array.isArray(options.includes)
|
const files = (
|
||||||
? options.includes
|
Array.isArray(options.files)
|
||||||
: (options.includes ? [options.includes] : [])
|
? options.files
|
||||||
);
|
: (options.files ? [options.files] : [])
|
||||||
const files = (
|
);
|
||||||
Array.isArray(options.files)
|
|
||||||
? options.files
|
const Linter = tslint.Linter;
|
||||||
: (options.files ? [options.files] : [])
|
const Configuration = tslint.Configuration;
|
||||||
|
let program: ts.Program | undefined = undefined;
|
||||||
|
let filesToLint: string[] = files;
|
||||||
|
|
||||||
|
if (options.tsConfigPath && files.length == 0) {
|
||||||
|
const tsConfigPath = path.join(process.cwd(), options.tsConfigPath);
|
||||||
|
|
||||||
|
if (!fs.existsSync(tsConfigPath)) {
|
||||||
|
throw new Error('Could not find tsconfig.');
|
||||||
|
}
|
||||||
|
|
||||||
|
program = Linter.createProgram(tsConfigPath);
|
||||||
|
filesToLint = Linter.getFileNames(program);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (includes.length > 0) {
|
||||||
|
const allFilesRel = _listAllFiles(root);
|
||||||
|
const pattern = '^('
|
||||||
|
+ (includes as string[])
|
||||||
|
.map(ex => '('
|
||||||
|
+ ex.split(/[\/\\]/g).map(f => f
|
||||||
|
.replace(/[\-\[\]{}()+?.^$|]/g, '\\$&')
|
||||||
|
.replace(/^\*\*/g, '(.+?)?')
|
||||||
|
.replace(/\*/g, '[^/\\\\]*'))
|
||||||
|
.join('[\/\\\\]')
|
||||||
|
+ ')')
|
||||||
|
.join('|')
|
||||||
|
+ ')($|/|\\\\)';
|
||||||
|
const re = new RegExp(pattern);
|
||||||
|
|
||||||
|
filesToLint.push(...allFilesRel
|
||||||
|
.filter(x => re.test(x))
|
||||||
|
.map(x => path.join(root, x)),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const Linter = tslint.Linter as LinterT;
|
const lintOptions = {
|
||||||
const Configuration = tslint.Configuration as ConfigurationT;
|
fix: true,
|
||||||
let program: ts.Program | undefined = undefined;
|
formatter: options.format || 'prose',
|
||||||
let filesToLint: string[] = files;
|
};
|
||||||
|
|
||||||
if (options.tsConfigPath && files.length == 0) {
|
const linter = new Linter(lintOptions, program);
|
||||||
const tsConfigPath = path.join(process.cwd(), options.tsConfigPath);
|
// If directory doesn't change, we
|
||||||
|
let lastDirectory: string | null = null;
|
||||||
|
let config;
|
||||||
|
|
||||||
if (!fs.existsSync(tsConfigPath)) {
|
for (const file of filesToLint) {
|
||||||
obs.error(new Error('Could not find tsconfig.'));
|
const dir = path.dirname(file);
|
||||||
|
if (lastDirectory !== dir) {
|
||||||
|
lastDirectory = dir;
|
||||||
|
config = _loadConfiguration(Configuration, options, root, file);
|
||||||
|
}
|
||||||
|
const content = _getFileContent(file, options, program);
|
||||||
|
|
||||||
return;
|
if (!content) {
|
||||||
}
|
continue;
|
||||||
program = Linter.createProgram(tsConfigPath);
|
|
||||||
filesToLint = Linter.getFileNames(program);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (includes.length > 0) {
|
linter.lint(file, content, config);
|
||||||
const allFilesRel = _listAllFiles(root);
|
}
|
||||||
const pattern = '^('
|
|
||||||
+ (includes as string[])
|
|
||||||
.map(ex => '('
|
|
||||||
+ ex.split(/[\/\\]/g).map(f => f
|
|
||||||
.replace(/[\-\[\]{}()+?.^$|]/g, '\\$&')
|
|
||||||
.replace(/^\*\*/g, '(.+?)?')
|
|
||||||
.replace(/\*/g, '[^/\\\\]*'))
|
|
||||||
.join('[\/\\\\]')
|
|
||||||
+ ')')
|
|
||||||
.join('|')
|
|
||||||
+ ')($|/|\\\\)';
|
|
||||||
const re = new RegExp(pattern);
|
|
||||||
|
|
||||||
filesToLint.push(...allFilesRel
|
const result = linter.getResult();
|
||||||
.filter(x => re.test(x))
|
|
||||||
.map(x => path.join(root, x)),
|
// Format and show the results.
|
||||||
);
|
if (!options.silent) {
|
||||||
|
const Formatter = tslint.findFormatter(options.format || 'prose');
|
||||||
|
if (!Formatter) {
|
||||||
|
throw new Error(`Invalid lint format "${options.format}".`);
|
||||||
}
|
}
|
||||||
|
const formatter = new Formatter();
|
||||||
|
|
||||||
const lintOptions = {
|
// Certain tslint formatters outputs '\n' when there are no failures.
|
||||||
fix: true,
|
// This will bloat the console when having schematics running refactor tasks.
|
||||||
formatter: options.format || 'prose',
|
// see https://github.com/palantir/tslint/issues/4244
|
||||||
};
|
const output = (formatter.format(result.failures, result.fixes) || '').trim();
|
||||||
|
if (output) {
|
||||||
const linter = new Linter(lintOptions, program);
|
context.logger.info(output);
|
||||||
// If directory doesn't change, we
|
|
||||||
let lastDirectory: string | null = null;
|
|
||||||
let config;
|
|
||||||
|
|
||||||
for (const file of filesToLint) {
|
|
||||||
const dir = path.dirname(file);
|
|
||||||
if (lastDirectory !== dir) {
|
|
||||||
lastDirectory = dir;
|
|
||||||
config = _loadConfiguration(Configuration, options, root, file);
|
|
||||||
}
|
|
||||||
const content = _getFileContent(file, options, program);
|
|
||||||
|
|
||||||
if (!content) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
linter.lint(file, content, config);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const result = linter.getResult();
|
if (!options.ignoreErrors && result.errorCount > 0) {
|
||||||
|
throw new Error('Lint errors were found.');
|
||||||
// Format and show the results.
|
}
|
||||||
if (!options.silent) {
|
|
||||||
const Formatter = tslint.findFormatter(options.format || 'prose');
|
|
||||||
if (!Formatter) {
|
|
||||||
throw new Error(`Invalid lint format "${options.format}".`);
|
|
||||||
}
|
|
||||||
const formatter = new Formatter();
|
|
||||||
|
|
||||||
// Certain tslint formatters outputs '\n' when there are no failures.
|
|
||||||
// This will bloat the console when having schematics running refactor tasks.
|
|
||||||
// see https://github.com/palantir/tslint/issues/4244
|
|
||||||
const output = (formatter.format(result.failures, result.fixes) || '').trim();
|
|
||||||
if (output) {
|
|
||||||
context.logger.info(output);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!options.ignoreErrors && result.errorCount > 0) {
|
|
||||||
obs.error(new Error('Lint errors were found.'));
|
|
||||||
} else {
|
|
||||||
obs.next();
|
|
||||||
obs.complete();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user