feat(tsc-wrapped): Support of vinyl like config file was added (#13987)

This feature was implemented in order to provide easier way of use in gulp
This commit is contained in:
jolly-roger
2017-01-24 22:51:14 +02:00
committed by Alex Rickabaugh
parent 83361d811d
commit 0c7726dd74
5 changed files with 91 additions and 11 deletions

View File

@ -15,6 +15,7 @@ import {check, tsc} from './tsc';
import NgOptions from './options';
import {MetadataWriterHost, DecoratorDownlevelCompilerHost, TsickleCompilerHost} from './compiler_host';
import {CliOptions} from './cli_options';
import {VinylFile, isVinylFile} from './vinyl_file';
export {UserError} from './tsc';
@ -23,11 +24,16 @@ export type CodegenExtension =
Promise<void>;
export function main(
project: string, cliOptions: CliOptions, codegen?: CodegenExtension,
project: string | VinylFile, cliOptions: CliOptions, codegen?: CodegenExtension,
options?: ts.CompilerOptions): Promise<any> {
try {
let projectDir = project;
if (fs.lstatSync(project).isFile()) {
// project is vinyl like file object
if (isVinylFile(project)) {
projectDir = path.dirname(project.path);
}
// project is path to project file
else if (fs.lstatSync(project).isFile()) {
projectDir = path.dirname(project);
}

View File

@ -11,6 +11,7 @@ import * as path from 'path';
import * as ts from 'typescript';
import AngularCompilerOptions from './options';
import {VinylFile, isVinylFile} from './vinyl_file';
/**
* Our interface to the TypeScript standard compiler.
@ -18,7 +19,8 @@ import AngularCompilerOptions from './options';
* you should implement a similar interface.
*/
export interface CompilerInterface {
readConfiguration(project: string, basePath: string, existingOptions?: ts.CompilerOptions):
readConfiguration(
project: string|VinylFile, basePath: string, existingOptions?: ts.CompilerOptions):
{parsed: ts.ParsedCommandLine, ngOptions: AngularCompilerOptions};
typeCheck(compilerHost: ts.CompilerHost, program: ts.Program): void;
emit(program: ts.Program): number;
@ -97,20 +99,30 @@ export class Tsc implements CompilerInterface {
constructor(private readFile = ts.sys.readFile, private readDirectory = ts.sys.readDirectory) {}
readConfiguration(project: string, basePath: string, existingOptions?: ts.CompilerOptions) {
readConfiguration(
project: string|VinylFile, basePath: string, existingOptions?: ts.CompilerOptions) {
this.basePath = basePath;
// Allow a directory containing tsconfig.json as the project value
// Note, TS@next returns an empty array, while earlier versions throw
try {
if (this.readDirectory(project).length > 0) {
if (!isVinylFile(project) && this.readDirectory(project).length > 0) {
project = path.join(project, 'tsconfig.json');
}
} catch (e) {
// Was not a directory, continue on assuming it's a file
}
const {config, error} = ts.readConfigFile(project, this.readFile);
let {config, error} = (() => {
// project is vinyl like file object
if (isVinylFile(project)) {
return {config: JSON.parse(project.contents.toString()), error: null};
}
// project is path to project file
else {
return ts.readConfigFile(project, this.readFile);
}
})();
check([error]);
// Do not inline `host` into `parseJsonConfigFileContent` until after

View File

@ -0,0 +1,19 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* 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
*/
export interface VinylFile extends Object {
// Absolute path to the virtual file
path: string;
// Content of the virtual file
contents: Buffer;
}
;
export function isVinylFile(obj: any): obj is VinylFile {
return (typeof obj === 'object') && ('path' in obj) && ('contents' in obj);
};