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')
if (!(await isNpx())) {
if (!isNpx()) {
const { installed, latest, isLatest } = await getVerInfo()
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.`))

View File

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