From 6202321a420e140e4cb01fdba8feca85cfb473a3 Mon Sep 17 00:00:00 2001 From: Xmader Date: Wed, 20 Jan 2021 11:29:49 -0500 Subject: [PATCH] refactor(npm-data): avoid running external npm cmd --- src/cli.ts | 2 +- src/npm-data.ts | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 523e81e..e481c06 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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.`)) diff --git a/src/npm-data.ts b/src/npm-data.ts index 676a5c5..b81fdf4 100644 --- a/src/npm-data.ts +++ b/src/npm-data.ts @@ -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 { - 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 { - return (await exec('npm info musescore-downloader version')).stdout.trim() +export async function getLatestVer (_fetch = getFetch()): Promise { + // 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 () {