From 1be9cbf6906adad1ad9aa5ececdf0a6c7237a62b Mon Sep 17 00:00:00 2001 From: kyli Date: Wed, 12 Jun 2024 11:22:33 +0800 Subject: [PATCH] feat: add logger for install (#141) --- .changeset/wise-dancers-admire.md | 6 ++++++ packages/core/src/utils/downloadPackage.ts | 9 ++++++--- packages/core/src/utils/packageManager.ts | 21 +++++++++++++++++---- 3 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 .changeset/wise-dancers-admire.md diff --git a/.changeset/wise-dancers-admire.md b/.changeset/wise-dancers-admire.md new file mode 100644 index 00000000..7a45e7f4 --- /dev/null +++ b/.changeset/wise-dancers-admire.md @@ -0,0 +1,6 @@ +--- +'@modern-js/codesmith': patch +--- + +feat: add logger for install +feat: install 添加 logger diff --git a/packages/core/src/utils/downloadPackage.ts b/packages/core/src/utils/downloadPackage.ts index 8678170d..a37b19e3 100644 --- a/packages/core/src/utils/downloadPackage.ts +++ b/packages/core/src/utils/downloadPackage.ts @@ -2,11 +2,12 @@ import os from 'os'; import { fs, semver } from '@modern-js/utils'; import axios from 'axios'; import tar from 'tar'; +import { fsExists } from './fsExists'; import { getNpmTarballUrl } from './getNpmTarballUrl'; import { getNpmVersion } from './getNpmVersion'; -import { fsExists } from './fsExists'; import { runInstall } from './packageManager'; import { CATCHE_VALIDITY_PREIOD } from '@/constants'; +import { Logger } from '@/logger'; async function isValidCache(cacheDir: string) { /* generator cache can use @@ -91,9 +92,10 @@ export async function downloadPackage( options: { registryUrl?: string; install?: boolean; + logger?: Logger; } = {}, ) { - const { registryUrl, install } = options; + const { registryUrl, install, logger } = options; let version; if (!semver.valid(pkgVersion)) { // get pkgName version @@ -108,6 +110,7 @@ export async function downloadPackage( version = pkgVersion; } const targetDir = `${os.tmpdir()}/csmith-generator/${pkgName}@${version}`; + logger?.debug?.(`Download package ${pkgName}@${version} to ${targetDir}`); if ((await fsExists(targetDir)) && (await isValidCache(targetDir))) { return targetDir; } @@ -122,7 +125,7 @@ export async function downloadPackage( await downloadAndDecompressTargz(tarballPkg, targetDir); if (install) { - await runInstall(targetDir, registryUrl); + await runInstall(targetDir, registryUrl, logger); } // write completed flag diff --git a/packages/core/src/utils/packageManager.ts b/packages/core/src/utils/packageManager.ts index 72249ab9..f8c9f6a2 100644 --- a/packages/core/src/utils/packageManager.ts +++ b/packages/core/src/utils/packageManager.ts @@ -1,5 +1,6 @@ import path from 'path'; import { fs, execa } from '@modern-js/utils'; +import { Logger } from '@/logger'; export async function canUseYarn() { try { @@ -23,7 +24,11 @@ export async function canUsePnpm() { } } -export async function runInstall(targetDir: string, registryUrl?: string) { +export async function runInstall( + targetDir: string, + registryUrl?: string, + logger?: Logger, +) { const options = { cwd: targetDir, env: process.env, @@ -41,19 +46,27 @@ export async function runInstall(targetDir: string, registryUrl?: string) { * no handle */ } + + const showLog = logger?.level === 'debug'; + if (await canUsePnpm()) { const params = [ 'install', '--prod', - '--reporter=silent', + showLog ? null : '--reporter=silent', // if debug mode, console install log '--ignore-scripts', - ]; + ].filter(Boolean) as string[]; if (registryUrl) { params.push(`--registry=${registryUrl}`); } await execa('pnpm', params, options); } else if (await canUseYarn()) { - const params = ['install', '--production', '--silent', '--ignore-scripts']; + const params = [ + 'install', + '--production', + showLog ? null : '--silent', + '--ignore-scripts', + ].filter(Boolean) as string[]; if (registryUrl) { params.push(`--registry=${registryUrl}`); }