Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #15 from marionebl/fix/resolve-correctly-on-win32
Browse files Browse the repository at this point in the history
fix: resolve correctly on win32
  • Loading branch information
KnisterPeter committed Nov 22, 2017
2 parents 1a73d41 + 8ffbcb6 commit 152e5ec
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/transpiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
9 changes: 5 additions & 4 deletions src/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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!;
Expand Down Expand Up @@ -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 {
Expand All @@ -150,7 +151,7 @@ export function transpileModule(input: string, transpileOptions: TranspileOption
};
}

function getDeclarationDiagnostics(program: ts.Program): ts.Diagnostic[] {
function getDeclarationDiagnostics(program: ts.Program): ReadonlyArray<ts.Diagnostic> {
try {
return program.getDeclarationDiagnostics();
} catch (e) {
Expand Down
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sep } from 'path';
import { Application, OutputArtifact } from './types';

export function addOutputArtifact(application: Application, artifact: OutputArtifact): void {
Expand All @@ -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('/');
}

0 comments on commit 152e5ec

Please sign in to comment.