Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add logger for install #141

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading