Skip to content

Commit 3c0c5bb

Browse files
authored
fix: packages generator overrides field when use npm (#3655)
* fix: packages generator overrides field when use npm * fix: generator test packageManager * feat: integration test add max workers params * feat: node 14 pnpm install use flat struct * fix: npm overrides install error * fix: run packages generator position
1 parent c8ba482 commit 3c0c5bb

File tree

8 files changed

+118
-58
lines changed

8 files changed

+118
-58
lines changed

.changeset/warm-moons-sneeze.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@modern-js/packages-generator': patch
3+
---
4+
5+
fix: packages generator overrides field when use npm
6+
7+
fix: 修复当使用 npm 安装依赖时 packages-generator overrides 字段

packages/generator/generators/mwa-generator/src/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,6 @@ export const handleTemplateFile = async (
204204
}
205205

206206
const { packagesInfo, buildTools } = context.config;
207-
if (packagesInfo && Object.keys(packagesInfo).length > 0) {
208-
await appApi.runSubGenerator(
209-
getGeneratorPath(PackagesGenerator, context.config.distTag),
210-
undefined,
211-
context.config,
212-
);
213-
}
214207

215208
if (buildTools === BuildTools.Rspack) {
216209
await appApi.runSubGenerator(
@@ -225,6 +218,14 @@ export const handleTemplateFile = async (
225218
);
226219
}
227220

221+
if (packagesInfo && Object.keys(packagesInfo).length > 0) {
222+
await appApi.runSubGenerator(
223+
getGeneratorPath(PackagesGenerator, context.config.distTag),
224+
undefined,
225+
context.config,
226+
);
227+
}
228+
228229
return { projectPath };
229230
};
230231

packages/generator/generators/packages-generator/src/index.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const handleTemplateFile = async (
2222
$set: update,
2323
},
2424
});
25-
} else {
25+
} else if (packageManager === PackageManager.Yarn) {
2626
const pkgInfo = fs.readJSONSync(
2727
path.join(context.materials.default.basePath, 'package.json'),
2828
'utf-8',
@@ -45,6 +45,29 @@ const handleTemplateFile = async (
4545
$set: update,
4646
},
4747
});
48+
} else {
49+
const pkgInfo = fs.readJSONSync(
50+
path.join(context.materials.default.basePath, 'package.json'),
51+
'utf-8',
52+
);
53+
const { dependencies = {}, devDependencies = {} } = pkgInfo;
54+
55+
const update: Record<string, string> = {};
56+
Object.entries(packagesInfo || {}).forEach(([name, version]) => {
57+
update[`overrides.${name}`] = version as string;
58+
if (dependencies[name]) {
59+
update[`dependencies.${name}`] = version as string;
60+
}
61+
if (devDependencies[name]) {
62+
update[`devDependencies.${name}`] = version as string;
63+
}
64+
});
65+
await jsonAPI.update(context.materials.default.get('package.json'), {
66+
query: {},
67+
update: {
68+
$set: update,
69+
},
70+
});
4871
}
4972
};
5073

tests/generator/module.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import path from 'path';
22
import { getModuleCases, getModuleNewCases } from '@modern-js/generator-cases';
3-
import { fs, nanoid } from '@modern-js/utils';
3+
import { fs, nanoid, semver } from '@modern-js/utils';
44
import { ModuleNewAction } from '@modern-js/new-action';
55
import { prepare } from './utils/prepare';
6-
import { execaWithStreamLog, usingTempDir } from './utils/tools';
6+
import {
7+
execaWithStreamLog,
8+
getPackageManager,
9+
usingTempDir,
10+
} from './utils/tools';
711
import {
812
runLintProject,
913
runCreteCommand,
@@ -62,10 +66,10 @@ async function runNewInModuleProject(
6266
cwd: path.join(tmpDir, project),
6367
},
6468
);
65-
const packageManager = project.includes('pnpm') ? 'pnpm' : 'npm';
69+
const packageManager = getPackageManager(project);
6670
const cases = getModuleNewCases();
6771
for (const config of cases) {
68-
await runModuleNewCommand(isLocal, packageManager, {
72+
await runModuleNewCommand(isLocal, project, {
6973
cwd: path.join(tmpDir, project),
7074
config: JSON.stringify({
7175
noNeedInstall: true,
@@ -83,7 +87,7 @@ async function runNewInModuleProject(
8387

8488
async function runModuleNewCommand(
8589
isLocal: boolean,
86-
packageManager: 'pnpm' | 'npm',
90+
project: string,
8791
options: {
8892
config: string;
8993
cwd: string;
@@ -101,13 +105,6 @@ async function runModuleNewCommand(
101105
config,
102106
cwd,
103107
});
104-
await execaWithStreamLog(
105-
packageManager,
106-
['install', '--ignore-scripts', '--force'],
107-
{
108-
cwd,
109-
},
110-
);
111108
} else {
112109
await execaWithStreamLog(
113110
'npm',
@@ -128,13 +125,19 @@ async function runModuleNewCommand(
128125
},
129126
},
130127
);
131-
await execaWithStreamLog(
132-
packageManager,
133-
['install', '--ignore-scripts', '--force'],
134-
{
135-
cwd,
136-
},
137-
);
128+
}
129+
const isNode16 = semver.gte(process.versions.node, '16.0.0');
130+
const params = ['install', '--ignore-scripts', '--force'];
131+
const packageManager = getPackageManager(project);
132+
if (isNode16 || project.includes('pnpm')) {
133+
await execaWithStreamLog(packageManager, params, {
134+
cwd,
135+
});
136+
} else {
137+
params.push('--shamefully-hoist');
138+
await execaWithStreamLog(packageManager, params, {
139+
cwd,
140+
});
138141
}
139142
}
140143

tests/generator/mwa.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import path from 'path';
22
import { getMWACases, getMWANewCases } from '@modern-js/generator-cases';
3-
import { fs, nanoid } from '@modern-js/utils';
3+
import { fs, nanoid, semver } from '@modern-js/utils';
44
import { MWANewAction } from '@modern-js/new-action';
55
import { prepare } from './utils/prepare';
6-
import { execaWithStreamLog, usingTempDir } from './utils/tools';
6+
import {
7+
execaWithStreamLog,
8+
getPackageManager,
9+
usingTempDir,
10+
} from './utils/tools';
711
import {
812
runLintProject,
913
runCreteCommand,
@@ -63,7 +67,7 @@ async function runNewMWAProject(
6367
cwd: path.join(tmpDir, project),
6468
},
6569
);
66-
const packageManager = project.includes('pnpm') ? 'pnpm' : 'npm';
70+
const packageManager = getPackageManager(project);
6771
const cases = getMWANewCases(isSimple ? 5 : undefined);
6872
let hasMicroFrontend = false;
6973
let hasSSG = false;
@@ -110,7 +114,7 @@ async function runNewMWAProject(
110114
if (config.actionType === 'refactor') {
111115
continue;
112116
}
113-
await runMWANewCommand(isLocal, packageManager, {
117+
await runMWANewCommand(isLocal, project, {
114118
cwd: path.join(tmpDir, project),
115119
config: JSON.stringify({
116120
noNeedInstall: true,
@@ -128,7 +132,7 @@ async function runNewMWAProject(
128132

129133
async function runMWANewCommand(
130134
isLocal: boolean,
131-
packageManager: 'pnpm' | 'npm',
135+
project: string,
132136
options: {
133137
config: string;
134138
cwd: string;
@@ -146,13 +150,6 @@ async function runMWANewCommand(
146150
config,
147151
cwd,
148152
});
149-
await execaWithStreamLog(
150-
packageManager,
151-
['install', '--ignore-scripts', '--force'],
152-
{
153-
cwd,
154-
},
155-
);
156153
} else {
157154
await execaWithStreamLog(
158155
'npm',
@@ -173,13 +170,19 @@ async function runMWANewCommand(
173170
},
174171
},
175172
);
176-
await execaWithStreamLog(
177-
packageManager,
178-
['install', '--ignore-scripts', '--force'],
179-
{
180-
cwd,
181-
},
182-
);
173+
}
174+
const isNode16 = semver.gte(process.versions.node, '16.0.0');
175+
const params = ['install', '--ignore-scripts', '--force'];
176+
const packageManager = getPackageManager(project);
177+
if (isNode16 || project.includes('pnpm')) {
178+
await execaWithStreamLog(packageManager, params, {
179+
cwd,
180+
});
181+
} else {
182+
params.push('--shamefully-hoist');
183+
await execaWithStreamLog(packageManager, params, {
184+
cwd,
185+
});
183186
}
184187
}
185188

tests/generator/utils/command.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path';
2-
import { fs } from '@modern-js/utils';
3-
import { execaWithStreamLog } from './tools';
2+
import { fs, semver } from '@modern-js/utils';
3+
import { execaWithStreamLog, getPackageManager } from './tools';
44

55
export async function runCreteCommand(
66
repoDir: string,
@@ -17,6 +17,7 @@ export async function runCreteCommand(
1717
const debug =
1818
process.env.DEBUG === 'true' || process.env.CUSTOM_DEBUG === 'true';
1919
const packages = process.env.PACKAGES;
20+
const packageManager = getPackageManager(projectName);
2021
if (isLocal) {
2122
return execaWithStreamLog(
2223
'node',
@@ -27,6 +28,7 @@ export async function runCreteCommand(
2728
JSON.stringify({
2829
packageName: projectName,
2930
...config,
31+
packageManager,
3032
}),
3133
'--dist-tag',
3234
'next',
@@ -57,6 +59,7 @@ export async function runCreteCommand(
5759
JSON.stringify({
5860
packageName: projectName,
5961
...config,
62+
packageManager,
6063
}),
6164
debug ? '--debug' : '',
6265
platform ? '--platform' : '',
@@ -81,14 +84,19 @@ export async function runInstallAndBuildProject(type: string, tmpDir: string) {
8184
.filter(project => project.includes(type))
8285
.map(async project => {
8386
console.info('install and build process', project);
84-
const packageManager = project.includes('pnpm') ? 'pnpm' : 'npm';
85-
await execaWithStreamLog(
86-
packageManager,
87-
['install', '--ignore-scripts', '--force'],
88-
{
87+
const packageManager = getPackageManager(project);
88+
const isNode16 = semver.gte(process.versions.node, '16.0.0');
89+
const params = ['install', '--ignore-scripts', '--force'];
90+
if (isNode16 || project.includes('pnpm')) {
91+
await execaWithStreamLog(packageManager, params, {
8992
cwd: path.join(tmpDir, project),
90-
},
91-
);
93+
});
94+
} else {
95+
params.push('--shamefully-hoist');
96+
await execaWithStreamLog(packageManager, params, {
97+
cwd: path.join(tmpDir, project),
98+
});
99+
}
92100
if (project.includes('monorepo')) {
93101
return Promise.resolve();
94102
}
@@ -109,7 +117,7 @@ export async function runLintProject(type: string, tmpDir: string) {
109117
)
110118
.map(async project => {
111119
console.info('lint process', project);
112-
const packageManager = project.includes('pnpm') ? 'pnpm' : 'npm';
120+
const packageManager = getPackageManager(project);
113121
await execaWithStreamLog(packageManager, ['run', 'lint'], {
114122
cwd: path.join(tmpDir, project),
115123
});

tests/generator/utils/tools.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { fs, execa } from '@modern-js/utils';
1+
// eslint-disable-next-line eslint-comments/disable-enable-pair
2+
/* eslint-disable consistent-return */
3+
import { fs, execa, semver } from '@modern-js/utils';
24

35
export async function usingTempDir(
46
tmpDir: string,
@@ -29,3 +31,16 @@ export async function execaWithStreamLog(
2931
process.exit(1);
3032
}
3133
}
34+
35+
export function getPackageManager(projectName: string) {
36+
const isNode16 = semver.gte(process.versions.node, '16.0.0');
37+
if (!isNode16) {
38+
return 'pnpm';
39+
}
40+
// eslint-disable-next-line no-nested-ternary
41+
return projectName.includes('pnpm')
42+
? 'pnpm'
43+
: projectName.includes('yarn')
44+
? 'yarn'
45+
: 'npm';
46+
}

tests/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"test:rspack": "jest -w 1 builder-rspack && pnpm test:builder:rspack",
99
"test:builder:rspack": "cd e2e/builder && pnpm test:rspack",
1010
"test:doc": "cd integration && jest -w 1 --testMatch **/doc/**/*.test.ts",
11-
"test": "pnpm test --filter api-service-koa & jest",
11+
"test": "pnpm test --filter api-service-koa & jest -w 8",
1212
"test:ut": "node --conditions=jsnext:source -r btsm ./node_modules/jest/bin/jest.js -c jest-ut.config.js --maxWorkers=2",
1313
"cypress:test": "pnpm --parallel --filter '@cypress-test/*' run dev & cypress open",
1414
"prepare": "node node_modules/puppeteer/install.js",

0 commit comments

Comments
 (0)