Skip to content

Commit

Permalink
fix(resolvePaths): Use cwd-relative paths instead of absolute paths (l…
Browse files Browse the repository at this point in the history
…int-staged#116)

* fix(resolvePaths): return CWD-relative paths - closes lint-staged#115
* chore(package): removing obsolete devDependency "tmp"

Fixes lint-staged#115
  • Loading branch information
rodneyrehm authored and okonet committed Dec 30, 2016
1 parent 97ad4b4 commit 968e0d8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
"npm-check": "^5.2.2",
"pre-commit": "^1.1.3",
"rewire": "^2.5.1",
"semantic-release": "^6.3.2",
"tmp": "^0.0.31"
"semantic-release": "^6.3.2"
},
"config": {
"commitizen": {
Expand Down
7 changes: 3 additions & 4 deletions src/resolvePaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
const path = require('path')

module.exports = function resolvePaths(filePaths, relativeTo) {
return filePaths.map((file) => {
if (!relativeTo) relativeTo = process.cwd() // eslint-disable-line
return path.resolve(path.relative(process.cwd(), relativeTo), file.filename)
})
const cwd = process.cwd()
const base = relativeTo || cwd
return filePaths.map(file => path.relative(cwd, path.resolve(base, file.filename)))
}
17 changes: 5 additions & 12 deletions test/resolvePaths.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import expect from 'expect'
import path from 'path'
import fs from 'fs'
import tmp from 'tmp'
import resolvePaths from '../src/resolvePaths'

const files = fs.readdirSync(path.resolve('test', '__fixtures__')).map(file => ({
filename: file
}))

const cwd = process.cwd()
const cwdParent = path.relative(cwd, '..')
const cwdParent = path.resolve('..')

describe('resolvePaths', () => {
it('should return Array of filenames', () => {
Expand All @@ -22,22 +20,17 @@ describe('resolvePaths', () => {
'test.css',
'test.js',
'test.txt'
].map(file => path.resolve(cwd, file))
]
)
})

it('should return relative paths if second parameter is set', () => {
expect(resolvePaths(files, '..')).toEqual(
it('should return CWD-relative paths if second parameter is set', () => {
expect(resolvePaths(files, cwdParent)).toEqual(
[
'test.css',
'test.js',
'test.txt'
].map(file => path.resolve(cwdParent, file))
].map(file => path.join('..', file))
)
})

it('should return absolute paths if they were absolute', () => {
const tmpFile = tmp.fileSync()
expect(resolvePaths([{ filename: tmpFile.name }], '..')).toEqual([tmpFile.name])
})
})

0 comments on commit 968e0d8

Please sign in to comment.