Comparing version 0.2.0 to 0.3.0
@@ -0,1 +1,5 @@ | ||
/** | ||
* The TraceLoc interface it provides the location | ||
* infomration and a toString function. | ||
*/ | ||
export interface ITraceLoc { | ||
@@ -8,2 +12,25 @@ readonly func: string; | ||
} | ||
/** | ||
* The user may change to expected root of the project | ||
* and filepaths returned file ITraceLoc.file will be | ||
* relative to the root parameter. The default if not set | ||
* (i.e. "", undefined or null) is ".", the current | ||
* working directory. | ||
* | ||
* @param root is the path to the directory containing the project | ||
* it may be relative or absolute. | ||
* @param returns previous value | ||
*/ | ||
export declare function setProjectRoot(root: string | undefined | null): string | undefined | null; | ||
/** | ||
* Return the ITraceLoc oject. | ||
* | ||
* @param callDepth is the stackframe index which to retrieve | ||
* the location information. Defaults to 0. A non-zero | ||
* value can be used to provide the location for the | ||
* n'th entry on stackframe. This is useful if custom | ||
* here() is created that calls this here(). See | ||
* example/t3.ts. | ||
* @return ITraceLoc | ||
*/ | ||
export declare function here(callDepth?: number): ITraceLoc; | ||
@@ -31,2 +58,3 @@ export declare class TraceLoc implements ITraceLoc { | ||
toString(): string; | ||
getRelativeFileName(prjRoot: string | undefined | null, fileName: string): string; | ||
/** | ||
@@ -33,0 +61,0 @@ * Update the location info |
@@ -6,2 +6,31 @@ "use strict"; | ||
const path = require("path"); | ||
let projectRoot; | ||
/** | ||
* The user may change to expected root of the project | ||
* and filepaths returned file ITraceLoc.file will be | ||
* relative to the root parameter. The default if not set | ||
* (i.e. "", undefined or null) is ".", the current | ||
* working directory. | ||
* | ||
* @param root is the path to the directory containing the project | ||
* it may be relative or absolute. | ||
* @param returns previous value | ||
*/ | ||
function setProjectRoot(root) { | ||
const prev = projectRoot; | ||
projectRoot = root; | ||
return prev; | ||
} | ||
exports.setProjectRoot = setProjectRoot; | ||
/** | ||
* Return the ITraceLoc oject. | ||
* | ||
* @param callDepth is the stackframe index which to retrieve | ||
* the location information. Defaults to 0. A non-zero | ||
* value can be used to provide the location for the | ||
* n'th entry on stackframe. This is useful if custom | ||
* here() is created that calls this here(). See | ||
* example/t3.ts. | ||
* @return ITraceLoc | ||
*/ | ||
function here(callDepth = 0) { | ||
@@ -62,2 +91,17 @@ return new TraceLoc(callDepth + 1); | ||
} | ||
getRelativeFileName(prjRoot, fileName) { | ||
let relative; | ||
// If nothing the use __dirname | ||
if (prjRoot === undefined || prjRoot === null) { | ||
prjRoot = "."; | ||
} | ||
if (prjRoot) { | ||
relative = path.relative(prjRoot, fileName); | ||
} | ||
else { | ||
// If no prjRoot return fileName | ||
relative = fileName; | ||
} | ||
return relative; | ||
} | ||
/** | ||
@@ -73,3 +117,2 @@ * Update the location info | ||
// log(`getLocation: stack[tos]=${stack[stack.length - 1]}`); | ||
const projectRoot = path.dirname(__dirname); | ||
// Check for non-anonymous function which means | ||
@@ -80,3 +123,4 @@ // location string is of the form; " at func (file:line:col)" | ||
if (r && r.length > 4) { | ||
relative = path.relative(projectRoot, r[2]); | ||
relative = this.getRelativeFileName(projectRoot, r[2]); | ||
// log(`len > 4 projectRoot=${projectRoot} r[2]=${r[2]} relative=${relative}`); | ||
this._file = relative; | ||
@@ -92,3 +136,4 @@ this._func = r[1]; | ||
if (r && r.length > 3) { | ||
relative = path.relative(projectRoot, r[1]); | ||
relative = this.getRelativeFileName(projectRoot, r[1]); | ||
// log(`len > 3 projectRoot=${projectRoot} r[1]=${r[1]} relative=${relative}`); | ||
this._file = relative; | ||
@@ -95,0 +140,0 @@ this._func = ""; |
export declare class TracingTests { | ||
testSetProjectRoot(): void; | ||
testTracing(): void; | ||
@@ -3,0 +4,0 @@ testTraceLocEmptyStack(): void; |
@@ -14,3 +14,40 @@ "use strict"; | ||
const traceloc_1 = require("../out/traceloc"); | ||
const os = require("os"); | ||
const path = require("path"); | ||
class TracingTests { | ||
testSetProjectRoot() { | ||
let loc; | ||
let orgVal = traceloc_1.setProjectRoot("weirdValue"); | ||
alsatian_1.Expect(traceloc_1.setProjectRoot(orgVal)).toBe("weirdValue"); | ||
alsatian_1.Expect(traceloc_1.setProjectRoot(orgVal)).toBe(orgVal); | ||
traceloc_1.setProjectRoot(undefined); | ||
loc = new traceloc_1.TraceLoc(); | ||
alsatian_1.Expect(loc.file).toBe("src/traceloc.spec.ts"); | ||
traceloc_1.setProjectRoot(null); | ||
loc = new traceloc_1.TraceLoc(); | ||
alsatian_1.Expect(loc.file).toBe("src/traceloc.spec.ts"); | ||
traceloc_1.setProjectRoot("."); | ||
loc = new traceloc_1.TraceLoc(); | ||
alsatian_1.Expect(loc.file).toBe("src/traceloc.spec.ts"); | ||
traceloc_1.setProjectRoot(""); | ||
loc = new traceloc_1.TraceLoc(); | ||
if (os.type() === "Linux") { | ||
alsatian_1.Expect(loc.file.indexOf("/")).toBe(0); | ||
} | ||
alsatian_1.Expect(loc.file.indexOf("src/traceloc.spec.ts")).toBeGreaterThan(0); | ||
traceloc_1.setProjectRoot("/"); | ||
loc = new traceloc_1.TraceLoc(); | ||
if (!path.relative("/", ".")) { | ||
// Current working directory is / be careful :) | ||
alsatian_1.Expect(loc.file.indexOf("src/traceloc.spec.ts")).toBe(0); | ||
} | ||
else { | ||
// Current working directory is not / | ||
alsatian_1.Expect(loc.file.indexOf("src/traceloc.spec.ts")).toBeGreaterThan(0); | ||
} | ||
traceloc_1.setProjectRoot(path.join(__dirname)); | ||
loc = new traceloc_1.TraceLoc(); | ||
alsatian_1.Expect(loc.file).toBe("../src/traceloc.spec.ts"); | ||
traceloc_1.setProjectRoot(orgVal); | ||
} | ||
testTracing() { | ||
@@ -99,2 +136,8 @@ const loc = traceloc_1.here(); | ||
__metadata("design:returntype", void 0) | ||
], TracingTests.prototype, "testSetProjectRoot", null); | ||
__decorate([ | ||
alsatian_1.Test(), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", []), | ||
__metadata("design:returntype", void 0) | ||
], TracingTests.prototype, "testTracing", null); | ||
@@ -101,0 +144,0 @@ __decorate([ |
{ | ||
"name": "traceloc", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Trace the current location, i.e. filename, line, col ...", | ||
@@ -41,3 +41,3 @@ "main": "./out/traceloc.js", | ||
"test:dbg": "node --inspect-brk ./node_modules/alsatian/cli/alsatian-cli.js out/traceloc.spec.js --tap", | ||
"pretest:traceloc": "yarn build && tsc -p src/traceloc.spec.tsconfig.json && yarn review", | ||
"pretest:traceloc": "yarn build && tsc -p src/traceloc.spec.tsconfig.json", | ||
"test:traceloc": "alsatian out/traceloc.spec.js", | ||
@@ -44,0 +44,0 @@ "test": "yarn test:traceloc && yarn test:js", |
@@ -15,6 +15,10 @@ # Trace the current location in a program [![Build Status](https://travis-ci.org/winksaville/traceloc.svg?branch=master)](https://travis-ci.org/winksaville/traceloc) | ||
## API | ||
A very simple API there one routine, here(), and an | ||
interface, ITraceLoc, are exported: | ||
A very simple API there two routines, here(), setProjectRoot() | ||
and an interface, ITraceLoc exported: | ||
``` | ||
interface ITraceLoc { | ||
/** | ||
* The TraceLoc interface it provides the location | ||
* infomration and a toString function. | ||
*/ | ||
export interface ITraceLoc { | ||
readonly func: string; | ||
@@ -24,7 +28,27 @@ readonly file: string; | ||
readonly col: number; | ||
toString(): string; | ||
} | ||
export here(callDepth=0): ITraceLoc; | ||
/** | ||
* The user may change to expected root of the project | ||
* and filepaths returned file ITraceLoc.file will be | ||
* relative to the root parameter. The default if not set | ||
* (i.e. "", undefined or null) is ".", the current | ||
* working directory. | ||
* | ||
* @param root is the full path to the directory containing the project | ||
* @param returns previous value | ||
*/ | ||
export declare function setProjectRoot(root: string | undefined | null): string | undefined | null; | ||
/** | ||
* Return the ITraceLoc oject. | ||
* | ||
* @param callDepth is the stackframe index which to retrieve | ||
* the location information. Defaults to 0. A non-zero | ||
* value can be used to provide the location for the | ||
* n'th entry on stackframe. This is useful if custom | ||
* here() is created that calls this here(). See | ||
* example/t3.ts. | ||
* @return ITraceLoc | ||
*/ | ||
export declare function here(callDepth?: number): ITraceLoc; | ||
``` | ||
@@ -31,0 +55,0 @@ ## Examples: |
const Expect = require("alsatian").Expect; | ||
const here = require("../out/traceloc").here; | ||
const setProjectRoot = require("../out/traceloc").setProjectRoot; | ||
setProjectRoot(__dirname); | ||
// A self executing anonymous function | ||
@@ -5,0 +8,0 @@ (function() { |
@@ -7,7 +7,54 @@ import { | ||
import { here, ITraceLoc, TraceLoc } from "../out/traceloc"; | ||
import { here, ITraceLoc, setProjectRoot, TraceLoc } from "../out/traceloc"; | ||
import * as os from "os"; | ||
import * as path from "path"; | ||
export class TracingTests { | ||
@Test() | ||
public testSetProjectRoot() { | ||
let loc: ITraceLoc; | ||
let orgVal = setProjectRoot("weirdValue"); | ||
Expect(setProjectRoot(orgVal)).toBe("weirdValue"); | ||
Expect(setProjectRoot(orgVal)).toBe(orgVal); | ||
setProjectRoot(undefined); | ||
loc = new TraceLoc(); | ||
Expect(loc.file).toBe("src/traceloc.spec.ts"); | ||
setProjectRoot(null); | ||
loc = new TraceLoc(); | ||
Expect(loc.file).toBe("src/traceloc.spec.ts"); | ||
setProjectRoot("."); | ||
loc = new TraceLoc(); | ||
Expect(loc.file).toBe("src/traceloc.spec.ts"); | ||
setProjectRoot(""); | ||
loc = new TraceLoc(); | ||
if (os.type() === "Linux") { | ||
Expect(loc.file.indexOf("/")).toBe(0); | ||
} | ||
Expect(loc.file.indexOf("src/traceloc.spec.ts")).toBeGreaterThan(0); | ||
setProjectRoot("/"); | ||
loc = new TraceLoc(); | ||
if (!path.relative("/", ".")) { | ||
// Current working directory is / be careful :) | ||
Expect(loc.file.indexOf("src/traceloc.spec.ts")).toBe(0); | ||
} else { | ||
// Current working directory is not / | ||
Expect(loc.file.indexOf("src/traceloc.spec.ts")).toBeGreaterThan(0); | ||
} | ||
setProjectRoot(path.join(__dirname)); | ||
loc = new TraceLoc(); | ||
Expect(loc.file).toBe("../src/traceloc.spec.ts"); | ||
setProjectRoot(orgVal); | ||
} | ||
@Test() | ||
public testTracing() { | ||
@@ -14,0 +61,0 @@ const loc = here(); |
@@ -6,2 +6,6 @@ import { install } from "source-map-support"; | ||
/** | ||
* The TraceLoc interface it provides the location | ||
* infomration and a toString function. | ||
*/ | ||
export interface ITraceLoc { | ||
@@ -16,2 +20,32 @@ readonly func: string; | ||
let projectRoot: string | undefined | null; | ||
/** | ||
* The user may change to expected root of the project | ||
* and filepaths returned file ITraceLoc.file will be | ||
* relative to the root parameter. The default if not set | ||
* (i.e. "", undefined or null) is ".", the current | ||
* working directory. | ||
* | ||
* @param root is the path to the directory containing the project | ||
* it may be relative or absolute. | ||
* @param returns previous value | ||
*/ | ||
export function setProjectRoot(root: string | undefined | null): string | undefined | null { | ||
const prev = projectRoot; | ||
projectRoot = root; | ||
return prev; | ||
} | ||
/** | ||
* Return the ITraceLoc oject. | ||
* | ||
* @param callDepth is the stackframe index which to retrieve | ||
* the location information. Defaults to 0. A non-zero | ||
* value can be used to provide the location for the | ||
* n'th entry on stackframe. This is useful if custom | ||
* here() is created that calls this here(). See | ||
* example/t3.ts. | ||
* @return ITraceLoc | ||
*/ | ||
export function here(callDepth = 0): ITraceLoc { | ||
@@ -80,2 +114,19 @@ return new TraceLoc(callDepth + 1); | ||
public getRelativeFileName(prjRoot: string | undefined | null, fileName: string): string { | ||
let relative: string; | ||
// If nothing the use __dirname | ||
if (prjRoot === undefined || prjRoot === null) { | ||
prjRoot = "."; | ||
} | ||
if (prjRoot) { | ||
relative = path.relative(prjRoot, fileName); | ||
} else { | ||
// If no prjRoot return fileName | ||
relative = fileName; | ||
} | ||
return relative; | ||
} | ||
/** | ||
@@ -91,3 +142,2 @@ * Update the location info | ||
// log(`getLocation: stack[tos]=${stack[stack.length - 1]}`); | ||
const projectRoot = path.dirname(__dirname); | ||
@@ -99,3 +149,4 @@ // Check for non-anonymous function which means | ||
if (r && r.length > 4) { | ||
relative = path.relative(projectRoot, r[2]); | ||
relative = this.getRelativeFileName(projectRoot, r[2]); | ||
// log(`len > 4 projectRoot=${projectRoot} r[2]=${r[2]} relative=${relative}`); | ||
this._file = relative; | ||
@@ -110,3 +161,4 @@ this._func = r[1]; | ||
if (r && r.length > 3) { | ||
relative = path.relative(projectRoot, r[1]); | ||
relative = this.getRelativeFileName(projectRoot, r[1]); | ||
// log(`len > 3 projectRoot=${projectRoot} r[1]=${r[1]} relative=${relative}`); | ||
this._file = relative; | ||
@@ -113,0 +165,0 @@ this._func = ""; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
93422
876
146