Skip to content

Commit

Permalink
(create-wdio): fix issues with executing wdio config, add support for…
Browse files Browse the repository at this point in the history
… bunx (#586)
  • Loading branch information
erwinheitzman authored Nov 29, 2023
1 parent 1092522 commit 7ea9e86
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
14 changes: 14 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ export const INSTALL_COMMAND: Record<PM, string> = {
bun: 'install'
} as const

export const EXECUTER: Record<PM, string> = {
npm: 'npx',
pnpm: 'pnpm',
yarn: 'yarn',
bun: 'bunx'
} as const

export const EXECUTE_COMMAND: Record<PM, string> = {
npm: '',
pnpm: 'exec',
yarn: 'exec',
bun: ''
} as const

export const DEV_FLAG: Record<PM, string> = {
npm: '--save-dev',
pnpm: '--save-dev',
Expand Down
15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { resolve } from 'import-meta-resolve'
import { runProgram, getPackageVersion } from './utils.js'
import {
ASCII_ROBOT, PROGRAM_TITLE, UNSUPPORTED_NODE_VERSION, DEFAULT_NPM_TAG,
INSTALL_COMMAND, DEV_FLAG, PMs
INSTALL_COMMAND, DEV_FLAG, PMs, EXECUTER,EXECUTE_COMMAND
} from './constants.js'
import type { ProgramOpts } from './types'
import { execSync } from 'node:child_process'
Expand Down Expand Up @@ -62,9 +62,11 @@ export async function createWebdriverIO(opts: ProgramOpts) {
* find package manager that was used to create project
*/
const pm = PMs.find((pm) => (
// for pnpm check for "~/Library/pnpm/store/v3/..."
// for pnpm check "~/Library/pnpm/store/v3/..."
// for NPM check "~/.npm/npx/..."
// for Yarn check "~/.yarn/bin/create-wdio"
// for Bun check "~/.bun/bin/create-wdio"
process.argv[1].includes(`${path.sep}${pm}${path.sep}`) ||
// for NPM and Yarn check for "~/.npm/npx/..." or "~/.yarn/bin/create-wdio"
process.argv[1].includes(`${path.sep}.${pm}${path.sep}`)
)) || 'npm'

Expand Down Expand Up @@ -92,11 +94,12 @@ export async function createWebdriverIO(opts: ProgramOpts) {
console.log(chalk.green.bold('✔ Success!'))
}

return runProgram(pm === 'npm' ? 'npx' : pm, [
`${pm === 'npm' ? '' : 'run '}${WDIO_COMMAND}`,
return runProgram(EXECUTER[pm], [
EXECUTE_COMMAND[pm],
WDIO_COMMAND,
'config',
...(opts.yes ? ['--yes'] : [])
], { cwd: root })
].filter(i => !!i), { cwd: root })
}

async function isCLIInstalled(path: string) {
Expand Down
22 changes: 20 additions & 2 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ test('createWebdriverIO with Yarn', async () => {
)
expect(runProgram).toBeCalledWith(
'yarn',
['run wdio', 'config'],
['exec', 'wdio', 'config'],
expect.any(Object)
)
expect(runProgram).toBeCalledTimes(2)
Expand Down Expand Up @@ -116,7 +116,25 @@ test('createWebdriverIO with pnpm', async () => {
)
expect(runProgram).toBeCalledWith(
'pnpm',
['run wdio', 'config'],
['exec', 'wdio', 'config'],
expect.any(Object)
)
expect(runProgram).toBeCalledTimes(2)
expect(fs.mkdir).toBeCalledTimes(0)
expect(fs.writeFile).toBeCalledTimes(0)
})

test('createWebdriverIO with bun', async () => {
process.argv = ['', '~/.bun/bin/create-wdio']
await createWebdriverIO({ npmTag: 'latest' } as ProgramOpts)
expect(runProgram).toBeCalledWith(
'bun',
['install', '@wdio/cli@latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledWith(
'bunx',
['wdio', 'config'],
expect.any(Object)
)
expect(runProgram).toBeCalledTimes(2)
Expand Down

0 comments on commit 7ea9e86

Please sign in to comment.