Skip to content

Commit

Permalink
fix(core): update package script logic to handle cli tool name as com…
Browse files Browse the repository at this point in the history
…mand (#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"
}
```
  • Loading branch information
Coly010 authored Jan 14, 2025
1 parent c6e9565 commit c2fa9a0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
49 changes: 49 additions & 0 deletions packages/devkit/src/utils/add-plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,55 @@ describe('addPlugin', () => {
expect(scripts['build:dev']).toBe('nx build');
});

it('should support replacing scripts where a command is the same as the cli entry point', async () => {
writeJson(tree, 'app1/package.json', {
name: 'app1',
scripts: {
dev: 'next',
build: 'tsc -b && next build',
preview: 'next preview',
},
});

createNodes = [
'**/next.config.{ts,js,cjs,mjs}',
() => [
[
'app1/next.config.js',
{
projects: {
app1: {
name: 'app1',
targets: {
build: { command: 'next build' },
dev: { command: 'next' },
preview: { command: 'next preview' },
},
},
},
},
],
],
];

await addPlugin(
tree,
graph,
'@nx/next/plugin',
createNodes,

{
targetName: ['build'],
},
true
);

const { scripts } = readJson<PackageJson>(tree, 'app1/package.json');
expect(scripts.dev).toBe('nx dev');
expect(scripts.build).toBe('tsc -b && nx build');
expect(scripts.preview).toBe('nx preview');
});

it('should support replacing multiple scripts', async () => {
writeJson(tree, 'app1/package.json', {
name: 'app1',
Expand Down
9 changes: 6 additions & 3 deletions packages/devkit/src/utils/add-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ function processProject(
if (!tree.exists(packageJsonPath)) {
return;
}

const packageJson = readJson<PackageJson>(tree, packageJsonPath);
if (!packageJson.scripts || !Object.keys(packageJson.scripts).length) {
return;
Expand All @@ -243,6 +244,9 @@ function processProject(
}

let hasChanges = false;
targetCommands.sort(
(a, b) => b.command.split(/\s/).length - a.command.split(/\s/).length
);
for (const targetCommand of targetCommands) {
const { command, target, configuration } = targetCommand;
const targetCommandRegex = new RegExp(
Expand Down Expand Up @@ -325,9 +329,8 @@ function processProject(

if (!hasArgsWithDifferentValues && !scriptHasExtraArgs) {
// they are the same, replace with the command removing the args
packageJson.scripts[scriptName] = packageJson.scripts[
scriptName
].replace(
const script = packageJson.scripts[scriptName];
packageJson.scripts[scriptName] = script.replace(
match,
match.replace(
commandRegex,
Expand Down

0 comments on commit c2fa9a0

Please sign in to comment.