Skip to content

Commit

Permalink
fix: do not throw on plugins names starting with @
Browse files Browse the repository at this point in the history
  • Loading branch information
auvred committed Aug 17, 2023
1 parent ae7b8a5 commit 30b1c81
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
7 changes: 3 additions & 4 deletions src/install-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { spawn } from 'node:child_process'
import fs from 'node:fs'

import { PLUGINS_PACKAGE_JSON_PATH, PLUGIN_STORE_PATH } from './paths.js'
import { splitPluginNameAndVersion } from './utils.js'

const npmExecutable = process.platform.startsWith('win') ? 'npm.cmd' : 'npm'

Expand Down Expand Up @@ -33,10 +34,8 @@ function filterPluginsToInstall(pluginNames: string[]) {
const installedPlugins: string[] = []

pluginNames.forEach(rawPluginName => {
let [pluginName, pluginVersion] = rawPluginName.split('@') as [
string,
string | undefined,
]
let { name: pluginName, version: pluginVersion } =
splitPluginNameAndVersion(rawPluginName)
if (
pluginVersion &&
(pluginVersion.startsWith('^') || pluginVersion.startsWith('~'))
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { resolve } from 'import-meta-resolve'
import { installPlugins } from './install-plugins.js'
import { parseArgs } from './parse-args.js'
import { PLUGINS_PACKAGE_JSON_PATH } from './paths.js'
import { splitPluginNameAndVersion } from './utils.js'

export async function run(args: string[]) {
const { prettierArgs, pluginNames } = parseArgs(args)
Expand All @@ -20,7 +21,7 @@ export async function run(args: string[]) {

const pnpPlugins: string[] = []
pluginNames.forEach(pluginName => {
pluginName = pluginName.split('@')[0]
pluginName = splitPluginNameAndVersion(pluginName).name
const resolved = resolve(pluginName, PLUGINS_PACKAGE_JSON_PATH.href)
pnpPlugins.push('--plugin', resolved)
})
Expand Down
6 changes: 5 additions & 1 deletion src/parse-args.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { splitPluginNameAndVersion } from './utils.js'

function extendPluginName(shortName: string) {
return 'prettier-plugin-' + shortName
}
Expand All @@ -11,7 +13,9 @@ export function parseArgs(args: string[]): {

function pushPluginName(pluginName: string) {
const isDuplicated = pluginNames.some(
name => name.split('@')[0] === pluginName.split('@')[0],
name =>
splitPluginNameAndVersion(name).name ===
splitPluginNameAndVersion(pluginName).name,
)

if (isDuplicated) {
Expand Down
19 changes: 19 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function splitPluginNameAndVersion(packageName: string): {
name: string
version: string | undefined
} {
let name = ''

if (packageName.startsWith('@')) {
name = '@'
packageName = packageName.slice(1)
}

name += packageName.split('@')[0]
const version = packageName.split('@')[1]

return {
name,
version,
}
}
15 changes: 15 additions & 0 deletions tests/integration/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ describe('prettier-pnp cli', () => {
expect(packageJson).toMatchSnapshot()
})

it('should install plugin with a name starting with @', () => {
const args1 = ['--pnp', '@prettier/plugin-php', 'index.js']

const result = runPrettierPnpCli(...args1)
expect(result.status).toEqual(0)
expect(result.stdout.split('\n').slice(0, 5).join('\n'))
.toMatchInlineSnapshot(`
"
---- Installing plugins ----
- @prettier/plugin-php
"
`)
})

it('should not try to install already installed plugins', () => {
const args1 = ['--pn', 'curly', 'index.js']
const args2 = ['--pn', 'curly', '--pn', 'organize-imports', 'index.js']
Expand Down

0 comments on commit 30b1c81

Please sign in to comment.