Skip to content

Commit

Permalink
Merge pull request #98 from deinsoftware/dev
Browse files Browse the repository at this point in the history
fix: 🐛 volta detection
  • Loading branch information
equiman authored Oct 31, 2023
2 parents 878a344 + 7405d83 commit 31cd323
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 24 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ Fixed for any bug fixes.
Security to invite users to upgrade in case of vulnerabilities.
-->

## 2.5.2 - 2023/10/31

## Fixed

- volta version detection when not installed
- if volta was not installed don't take in care the volta pin version on `package.json`

## 2.5.1 - 2023/10/19

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swpm",
"version": "2.5.1",
"version": "2.5.2",
"engines": {
"node": ">=16"
},
Expand Down
5 changes: 3 additions & 2 deletions src/alias/sag.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { it, expect, describe } from 'vitest'
import { testCommandResult } from '../../.vitest/helpers'
import { addPackageCases, addVoltaCases } from '../cli/swpm/commands/add.test'
import { commandVerification } from '../../bin/src/helpers/get'

const voltaVersion = testCommandResult('volta --version')
const isVoltaInstalled = await commandVerification('volta')

describe('sag', () => {
it.each(voltaVersion ? addVoltaCases : addPackageCases)('%s', (pkg, expected) => {
it.each(isVoltaInstalled ? addVoltaCases : addPackageCases)('%s', (pkg, expected) => {
const result = testCommandResult(`sag vite --test ${pkg}`)
expect(result).toBe(expected)
})
Expand Down
5 changes: 3 additions & 2 deletions src/cli/swpm/commands/add.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { it, expect, describe } from 'vitest'
import { testCommandResult } from '../../../../.vitest/helpers'
import { commandVerification } from '../../../../bin/src/helpers/get'

export const addCases = [
['npm', 'npm add vite'],
Expand Down Expand Up @@ -88,10 +89,10 @@ export const addPackageCases = [
['bun', 'bun add vite --global']
]

const voltaVersion = testCommandResult('volta --version')
const isVoltaInstalled = await commandVerification('volta')

describe('add --global', () => {
it.each(voltaVersion ? addVoltaCases : addPackageCases)('%s', (pkg, expected) => {
it.each(isVoltaInstalled ? addVoltaCases : addPackageCases)('%s', (pkg, expected) => {
const result = testCommandResult(`swpm add vite --global --test ${pkg}`)
expect(result).toBe(expected)
})
Expand Down
4 changes: 2 additions & 2 deletions src/cli/swpm/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ const add: CommandModule<Record<string, unknown>, OptionsProps> = {
if (!yargs?.package) return
if (!cmdr?.cmd) return

if (('package' in yargs) && findVoltaGlobals({ yargs, cmdr, flags: ['add', 'install'] })
) {
const voltaGlobals = await findVoltaGlobals({ yargs, cmdr, flags: ['add', 'install'] })
if (('package' in yargs) && voltaGlobals) {
cmdr.cmd = 'volta'
cmdr.args = ['install', yargs.package!]
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/swpm/commands/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ const remove: CommandModule<Record<string, unknown>, OptionsProps> = {
handler: async (yargs) => {
if (!cmdr?.cmd) return

if (findVoltaGlobals({ yargs, cmdr, flags: ['uninstall', 'remove'] })
) {
const voltaGlobals = await findVoltaGlobals({ yargs, cmdr, flags: ['uninstall', 'remove'] })
if (voltaGlobals) {
if (('pkg' in yargs) && ('package' in yargs)) {
cmdr.cmd = 'volta'
cmdr.args = ['uninstall', yargs.package!]
Expand Down
7 changes: 4 additions & 3 deletions src/helpers/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import chalk from 'chalk'
import { stripIndents } from 'common-tags'
import { exit } from 'node:process'
import prompts from 'prompts'
import { getCommandResult } from './cmds.js'

import type { CommanderPackage } from '../translator/commander.types.js'
import type {
Expand All @@ -17,13 +16,15 @@ import type {
TranslateArgsProp,
TranslateFlagProp
} from './args.types.js'
import { commandVerification } from './get.js'

export const findVoltaGlobals = ({ yargs, cmdr, flags }: FindVoltaGlobalsProps) => {
export const findVoltaGlobals = async ({ yargs, cmdr, flags }: FindVoltaGlobalsProps) => {
const hasGlobalOperations = (
yargs?.global &&
flags.some((flag) => cmdr?.args.includes(flag))
)
return hasGlobalOperations && getCommandResult({ command: 'volta --version' })
const isVoltaInstalled = await commandVerification('volta')
return hasGlobalOperations && isVoltaInstalled
}

const findFlagIndex = ({ args, flag }: FindFlagIndexProps) => {
Expand Down
14 changes: 10 additions & 4 deletions src/helpers/get.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { it, expect, describe, vi } from 'vitest'
import { detectVoltaPin } from './get'
import { commandVerification, detectVoltaPin } from './get'
import { CommanderPackage, PackageJson } from '../translator/commander.types'
import { getPackageJson } from './files.js'

Expand Down Expand Up @@ -56,8 +56,12 @@ describe('detectVoltaPin', () => {
cmd: 'npm',
args: []
}
const result = await detectVoltaPin(cmdr)
expect(result).toBe(true)

const isVoltaInstalled = await commandVerification('volta')
if (isVoltaInstalled) {
const result = await detectVoltaPin(cmdr)
expect(result).toBe(true)
}
})

it('should return false if cmdr.cmd is found in packageJson.volta', async () => {
Expand All @@ -73,7 +77,9 @@ describe('detectVoltaPin', () => {
cmd: 'npm',
args: []
}

const isVoltaInstalled = await commandVerification('volta')
const result = await detectVoltaPin(cmdr)
expect(result).toBe(false)
expect(isVoltaInstalled && result).toBe(false)
})
})
3 changes: 3 additions & 0 deletions src/helpers/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export const getCurrentPackageManager = async (): Promise<{origin: CommanderPack
export const detectVoltaPin = async (cmdr: CommanderPackage) => {
if (!cmdr?.cmd) return

const isVoltaInstalled = await commandVerification('volta')
if (!isVoltaInstalled) return

const packageJson = await getPackageJson()
if (!packageJson) return

Expand Down
10 changes: 5 additions & 5 deletions src/libs/autoUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import chalk, { type ForegroundColorName } from 'chalk'
import { stripIndent } from 'common-tags'
import updateNotifier, { type NotifyOptions } from 'update-notifier'

import { getCommandResult } from '../helpers/cmds.js'
import { getSwpmInfo } from '../helpers/info.js'

import type { CommanderPackage } from '../translator/commander.types.js'
import { commandVerification } from '../helpers/get.js'

const ONE_DAY_MS = 1000 * 60 * 60 * 24

Expand All @@ -20,19 +20,19 @@ export const autoUpdate = async (cmdr: CommanderPackage) => {
shouldNotifyInNpmScript: true,
updateCheckInterval: ONE_DAY_MS
}
const notifier = await updateNotifier(settings)
const notifier = updateNotifier(settings)

if (notifier?.update) {
const { latest, current, type } = notifier.update

let command = 'npm install swpm --location=global'

const voltaVersion = getCommandResult({ command: 'volta --version' })
if (voltaVersion) {
const isVoltaInstalled = await commandVerification('volta')
if (isVoltaInstalled) {
command = 'volta install swpm'
}

if (!voltaVersion && !cmdr?.config) {
if (!isVoltaInstalled && !cmdr?.config) {
const { install } = cmdr?.config ?? {}
if (install) {
command = install
Expand Down
6 changes: 3 additions & 3 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export default defineConfig({
reporter: ['text', 'html', 'lcov'],
include: [...include],
exclude: [...exclude],
statements: 52,
branches: 77,
statements: 51,
branches: 76,
functions: 55,
lines: 52
lines: 51
}
}
})

0 comments on commit 31cd323

Please sign in to comment.