Skip to content

Commit

Permalink
feat: introduce --quiet cli arg
Browse files Browse the repository at this point in the history
  • Loading branch information
auvred committed Oct 2, 2023
1 parent 599a55f commit d76fa34
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 22 deletions.
29 changes: 24 additions & 5 deletions src/install-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import fs from 'node:fs'
import { PLUGINS_PACKAGE_JSON_PATH, PLUGIN_STORE_PATH } from './paths.js'
import { splitPluginNameAndVersion } from './utils.js'

import type { ExtraArgs } from './parse-args.js'

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

function listStoreDependencies(): [string, string][] {
Expand Down Expand Up @@ -77,7 +79,10 @@ function filterPluginsToInstall(pluginNames: string[]) {
}
}

export async function installPlugins(pluginNames: string[]) {
export async function installPlugins(
pluginNames: string[],
extraArgs: ExtraArgs,
) {
if (!pluginNames.length) {
return
}
Expand All @@ -93,14 +98,16 @@ export async function installPlugins(pluginNames: string[]) {
const { pluginsToInstall, installedPlugins } =
filterPluginsToInstall(pluginNames)

if (installedPlugins.length) {
if (installedPlugins.length && !extraArgs.quiet) {
console.log('\n----- Already installed ----\n')
installedPlugins.forEach(pluginName => console.log(` - ${pluginName}`))
}

if (pluginsToInstall.length) {
console.log('\n---- Installing plugins ----\n')
pluginsToInstall.forEach(pluginName => console.log(` - ${pluginName}`))
if (!extraArgs.quiet) {
console.log('\n---- Installing plugins ----\n')
pluginsToInstall.forEach(pluginName => console.log(` - ${pluginName}`))
}

const child = spawn(
npmExecutable,
Expand All @@ -118,18 +125,30 @@ export async function installPlugins(pluginNames: string[]) {
{
cwd: PLUGIN_STORE_PATH,
env: Object.assign({}, process.env, { NODE_ENV: 'production' }),
stdio: 'inherit',
stdio: extraArgs.quiet ? ['inherit', 'pipe', 'pipe'] : 'inherit',
},
)

let stderr = ''

if (extraArgs.quiet) {
child.stderr?.on('data', data => (stderr += data))
}

return await new Promise<void>((resolve, reject) => {
child.on('error', err => {
if (extraArgs.quiet) {
process.stderr.write(stderr)
}
reject(err)
})
child.on('exit', code => {
if (code === 0) {
return resolve()
}
if (extraArgs.quiet) {
process.stderr.write(stderr)
}
reject()
})
})
Expand Down
8 changes: 5 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { PLUGINS_PACKAGE_JSON_PATH } from './paths.js'
import { splitPluginNameAndVersion } from './utils.js'

export async function run(args: string[]) {
const { prettierArgs, pluginNames } = parseArgs(args)
const { prettierArgs, pluginNames, extraArgs } = parseArgs(args)

if (pluginNames.length) {
try {
await installPlugins(pluginNames)
console.log('\n----- Running prettier -----\n')
await installPlugins(pluginNames, extraArgs)
if (!extraArgs.quiet) {
console.log('\n----- Running prettier -----\n')
}
} catch {
process.exit(1)
}
Expand Down
58 changes: 44 additions & 14 deletions src/parse-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ function extendPluginName(shortName: string) {
return 'prettier-plugin-' + shortName
}

export interface ExtraArgs {
quiet: boolean
}

export function parseArgs(args: string[]): {
prettierArgs: string[]
pluginNames: string[]
extraArgs: ExtraArgs
} {
const prettierArgs: string[] = []
const pluginNames: string[] = []
const extraArgs: ExtraArgs = {
quiet: false,
}

function pushPluginName(pluginName: string) {
const isDuplicated = pluginNames.some(
Expand All @@ -25,34 +33,55 @@ export function parseArgs(args: string[]): {
pluginNames.push(pluginName)
}

function processArg(key: string, value?: string): boolean {
switch (key) {
case 'pnp':
if (value) {
pushPluginName(value)
return true
}
return false
case 'pn':
if (value) {
pushPluginName(extendPluginName(value))
return true
}
return false
case 'quiet':
extraArgs.quiet = true
return true
}

return false
}

for (let index = 0; index < args.length; index++) {
const arg = args[index]
if (/^--.+=/.test(arg)) {
const match = arg.match(/^--([^=]+)=([\s\S]*)$/)
if (match) {
const key = match[1]
const value = match[2]
if (key === 'pnp') {
pushPluginName(value)
if (processArg(key, value)) {
continue
} else if (key === 'pn') {
pushPluginName(extendPluginName(value))
}
}
} else if (/^--.+/.test(arg)) {
const match = arg.match(/^--(.+)/)
if (match) {
const key = match[1]
if (key === 'pnp' || key === 'pn') {
const next = args[index + 1]
if (next !== undefined && !/^(-|--)[^-]/.test(next)) {
if (key === 'pnp') {
pushPluginName(next)
} else if (key === 'pn') {
pushPluginName(extendPluginName(next))
}
index += 1
}
const next = args[index + 1]

if (processArg(key)) {
continue
}

if (
next !== undefined &&
!/^(-|--)[^-]/.test(next) &&
processArg(key, next)
) {
index += 1
continue
}
}
Expand All @@ -64,5 +93,6 @@ export function parseArgs(args: string[]): {
return {
prettierArgs,
pluginNames,
extraArgs,
}
}
11 changes: 11 additions & 0 deletions tests/integration/__tests__/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,14 @@ if (false) {
}
"
`;

exports[`prettier-pnp cli > w/ --quite > should be less verbose 1`] = `
"/** --- FIXTURE --- */
const a = 5
if (false) {
true
}
"
`;
27 changes: 27 additions & 0 deletions tests/integration/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,31 @@ describe('prettier-pnp cli', () => {
"
`)
})

describe('w/ --quite', () => {
it('should be less verbose', () => {
const args = ['--pn', 'curly', '--no-semi', '--quiet', 'index.js']

assertCliCallResult(...args)
})

it('should print out npm stderr', () => {
const args = ['--pn', 'curly_', '--quiet', 'index.js']

const result = runPrettierPnpCli(...args)
expect(result.status).toEqual(1)
expect(result.stdout).toEqual('')
expect(result.stderr.split('\n').slice(0, -2).join('\n'))
.toMatchInlineSnapshot(`
"npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/prettier-plugin-curly_ - Not found
npm ERR! 404
npm ERR! 404 'prettier-plugin-curly_@*' is not in this registry.
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
"
`)
})
})
})

0 comments on commit d76fa34

Please sign in to comment.