refactor(npm-data): avoid running external npm cmd

This commit is contained in:
Xmader 2021-01-20 11:29:49 -05:00
parent 3c72b5a92f
commit 6202321a42
No known key found for this signature in database
GPG Key ID: A20B97FB9EB730E4
2 changed files with 15 additions and 11 deletions

View File

@ -186,7 +186,7 @@ void (async () => {
) )
spinner.succeed('OK') spinner.succeed('OK')
if (!(await isNpx())) { if (!isNpx()) {
const { installed, latest, isLatest } = await getVerInfo() const { installed, latest, isLatest } = await getVerInfo()
if (!isLatest) { if (!isLatest) {
console.log(chalk.yellowBright(`\nYour installed version (${installed}) of the musescore-downloader CLI is not the latest one (${latest})!\nRun npm i -g musescore-downloader to update.`)) console.log(chalk.yellowBright(`\nYour installed version (${installed}) of the musescore-downloader CLI is not the latest one (${latest})!\nRun npm i -g musescore-downloader to update.`))

View File

@ -1,22 +1,26 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { exec as _exec } from 'child_process' import { name as pkgName, version as pkgVer } from '../package.json'
import { promisify } from 'util' import { getFetch } from './utils'
import { version } from '../package.json'
const exec = promisify(_exec) const IS_NPX_REG = /_npx(\/|\\)\d+\1/
const NPM_REGISTRY = 'https://registry.npmjs.org'
export async function isNpx (): Promise<boolean> { export function isNpx (): boolean {
const output = await exec('npm list -g musescore-downloader') // file is in a npx cache dir
return output.stdout.includes('(empty)') // TODO: installed locally?
return __dirname.match(IS_NPX_REG) !== null
} }
export function getInstalledVer (): string { export function getInstalledVer (): string {
return version return pkgVer
} }
export async function getLatestVer (): Promise<string> { export async function getLatestVer (_fetch = getFetch()): Promise<string> {
return (await exec('npm info musescore-downloader version')).stdout.trim() // fetch pkg info from the npm registry
const r = await _fetch(`${NPM_REGISTRY}/${pkgName}`)
const json = await r.json()
return json['dist-tags'].latest as string
} }
export async function getVerInfo () { export async function getVerInfo () {