Skip to content

Commit

Permalink
feat: add logger for install (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
KyrieLii authored Jun 12, 2024
1 parent 4a1331b commit 1be9cbf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/wise-dancers-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/codesmith': patch
---

feat: add logger for install
feat: install 添加 logger
9 changes: 6 additions & 3 deletions packages/core/src/utils/downloadPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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
Expand Down
21 changes: 17 additions & 4 deletions packages/core/src/utils/packageManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';
import { fs, execa } from '@modern-js/utils';
import { Logger } from '@/logger';

export async function canUseYarn() {
try {
Expand All @@ -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,
Expand All @@ -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}`);
}
Expand Down

0 comments on commit 1be9cbf

Please sign in to comment.