Skip to content

Commit

Permalink
feat: default command to powershell if available (#177)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

Powershell will become the default on windows if the Command setting has not changed from the default. To change it back just delete the Command setting.
  • Loading branch information
UziTech authored Oct 7, 2020
1 parent e97c619 commit 350d8c2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
4 changes: 1 addition & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"node-pty-prebuilt-multiarch": "^0.9.0",
"uuid": "^8.3.1",
"whatwg-url": "^8.3.0",
"which": "^2.0.2",
"xterm": "4.9.0",
"xterm-addon-fit": "0.4.0",
"xterm-addon-ligatures": "^0.3.0",
Expand Down
52 changes: 51 additions & 1 deletion spec/config-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { configDefaults, resetConfigDefaults } from '../src/config'
import { configDefaults, resetConfigDefaults, setInitialCommand } from '../src/config'

import os from 'os'
import path from 'path'
Expand Down Expand Up @@ -448,4 +448,54 @@ describe('config', () => {
expect(configDefaults.apiOpenPosition).toBe('Center')
})
})

describe('setInitialCommand()', () => {
const savedPlatform = process.platform

beforeEach(() => {
Object.defineProperty(process, 'platform', {
value: 'win32',
})
atom.config.set('x-terminal.command', configDefaults.command)
})

afterEach(() => {
Object.defineProperty(process, 'platform', {
value: savedPlatform,
})
})

it('should set x-terminal.command to pwsh', async () => {
const shell = 'path/to/pwsh.exe'
await setInitialCommand(async (file) => {
if (file === 'pwsh.exe') {
return shell
}
throw new Error('ENOENT')
})

expect(atom.config.get('x-terminal.command')).toBe(shell)
})

it('should set x-terminal.command to powershell', async () => {
const shell = 'path/to/powershell.exe'
await setInitialCommand(async (file) => {
if (file === 'powershell.exe') {
return shell
}
throw new Error('ENOENT')
})

expect(atom.config.get('x-terminal.command')).toBe(shell)
})

it('should set x-terminal.command to powershell', async () => {
const shell = configDefaults.command
await setInitialCommand(async () => {
throw new Error('ENOENT')
})

expect(atom.config.get('x-terminal.command')).toBe(shell)
})
})
})
26 changes: 26 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import os from 'os'
import path from 'path'
import which from 'which'

export function resetConfigDefaults () {
return {
Expand Down Expand Up @@ -950,3 +951,28 @@ function configToData (obj, prefix) {
}

export const CONFIG_DATA = configToData(config, 'x-terminal')

export async function setInitialCommand (which) {
let command
if (process.platform === 'win32') {
try {
command = await which('pwsh.exe')
} catch (e1) {
try {
command = await which('powershell.exe')
} catch (e2) {
// powershell not found
}
}
}

if (command && atom.config.get('x-terminal.command') === configDefaults.command) {
atom.config.set('x-terminal.command', command)
}
}

// set shell command automatically on first install
if (localStorage.getItem('x-terminal.initialCommandSet') === null) {
localStorage.setItem('x-terminal.initialCommandSet', 'true')
setInitialCommand(which)
}

0 comments on commit 350d8c2

Please sign in to comment.