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(core): update package script logic to handle cli tool name as command #29617

Merged
merged 1 commit into from
Jan 14, 2025
Merged
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
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
Loading