broccoli-typify
Advanced tools
Comparing version 0.1.6 to 0.2.0
/// <reference path="../types/all.d.ts" /> | ||
import * as ts from 'typescript'; | ||
export declare const INTERNAL_TYPINGS_PATH: string; | ||
export interface DiffingCompilerOptions { | ||
@@ -9,4 +8,4 @@ tsOptions: ts.CompilerOptions; | ||
includeExtensions?: string[]; | ||
internalTypings?: boolean; | ||
instanceName?: string; | ||
} | ||
export declare var Compiler: (inputTrees: BroccoliTree | BroccoliTree[], options?: any) => BroccoliTree; |
@@ -13,19 +13,2 @@ /// <reference path="../types/all.d.ts" /> | ||
var debug = debugImport('broccoli-typify:typescript'); | ||
// Sub-directory where the @internal typing files (.d.ts) are stored | ||
exports.INTERNAL_TYPINGS_PATH = 'internal_typings'; | ||
// Monkey patch the TS compiler to be able to re-emit files with @internal symbols | ||
var tsEmitInternal = false; | ||
var originalEmitFiles = ts.emitFiles; | ||
ts.emitFiles = function (resolver, host, targetSourceFile) { | ||
if (tsEmitInternal) { | ||
var orignalgetCompilerOptions_1 = host.getCompilerOptions; | ||
host.getCompilerOptions = function () { | ||
var options = clone(orignalgetCompilerOptions_1.call(host)); | ||
options.stripInternal = false; | ||
options.outDir = options.outDir + "/" + exports.INTERNAL_TYPINGS_PATH; | ||
return options; | ||
}; | ||
} | ||
return originalEmitFiles(resolver, host, targetSourceFile); | ||
}; | ||
/** | ||
@@ -49,5 +32,2 @@ * Broccoli plugin that implements incremental Typescript compiler. | ||
this.previousRunFailed = false; | ||
// Whether to generate the @internal typing files (they are only generated when `stripInternal` is | ||
// true) | ||
this.genInternalTypings = false; | ||
if (options && options.rootFilePaths) { | ||
@@ -62,20 +42,17 @@ this.rootFilePaths = options.rootFilePaths.splice(0); | ||
} | ||
if (options && options.internalTypings) { | ||
this.genInternalTypings = true; | ||
} | ||
this.tsOpts = (options && options.tsOptions) || {}; | ||
if (this.tsOpts.stripInternal === false) { | ||
// @internal are included in the generated .d.ts, do not generate them separately | ||
this.genInternalTypings = false; | ||
} | ||
this.tsOpts.rootDir = inputPath; | ||
this.tsOpts.outDir = this.cachePath; | ||
this.instanceName = this.options.instanceName; | ||
if (this.rootFilePaths && this.rootFilePaths.length) { | ||
debug("CustomLanguageServiceHost rootFilePaths " + this.rootFilePaths.join(";")); | ||
} | ||
debug("CustomLanguageServiceHost inputPath " + this.inputPath); | ||
debug("[name " + this.instanceName + "] CustomLanguageServiceHost inputPath " + this.inputPath); | ||
var localTypesFolder = options.localTypesFolder || process.cwd() + "/local-types"; | ||
this.tsServiceHost = new CustomLanguageServiceHost(this.tsOpts, this.rootFilePaths, this.fileRegistry, this.inputPath, localTypesFolder); | ||
this.tsServiceHost = new CustomLanguageServiceHost(this.tsOpts, this.rootFilePaths, this.fileRegistry, this.inputPath, localTypesFolder, this.instanceName); | ||
this.tsService = ts.createLanguageService(this.tsServiceHost, ts.createDocumentRegistry()); | ||
} | ||
DiffingTSCompiler.prototype.debugWithName = function (msg) { | ||
debug("[name " + this.instanceName + "] " + msg); | ||
}; | ||
DiffingTSCompiler.prototype.rebuild = function (treeDiff) { | ||
@@ -108,6 +85,6 @@ var _this = this; | ||
var program_1 = this.tsService.getProgram(); | ||
tsEmitInternal = false; | ||
pathsToEmit.forEach(function (tsFilePath) { | ||
var output = _this.tsService.getEmitOutput(tsFilePath); | ||
if (output.emitSkipped) { | ||
// there was an error, report it | ||
var errorFound = _this.collectErrors(tsFilePath); | ||
@@ -132,3 +109,3 @@ if (errorFound) { | ||
this.previousRunFailed = true; | ||
var error = new Error('Typescript found the following errors:\n' + errorMessages.join('\n')); | ||
var error = new Error(("[name " + this.instanceName + "] Typescript found the following errors:\n") + errorMessages.join('\n')); | ||
error['showStack'] = false; | ||
@@ -140,19 +117,2 @@ throw error; | ||
} | ||
else if (this.genInternalTypings) { | ||
// serialize the .d.ts files containing @internal symbols | ||
tsEmitInternal = true; | ||
pathsToEmit.forEach(function (tsFilePath) { | ||
var output = _this.tsService.getEmitOutput(tsFilePath); | ||
if (!output.emitSkipped) { | ||
output.outputFiles.forEach(function (o) { | ||
if (endsWith(o.name, '.d.ts')) { | ||
var destDirPath = path.dirname(o.name); | ||
fse.mkdirsSync(destDirPath); | ||
fs.writeFileSync(o.name, o.text, FS_OPTS); | ||
} | ||
}); | ||
} | ||
}); | ||
tsEmitInternal = false; | ||
} | ||
} | ||
@@ -184,3 +144,2 @@ }; | ||
var diagnostics = []; | ||
tsEmitInternal = false; | ||
var emitResult = program.emit(undefined, function (absoluteFilePath, fileContent) { | ||
@@ -198,13 +157,2 @@ fse.mkdirsSync(path.dirname(absoluteFilePath)); | ||
}); | ||
if (this.genInternalTypings) { | ||
// serialize the .d.ts files containing @internal symbols | ||
tsEmitInternal = true; | ||
program.emit(undefined, function (absoluteFilePath, fileContent) { | ||
if (endsWith(absoluteFilePath, '.d.ts')) { | ||
fse.mkdirsSync(path.dirname(absoluteFilePath)); | ||
fs.writeFileSync(absoluteFilePath, fileContent, FS_OPTS); | ||
} | ||
}); | ||
tsEmitInternal = false; | ||
} | ||
if (emitResult.emitSkipped) { | ||
@@ -224,3 +172,3 @@ var allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); | ||
this.previousRunFailed = true; | ||
var error = new Error('Typescript found the following errors:\n' + errorMessages_1.join('\n')); | ||
var error = new Error(("[name " + this.instanceName + "] Typescript found the following errors:\n") + errorMessages_1.join('\n')); | ||
error['showStack'] = false; | ||
@@ -257,3 +205,3 @@ throw error; | ||
var CustomLanguageServiceHost = (function () { | ||
function CustomLanguageServiceHost(compilerOptions, fileNames, fileRegistry, treeInputPath, localTypesFolder) { | ||
function CustomLanguageServiceHost(compilerOptions, fileNames, fileRegistry, treeInputPath, localTypesFolder, instanceName) { | ||
this.compilerOptions = compilerOptions; | ||
@@ -264,2 +212,3 @@ this.fileNames = fileNames; | ||
this.localTypesFolder = localTypesFolder; | ||
this.instanceName = instanceName; | ||
this.currentDirectory = process.cwd(); | ||
@@ -336,3 +285,3 @@ this.defaultLibFilePath = ts.getDefaultLibFilePath(compilerOptions).replace(/\\/g, '/'); | ||
} | ||
debug("resolveModuleNames skipping module '" + name + "'"); | ||
("[name " + _this.instanceName + "] resolveModuleNames skipping module '" + name + "'"); | ||
return undefined; | ||
@@ -339,0 +288,0 @@ }); |
{ | ||
"name": "broccoli-typify", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"description": "Provide broccoli building blocks for typescript pipelines.", | ||
@@ -23,3 +23,3 @@ "main": "dist/index.js", | ||
], | ||
"bin" : "bin/broccoli-typify.js", | ||
"bin": "bin/broccoli-typify.js", | ||
"typings": "./dist/index.d.ts", | ||
@@ -42,3 +42,3 @@ "author": "Marius Seritan <git@winding-lines.com>", | ||
"symlink-or-copy": "^1.1.3", | ||
"typescript": "^2.0.0-dev.20160706" | ||
"typescript": "^2.0.0" | ||
}, | ||
@@ -54,5 +54,4 @@ "devDependencies": { | ||
"chai": "^1.10.0", | ||
"minimatch": "^3.0.2", | ||
"mkdirp": "^0.5.1", | ||
"mocha": "^1.21.4", | ||
"mocha": "^2.5.3", | ||
"rimraf": "^2.5.3", | ||
@@ -59,0 +58,0 @@ "walk-sync": "^0.2.6" |
@@ -8,5 +8,5 @@ /// <reference path="../types/all.d.ts" /> | ||
import * as ts from 'typescript'; | ||
import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin'; | ||
import { wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult } from './diffing-broccoli-plugin'; | ||
type FileRegistry = ts.Map<{version: number}>; | ||
type FileRegistry = ts.Map<{ version: number }>; | ||
@@ -19,23 +19,2 @@ const FS_OPTS = { | ||
// Sub-directory where the @internal typing files (.d.ts) are stored | ||
export const INTERNAL_TYPINGS_PATH: string = 'internal_typings'; | ||
// Monkey patch the TS compiler to be able to re-emit files with @internal symbols | ||
let tsEmitInternal: boolean = false; | ||
const originalEmitFiles: Function = (<any>ts).emitFiles; | ||
(<any>ts).emitFiles = function(resolver: any, host: any, targetSourceFile: any): any { | ||
if (tsEmitInternal) { | ||
const orignalgetCompilerOptions = host.getCompilerOptions; | ||
host.getCompilerOptions = () => { | ||
let options = clone(orignalgetCompilerOptions.call(host)); | ||
options.stripInternal = false; | ||
options.outDir = `${options.outDir}/${INTERNAL_TYPINGS_PATH}`; | ||
return options; | ||
} | ||
} | ||
return originalEmitFiles(resolver, host, targetSourceFile); | ||
}; | ||
export interface DiffingCompilerOptions { | ||
@@ -46,3 +25,3 @@ tsOptions: ts.CompilerOptions, | ||
includeExtensions?: string[], | ||
internalTypings?: boolean | ||
instanceName?: string | ||
} | ||
@@ -68,9 +47,11 @@ | ||
private previousRunFailed: boolean = false; | ||
// Whether to generate the @internal typing files (they are only generated when `stripInternal` is | ||
// true) | ||
private genInternalTypings: boolean = false; | ||
private instanceName: string; | ||
static includeExtensions = ['.ts','.js']; | ||
static includeExtensions = ['.ts', '.js']; | ||
constructor(public inputPath: string, public cachePath: string, public options?: DiffingCompilerOptions ) { | ||
private debugWithName(msg: string) { | ||
debug(`[name ${this.instanceName}] ${msg}`); | ||
} | ||
constructor(public inputPath: string, public cachePath: string, public options?: DiffingCompilerOptions) { | ||
if (options && options.rootFilePaths) { | ||
@@ -86,15 +67,7 @@ this.rootFilePaths = options.rootFilePaths.splice(0); | ||
if (options && options.internalTypings) { | ||
this.genInternalTypings = true; | ||
} | ||
this.tsOpts = (options && options.tsOptions) || {}; | ||
if ((<any>this.tsOpts).stripInternal === false) { | ||
// @internal are included in the generated .d.ts, do not generate them separately | ||
this.genInternalTypings = false; | ||
} | ||
this.tsOpts.rootDir = inputPath; | ||
this.tsOpts.outDir = this.cachePath; | ||
this.instanceName = this.options.instanceName; | ||
@@ -104,10 +77,11 @@ if (this.rootFilePaths && this.rootFilePaths.length) { | ||
} | ||
debug("CustomLanguageServiceHost inputPath " + this.inputPath); | ||
debug(`[name ${this.instanceName}] CustomLanguageServiceHost inputPath ${this.inputPath}`); | ||
let localTypesFolder = options.localTypesFolder || `${process.cwd()}/local-types`; | ||
this.tsServiceHost = new CustomLanguageServiceHost( | ||
this.tsOpts, | ||
this.rootFilePaths, | ||
this.fileRegistry, | ||
this.inputPath, | ||
localTypesFolder); | ||
this.tsOpts, | ||
this.rootFilePaths, | ||
this.fileRegistry, | ||
this.inputPath, | ||
localTypesFolder, | ||
this.instanceName); | ||
this.tsService = ts.createLanguageService(this.tsServiceHost, ts.createDocumentRegistry()); | ||
@@ -124,3 +98,3 @@ } | ||
if (!this.fileRegistry[tsFilePath]) { | ||
this.fileRegistry[tsFilePath] = {version: 0}; | ||
this.fileRegistry[tsFilePath] = { version: 0 }; | ||
this.rootFilePaths.push(tsFilePath); | ||
@@ -147,7 +121,7 @@ } else { | ||
let program = this.tsService.getProgram(); | ||
tsEmitInternal = false; | ||
pathsToEmit.forEach((tsFilePath) => { | ||
let output = this.tsService.getEmitOutput(tsFilePath); | ||
if (output.emitSkipped) { | ||
if (output.emitSkipped) { | ||
// there was an error, report it | ||
let errorFound = this.collectErrors(tsFilePath); | ||
@@ -173,3 +147,3 @@ if (errorFound) { | ||
var error = | ||
new Error('Typescript found the following errors:\n' + errorMessages.join('\n')); | ||
new Error(`[name ${this.instanceName}] Typescript found the following errors:\n` + errorMessages.join('\n')); | ||
(<any>error)['showStack'] = false; | ||
@@ -179,18 +153,2 @@ throw error; | ||
this.doFullBuild(); | ||
} else if (this.genInternalTypings) { | ||
// serialize the .d.ts files containing @internal symbols | ||
tsEmitInternal = true; | ||
pathsToEmit.forEach((tsFilePath) => { | ||
let output = this.tsService.getEmitOutput(tsFilePath); | ||
if (!output.emitSkipped) { | ||
output.outputFiles.forEach(o => { | ||
if (endsWith(o.name, '.d.ts')) { | ||
let destDirPath = path.dirname(o.name); | ||
fse.mkdirsSync(destDirPath); | ||
fs.writeFileSync(o.name, o.text, FS_OPTS); | ||
} | ||
}); | ||
} | ||
}); | ||
tsEmitInternal = false; | ||
} | ||
@@ -202,4 +160,4 @@ } | ||
let allDiagnostics = this.tsService.getCompilerOptionsDiagnostics() | ||
.concat(this.tsService.getSyntacticDiagnostics(tsFilePath)) | ||
.concat(this.tsService.getSemanticDiagnostics(tsFilePath)); | ||
.concat(this.tsService.getSyntacticDiagnostics(tsFilePath)) | ||
.concat(this.tsService.getSemanticDiagnostics(tsFilePath)); | ||
let errors: string[] = []; | ||
@@ -226,3 +184,2 @@ | ||
let diagnostics: ts.Diagnostic[] = []; | ||
tsEmitInternal = false; | ||
@@ -237,19 +194,7 @@ let emitResult = program.emit(undefined, (absoluteFilePath, fileContent) => { | ||
const originalFile = absoluteFilePath.replace(this.tsOpts.outDir, this.tsOpts.rootDir) | ||
.replace(/\.d\.ts$/, '.ts'); | ||
.replace(/\.d\.ts$/, '.ts'); | ||
const sourceFile = program.getSourceFile(originalFile); | ||
} | ||
}); | ||
if (this.genInternalTypings) { | ||
// serialize the .d.ts files containing @internal symbols | ||
tsEmitInternal = true; | ||
program.emit(undefined, (absoluteFilePath, fileContent) => { | ||
if (endsWith(absoluteFilePath, '.d.ts')) { | ||
fse.mkdirsSync(path.dirname(absoluteFilePath)); | ||
fs.writeFileSync(absoluteFilePath, fileContent, FS_OPTS); | ||
} | ||
}); | ||
tsEmitInternal = false; | ||
} | ||
if (emitResult.emitSkipped) { | ||
@@ -272,3 +217,3 @@ let allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); | ||
var error = | ||
new Error('Typescript found the following errors:\n' + errorMessages.join('\n')); | ||
new Error(`[name ${this.instanceName}] Typescript found the following errors:\n` + errorMessages.join('\n')); | ||
(<any>error)['showStack'] = false; | ||
@@ -299,8 +244,8 @@ throw error; | ||
function fileExists(fileName: string): boolean { | ||
return ts.sys.fileExists(fileName); | ||
} | ||
return ts.sys.fileExists(fileName); | ||
} | ||
function readFile(fileName: string): string { | ||
return ts.sys.readFile(fileName); | ||
} | ||
function readFile(fileName: string): string { | ||
return ts.sys.readFile(fileName); | ||
} | ||
@@ -314,7 +259,9 @@ | ||
constructor( | ||
private compilerOptions: ts.CompilerOptions, | ||
private fileNames: string[], | ||
private fileRegistry: FileRegistry, | ||
private treeInputPath: string, | ||
private localTypesFolder: string) { | ||
private compilerOptions: ts.CompilerOptions, | ||
private fileNames: string[], | ||
private fileRegistry: FileRegistry, | ||
private treeInputPath: string, | ||
private localTypesFolder: string, | ||
private instanceName: string | ||
) { | ||
this.currentDirectory = process.cwd(); | ||
@@ -373,7 +320,7 @@ this.defaultLibFilePath = ts.getDefaultLibFilePath(compilerOptions).replace(/\\/g, '/'); | ||
resolveModuleNames(moduleNames: string[], containingFile: string): ts.ResolvedModule[] { | ||
return moduleNames.map(name=>{ | ||
return moduleNames.map(name => { | ||
// first try the default resolution | ||
let result = ts.resolveModuleName(name, containingFile, this.compilerOptions, {fileExists, readFile}); | ||
let result = ts.resolveModuleName(name, containingFile, this.compilerOptions, { fileExists, readFile }); | ||
if (result.resolvedModule) { | ||
return result.resolvedModule; | ||
return result.resolvedModule; | ||
} | ||
@@ -392,7 +339,7 @@ let candidatePaths: string[] = []; | ||
// however we support a local-types for development. | ||
const module = (name.indexOf('npm:')===0) ? name.split(':')[1] : name; | ||
const module = (name.indexOf('npm:') === 0) ? name.split(':')[1] : name; | ||
candidatePaths.push(`${this.localTypesFolder}/${module}/index.d.ts`); | ||
candidatePaths.push(`${this.currentDirectory}/node_modules/@types/${module}/index.d.ts`); | ||
} | ||
for( let i=0; i<candidatePaths.length; i++) { | ||
for (let i = 0; i < candidatePaths.length; i++) { | ||
if (fs.existsSync(candidatePaths[i])) { | ||
@@ -405,3 +352,3 @@ return { | ||
} | ||
debug(`resolveModuleNames skipping module '${name}'`); | ||
(`[name ${this.instanceName}] resolveModuleNames skipping module '${name}'`); | ||
return undefined; | ||
@@ -408,0 +355,0 @@ }); |
12
113879
2565
Updatedtypescript@^2.0.0