Skip to content

Commit

Permalink
feat: optimize generator download
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Sep 30, 2024
1 parent 1ee1af4 commit f0f55c4
Show file tree
Hide file tree
Showing 56 changed files with 667 additions and 433 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': patch
'@modern-js/tailwindcss-generator': patch
'@modern-js/dependence-generator': patch
'@modern-js/module-doc-generator': patch
'@modern-js/changeset-generator': patch
'@modern-js/generator-generator': patch
'@modern-js/router-v5-generator': patch
'@modern-js/packages-generator': patch
'@modern-js/upgrade-generator': patch
'@modern-js/module-generator': patch
'@modern-js/server-generator': patch
'@modern-js/entry-generator': patch
'@modern-js/base-generator': patch
'@modern-js/repo-generator': patch
'@modern-js/bff-generator': patch
'@modern-js/mwa-generator': patch
'@modern-js/ssg-generator': patch
'@modern-js/generator-plugin-plugin': patch
'@modern-js/generator-common': patch
'@modern-js/generator-plugin': patch
'@modern-js/generator-utils': patch
'@modern-js/sandpack-react': patch
'@modern-js/new-action': patch
'@modern-js/plugin-i18n': patch
'@modern-js/create': patch
---

feat: optimize generator download

feat: 优化生成器下载
24 changes: 13 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,19 @@
"overrides": {
"@types/react": "^18",
"@types/react-dom": "^18",
"@modern-js/codesmith-api-app":"0.0.0-canary-20240929033114",
"@modern-js/codesmith-api-ejs":"0.0.0-canary-20240929033114",
"@modern-js/codesmith-api-fs":"0.0.0-canary-20240929033114",
"@modern-js/codesmith-api-git":"0.0.0-canary-20240929033114",
"@modern-js/codesmith-api-handlebars":"0.0.0-canary-20240929033114",
"@modern-js/codesmith-api-json":"0.0.0-canary-20240929033114",
"@modern-js/codesmith-api-npm":"0.0.0-canary-20240929033114",
"@modern-js/codesmith-cli":"0.0.0-canary-20240929033114",
"@modern-js/codesmith-formily":"0.0.0-canary-20240929033114",
"@modern-js/codesmith":"0.0.0-canary-20240929033114",
"@modern-js/inquirer-types":"0.0.0-canary-20240929033114"
"@modern-js/plugin-i18n":"workspace:*",
"@modern-js/codesmith-api-app":"0.0.0-canary-20240930052349",
"@modern-js/codesmith-api-ejs":"0.0.0-canary-20240930052349",
"@modern-js/codesmith-api-fs":"0.0.0-canary-20240930052349",
"@modern-js/codesmith-api-git":"0.0.0-canary-20240930052349",
"@modern-js/codesmith-api-handlebars":"0.0.0-canary-20240930052349",
"@modern-js/codesmith-api-json":"0.0.0-canary-20240930052349",
"@modern-js/codesmith-api-npm":"0.0.0-canary-20240930052349",
"@modern-js/codesmith-cli":"0.0.0-canary-20240930052349",
"@modern-js/codesmith-formily":"0.0.0-canary-20240930052349",
"@modern-js/codesmith":"0.0.0-canary-20240930052349",
"@modern-js/inquirer-types":"0.0.0-canary-20240930052349",
"@modern-js/codesmith-utils":"0.0.0-canary-20240930052349"
},
"peerDependencyRules": {
"allowedVersions": {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/plugin-i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
"test": "jest --passWithNoTests"
},
"dependencies": {
"@modern-js/utils": "workspace:*",
"@swc/helpers": "0.5.13"
"@swc/helpers": "0.5.13",
"@modern-js/codesmith-utils": "2.5.1"
},
"devDependencies": {
"@scripts/build": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/plugin-i18n/src/index.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';
import { getObjKeyMap } from './utils';

type Language = string;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/plugin-i18n/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject, isString } from '@modern-js/utils/lodash';
import { isObject, isString } from '@modern-js/codesmith-utils/lodash';

interface ITem {
[key: string]: string | ITem;
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/generator-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"devDependencies": {
"@modern-js/codesmith": "2.5.1",
"@modern-js/codesmith-api-app": "2.5.1",
"@modern-js/utils": "workspace:*",
"@modern-js/codesmith-utils": "2.5.1",
"@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
6 changes: 4 additions & 2 deletions packages/generator/generator-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
"@modern-js/codesmith-api-json": "2.5.1",
"@modern-js/codesmith-api-npm": "2.5.1",
"@modern-js/codesmith-formily": "2.5.1",
"@modern-js/codesmith-utils": "2.5.1",
"@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/lodash';
import { PluginInputContext, type Schema } from '../src/context/input';

describe('PluginInputContext Test', () => {
Expand Down
7 changes: 6 additions & 1 deletion 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.5.1",
"glob": "7.2.0",
"js-yaml": "4.1.0",
"json5": "2.2.3",
"@swc/helpers": "0.5.13"
},
"devDependencies": {
"@modern-js/codesmith": "2.5.1",
"@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
28 changes: 11 additions & 17 deletions packages/generator/generator-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import path from 'path';
import type { GeneratorContext } from '@modern-js/codesmith';
import { execa } from '@modern-js/codesmith-utils/execa';
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 {
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';

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 f0f55c4

Please sign in to comment.