-
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.
chore(core): split package.json scripts from project.json plugin (#20041
- Loading branch information
1 parent
d1dd605
commit 4dc6980
Showing
5 changed files
with
139 additions
and
253 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.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,74 @@ | ||
import * as memfs from 'memfs'; | ||
|
||
import '../../../internal-testing-utils/mock-fs'; | ||
|
||
import { CreatePackageJsonProjectsNextToProjectJson } from './package-json-next-to-project-json'; | ||
import { | ||
CreateNodesContext, | ||
CreateNodesFunction, | ||
} from '../../../utils/nx-plugin'; | ||
const { createNodes } = CreatePackageJsonProjectsNextToProjectJson; | ||
|
||
describe('nx project.json plugin', () => { | ||
let context: CreateNodesContext; | ||
let createNodesFunction: CreateNodesFunction; | ||
|
||
beforeEach(() => { | ||
context = { | ||
nxJsonConfiguration: {}, | ||
workspaceRoot: '/root', | ||
}; | ||
createNodesFunction = createNodes[1]; | ||
}); | ||
|
||
it('should build projects from project.json', () => { | ||
memfs.vol.fromJSON( | ||
{ | ||
'packages/lib-a/project.json': JSON.stringify({ | ||
name: 'lib-a', | ||
targets: { | ||
build: { | ||
executor: 'nx:run-commands', | ||
options: {}, | ||
}, | ||
}, | ||
}), | ||
'packages/lib-a/package.json': JSON.stringify({ | ||
name: 'lib-a', | ||
scripts: { | ||
test: 'jest', | ||
}, | ||
}), | ||
}, | ||
'/root' | ||
); | ||
|
||
expect( | ||
createNodesFunction('packages/lib-a/project.json', undefined, context) | ||
).toMatchInlineSnapshot(` | ||
{ | ||
"projects": { | ||
"lib-a": { | ||
"name": "lib-a", | ||
"root": "packages/lib-a", | ||
"targets": { | ||
"nx-release-publish": { | ||
"dependsOn": [ | ||
"^nx-release-publish", | ||
], | ||
"executor": "@nx/js:release-publish", | ||
"options": {}, | ||
}, | ||
"test": { | ||
"executor": "nx:run-script", | ||
"options": { | ||
"script": "test", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
`); | ||
}); | ||
}); |
58 changes: 58 additions & 0 deletions
58
packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.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,58 @@ | ||
import { dirname, join } from 'path'; | ||
import { existsSync } from 'fs'; | ||
import { NxPluginV2 } from '../../../utils/nx-plugin'; | ||
import { readJsonFile } from '../../../utils/fileutils'; | ||
import { ProjectConfiguration } from '../../../config/workspace-json-project-json'; | ||
import { | ||
PackageJson, | ||
readTargetsFromPackageJson, | ||
} from '../../../utils/package-json'; | ||
|
||
// TODO: Remove this one day, this should not need to be done. | ||
|
||
export const CreatePackageJsonProjectsNextToProjectJson: NxPluginV2 = { | ||
name: 'nx-core-build-package-json-nodes-next-to-project-json-nodes', | ||
createNodes: [ | ||
'{project.json,**/project.json}', | ||
(file, _, { workspaceRoot }) => { | ||
const project = createProjectFromPackageJsonNextToProjectJson( | ||
file, | ||
workspaceRoot | ||
); | ||
|
||
return project | ||
? { | ||
projects: { | ||
[project.name]: project, | ||
}, | ||
} | ||
: {}; | ||
}, | ||
], | ||
}; | ||
|
||
function createProjectFromPackageJsonNextToProjectJson( | ||
projectJsonPath: string, | ||
workspaceRoot: string | ||
): ProjectConfiguration | null { | ||
const root = dirname(projectJsonPath); | ||
const packageJsonPath = join(workspaceRoot, root, 'package.json'); | ||
if (!existsSync(packageJsonPath)) { | ||
return null; | ||
} | ||
try { | ||
const packageJson: PackageJson = readJsonFile(packageJsonPath); | ||
|
||
let { nx, name } = packageJson; | ||
|
||
return { | ||
...nx, | ||
name, | ||
root, | ||
targets: readTargetsFromPackageJson(packageJson), | ||
} as ProjectConfiguration; | ||
} catch (e) { | ||
console.log(e); | ||
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.