Skip to content

Commit f76d4f2

Browse files
wraithgarlukekarrys
authored andcommitted
fix: consolidate is-windows code
1 parent d8dcc02 commit f76d4f2

16 files changed

+63
-70
lines changed

lib/commands/completion.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const nopt = require('nopt')
3737
const configNames = Object.keys(definitions)
3838
const shorthandNames = Object.keys(shorthands)
3939
const allConfs = configNames.concat(shorthandNames)
40-
const isWindowsShell = require('../utils/is-windows-shell.js')
40+
const { isWindowsShell } = require('../utils/is-windows.js')
4141
const fileExists = require('../utils/file-exists.js')
4242

4343
const { promisify } = require('util')

lib/commands/run-script.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { isServerPackage } = runScript
55
const rpj = require('read-package-json-fast')
66
const log = require('../utils/log-shim.js')
77
const didYouMean = require('../utils/did-you-mean.js')
8-
const isWindowsShell = require('../utils/is-windows-shell.js')
8+
const { isWindowsShell } = require('../utils/is-windows.js')
99

1010
const cmdList = [
1111
'publish',

lib/utils/config/definitions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { version: npmVersion } = require('../../../package.json')
77
const ciDetect = require('@npmcli/ci-detect')
88
const ciName = ciDetect()
99
const querystring = require('querystring')
10-
const isWindows = require('../is-windows.js')
10+
const { isWindows } = require('../is-windows.js')
1111
const { join } = require('path')
1212

1313
// used by cafile flattening to flatOptions.ca

lib/utils/error-message.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module.exports = (er, npm) => {
6060
npm.config.loaded &&
6161
er.dest.startsWith(npm.config.get('cache'))
6262

63-
const isWindows = require('./is-windows.js')
63+
const { isWindows } = require('./is-windows.js')
6464

6565
if (!isWindows && (isCachePath || isCacheDest)) {
6666
// user probably doesn't need this, but still add it to the debug log

lib/utils/is-windows-bash.js

-3
This file was deleted.

lib/utils/is-windows-shell.js

-3
This file was deleted.

lib/utils/is-windows.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
module.exports = process.platform === 'win32'
1+
const isWindows = process.platform === 'win32'
2+
const isWindowsShell = isWindows &&
3+
!/^MINGW(32|64)$/.test(process.env.MSYSTEM) && process.env.TERM !== 'cygwin'
4+
5+
exports.isWindows = isWindows
6+
exports.isWindowsShell = isWindowsShell

lib/utils/path.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// return the PATH array in a cross-platform way
2+
// TODO this is only used in a single place
23
const PATH = process.env.PATH || process.env.Path || process.env.path
34
const { delimiter } = require('path')
45
module.exports = PATH.split(delimiter)

test/lib/commands/completion.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const loadMockCompletion = async (t, o = {}) => {
1717
}
1818
const res = await _loadMockNpm(t, {
1919
mocks: {
20-
'../../lib/utils/is-windows-shell.js': !!windows,
20+
'../../lib/utils/is-windows.js': { isWindowsShell: !!windows },
2121
...options.mocks,
2222
},
2323
...options,

test/lib/commands/explore.js

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ const output = []
4747
const logs = []
4848
const getExplore = (windows) => {
4949
const Explore = t.mock('../../../lib/commands/explore.js', {
50-
'../../../lib/utils/is-windows.js': windows,
5150
path: require('path')[windows ? 'win32' : 'posix'],
5251
'read-package-json-fast': mockRPJ,
5352
'@npmcli/run-script': mockRunScript,

test/lib/commands/run-script.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const getRS = windows => {
6262
}
6363
),
6464
'proc-log': log,
65-
'../../../lib/utils/is-windows-shell.js': windows,
65+
'../../../lib/utils/is-windows.js': { isWindowsShell: windows },
6666
})
6767
return new RunScript(npm)
6868
}
@@ -859,7 +859,7 @@ t.test('workspaces', t => {
859859
throw new Error('err')
860860
},
861861
'proc-log': log,
862-
'../../../lib/utils/is-windows-shell.js': false,
862+
'../../../lib/utils/is-windows.js': { isWindowsShell: false },
863863
})
864864
const runScript = new RunScript(npm)
865865

@@ -877,7 +877,7 @@ t.test('workspaces', t => {
877877
RUN_SCRIPTS.push(opts)
878878
},
879879
'proc-log': log,
880-
'../../../lib/utils/is-windows-shell.js': false,
880+
'../../../lib/utils/is-windows.js': { isWindowsShell: false },
881881
})
882882
const runScript = new RunScript(npm)
883883

test/lib/utils/config/definitions.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ t.test('editor', t => {
5353
t.test('has neither EDITOR nor VISUAL, system specific', t => {
5454
mockGlobals(t, { 'process.env': { EDITOR: undefined, VISUAL: undefined } })
5555
const defsWin = t.mock(defpath, {
56-
[isWin]: true,
56+
[isWin]: { isWindows: true },
5757
})
5858
t.equal(defsWin.editor.default, 'notepad.exe')
5959
const defsNix = t.mock(defpath, {
60-
[isWin]: false,
60+
[isWin]: { isWindows: false },
6161
})
6262
t.equal(defsNix.editor.default, 'vi')
6363
t.end()
@@ -69,12 +69,12 @@ t.test('shell', t => {
6969
t.test('windows, env.ComSpec then cmd.exe', t => {
7070
mockGlobals(t, { 'process.env.ComSpec': 'command.com' })
7171
const defsComSpec = t.mock(defpath, {
72-
[isWin]: true,
72+
[isWin]: { isWindows: true },
7373
})
7474
t.equal(defsComSpec.shell.default, 'command.com')
7575
mockGlobals(t, { 'process.env.ComSpec': undefined })
7676
const defsNoComSpec = t.mock(defpath, {
77-
[isWin]: true,
77+
[isWin]: { isWindows: true },
7878
})
7979
t.equal(defsNoComSpec.shell.default, 'cmd')
8080
t.end()
@@ -83,12 +83,12 @@ t.test('shell', t => {
8383
t.test('nix, SHELL then sh', t => {
8484
mockGlobals(t, { 'process.env.SHELL': '/usr/local/bin/bash' })
8585
const defsShell = t.mock(defpath, {
86-
[isWin]: false,
86+
[isWin]: { isWindows: false },
8787
})
8888
t.equal(defsShell.shell.default, '/usr/local/bin/bash')
8989
mockGlobals(t, { 'process.env.SHELL': undefined })
9090
const defsNoShell = t.mock(defpath, {
91-
[isWin]: false,
91+
[isWin]: { isWindows: false },
9292
})
9393
t.equal(defsNoShell.shell.default, 'sh')
9494
t.end()
@@ -158,18 +158,18 @@ t.test('unicode allowed?', t => {
158158
t.test('cache', t => {
159159
mockGlobals(t, { 'process.env.LOCALAPPDATA': 'app/data/local' })
160160
const defsWinLocalAppData = t.mock(defpath, {
161-
[isWin]: true,
161+
[isWin]: { isWindows: true },
162162
})
163163
t.equal(defsWinLocalAppData.cache.default, 'app/data/local/npm-cache')
164164

165165
mockGlobals(t, { 'process.env.LOCALAPPDATA': undefined })
166166
const defsWinNoLocalAppData = t.mock(defpath, {
167-
[isWin]: true,
167+
[isWin]: { isWindows: true },
168168
})
169169
t.equal(defsWinNoLocalAppData.cache.default, '~/npm-cache')
170170

171171
const defsNix = t.mock(defpath, {
172-
[isWin]: false,
172+
[isWin]: { isWindows: false },
173173
})
174174
t.equal(defsNix.cache.default, '~/.npm')
175175

test/lib/utils/is-windows-bash.js

-30
This file was deleted.

test/lib/utils/is-windows-shell.js

-8
This file was deleted.

test/lib/utils/is-windows.js

+37-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
const t = require('tap')
2-
const actuallyWindows = process.platform === 'win32'
3-
t.equal(actuallyWindows, require('../../../lib/utils/is-windows.js'))
4-
Object.defineProperty(process, 'platform', {
5-
value: actuallyWindows ? 'posix' : 'win32',
2+
3+
const mockGlobals = require('../../fixtures/mock-globals')
4+
5+
t.test('is not windows', async t => {
6+
mockGlobals(t, { 'process.platform': 'posix' })
7+
t.match({
8+
isWindows: false,
9+
isWindowsShell: false,
10+
}, t.mock('../../../lib/utils/is-windows.js'))
11+
})
12+
13+
t.test('is windows, shell', async t => {
14+
mockGlobals(t, {
15+
'process.platform': 'win32',
16+
'process.env': {
17+
MSYSTEM: 'notmingw',
18+
TERM: 'notcygwin',
19+
},
20+
})
21+
t.match({
22+
isWindows: true,
23+
isWindowsShell: true,
24+
}, t.mock('../../../lib/utils/is-windows.js'))
25+
})
26+
27+
t.test('is windows, not shell', async t => {
28+
mockGlobals(t, {
29+
'process.platform': 'win32',
30+
'process.env': {
31+
MSYSTEM: 'MINGW32',
32+
TERM: 'cygwin',
33+
},
34+
})
35+
t.match({
36+
isWindows: true,
37+
isWindowsShell: false,
38+
}, t.mock('../../../lib/utils/is-windows.js'))
639
})
7-
delete require.cache[require.resolve('../../../lib/utils/is-windows.js')]
8-
t.equal(!actuallyWindows, require('../../../lib/utils/is-windows.js'))

test/lib/utils/path.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const t = require('tap')
22
const mod = '../../../lib/utils/path.js'
3-
const delim = require('../../../lib/utils/is-windows.js') ? ';' : ':'
3+
const { isWindows } = require('../../../lib/utils/is-windows.js')
4+
const delim = isWindows ? ';' : ':'
45
Object.defineProperty(process, 'env', {
56
value: {},
67
})

0 commit comments

Comments
 (0)