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

fix(codemod): configPath maybe .umirc.ts or .umirc.js #11936

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions codemod/src/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export async function prepare(opts: { cwd: string; pattern: any; args?: any }) {

return {
config,
configPath: mainConfigFile,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议这个命名最好叫 mainConfigPath 或者 mainConfigFile ,因为 configPath 指代哪个 config 文件不明确。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

absAppJSPath,
pkg,
pkgPath,
Expand Down
13 changes: 3 additions & 10 deletions codemod/src/runner/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as t from '@umijs/bundler-utils/compiled/babel/types';
import { lodash } from '@umijs/utils';
import assert from 'assert';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import { update as appJSUpdate } from '../appJSUpdater';
import { update } from '../configUpdater';
import { info } from '../logger';
Expand Down Expand Up @@ -123,17 +121,12 @@ export class Runner {
setKeys['layout.locale'] = false;
}

const configFile = join(this.cwd, 'config/config.ts');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么不直接改这一行而是要改下面很多行呢。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在这里改的话,需要使用 tryPaths 或者 prepare 中的逻辑推断下用户实际使用的配置文件是否是 config/config.ts, config/config.js 还是 .umirc.ts, .umirc.js,而这个逻辑在前置流程里 prepare 已经做了,所以通过 context 传递可以避免重复开发

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里可以这样获得最少的代码更改吧:

  const configFile = this.context.configPath;

assert(
existsSync(configFile),
`Could not find config file at ${configFile}`,
);
const {
config: { code },
routesConfig: { filePath: routeFilePath, code: routeCode } = {} as any,
} = update({
code: readFileSync(configFile, 'utf-8'),
filePath: configFile,
code: readFileSync(this.context.configPath, 'utf-8'),
filePath: this.context.configPath,
updates: {
del: deleteKeys,
set: setKeys,
Expand Down Expand Up @@ -164,7 +157,7 @@ export class Runner {
},
},
});
writeFileSync(configFile, code);
writeFileSync(this.context.configPath, code);
if (routeCode) {
writeFileSync(routeFilePath, routeCode);
}
Expand Down