From 8ffbcb6995101900889d84217d0a0caa67334861 Mon Sep 17 00:00:00 2001 From: Mario Nebl Date: Wed, 22 Nov 2017 19:21:54 +0100 Subject: [PATCH] fix: resolve correctly on win32 --- src/index.ts | 4 ++-- src/transpiler.test.ts | 2 +- src/transpiler.ts | 9 +++++---- src/utils.ts | 5 +++++ 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index c95e6f1..f1d872e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,7 +12,7 @@ function writeDeclaration(input: PatterplateFile, output: TranspileOutput, appli if (output.declarationText) { const patterns = Object .keys(input.pattern.manifest.patterns || {}); - let minDepth: number = -1; + let minDepth = -1; patterns.forEach(pattern => { const remote = ((input.pattern.manifest.patterns || {}) as any)[pattern]; const remoteDepth = remote.split(sep); @@ -88,7 +88,7 @@ function transpileFile(file: PatterplateFile, compilerOptions: ts.CompilerOption } function buildPattternMap(file: PatterplateFile, map: DependencyMap): void { - map[file.path] = file; + map[utils.normalizePath(file.path)] = file; if (file.dependencies) { Object .keys(file.dependencies) diff --git a/src/transpiler.test.ts b/src/transpiler.test.ts index 651f259..158972a 100644 --- a/src/transpiler.test.ts +++ b/src/transpiler.test.ts @@ -16,7 +16,7 @@ test('Error during declaration building should fail fast', t => { export default ColorOptions; `; - const options = ts.getDefaultCompilerOptions(); + const options: any = ts.getDefaultCompilerOptions(); const manifest = {}; const root = '/tmp'; diff --git a/src/transpiler.ts b/src/transpiler.ts index f4427ef..73b0906 100644 --- a/src/transpiler.ts +++ b/src/transpiler.ts @@ -3,6 +3,7 @@ import * as path from 'path'; import * as ts from 'typescript'; import { mapJsx, mapTarget, mapModule } from './options'; import { DependencyMap } from './types'; +import * as utils from './utils'; export interface TranspileOptions { compilerOptions?: ts.CompilerOptions; @@ -57,7 +58,7 @@ export function transpileModule(input: string, transpileOptions: TranspileOption } // if jsx is specified then treat file as .tsx - const inputFileName = transpileOptions.fileName || (options.jsx ? 'module.tsx' : 'module.ts'); + const inputFileName = utils.normalizePath(transpileOptions.fileName || (options.jsx ? 'module.tsx' : 'module.ts')); const sourceFile = ts.createSourceFile(inputFileName, input, options.target || ts.ScriptTarget.ES5); if (transpileOptions.moduleName) { sourceFile.moduleName = transpileOptions.moduleName; @@ -75,7 +76,7 @@ export function transpileModule(input: string, transpileOptions: TranspileOption return sourceFile; } try { - return ts.createSourceFile(fileName, ts.sys.readFile(fileName), options.target || ts.ScriptTarget.ES5); + return ts.createSourceFile(fileName, ts.sys.readFile(fileName) || '', options.target || ts.ScriptTarget.ES5); } catch (e) { console.error('failed to read source-file', fileName); return undefined!; @@ -126,7 +127,7 @@ export function transpileModule(input: string, transpileOptions: TranspileOption ); allDiagnostics.forEach(diagnostic => { const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); - if (diagnostic.file) { + if (diagnostic.file && typeof diagnostic.start === 'number') { const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); throw new Error(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); } else { @@ -150,7 +151,7 @@ export function transpileModule(input: string, transpileOptions: TranspileOption }; } -function getDeclarationDiagnostics(program: ts.Program): ts.Diagnostic[] { +function getDeclarationDiagnostics(program: ts.Program): ReadonlyArray { try { return program.getDeclarationDiagnostics(); } catch (e) { diff --git a/src/utils.ts b/src/utils.ts index 1f9d835..6c69348 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,4 @@ +import { sep } from 'path'; import { Application, OutputArtifact } from './types'; export function addOutputArtifact(application: Application, artifact: OutputArtifact): void { @@ -10,3 +11,7 @@ export function addOutputArtifact(application: Application, artifact: OutputArti console.warn(`Tried to write additional artifacts but your patternplate version is outdated. Try to update`); } } + +export function normalizePath(input: string): string { + return input.split(sep).join('/'); +}