Skip to content

Commit

Permalink
feat(perf): remove semver, fs-extra (#762)
Browse files Browse the repository at this point in the history
* feat(perf): remove version check and semver

* fix: use native fs

* chore: no node prefix until we drop node14
  • Loading branch information
mshanemc committed Aug 31, 2023
1 parent 907f0a8 commit fdab7b4
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 60 deletions.
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"cli-progress": "^3.12.0",
"debug": "^4.3.4",
"ejs": "^3.1.8",
"fs-extra": "^9.1.0",
"get-package-type": "^0.1.0",
"globby": "^11.1.0",
"hyperlinker": "^1.0.0",
Expand All @@ -24,7 +23,7 @@
"natural-orderby": "^2.0.3",
"object-treeify": "^1.1.33",
"password-prompt": "^1.1.2",
"semver": "^7.5.3",
"semver": "^7.5.4",
"slice-ansi": "^4.0.0",
"string-width": "^4.2.3",
"strip-ansi": "^6.0.1",
Expand All @@ -47,15 +46,14 @@
"@types/chai-as-promised": "^7.1.5",
"@types/clean-stack": "^2.1.1",
"@types/ejs": "^3.1.2",
"@types/fs-extra": "^9.0.13",
"@types/indent-string": "^4.0.1",
"@types/js-yaml": "^3.12.7",
"@types/mocha": "^8.2.3",
"@types/nock": "^11.1.0",
"@types/node": "^16",
"@types/node-notifier": "^8.0.2",
"@types/proxyquire": "^1.3.28",
"@types/semver": "^7.5.0",
"@types/semver": "^7.5.1",
"@types/shelljs": "^0.8.11",
"@types/slice-ansi": "^4.0.0",
"@types/strip-ansi": "^5.2.1",
Expand Down Expand Up @@ -120,4 +118,4 @@
"test:perf": "ts-node test/perf/parser.perf.ts"
},
"types": "lib/index.d.ts"
}
}
2 changes: 1 addition & 1 deletion src/cli-ux/action/spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as chalk from 'chalk'
import * as supportsColor from 'supports-color'
const stripAnsi = require('strip-ansi')
const ansiStyles = require('ansi-styles')
const ansiEscapes = require('ansi-escapes')
import {errtermwidth} from '../../screen'
import spinners from './spinners'
import {ActionBase, ActionType} from './base'
Expand Down Expand Up @@ -74,6 +73,7 @@ export default class SpinnerAction extends ActionBase {

private _reset() {
if (!this.output) return
const ansiEscapes = require('ansi-escapes')
const lines = this._lines(this.output)
this._write(this.std, ansiEscapes.cursorLeft + ansiEscapes.cursorUp(lines) + ansiEscapes.eraseDown)
this.output = undefined
Expand Down
10 changes: 4 additions & 6 deletions src/cli-ux/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import * as semver from 'semver'
import {PJSON} from '../interfaces/pjson'
import {requireJson} from '../util'
import spinner from './action/spinner'
import simple from './action/spinner'
import pride from './action/pride-spinner'
import {ActionBase} from './action/base'

const version = semver.parse(requireJson<PJSON>(__dirname, '..', '..', 'package.json').version)!

export type Levels = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace'

export interface ConfigMessage {
Expand Down Expand Up @@ -58,9 +55,10 @@ export class Config {
}

function fetch() {
if (globals[version.major]) return globals[version.major]
globals[version.major] = new Config()
return globals[version.major]
const major = requireJson<PJSON>(__dirname, '..', '..', 'package.json').version.split('.')[0]
if (globals[major]) return globals[major]
globals[major] = new Config()
return globals[major]
}

export const config: Config = fetch()
Expand Down
5 changes: 3 additions & 2 deletions src/cli-ux/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import config from './config'

import * as chalk from 'chalk'
import {stderr} from './stream'
const ansiEscapes = require('ansi-escapes')
const passwordPrompt = require('password-prompt')

export interface IPromptOptions {
prompt?: string;
Expand Down Expand Up @@ -78,6 +76,7 @@ async function single(options: IPromptConfig): Promise<string> {
}

function replacePrompt(prompt: string) {
const ansiEscapes = require('ansi-escapes')
stderr.write(ansiEscapes.cursorHide + ansiEscapes.cursorUp(1) + ansiEscapes.cursorLeft + prompt +
ansiEscapes.cursorDown(1) + ansiEscapes.cursorLeft + ansiEscapes.cursorShow)
}
Expand All @@ -93,6 +92,8 @@ async function _prompt(name: string, inputOptions: Partial<IPromptOptions> = {})
default: '',
...inputOptions,
}
const passwordPrompt = require('password-prompt')

switch (options.type) {
case 'normal':
return normal(options)
Expand Down
7 changes: 4 additions & 3 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import {CompletableOptionFlag, Arg} from '../interfaces/parser'
import {stdout} from '../cli-ux/stream'
import {Performance} from '../performance'
import {settings} from '../settings'
import {userInfo as osUserInfo} from 'node:os'
import {sep} from 'node:path'
import {lt} from 'semver'
import {userInfo as osUserInfo} from 'os'
import {sep} from 'path'

// eslint-disable-next-line new-cap
const debug = Debug()
Expand Down Expand Up @@ -139,6 +138,8 @@ export class Config implements IConfig {

if (typeof opts === 'string') opts = {root: opts}
if (isConfig(opts)) {
const {lt} = await import('semver')

const currentConfigBase = BASE.replace('@oclif/core@', '')
const incomingConfigBase = opts._base.replace('@oclif/core@', '')
/**
Expand Down
6 changes: 3 additions & 3 deletions src/errors/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from 'fs-extra'
import * as path from 'path'
import * as fs from 'fs/promises'
import {dirname} from 'path'
import stripAnsi = require('strip-ansi')

const timestamp = () => new Date().toISOString()
Expand Down Expand Up @@ -34,7 +34,7 @@ export class Logger {
if (this.buffer.length === 0) return
const mylines = this.buffer
this.buffer = []
await fs.mkdirp(path.dirname(this.file))
await fs.mkdir(dirname(this.file), {recursive: true})
await fs.appendFile(this.file, mylines.join('\n') + '\n')
})
await this.flushing
Expand Down
11 changes: 0 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as semver from 'semver'

import {Command} from './command'
import {run, execute} from './main'
import {Config, Plugin, tsPath, toCached} from './config'
Expand All @@ -14,7 +12,6 @@ import {Hook} from './interfaces/hooks'
import {settings, Settings} from './settings'
import {HelpSection, HelpSectionRenderer, HelpSectionKeyValueTable} from './help/formatter'
import * as ux from './cli-ux'
import {requireJson} from './util'
import {stderr, stdout} from './cli-ux/stream'
import {Performance} from './performance'

Expand Down Expand Up @@ -62,12 +59,4 @@ function checkCWD() {
}
}

function checkNodeVersion() {
const pjson = requireJson<Interfaces.PJSON>(__dirname, '..', 'package.json')
if (!semver.satisfies(process.versions.node, pjson.engines.node)) {
stderr.write(`WARNING\nWARNING Node version must be ${pjson.engines.node} to use this CLI\nWARNING Current node version: ${process.versions.node}\nWARNING\n`)
}
}

checkCWD()
checkNodeVersion()
8 changes: 4 additions & 4 deletions src/module-loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path'
import * as url from 'url'
import * as fs from 'fs-extra'
import {existsSync, lstatSync} from 'fs'

import {ModuleLoadError} from './errors'
import {Config as IConfig} from './interfaces'
Expand Down Expand Up @@ -143,10 +143,10 @@ export default class ModuleLoader {

let fileExists = false
let isDirectory = false
if (fs.existsSync(filePath)) {
if (existsSync(filePath)) {
fileExists = true
try {
if (fs.lstatSync(filePath)?.isDirectory?.()) {
if (lstatSync(filePath)?.isDirectory?.()) {
fileExists = false
isDirectory = true
}
Expand Down Expand Up @@ -184,7 +184,7 @@ export default class ModuleLoader {
for (const extension of s_EXTENSIONS) {
const testPath = `${filePath}${extension}`

if (fs.existsSync(testPath)) {
if (existsSync(testPath)) {
return testPath
}
}
Expand Down
8 changes: 4 additions & 4 deletions test/cli-ux/fancy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expect, fancy as base, FancyTypes} from 'fancy-test'
import * as fs from 'fs-extra'
import * as path from 'path'
import {rm} from 'fs/promises'
import {join} from 'path'

import {ux} from '../../src'

Expand All @@ -14,8 +14,8 @@ let count = 0
export const fancy = base
.do(async (ctx: {count: number; base: string}) => {
ctx.count = count++
ctx.base = path.join(__dirname, '../tmp', `test-${ctx.count}`)
await fs.remove(ctx.base)
ctx.base = join(__dirname, '../tmp', `test-${ctx.count}`)
await rm(ctx.base, {recursive: true, force: true})
const chalk = require('chalk')
chalk.level = 0
})
Expand Down
4 changes: 2 additions & 2 deletions test/errors/handle.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect, fancy} from 'fancy-test'
import * as fs from 'fs-extra'
import {readFileSync} from 'node:fs'
import * as path from 'path'
import * as process from 'process'

Expand Down Expand Up @@ -106,7 +106,7 @@ describe('handle', () => {
handle(new CLIError('uh oh!'))
expect(ctx.stderr).to.equal(` ${x} Error: uh oh!\n`)
await config.errorLogger!.flush()
expect(fs.readFileSync(errlog, 'utf8')).to.contain('Error: uh oh!')
expect(readFileSync(errlog, 'utf8')).to.contain('Error: uh oh!')
expect(process.exitCode).to.equal(2)
})

Expand Down
8 changes: 4 additions & 4 deletions test/errors/warn.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {expect, fancy} from 'fancy-test'
import * as fs from 'fs-extra'
import * as path from 'path'
import {readFile} from 'node:fs/promises'
import {join} from 'node:path'

import {config, warn} from '../../src/errors'

const errlog = path.join(__dirname, '../tmp/mytest/warn.log')
const errlog = join(__dirname, '../tmp/mytest/warn.log')

describe('warn', () => {
fancy
Expand All @@ -20,6 +20,6 @@ describe('warn', () => {
expect(ctx.stderr).to.contain('Warning: foo!')
expect(process.exitCode).to.be.undefined
await config.errorLogger!.flush()
expect(fs.readFileSync(errlog, 'utf8')).to.contain('Warning: foo!')
expect(await readFile(errlog, 'utf8')).to.contain('Warning: foo!')
})
})
4 changes: 2 additions & 2 deletions test/integration/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import {rm} from 'shelljs'
import {mkdirp} from 'fs-extra'
import {mkdir} from 'node:fs/promises'
import * as cp from 'child_process'
import * as chalk from 'chalk'
import * as fs from 'fs'
Expand Down Expand Up @@ -87,7 +87,7 @@ export async function setup(testFile: string, options: Options): Promise<Executo
return executor
}

await mkdirp(location)
await mkdir(location, {recursive: true})
rm('-rf', testDir)

const clone = `git clone ${options.repo} ${testDir}`
Expand Down
1 change: 0 additions & 1 deletion test/parser/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable max-nested-callbacks */
import {assert, expect, config} from 'chai'
import * as fs from 'fs'

Expand Down
24 changes: 12 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -621,13 +621,6 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.0.tgz#5fb2e536c1ae9bf35366eed879e827fa59ca41c2"
integrity sha512-WulqXMDUTYAXCjZnk6JtIHPigp55cVtDgDrO2gHRwhyJto21+1zbVCtOYB2L1F9w4qCQ0rOGWBnBe0FNTiEJIQ==

"@types/fs-extra@^9.0.13":
version "9.0.13"
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-9.0.13.tgz#7594fbae04fe7f1918ce8b3d213f74ff44ac1f45"
integrity sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA==
dependencies:
"@types/node" "*"

"@types/glob@*":
version "7.2.0"
resolved "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz#bc1b5bf3aa92f25bd5dd39f35c57361bdce5b2eb"
Expand Down Expand Up @@ -707,10 +700,10 @@
resolved "https://registry.yarnpkg.com/@types/proxyquire/-/proxyquire-1.3.28.tgz#05a647bb0d8fe48fc8edcc193e43cc79310faa7d"
integrity sha512-SQaNzWQ2YZSr7FqAyPPiA3FYpux2Lqh3HWMZQk47x3xbMCqgC/w0dY3dw9rGqlweDDkrySQBcaScXWeR+Yb11Q==

"@types/semver@^7.5.0":
version "7.5.0"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.0.tgz#591c1ce3a702c45ee15f47a42ade72c2fd78978a"
integrity sha512-G8hZ6XJiHnuhQKR7ZmysCeJWE08o8T0AXtk5darsCaTVsYZhhgUrq53jizaR2FvsoeCwJhlmwTjkXBY5Pn/ZHw==
"@types/semver@^7.5.1":
version "7.5.1"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.1.tgz#0480eeb7221eb9bc398ad7432c9d7e14b1a5a367"
integrity sha512-cJRQXpObxfNKkFAZbJl2yjWtJCqELQIdShsogr1d2MilP8dKD9TE/nEKHkJgUNHdGKCQaf9HbIynuV2csLGVLg==

"@types/shelljs@^0.8.11":
version "0.8.11"
Expand Down Expand Up @@ -2955,13 +2948,20 @@ semver@^6.1.0, semver@^6.3.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==

semver@^7.2.1, semver@^7.3.4, semver@^7.3.5, semver@^7.3.7, semver@^7.5.0, semver@^7.5.3:
semver@^7.2.1, semver@^7.3.4, semver@^7.3.5, semver@^7.3.7, semver@^7.5.0:
version "7.5.3"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.3.tgz#161ce8c2c6b4b3bdca6caadc9fa3317a4c4fe88e"
integrity sha512-QBlUtyVk/5EeHbi7X0fw6liDZc7BBmEaSYn01fMU1OUYbf6GPsbTtd8WmnqbI20SeycoHSeiybkE/q1Q+qlThQ==
dependencies:
lru-cache "^6.0.0"

semver@^7.5.4:
version "7.5.4"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.4.tgz#483986ec4ed38e1c6c48c34894a9182dbff68a6e"
integrity sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==
dependencies:
lru-cache "^6.0.0"

serialize-javascript@5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/serialize-javascript/-/serialize-javascript-5.0.1.tgz#7886ec848049a462467a97d3d918ebb2aaf934f4"
Expand Down

0 comments on commit fdab7b4

Please sign in to comment.