Skip to content

Commit

Permalink
#96: Without outDir twc outputs to project root
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Busłowicz committed Jul 16, 2017
1 parent 6e9d848 commit b992593
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import {
ModuleBlock, ModuleDeclaration, NamespaceImport, Node, NoSubstitutionTemplateLiteral, PropertyAccessExpression, PropertyDeclaration,
PropertySignature, SourceFile, Statement, StringLiteral, SyntaxKind, TemplateExpression, TypeLiteralNode, TypeNode
} from "typescript";
import { cache, paths, projectRoot } from "./config";
import { cache, outPath, paths, projectRoot } from "./config";
import * as decoratorsMap from "./decorators";
import { DecoratorExtras } from "./decorators";
import {
DecoratorsMixin, getRoot, hasDecorator, hasModifier, inheritsFrom, InitializerWrapper, isExtendsDeclaration, isOneOf, isStatic,
JSDocMixin, Link, notPrivate, notStatic, outPath, ParsedDecorator, RefUpdaterMixin, stripQuotes
JSDocMixin, Link, notPrivate, notStatic, ParsedDecorator, RefUpdaterMixin, stripQuotes
} from "./helpers";
import * as buildTargets from "./targets";
import { parseDeclaration, parseDeclarationType, ValidValue } from "./type-analyzer";
Expand Down
3 changes: 1 addition & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { existsSync, mkdirSync, readFileSync, watchFile, writeFileSync } from "f
import { dirname, join } from "path";
import { createSourceFile, MapLike, SourceFile } from "typescript";
import { Module } from "./builder";
import { cache, cli, compilerOptions, compileTo, errors, files, twc } from "./config";
import { outPath } from "./helpers";
import { cache, cli, compilerOptions, compileTo, errors, files, outPath, twc } from "./config";

/**
* Make sure the path exists. If it doesn't, create it.
Expand Down
16 changes: 16 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ export function findRootDir(files: Array<string>): string {
return first.slice(0, i).join(sep);
}

/**
* Create an output path for the file.
*
* @param path File path to create output path for
* @param outDir Output path
* @param rootDir Sources root path (Longest Common Prefix)
*
* @returns Path relative to outDir
*/
export function outPath(path: string, { outDir, rootDir } = compilerOptions) {
if (outDir) {
return join(outDir, relative(rootDir, path));
}
return path;
}

/**
* Read file synchronously and return as a string.
*
Expand Down
16 changes: 1 addition & 15 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { readFileSync } from "fs";
import { dirname, join, relative, resolve } from "path";
import { dirname, resolve } from "path";
import {
BinaryExpression, CallExpression, ClassDeclaration, ClassElement, ExpressionStatement, forEachChild, FunctionExpression, HeritageClause,
Identifier, InterfaceDeclaration, isFunctionLike, isGetAccessorDeclaration, isIdentifier, isPropertyDeclaration, isSetAccessorDeclaration,
JSDoc, NamedDeclaration, Node, PrefixUnaryExpression, PropertyAccessExpression, SourceFile, SyntaxKind
} from "typescript";
import { Constructor } from "../types/index";
import { ImportedNode, Method } from "./builder";
import { compilerOptions } from "./config";

/**
* List of types that do not change the overall type.
Expand Down Expand Up @@ -618,16 +617,3 @@ export const updateImportedRefs = (src: Node, vars: Map<string, ImportedNode>):
}, src.getFullText().split(""))
.join("");
};

/**
* Create an output path for the file.
*
* @param path File path to create output path for
* @param outDir Output path
* @param rootDir Sources root path (Longest Common Prefix)
*
* @returns Path relative to outDir
*/
export const outPath = (path: string, { outDir = "", rootDir } = compilerOptions) => {
return join(outDir, relative(rootDir, path));
};
21 changes: 20 additions & 1 deletion tests/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";
import { createSourceFile, ScriptTarget } from "typescript";
import { cache, findRootDir } from "../src/config";
import { cache, findRootDir, outPath } from "../src/config";

describe("config", () => {
describe("findRootDir", () => {
Expand Down Expand Up @@ -64,4 +64,23 @@ describe("config", () => {
expect(cache.modules.has("bower:some.html")).to.equal(false);
});
});
describe("outPath()", () => {
it("should calculate path with no rootDir and outDir", () => {
expect(outPath("file.ts", { rootDir: "" })).to.equal("file.ts");
expect(outPath("deep/file.ts", { rootDir: "" })).to.equal("deep/file.ts");
});
it("should calculate path with rootDir set", () => {
expect(outPath("src/file.ts", { rootDir: "src" })).to.equal("src/file.ts");
expect(outPath("src/deep/file.ts", { rootDir: "src" })).to.equal("src/deep/file.ts");
});
it("should calculate path with outDir set", () => {
expect(outPath("file.ts", { rootDir: "", outDir: "dist" })).to.equal("dist/file.ts");
expect(outPath("deep/file.ts", { rootDir: "", outDir: "dist" })).to.equal("dist/deep/file.ts");
});
it("should calculate path with both rootDir and outDir set", () => {
expect(outPath("src/file.ts", { rootDir: "src", outDir: "dist" })).to.equal("dist/file.ts");
expect(outPath("src/deep/file.ts", { rootDir: "src", outDir: "dist" })).to.equal("dist/deep/file.ts");
expect(outPath("src/deep/file.ts", { rootDir: "src/deep", outDir: "dist" })).to.equal("dist/file.ts");
});
});
});
20 changes: 1 addition & 19 deletions tests/tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as decoratorsMap from "../src/decorators";
import {
flatExtends, flattenArray, getDecorators, getText, hasArguments, hasDecorator, hasModifier, hasOperator, hasOperatorToken,
hasOriginalKeywordKind, inheritsFrom, InitializerWrapper, isAllOf, isExtendsDeclaration, isOneOf, isPrivate, isPublic, isStatic,
isTransparent, Link, notPrivate, notPublic, notStatic, notTransparent, outPath, Ref, toProperty, toString
isTransparent, Link, notPrivate, notPublic, notStatic, notTransparent, Ref, toProperty, toString
} from "../src/helpers";
import {
getFinalType, getSimpleKind, parseDeclaration, parseDeclarationInitializer, parseDeclarationType, parseExpression,
Expand Down Expand Up @@ -301,24 +301,6 @@ describe("helpers", () => {
// TODO: write getQuoteChar tests
// TODO: write getRoot tests
// TODO: write updateImportedRefs tests
describe("outPath()", () => {
it("should calculate path with no rootDir and outDir", () => {
expect(outPath("file.ts", { rootDir: "" })).to.equal("file.ts");
expect(outPath("deep/file.ts", { rootDir: "" })).to.equal("deep/file.ts");
});
it("should calculate path with outDir set", () => {
expect(outPath("file.ts", { rootDir: "", outDir: "dist" })).to.equal("dist/file.ts");
expect(outPath("deep/file.ts", { rootDir: "", outDir: "dist" })).to.equal("dist/deep/file.ts");
});
it("should calculate path with rootDir set", () => {
expect(outPath("src/file.ts", { rootDir: "src" })).to.equal("file.ts");
expect(outPath("src/deep/file.ts", { rootDir: "src" })).to.equal("deep/file.ts");
});
it("should calculate path with both rootDir and outDir set", () => {
expect(outPath("src/file.ts", { rootDir: "src", outDir: "dist" })).to.equal("dist/file.ts");
expect(outPath("src/deep/file.ts", { rootDir: "src", outDir: "dist" })).to.equal("dist/deep/file.ts");
});
});
});
describe("type analyzer", () => {
describe("parseUnionOrIntersectionType()", () => {
Expand Down

0 comments on commit b992593

Please sign in to comment.