Skip to content

Commit bd35bfe

Browse files
Coly010FrozenPandaz
authored andcommitted
fix(core): update package script logic to handle cli tool name as command (#29617)
## Current Behavior When we have an inferred target command that matches the entry point of the cli tool, there is a chance we do not replace package scripts correctly e.g. ``` { "dev": "vite", "build": "tsc -b && vite build", "preview": "vite preview" } ``` this could result in package scripts being updated to ``` { "dev": "nx dev", "build": "tsc -b && nx vite:build", "preview": "nx dev preview" } ``` ## Expected Behavior We should update the package scripts correctly to match the desired inferred target ``` { "preview": "nx preview" } ``` (cherry picked from commit c2fa9a0)
1 parent ed3c5cb commit bd35bfe

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

packages/devkit/src/utils/add-plugin.spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,55 @@ describe('addPlugin', () => {
290290
expect(scripts['build:dev']).toBe('nx build');
291291
});
292292

293+
it('should support replacing scripts where a command is the same as the cli entry point', async () => {
294+
writeJson(tree, 'app1/package.json', {
295+
name: 'app1',
296+
scripts: {
297+
dev: 'next',
298+
build: 'tsc -b && next build',
299+
preview: 'next preview',
300+
},
301+
});
302+
303+
createNodes = [
304+
'**/next.config.{ts,js,cjs,mjs}',
305+
() => [
306+
[
307+
'app1/next.config.js',
308+
{
309+
projects: {
310+
app1: {
311+
name: 'app1',
312+
targets: {
313+
build: { command: 'next build' },
314+
dev: { command: 'next' },
315+
preview: { command: 'next preview' },
316+
},
317+
},
318+
},
319+
},
320+
],
321+
],
322+
];
323+
324+
await addPlugin(
325+
tree,
326+
graph,
327+
'@nx/next/plugin',
328+
createNodes,
329+
330+
{
331+
targetName: ['build'],
332+
},
333+
true
334+
);
335+
336+
const { scripts } = readJson<PackageJson>(tree, 'app1/package.json');
337+
expect(scripts.dev).toBe('nx dev');
338+
expect(scripts.build).toBe('tsc -b && nx build');
339+
expect(scripts.preview).toBe('nx preview');
340+
});
341+
293342
it('should support replacing multiple scripts', async () => {
294343
writeJson(tree, 'app1/package.json', {
295344
name: 'app1',

packages/devkit/src/utils/add-plugin.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ function processProject(
232232
if (!tree.exists(packageJsonPath)) {
233233
return;
234234
}
235+
235236
const packageJson = readJson<PackageJson>(tree, packageJsonPath);
236237
if (!packageJson.scripts || !Object.keys(packageJson.scripts).length) {
237238
return;
@@ -243,6 +244,9 @@ function processProject(
243244
}
244245

245246
let hasChanges = false;
247+
targetCommands.sort(
248+
(a, b) => b.command.split(/\s/).length - a.command.split(/\s/).length
249+
);
246250
for (const targetCommand of targetCommands) {
247251
const { command, target, configuration } = targetCommand;
248252
const targetCommandRegex = new RegExp(
@@ -325,9 +329,8 @@ function processProject(
325329

326330
if (!hasArgsWithDifferentValues && !scriptHasExtraArgs) {
327331
// they are the same, replace with the command removing the args
328-
packageJson.scripts[scriptName] = packageJson.scripts[
329-
scriptName
330-
].replace(
332+
const script = packageJson.scripts[scriptName];
333+
packageJson.scripts[scriptName] = script.replace(
331334
match,
332335
match.replace(
333336
commandRegex,

0 commit comments

Comments
 (0)