Skip to content

Commit

Permalink
fix(core): remove @nrwl/workspace when adding nx to monorepo (#9813)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Apr 13, 2022
1 parent 7f89488 commit bfbe8fc
Show file tree
Hide file tree
Showing 18 changed files with 258 additions and 76 deletions.
28 changes: 27 additions & 1 deletion docs/generated/packages/nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,31 @@
"root": "/packages/nx",
"source": "/packages/nx/src",
"generators": [],
"executors": []
"executors": [
{
"name": "run-script",
"implementation": "/packages/nx/src/executors/run-script/run-script.impl.ts",
"schema": {
"title": "Run Script",
"description": "Run any NPM script of a project in the project's root directory.",
"type": "object",
"cli": "nx",
"outputCapture": "pipe",
"properties": {
"script": {
"type": "string",
"description": "An npm script name in the `package.json` file of the project (e.g., `build`)."
}
},
"additionalProperties": true,
"required": ["script"],
"examplesFile": "`workspace.json`:\n\n```json\n\"frontend\": {\n \"root\": \"packages/frontend\",\n \"targets\": {\n \"build\": {\n \"executor\": \"@nrwl/workspace:run-script\",\n \"options\": {\n \"script\": \"build-my-project\"\n }\n }\n }\n}\n```\n\n```bash\nnx run frontend:build\n```\n\nThe `build` target is going to run `npm run build-my-project` (or `yarn build-my-project`) in the `packages/frontend` directory.\n\n#### Caching Artifacts\n\nBy default, Nx is going to cache `dist/packages/frontend`, `packages/frontend/dist`, `packages/frontend/build`, `packages/frontend/public`. If your npm script writes files to other places, you can override the list of cached outputs as follows:\n\n```json\n\"frontend\": {\n \"root\": \"packages/frontend\",\n \"targets\": {\n \"build\": {\n \"executor\": \"@nrwl/workspace:run-script\",\n \"outputs\": [\"packages/frontend/dist\", \"packaged/frontend/docs\"],\n \"options\": {\n \"script\": \"build-my-project\"\n }\n }\n }\n}\n```\n",
"presets": []
},
"description": "Run an NPM script using Nx.",
"aliases": [],
"hidden": false,
"path": "/packages/nx/src/executors/run-script/schema.json"
}
]
}
2 changes: 1 addition & 1 deletion docs/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
{
"name": "nx",
"path": "generated/packages/nx.json",
"schemas": { "executors": [], "generators": [] }
"schemas": { "executors": ["run-script"], "generators": [] }
},
{
"name": "nx-plugin",
Expand Down
3 changes: 1 addition & 2 deletions packages/add-nx-to-monorepo/src/add-nx-to-monorepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function detectWorkspaceScope(repoRoot: string) {
function createNxJsonFile(repoRoot: string) {
const scope = detectWorkspaceScope(repoRoot);
const res = {
extends: '@nrwl/workspace/presets/npm.json',
extends: 'nx/presets/npm.json',
npmScope: scope,
tasksRunnerOptions: {
default: {
Expand Down Expand Up @@ -267,7 +267,6 @@ function deduceDefaultBase() {
function addDepsToPackageJson(repoRoot: string, useCloud: boolean) {
const json = readJsonFile(repoRoot, `package.json`);
if (!json.devDependencies) json.devDependencies = {};
json.devDependencies['@nrwl/workspace'] = 'NX_VERSION';
json.devDependencies['nx'] = 'NX_VERSION';
if (useCloud) {
json.devDependencies['@nrwl/nx-cloud'] = 'latest';
Expand Down
40 changes: 40 additions & 0 deletions packages/nx/docs/run-script-examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
`workspace.json`:

```json
"frontend": {
"root": "packages/frontend",
"targets": {
"build": {
"executor": "@nrwl/workspace:run-script",
"options": {
"script": "build-my-project"
}
}
}
}
```

```bash
nx run frontend:build
```

The `build` target is going to run `npm run build-my-project` (or `yarn build-my-project`) in the `packages/frontend` directory.

#### Caching Artifacts

By default, Nx is going to cache `dist/packages/frontend`, `packages/frontend/dist`, `packages/frontend/build`, `packages/frontend/public`. If your npm script writes files to other places, you can override the list of cached outputs as follows:

```json
"frontend": {
"root": "packages/frontend",
"targets": {
"build": {
"executor": "@nrwl/workspace:run-script",
"outputs": ["packages/frontend/dist", "packaged/frontend/docs"],
"options": {
"script": "build-my-project"
}
}
}
}
```
9 changes: 9 additions & 0 deletions packages/nx/executors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"executors": {
"run-script": {
"implementation": "./src/executors/run-script/run-script.impl",
"schema": "./src/executors/run-script/schema.json",
"description": "Run an NPM script using Nx."
}
}
}
3 changes: 3 additions & 0 deletions packages/nx/migrations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"migrations": {}
}
54 changes: 29 additions & 25 deletions packages/nx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,33 @@
"enquirer": "~2.3.6",
"tslib": "^2.3.0"
},
"ng-update": {
"requirements": {},
"packageGroup": {
"@nrwl/workspace": "*",
"@nrwl/angular": "*",
"@nrwl/cypress": "*",
"@nrwl/devkit": "*",
"@nrwl/eslint-plugin-nx": "*",
"@nrwl/express": "*",
"@nrwl/jest": "*",
"@nrwl/linter": "*",
"@nrwl/nest": "*",
"@nrwl/next": "*",
"@nrwl/node": "*",
"@nrwl/nx-plugin": "*",
"@nrwl/react": "*",
"@nrwl/storybook": "*",
"@nrwl/web": "*",
"@nrwl/js": "*",
"@nrwl/cli": "*",
"@nrwl/nx-cloud": "latest",
"@nrwl/react-native": "*",
"@nrwl/detox": "*"
}
}
"nx-migrations": {
"migrations": "./migrations.json",
"packageGroup": [
"@nrwl/workspace",
"@nrwl/angular",
"@nrwl/cypress",
"@nrwl/devkit",
"@nrwl/eslint-plugin-nx",
"@nrwl/express",
"@nrwl/jest",
"@nrwl/linter",
"@nrwl/nest",
"@nrwl/next",
"@nrwl/node",
"@nrwl/nx-plugin",
"@nrwl/react",
"@nrwl/storybook",
"@nrwl/web",
"@nrwl/js",
"@nrwl/cli",
{
"package": "@nrwl/nx-cloud",
"version": "latest"
},
"@nrwl/react-native",
"@nrwl/detox"
]
},
"executors": "./executors.json"
}
21 changes: 21 additions & 0 deletions packages/nx/presets/core.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"implicitDependencies": {
"package.json": {
"dependencies": "*",
"devDependencies": "*"
},
".eslintrc.json": "*"
},
"targetDependencies": {
"build": [
{
"target": "build",
"projects": "dependencies"
}
]
},
"workspaceLayout": {
"appsDir": "packages",
"libsDir": "packages"
}
}
20 changes: 20 additions & 0 deletions packages/nx/presets/npm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"implicitDependencies": {
"package.json": {
"dependencies": "*",
"devDependencies": "*"
},
".eslintrc.json": "*"
},
"targetDependencies": {
"build": [
{
"target": "build",
"projects": "dependencies"
}
]
},
"workspaceLayout": {
"libsDir": "packages"
}
}
6 changes: 6 additions & 0 deletions packages/nx/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
},
"outputs": ["{options.outputPath}"]
},
"echo": {
"executor": "nx:command",
"options": {
"command": "echo hi"
}
},
"build": {
"executor": "@nrwl/workspace:run-commands",
"outputs": ["build/packages/nx"],
Expand Down
8 changes: 7 additions & 1 deletion packages/nx/src/command-line/migrate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,9 +771,15 @@ describe('Migration', () => {
expect(
parseMigrationsOptions({ packageAndVersion: 'next' })
).toMatchObject({
targetPackage: '@nrwl/workspace',
targetPackage: 'nx',
targetVersion: 'next',
});
expect(
parseMigrationsOptions({ packageAndVersion: '13.10.0' })
).toMatchObject({
targetPackage: '@nrwl/workspace',
targetVersion: '13.10.0',
});
expect(
parseMigrationsOptions({ packageAndVersion: '@nrwl/[email protected]' })
).toMatchObject({
Expand Down
39 changes: 33 additions & 6 deletions packages/nx/src/command-line/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,28 @@ export class Migrator {
packageName === '@nrwl/workspace' &&
lt(targetVersion, '14.0.0-beta.0')
) {
migration.packageGroup =
require('../../package.json')['ng-update'].packageGroup;
migration.packageGroup = {
'@nrwl/workspace': targetVersion,
'@nrwl/angular': targetVersion,
'@nrwl/cypress': targetVersion,
'@nrwl/devkit': targetVersion,
'@nrwl/eslint-plugin-nx': targetVersion,
'@nrwl/express': targetVersion,
'@nrwl/jest': targetVersion,
'@nrwl/linter': targetVersion,
'@nrwl/nest': targetVersion,
'@nrwl/next': targetVersion,
'@nrwl/node': targetVersion,
'@nrwl/nx-plugin': targetVersion,
'@nrwl/react': targetVersion,
'@nrwl/storybook': targetVersion,
'@nrwl/web': targetVersion,
'@nrwl/js': targetVersion,
'@nrwl/cli': targetVersion,
'@nrwl/nx-cloud': 'latest',
'@nrwl/react-native': targetVersion,
'@nrwl/detox': targetVersion,
};
}

if (migration.packageGroup) {
Expand Down Expand Up @@ -348,7 +368,7 @@ function versionOverrides(overrides: string, param: string) {
function parseTargetPackageAndVersion(args: string) {
if (!args) {
throw new Error(
`Provide the correct package name and version. E.g., @nrwl/workspace@9.0.0.`
`Provide the correct package name and version. E.g., my-package@9.0.0.`
);
}

Expand All @@ -363,7 +383,7 @@ function parseTargetPackageAndVersion(args: string) {
const maybeVersion = args.substring(i + 1);
if (!targetPackage || !maybeVersion) {
throw new Error(
`Provide the correct package name and version. E.g., @nrwl/workspace@9.0.0.`
`Provide the correct package name and version. E.g., my-package@9.0.0.`
);
}
const targetVersion = normalizeVersionWithTagCheck(maybeVersion);
Expand All @@ -375,9 +395,16 @@ function parseTargetPackageAndVersion(args: string) {
args === 'latest' ||
args === 'next'
) {
const targetVersion = normalizeVersionWithTagCheck(args);
const targetPackage =
args.match(/^\d+(?:\.\d+)?(?:\.\d+)?$/) &&
lt(targetVersion, '14.0.0-beta.0')
? '@nrwl/workspace'
: 'nx';

return {
targetPackage: '@nrwl/workspace',
targetVersion: normalizeVersionWithTagCheck(args),
targetPackage,
targetVersion,
};
} else {
return {
Expand Down
35 changes: 35 additions & 0 deletions packages/nx/src/executors/run-script/run-script.impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { execSync } from 'child_process';
import { getPackageManagerCommand } from '../../utils/package-manager';
import type { ExecutorContext } from '../../config/misc-interfaces';
import * as path from 'path';

export interface RunScriptOptions {
script: string;
}

export default async function (
options: RunScriptOptions,
context: ExecutorContext
) {
const pm = getPackageManagerCommand();
const script = options.script;
delete options.script;

const args = [];
Object.keys(options).forEach((r) => {
args.push(`--${r}=${options[r]}`);
});

try {
execSync(pm.run(script, args.join(' ')), {
stdio: ['inherit', 'inherit', 'inherit'],
cwd: path.join(
context.root,
context.workspace.projects[context.projectName].root
),
});
return { success: true };
} catch (e) {
return { success: false };
}
}
16 changes: 16 additions & 0 deletions packages/nx/src/executors/run-script/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"title": "Run Script",
"description": "Run any NPM script of a project in the project's root directory.",
"type": "object",
"cli": "nx",
"outputCapture": "pipe",
"properties": {
"script": {
"type": "string",
"description": "An npm script name in the `package.json` file of the project (e.g., `build`)."
}
},
"additionalProperties": true,
"required": ["script"],
"examplesFile": "../../../docs/run-script-examples.md"
}
6 changes: 3 additions & 3 deletions packages/nx/src/utils/package-json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
} from './package-json';

describe('buildTargetFromScript', () => {
it('should use @nrwl/workspace:run-script', () => {
it('should use nx:run-script', () => {
const target = buildTargetFromScript('build', null);
expect(target.executor).toEqual('@nrwl/workspace:run-script');
expect(target.executor).toEqual('nx:run-script');
});

it('should use options provided in nx target package json configuration', () => {
Expand Down Expand Up @@ -35,6 +35,6 @@ describe('buildTargetFromScript', () => {
});

expect(target.options.script).toEqual('build');
expect(target.executor).toEqual('@nrwl/workspace:run-script');
expect(target.executor).toEqual('nx:run-script');
});
});
2 changes: 1 addition & 1 deletion packages/nx/src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function buildTargetFromScript(

return {
...nxTargetConfiguration,
executor: '@nrwl/workspace:run-script',
executor: 'nx:run-script',
options: {
...(nxTargetConfiguration.options || {}),
script,
Expand Down
Loading

1 comment on commit bfbe8fc

@vercel
Copy link

@vercel vercel bot commented on bfbe8fc Apr 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.