Skip to content

Commit

Permalink
chore: support powershell file(*.ps1)
Browse files Browse the repository at this point in the history
PR-URL: #6
Credit: @NoDocCat
Close: #6
Reviewed-by: @isaacs
  • Loading branch information
suiyun39 authored and isaacs committed Nov 5, 2019
1 parent 9679b59 commit 032a909
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ var fs = require('graceful-fs')
function extractPath (path, cmdshimContents) {
if (/[.]cmd$/.test(path)) {
return extractPathFromCmd(cmdshimContents)
} else if (/[.]ps1$/.test(path)) {
return extractPathFromPowershell(cmdshimContents)
} else {
return extractPathFromCygwin(cmdshimContents)
}
}

function extractPathFromPowershell (cmdshimContents) {
var matches = cmdshimContents.match(/"[$]basedir[/]([^"]+?)"\s+[$]args/)
return matches && matches[1]
}

function extractPathFromCmd (cmdshimContents) {
var matches = cmdshimContents.match(/"%(?:~dp0|dp0%)\\([^"]+?)"\s+%[*]/)
return matches && matches[1]
Expand Down
34 changes: 34 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ var readCmdShim = require('../index.js')
var workDir = path.join(__dirname, path.basename(__filename, '.js'))
var testShbang = path.join(workDir, 'test-shbang')
var testShbangCmd = testShbang + '.cmd'
var testShbangPowershell = testShbang + '.ps1'
var testShim = path.join(workDir, 'test')
var testShimCmd = testShim + '.cmd'
var testShimPowershell = testShim + '.ps1'

test('setup', function (t) {
rimraf.sync(workDir)
Expand Down Expand Up @@ -88,6 +90,38 @@ test('sync-read-shbang-cygwin', function (t) {
t.done()
})

test('async-read-no-shbang-powershell', function (t) {
t.plan(2)
readCmdShim(testShimPowershell, function (er, dest) {
t.error(er)
t.is(dest, '../integration.js')
t.done()
})
})

test('sync-read-no-shbang-powershell', function (t) {
t.plan(1)
var dest = readCmdShim.sync(testShimPowershell)
t.is(dest, '../integration.js')
t.done()
})

test('async-read-shbang-powershell', function (t) {
t.plan(2)
readCmdShim(testShbangPowershell, function (er, dest) {
t.error(er)
t.is(dest, 'test-shbang.js')
t.done()
})
})

test('sync-read-shbang-powershell', function (t) {
t.plan(1)
var dest = readCmdShim.sync(testShbangPowershell)
t.is(dest, 'test-shbang.js')
t.done()
})

test('async-read-dir', function (t) {
t.plan(2)
readCmdShim(workDir, function (er) {
Expand Down

0 comments on commit 032a909

Please sign in to comment.