Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: default command to powershell if available #177

Merged
merged 1 commit into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.0",
"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)
}