-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): move target defaults handling to nx plugin (#21104)
- Loading branch information
1 parent
2e2dada
commit 06717de
Showing
23 changed files
with
353 additions
and
357 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...x/plugins/package-json-workspaces.spec.ts → ...kage-json-workspaces/create-nodes.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 11 additions & 11 deletions
22
...ges/nx/plugins/package-json-workspaces.ts → ...s/package-json-workspaces/create-nodes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './create-nodes'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/nx/src/plugins/target-defaults/target-defaults-plugin.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import * as memfs from 'memfs'; | ||
|
||
import '../../../src/internal-testing-utils/mock-fs'; | ||
|
||
import { TargetDefaultsPlugin } from './target-defaults-plugin'; | ||
import { CreateNodesContext } from '../../utils/nx-plugin'; | ||
const { | ||
createNodes: [, createNodesFn], | ||
} = TargetDefaultsPlugin; | ||
|
||
describe('nx project.json plugin', () => { | ||
let context: CreateNodesContext; | ||
beforeEach(() => { | ||
context = { | ||
nxJsonConfiguration: { | ||
targetDefaults: { | ||
build: { | ||
dependsOn: ['^build'], | ||
}, | ||
}, | ||
}, | ||
workspaceRoot: '/root', | ||
}; | ||
}); | ||
|
||
it('should add target default info to project json projects', () => { | ||
memfs.vol.fromJSON( | ||
{ | ||
'project.json': JSON.stringify({ | ||
name: 'root', | ||
targets: { echo: { command: 'echo root project' } }, | ||
}), | ||
'packages/lib-a/project.json': JSON.stringify({ | ||
name: 'lib-a', | ||
targets: { | ||
build: { | ||
executor: 'nx:run-commands', | ||
options: {}, | ||
}, | ||
}, | ||
}), | ||
}, | ||
'/root' | ||
); | ||
|
||
expect(createNodesFn('project.json', undefined, context)) | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"projects": { | ||
".": { | ||
"targets": { | ||
"build": { | ||
"dependsOn": [ | ||
"^build", | ||
], | ||
Symbol(ONLY_MODIFIES_EXISTING_TARGET): true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
`); | ||
expect(createNodesFn('packages/lib-a/project.json', undefined, context)) | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"projects": { | ||
"packages/lib-a": { | ||
"targets": { | ||
"build": { | ||
"dependsOn": [ | ||
"^build", | ||
], | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
`); | ||
}); | ||
}); |
129 changes: 129 additions & 0 deletions
129
packages/nx/src/plugins/target-defaults/target-defaults-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { minimatch } from 'minimatch'; | ||
import { existsSync } from 'node:fs'; | ||
import { basename, dirname, join } from 'node:path'; | ||
|
||
import { | ||
ProjectConfiguration, | ||
TargetConfiguration, | ||
} from '../../config/workspace-json-project-json'; | ||
import { readJsonFile } from '../../utils/fileutils'; | ||
import { combineGlobPatterns } from '../../utils/globs'; | ||
import { NxPluginV2 } from '../../utils/nx-plugin'; | ||
import { PackageJson } from '../../utils/package-json'; | ||
import { getGlobPatternsFromPackageManagerWorkspaces } from '../package-json-workspaces'; | ||
|
||
/** | ||
* This symbol marks that a target provides information which should modify a target already registered | ||
* on the project via other plugins. If the target has not already been registered, and this symbol is true, | ||
* the information provided by it will be discarded. | ||
*/ | ||
export const ONLY_MODIFIES_EXISTING_TARGET = Symbol( | ||
'ONLY_MODIFIES_EXISTING_TARGET' | ||
); | ||
|
||
export const TargetDefaultsPlugin: NxPluginV2 = { | ||
name: 'nx/core/target-defaults', | ||
createNodes: [ | ||
'{package.json,**/package.json,project.json,**/project.json}', | ||
(configFile, _, ctx) => { | ||
const fileName = basename(configFile); | ||
const root = dirname(configFile); | ||
|
||
const packageManagerWorkspacesGlob = combineGlobPatterns( | ||
getGlobPatternsFromPackageManagerWorkspaces(ctx.workspaceRoot) | ||
); | ||
|
||
// Only process once if package.json + project.json both exist | ||
if ( | ||
fileName === 'package.json' && | ||
existsSync(join(ctx.workspaceRoot, root, 'project.json')) | ||
) { | ||
return {}; | ||
} else if ( | ||
fileName === 'package.json' && | ||
!minimatch(configFile, packageManagerWorkspacesGlob) | ||
) { | ||
return {}; | ||
} | ||
|
||
// If no target defaults, this does nothing | ||
const targetDefaults = ctx.nxJsonConfiguration?.targetDefaults; | ||
if (!targetDefaults) { | ||
return {}; | ||
} | ||
|
||
const projectJson = readJsonOrNull<ProjectConfiguration>( | ||
join(ctx.workspaceRoot, root, 'project.json') | ||
); | ||
const packageJson = readJsonOrNull<PackageJson>( | ||
join(ctx.workspaceRoot, root, 'package.json') | ||
); | ||
const projectDefinedTargets = new Set( | ||
Object.keys({ | ||
...packageJson?.scripts, | ||
...projectJson?.targets, | ||
}) | ||
); | ||
|
||
const executorToTargetMap = getExecutorToTargetMap( | ||
packageJson, | ||
projectJson | ||
); | ||
|
||
const newTargets: Record< | ||
string, | ||
TargetConfiguration & { [ONLY_MODIFIES_EXISTING_TARGET]?: boolean } | ||
> = {}; | ||
for (const defaultSpecifier in targetDefaults) { | ||
const targetName = | ||
executorToTargetMap.get(defaultSpecifier) ?? defaultSpecifier; | ||
newTargets[targetName] = structuredClone( | ||
targetDefaults[defaultSpecifier] | ||
); | ||
// TODO: Remove this after we figure out a way to define new targets | ||
// in target defaults | ||
if (!projectDefinedTargets.has(targetName)) { | ||
newTargets[targetName][ONLY_MODIFIES_EXISTING_TARGET] = true; | ||
} | ||
} | ||
|
||
return { | ||
projects: { | ||
[root]: { | ||
targets: newTargets, | ||
}, | ||
}, | ||
}; | ||
}, | ||
], | ||
}; | ||
|
||
function getExecutorToTargetMap( | ||
packageJson: PackageJson, | ||
projectJson: ProjectConfiguration | ||
) { | ||
const executorToTargetMap = new Map<string, string>(); | ||
if (packageJson?.scripts) { | ||
for (const script in packageJson.scripts) { | ||
executorToTargetMap.set('nx:run-script', script); | ||
} | ||
} | ||
if (projectJson?.targets) { | ||
for (const target in projectJson.targets) { | ||
if (projectJson.targets[target].executor) { | ||
executorToTargetMap.set(projectJson.targets[target].executor, target); | ||
} else if (projectJson.targets[target].command) { | ||
executorToTargetMap.set('nx:run-commands', target); | ||
} | ||
} | ||
} | ||
return executorToTargetMap; | ||
} | ||
|
||
function readJsonOrNull<T extends Object = any>(path: string) { | ||
if (existsSync(path)) { | ||
return readJsonFile<T>(path); | ||
} else { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.