Skip to content

Commit

Permalink
feat: optimize generator download (#6377)
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin authored Oct 16, 2024
1 parent a9e3eb7 commit d6986c5
Show file tree
Hide file tree
Showing 74 changed files with 1,364 additions and 894 deletions.
31 changes: 31 additions & 0 deletions .changeset/four-donkeys-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
'@modern-js/storybook-next-generator': minor
'@modern-js/tailwindcss-generator': minor
'@modern-js/dependence-generator': minor
'@modern-js/module-doc-generator': minor
'@modern-js/changeset-generator': minor
'@modern-js/generator-generator': minor
'@modern-js/router-v5-generator': minor
'@modern-js/packages-generator': minor
'@modern-js/upgrade-generator': minor
'@modern-js/module-generator': minor
'@modern-js/server-generator': minor
'@modern-js/entry-generator': minor
'@modern-js/base-generator': minor
'@modern-js/repo-generator': minor
'@modern-js/bff-generator': minor
'@modern-js/mwa-generator': minor
'@modern-js/ssg-generator': minor
'@modern-js/generator-plugin-plugin': minor
'@modern-js/generator-common': minor
'@modern-js/generator-plugin': minor
'@modern-js/generator-utils': minor
'@modern-js/sandpack-react': patch
'@modern-js/new-action': patch
'@modern-js/plugin-i18n': patch
'@modern-js/create': patch
---

feat: optimize generator download

feat: 优化生成器下载
2 changes: 2 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
"node_modules/**/*",
"dist/**/*",
"dist-1/**/*",
"dist-2/**/*",
"dist-swc/**/*",
"output/**/*",
".output/**/*",
".netlify/**/*",
Expand Down
8 changes: 4 additions & 4 deletions packages/generator/generator-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
"test": "jest --passWithNoTests"
},
"dependencies": {
"@modern-js/codesmith-formily": "2.5.2",
"@modern-js/codesmith-formily": "2.6.3",
"@modern-js/plugin-i18n": "workspace:*",
"@swc/helpers": "0.5.13"
},
"devDependencies": {
"@modern-js/codesmith": "2.5.2",
"@modern-js/codesmith-api-app": "2.5.2",
"@modern-js/utils": "workspace:*",
"@modern-js/codesmith": "2.6.3",
"@modern-js/codesmith-api-app": "2.6.3",
"@modern-js/codesmith-utils": "2.6.3",
"@scripts/build": "workspace:*",
"@scripts/jest-config": "workspace:*",
"@types/jest": "^29",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
MaterialsManager,
} from '@modern-js/codesmith';
import { AppAPI } from '@modern-js/codesmith-api-app';
import { fs } from '@modern-js/utils';
import { fs } from '@modern-js/codesmith-utils/fs-extra';
import { getPackageManagerSchema } from '../src/common/packageManager';

describe('test package manager schema', () => {
Expand Down
16 changes: 9 additions & 7 deletions packages/generator/generator-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@
"test": "jest"
},
"dependencies": {
"@modern-js/codesmith": "2.5.2",
"@modern-js/codesmith-api-git": "2.5.2",
"@modern-js/codesmith-api-json": "2.5.2",
"@modern-js/codesmith-api-npm": "2.5.2",
"@modern-js/codesmith-formily": "2.5.2",
"@modern-js/codesmith": "2.6.3",
"@modern-js/codesmith-api-git": "2.6.3",
"@modern-js/codesmith-api-json": "2.6.3",
"@modern-js/codesmith-api-npm": "2.6.3",
"@modern-js/codesmith-formily": "2.6.3",
"@modern-js/codesmith-utils": "2.6.3",
"@modern-js/generator-common": "workspace:*",
"@modern-js/generator-utils": "workspace:*",
"@modern-js/new-action": "workspace:*",
"@modern-js/plugin-i18n": "workspace:*",
"@modern-js/utils": "workspace:*",
"@swc/helpers": "0.5.13",
"ejs": "^3.1.9",
"handlebars": "^4.7.7"
"globby": "11.1.0",
"handlebars": "^4.7.7",
"json5": "2.2.3"
},
"devDependencies": {
"@scripts/build": "workspace:*",
Expand Down
8 changes: 4 additions & 4 deletions packages/generator/generator-plugin/src/context/input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Schema } from '@modern-js/codesmith-formily';
import { isFunction } from '@modern-js/utils/lodash';
import { isFunction } from '@modern-js/codesmith-utils/lodash';

export type { Schema };
export class PluginInputContext {
Expand Down Expand Up @@ -97,7 +97,7 @@ export class PluginInputContext {
const schema = this.solutionSchema[key];
if (schema) {
(schema as Record<string, unknown>)[field] = isFunction(value)
? value(schema)
? (value as (input: Schema) => unknown)(schema)
: value;
return;
}
Expand All @@ -107,15 +107,15 @@ export class PluginInputContext {
if (beforeSchema) {
findFlag = true;
(beforeSchema as Record<string, unknown>)[field] = isFunction(value)
? value(schema)
? (value as (input: Schema) => unknown)(schema)
: value;
break;
}
const afterSchema = this.extendInputMap[inputKey].after[key];
if (afterSchema) {
findFlag = true;
(afterSchema as Record<string, unknown>)[field] = isFunction(value)
? value(schema)
? (value as (input: Schema) => unknown)(schema)
: value;
break;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/generator/generator-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import {
getPackageInfo,
} from '@modern-js/codesmith';
import type { Schema } from '@modern-js/codesmith-formily';
import { fs } from '@modern-js/codesmith-utils/fs-extra';
import { isFunction, merge } from '@modern-js/codesmith-utils/lodash';
import {
type Solution,
SolutionSchemas,
i18n as commonI18n,
} from '@modern-js/generator-common';
import { fs, i18n as utilsI18n } from '@modern-js/generator-utils';
import { isFunction, merge } from '@modern-js/utils/lodash';
import { i18n as utilsI18n } from '@modern-js/generator-utils';
import type { ICustomInfo } from './common';
import {
LifeCycle,
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/generator-plugin/src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import { fs } from '@modern-js/generator-utils';
import { globby } from '@modern-js/utils';
import globby from 'globby';

export enum FileType {
Text = 'text',
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/generator-plugin/src/utils/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get } from '@modern-js/utils/lodash';
import { get } from '@modern-js/codesmith-utils/lodash';

export function requireModule(modulePath: string) {
const module = require(modulePath);
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/generator-plugin/tests/input.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject } from '@modern-js/utils/lodash';
import { isObject } from '@modern-js/codesmith-utils';
import { PluginInputContext, type Schema } from '../src/context/input';

describe('PluginInputContext Test', () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/generator/generator-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@
"dependencies": {
"@modern-js/generator-common": "workspace:*",
"@modern-js/plugin-i18n": "workspace:*",
"@modern-js/utils": "workspace:*",
"@modern-js/codesmith-utils": "2.6.3",
"glob": "7.2.0",
"js-yaml": "4.1.0",
"json5": "2.2.3",
"@swc/helpers": "0.5.13"
},
"devDependencies": {
"@modern-js/codesmith": "2.5.2",
"@modern-js/codesmith": "2.6.3",
"@scripts/build": "workspace:*",
"@scripts/jest-config": "workspace:*",
"@types/jest": "^29",
"@types/glob": "7.2.0",
"@types/js-yaml": "4.0.9",
"@types/node": "^14",
"jest": "^29",
"typescript": "^5"
Expand Down
57 changes: 18 additions & 39 deletions packages/generator/generator-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,44 @@
import path from 'path';
import type { GeneratorContext } from '@modern-js/codesmith';
import { getNpmVersion, getPackageInfo } from '@modern-js/codesmith';
import { fs } from '@modern-js/codesmith-utils/fs-extra';
import { ora } from '@modern-js/codesmith-utils/ora';
import { Solution, SolutionToolsMap } from '@modern-js/generator-common';
import {
fs,
canUseNpm,
canUsePnpm,
execa,
getMonorepoPackages,
ora,
} from '@modern-js/utils';
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';

export {
ora,
fs,
semver,
execa,
chalk,
readTsConfigByFile,
getPackageManager,
canUseNpm,
canUsePnpm,
canUseYarn,
isReact18,
} from '@modern-js/utils';
} from '@modern-js/codesmith-utils/npm';
export { fs } from '@modern-js/codesmith-utils/fs-extra';
export { ora } from '@modern-js/codesmith-utils/ora';
export { semver } from '@modern-js/codesmith-utils/semver';
export { execa } from '@modern-js/codesmith-utils/execa';
export { chalk } from '@modern-js/codesmith-utils/chalk';

export { i18n } from './locale';

export async function getPackageVersion(
packageName: string,
registry?: string,
) {
registryUrl?: string,
): Promise<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);
}
const { name, version: pkgVersion } = getPackageInfo(packageName);
const version = await getNpmVersion(name, {
version: pkgVersion,
registryUrl,
});
spinner.stop();
throw new Error('not found npm, please install npm before');
return version;
}

export async function getModernVersion(
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/generator-utils/src/utils/fsExist.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fs } from '@modern-js/utils';
import { fs } from '@modern-js/codesmith-utils/fs-extra';

export async function fileExist(filePath: string) {
try {
Expand Down
12 changes: 12 additions & 0 deletions packages/generator/generator-utils/src/utils/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import path from 'path';
import { fs } from '@modern-js/codesmith-utils/fs-extra';
import json5 from 'json5';

export const readTsConfig = (root: string) => {
return readTsConfigByFile(path.resolve(root, './tsconfig.json'));
};

export const readTsConfigByFile = (filename: string) => {
const content = fs.readFileSync(path.resolve(filename), 'utf-8');
return json5.parse(content);
};
4 changes: 3 additions & 1 deletion packages/generator/generator-utils/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './stripAnsi';
export * from './package';
export { getGeneratorPath } from './getGeneratorPath';
export * from './getGeneratorPath';
export * from './get';
export * from './is';
23 changes: 23 additions & 0 deletions packages/generator/generator-utils/src/utils/is.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import path from 'path';
import { fs } from '@modern-js/codesmith-utils/fs-extra';
import { semver } from '@modern-js/codesmith-utils/semver';

export const isReact18 = (cwd: string = process.cwd()) => {
const pkgPath = path.join(cwd, 'package.json');

if (!fs.existsSync(pkgPath)) {
return false;
}

const pkgInfo = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
const deps = {
...pkgInfo.devDependencies,
...pkgInfo.dependencies,
};

if (typeof deps.react !== 'string') {
return false;
}

return semver.satisfies(semver.minVersion(deps.react)!, '>=18.0.0');
};
Loading

0 comments on commit d6986c5

Please sign in to comment.