Skip to content

Commit

Permalink
Merge pull request #14 from stefanpenner/typescript
Browse files Browse the repository at this point in the history
typescript
  • Loading branch information
stefanpenner committed Mar 9, 2019
2 parents b096874 + 052c65b commit 832050c
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules/
*.js
*.js.map
*.d.ts
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!**.js
15 changes: 7 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ node_js:
- "6"
- "8"
- "10"
- "10"

script: npm test
before_install:
- npm config set spin false
cache:
yarn: true

install:
- node --version
- npm --version
- git --version
- npm install --no-optional
before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.13.0
- export PATH="$HOME/.yarn/bin:$PATH"
- yarn --version
16 changes: 9 additions & 7 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ environment:
- nodejs_version: "6"
- nodejs_version: "8"
- nodejs_version: "10"
- nodejs_version: "11"

# Install scripts. (runs after repo cloning)
install:
- git rev-parse HEAD
# Get the latest stable version of Node 0.STABLE.latest
- ps: Install-Product node $env:nodejs_version
# Typical npm stuff.
- md C:\nc
- npm config set cache C:\nc
- npm version
- npm install
- choco install yarn --version=1.13.0
- appveyor-retry yarn
- appveyor-retry yarn add mocha-appveyor-reporter # must be installed locally.

cache:
- '%LOCALAPPDATA%\Yarn'

# Post-install test scripts.
test_script:
# Output useful info for debugging.
- npm version
- cmd: npm test
- git rev-parse HEAD
- cmd: yarn run test

# Don't actually build.
build: off
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const _default: (position?: number) => any;
export = _default;
17 changes: 8 additions & 9 deletions index.js → index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
'use strict';

// Call this function in a another function to find out the file from
// which that function was called from. (Inspects the v8 stack trace)
//
// Inspired by http://stackoverflow.com/questions/13227489

module.exports = function getCallerFile(position = 2) {

export = function getCallerFile(position = 2) {
if (position >= Error.stackTraceLimit) {
throw new TypeError('getCallerFile(position) requires position be less then Error.stackTraceLimit but position was: `' + position + '` and Error.stackTraceLimit was: `' + Error.stackTraceLimit + '`');
}

const oldPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (err, stack) => stack;
Error.prepareStackTrace = (_, stack) => stack;
const stack = new Error().stack;
Error.prepareStackTrace = oldPrepareStackTrace;


// stack[0] holds this file
// stack[1] holds where this function was called
// stack[2] holds the file we're interested in
return stack[position] ? stack[position].getFileName() : undefined;
if (stack !== null && typeof stack === 'object') {
// stack[0] holds this file
// stack[1] holds where this function was called
// stack[2] holds the file we're interested in
return stack[position] ? (stack[position] as any).getFileName() : undefined;
}
};
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
"test": "tests"
},
"files": [
"index.js"
"index.js",
"index.js.map",
"index.d.ts"
],
"scripts": {
"prepare": "tsc",
"test": "mocha test",
"test:debug": "mocha test"
},
Expand All @@ -24,11 +27,18 @@
},
"homepage": "https://github.com/stefanpenner/get-caller-file#readme",
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/ensure-posix-path": "^1.0.0",
"@types/mocha": "^5.2.6",
"chai": "^4.1.2",
"ensure-posix-path": "^1.0.1",
"mocha": "^5.2.0"
"mocha": "^5.2.0",
"typescript": "^3.3.3333"
},
"engines": {
"node": "6.* || 8.* || >= 10.*"
},
"dependencies": {
"@types/node": "^11.10.5"
}
}
1 change: 1 addition & 0 deletions test.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
9 changes: 6 additions & 3 deletions test.js → test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
'use strict';

const getCallerFile = require('./');
const expect = require('chai').expect;
import getCallerFile = require('./');
import chai = require('chai');
import ensurePosix = require('ensure-posix-path');

const expect = chai.expect;

const foo = require('./fixtures/foo');
const bar = require('./fixtures/bar');
const ensurePosix = require('ensure-posix-path');

describe('getCallerFile', function() {
const originalStackTraceLimit = Error.stackTraceLimit;
Expand Down
10 changes: 10 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"typeRoots": ["node_modules/@types"],
"sourceMap": true,
"strict": true
}
}
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@
# yarn lockfile v1


"@types/chai@^4.1.7":
version "4.1.7"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.7.tgz#1b8e33b61a8c09cbe1f85133071baa0dbf9fa71a"
integrity sha512-2Y8uPt0/jwjhQ6EiluT0XCri1Dbplr0ZxfFXUz+ye13gaqE8u5gL5ppao1JrUYr9cIip5S6MvQzBS7Kke7U9VA==

"@types/ensure-posix-path@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/ensure-posix-path/-/ensure-posix-path-1.0.0.tgz#4942811afa582b6920c3bcd1361e48c25c4f4cdc"
integrity sha512-aShF4MyAr6mnok9ofP21H5myNfAXlX1BB0QoysrIg/NH/UOHpH5IoETUyK+ui7Oc1IxaiH20Kpwfx9NpwUnHaw==

"@types/mocha@^5.2.6":
version "5.2.6"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.2.6.tgz#b8622d50557dd155e9f2f634b7d68fd38de5e94b"
integrity sha512-1axi39YdtBI7z957vdqXI4Ac25e7YihYQtJa+Clnxg1zTJEaIRbndt71O3sP4GAMgiAm0pY26/b9BrY4MR/PMw==

"@types/node@^11.10.5":
version "11.10.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.10.5.tgz#fbaca34086bdc118011e1f05c47688d432f2d571"
integrity sha512-DuIRlQbX4K+d5I+GMnv+UfnGh+ist0RdlvOp+JZ7ePJ6KQONCFQv/gKYSU1ZzbVdFSUCKZOltjmpFAGGv5MdYA==

assertion-error@^1.0.1:
version "1.1.0"
resolved "https://registry.yarnpkg.com/assertion-error/-/assertion-error-1.1.0.tgz#e60b6b0e8f301bd97e5375215bda406c85118c0b"
Expand Down Expand Up @@ -170,6 +190,11 @@ type-detect@^4.0.0:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"

typescript@^3.3.3333:
version "3.3.3333"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.3.3333.tgz#171b2c5af66c59e9431199117a3bcadc66fdcfd6"
integrity sha512-JjSKsAfuHBE/fB2oZ8NxtRTk5iGcg6hkYXMnZ3Wc+b2RSqejEqTaem11mHASMnFilHrax3sLK0GDzcJrekZYLw==

wrappy@1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"

0 comments on commit 832050c

Please sign in to comment.