Skip to content

Commit

Permalink
chore(core): split package.json scripts from project.json plugin (#20041
Browse files Browse the repository at this point in the history
)
  • Loading branch information
FrozenPandaz authored Nov 3, 2023
1 parent d1dd605 commit 4dc6980
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 253 deletions.
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",
},
},
},
},
},
}
`);
});
});
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;
}
}
195 changes: 1 addition & 194 deletions packages/nx/src/plugins/project-json/build-nodes/project-json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,10 @@ import * as memfs from 'memfs';

import '../../../internal-testing-utils/mock-fs';

import { PackageJson } from '../../../utils/package-json';

import {
CreateProjectJsonProjectsPlugin,
mergeNpmScriptsWithTargets,
} from './project-json';
import { CreateProjectJsonProjectsPlugin } from './project-json';
import { CreateNodesContext } from '../../../utils/nx-plugin';
const { createNodes } = CreateProjectJsonProjectsPlugin;

const defaultReleasePublishTarget = {
'nx-release-publish': {
dependsOn: ['^nx-release-publish'],
executor: '@nx/js:release-publish',
options: {},
},
};

describe('nx project.json plugin', () => {
let context: CreateNodesContext;
beforeEach(() => {
Expand All @@ -44,13 +31,6 @@ describe('nx project.json plugin', () => {
},
},
}),
'packages/lib-a/package.json': JSON.stringify({
name: 'lib-a',
scripts: {
build: 'should not see me',
test: 'jest',
},
}),
},
'/root'
);
Expand Down Expand Up @@ -81,183 +61,10 @@ describe('nx project.json plugin', () => {
"executor": "nx:run-commands",
"options": {},
},
"nx-release-publish": {
"dependsOn": [
"^nx-release-publish",
],
"executor": "@nx/js:release-publish",
"options": {},
},
"test": {
"executor": "nx:run-script",
"options": {
"script": "test",
},
},
},
},
},
}
`);
});

describe('mergeNpmScriptsWithTargets', () => {
const packageJson: PackageJson = {
name: 'my-app',
version: '0.0.0',
scripts: {
build: 'echo 1',
},
};

const packageJsonBuildTarget = {
executor: 'nx:run-script',
options: {
script: 'build',
},
};

it('should prefer project.json targets', () => {
const projectJsonTargets = {
build: {
executor: 'nx:run-commands',
options: {
command: 'echo 2',
},
},
};

const result = mergeNpmScriptsWithTargets(
packageJson,
projectJsonTargets
);
expect(result).toEqual({
...projectJsonTargets,
...defaultReleasePublishTarget,
});
});

it('should provide targets from project.json and package.json', () => {
const projectJsonTargets = {
clean: {
executor: 'nx:run-commands',
options: {
command: 'echo 2',
},
},
};

const result = mergeNpmScriptsWithTargets(
packageJson,
projectJsonTargets
);
expect(result).toEqual({
...projectJsonTargets,
build: packageJsonBuildTarget,
...defaultReleasePublishTarget,
});
});

it('should contain extended options from nx property in package.json', () => {
const result = mergeNpmScriptsWithTargets(
{
name: 'my-other-app',
version: '',
scripts: {
build: 'echo 1',
},
nx: {
targets: {
build: {
outputs: ['custom'],
},
},
},
},
null
);
expect(result).toEqual({
build: { ...packageJsonBuildTarget, outputs: ['custom'] },
...defaultReleasePublishTarget,
});
});

it('should work when project.json targets is null', () => {
const result = mergeNpmScriptsWithTargets(packageJson, null);

expect(result).toEqual({
build: {
executor: 'nx:run-script',
options: {
script: 'build',
},
},
...defaultReleasePublishTarget,
});
});

it("should work when project root is ''", () => {
const result = mergeNpmScriptsWithTargets(
{
name: 'my-app',
version: '',
scripts: {
test: 'echo testing',
},
},
{
build: {
executor: 'nx:run-commands',
options: { command: 'echo hi' },
},
}
);

expect(result).toEqual({
build: {
executor: 'nx:run-commands',
options: { command: 'echo hi' },
},
test: {
executor: 'nx:run-script',
options: { script: 'test' },
},
...defaultReleasePublishTarget,
});
});

it('should ignore scripts that are not in includedScripts', () => {
const result = mergeNpmScriptsWithTargets(
{
name: 'included-scripts-test',
version: '',
scripts: {
test: 'echo testing',
fail: 'exit 1',
},
nx: {
includedScripts: ['test'],
},
},
{
build: {
executor: 'nx:run-commands',
options: { command: 'echo hi' },
},
}
);

expect(result).toEqual({
build: {
executor: 'nx:run-commands',
options: { command: 'echo hi' },
},
test: {
executor: 'nx:run-script',
options: { script: 'test' },
},
...defaultReleasePublishTarget,
});
});
});
});
Loading

0 comments on commit 4dc6980

Please sign in to comment.