Skip to content

Commit

Permalink
feat: op get package version utils
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Oct 14, 2024
1 parent f41bc3e commit 1dcb03c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 42 deletions.
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,18 @@
"overrides": {
"@types/react": "^18",
"@types/react-dom": "^18",
"@modern-js/codesmith-api-app":"0.0.0-canary-20241014021243",
"@modern-js/codesmith-api-ejs":"0.0.0-canary-20241014021243",
"@modern-js/codesmith-api-fs":"0.0.0-canary-20241014021243",
"@modern-js/codesmith-api-git":"0.0.0-canary-20241014021243",
"@modern-js/codesmith-api-handlebars":"0.0.0-canary-20241014021243",
"@modern-js/codesmith-api-json":"0.0.0-canary-20241014021243",
"@modern-js/codesmith-api-npm":"0.0.0-canary-20241014021243",
"@modern-js/codesmith-cli":"0.0.0-canary-20241014021243",
"@modern-js/codesmith-formily":"0.0.0-canary-20241014021243",
"@modern-js/codesmith":"0.0.0-canary-20241014021243",
"@modern-js/inquirer-types":"0.0.0-canary-20241014021243",
"@modern-js/codesmith-utils":"0.0.0-canary-20241014021243"
"@modern-js/codesmith-api-app":"0.0.0-canary-20241014055716",
"@modern-js/codesmith-api-ejs":"0.0.0-canary-20241014055716",
"@modern-js/codesmith-api-fs":"0.0.0-canary-20241014055716",
"@modern-js/codesmith-api-git":"0.0.0-canary-20241014055716",
"@modern-js/codesmith-api-handlebars":"0.0.0-canary-20241014055716",
"@modern-js/codesmith-api-json":"0.0.0-canary-20241014055716",
"@modern-js/codesmith-api-npm":"0.0.0-canary-20241014055716",
"@modern-js/codesmith-cli":"0.0.0-canary-20241014055716",
"@modern-js/codesmith-formily":"0.0.0-canary-20241014055716",
"@modern-js/codesmith":"0.0.0-canary-20241014055716",
"@modern-js/inquirer-types":"0.0.0-canary-20241014055716",
"@modern-js/codesmith-utils":"0.0.0-canary-20241014055716"
},
"peerDependencyRules": {
"allowedVersions": {
Expand Down
35 changes: 5 additions & 30 deletions packages/generator/generator-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import path from 'path';
import type { GeneratorContext } from '@modern-js/codesmith';
import { execa } from '@modern-js/codesmith-utils/execa';
import { getNpmVersion, getPackageInfo } from '@modern-js/codesmith';
import { fs } from '@modern-js/codesmith-utils/fs-extra';
import { canUseNpm, canUsePnpm } from '@modern-js/codesmith-utils/npm';
import { ora } from '@modern-js/codesmith-utils/ora';
import { Solution, SolutionToolsMap } from '@modern-js/generator-common';
import { i18n, localeKeys } from './locale';
import { fileExist } from './utils/fsExist';
import { getMonorepoPackages } from './utils/monorepo';
import { getAvailableVersion, isPackageExist } from './utils/package';
import { stripAnsi } from './utils/stripAnsi';

export * from './utils';

Expand All @@ -28,32 +25,10 @@ export { i18n } from './locale';

export async function getPackageVersion(
packageName: string,
registry?: string,
) {
const spinner = ora({
text: 'Load Generator...',
spinner: 'runner',
}).start();
if (await canUsePnpm()) {
const args = ['info', packageName, 'version'];
if (registry) {
args.push(`--registry=${registry}`);
}
const result = await execa('pnpm', args);
spinner.stop();
return stripAnsi(result.stdout);
}
if (await canUseNpm()) {
const args = ['view', packageName, 'version'];
if (registry) {
args.push(`--registry=${registry}`);
}
const result = await execa('npm', args);
spinner.stop();
return stripAnsi(result.stdout);
}
spinner.stop();
throw new Error('not found npm, please install npm before');
registryUrl = '',
): Promise<string> {
const { name, version } = getPackageInfo(packageName);
return getNpmVersion(name, { version, registryUrl });
}

export async function getModernVersion(
Expand Down
12 changes: 12 additions & 0 deletions packages/generator/generators/repo-generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ const handlePlugin = async (
export default async (context: GeneratorContext, generator: GeneratorCore) => {
process.setMaxListeners(20);

const { distTag } = context.config;

// avoid create tools version is older
await generator.prepareGlobal();
await generator.prepareGenerators([
`@modern-js/repo-generator@${distTag || 'latest'}`,
`@modern-js/base-generator@${distTag || 'latest'}`,
`@modern-js/mwa-generator@${distTag || 'latest'}`,
`@modern-js/entry-generator@${distTag || 'latest'}`,
`@modern-js/module-generator@${distTag || 'latest'}`,
]);

const appApi = new AppAPI(context, generator);

const { locale } = context.config;
Expand Down

0 comments on commit 1dcb03c

Please sign in to comment.