Skip to content

Commit

Permalink
feat(core): reorganize packges to consolidate all cli-related code in nx
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Mar 14, 2022
1 parent 5c0da2f commit 6f038e7
Show file tree
Hide file tree
Showing 239 changed files with 1,435 additions and 5,009 deletions.
2 changes: 1 addition & 1 deletion docs/shared/migration/adding-to-monorepo.md
Expand Up @@ -148,7 +148,7 @@ Nx + Lerna:
"devDependencies": {
"lerna": "*",
"@nrwl/workspace": "*",
"@nrwl/tao": "*"
"nx": "*"
}
}
```
Expand Down
1 change: 0 additions & 1 deletion docs/shared/migration/migration-angularjs.md
Expand Up @@ -107,7 +107,6 @@ Your `package.json` should now look like this:
"@nrwl/cli": "13.4.6",
"@nrwl/eslint-plugin-nx": "13.4.6",
"@nrwl/linter": "13.4.6",
"@nrwl/tao": "13.4.6",
"@nrwl/workspace": "13.4.6",
"@types/jasmine": "~3.5.0",
"@types/jasminewd2": "~2.0.3",
Expand Down
3 changes: 1 addition & 2 deletions docs/shared/nx-core.md
Expand Up @@ -32,8 +32,7 @@ package.json
"scripts": {},
"private": true,
"devDependencies": {
"@nrwl/cli": "12.8.0",
"@nrwl/tao": "12.8.0",
"nx": "12.8.0",
"@nrwl/workspace": "12.8.0",
"@types/node": "14.14.33",
"typescript": "~4.3.5"
Expand Down
3 changes: 1 addition & 2 deletions e2e/add-nx-to-monorepo/src/add-nx-to-monorepo.test.ts
@@ -1,11 +1,10 @@
import {
createNonNxProjectDirectory,
readProjectConfig,
runCommand,
tmpProjPath,
updateFile,
} from '@nrwl/e2e/utils';
import { Workspaces } from '@nrwl/tao/src/shared/workspace';
import { Workspaces } from 'nx/src/shared/workspace';

describe('add-nx-to-monorepo', () => {
it('should not throw', () => {
Expand Down
3 changes: 2 additions & 1 deletion e2e/angular-core/src/ng-add.test.ts
@@ -1,3 +1,5 @@
import { PackageManager } from 'nx/src/shared/package-manager';

process.env.SELECTED_CLI = 'angular';

import {
Expand All @@ -13,7 +15,6 @@ import {
uniq,
updateFile,
} from '@nrwl/e2e/utils';
import { PackageManager } from '@nrwl/tao/src/shared/package-manager';

describe('convert Angular CLI workspace to an Nx workspace', () => {
let project: string;
Expand Down
2 changes: 1 addition & 1 deletion e2e/cli/project.json
Expand Up @@ -30,5 +30,5 @@
"outputs": ["coverage/e2e/cli"]
}
},
"implicitDependencies": ["cli", "js"]
"implicitDependencies": ["js"]
}
23 changes: 10 additions & 13 deletions e2e/cli/src/cli.test.ts
Expand Up @@ -15,7 +15,7 @@ import {
describe('Cli', () => {
beforeEach(() => newProject());

it('vvvshould execute long running tasks', () => {
it('vvvshould execute long running tasks', async () => {
const myapp = uniq('myapp');
runCLI(`generate @nrwl/web:app ${myapp}`);
updateProjectConfig(myapp, (c) => {
Expand Down Expand Up @@ -210,15 +210,13 @@ describe('migrate', () => {
})
);

updateFile(
'./node_modules/@nrwl/tao/src/commands/migrate.js',
(content) => {
const start = content.indexOf('// testing-fetch-start');
const end = content.indexOf('// testing-fetch-end');

const before = content.substring(0, start);
const after = content.substring(end);
const newFetch = `
updateFile('./node_modules/nx/src/commands/migrate.js', (content) => {
const start = content.indexOf('// testing-fetch-start');
const end = content.indexOf('// testing-fetch-end');

const before = content.substring(0, start);
const after = content.substring(end);
const newFetch = `
function createFetcher(logger) {
return function fetch(packageName) {
if (packageName === 'migrate-parent-package') {
Expand Down Expand Up @@ -250,9 +248,8 @@ describe('migrate', () => {
}
`;

return `${before}${newFetch}${after}`;
}
);
return `${before}${newFetch}${after}`;
});

runCLI(
'migrate migrate-parent-package@2.0.0 --from="migrate-parent-package@1.0.0"',
Expand Down
4 changes: 2 additions & 2 deletions e2e/utils/index.ts
Expand Up @@ -4,8 +4,6 @@ import {
ProjectConfiguration,
WorkspaceJsonConfiguration,
} from '@nrwl/devkit';
import { detectPackageManager } from '@nrwl/tao/src/shared/package-manager';
import { Workspaces } from '@nrwl/tao/src/shared/workspace';
import { angularCliVersion } from '@nrwl/workspace/src/utils/versions';
import { ChildProcess, exec, execSync } from 'child_process';
import {
Expand All @@ -29,6 +27,8 @@ import { promisify } from 'util';
import chalk = require('chalk');
import isCI = require('is-ci');
import treeKill = require('tree-kill');
import { Workspaces } from '../../packages/nx/src/shared/workspace';
import { detectPackageManager } from '../../packages/create-nx-workspace/bin/package-manager';

const kill = require('kill-port');
export const isWindows = require('is-windows');
Expand Down
Expand Up @@ -5,7 +5,7 @@ import {
} from '@angular-devkit/build-angular/src/builders/dev-server';
import { JsonObject } from '@angular-devkit/core';
import { joinPathFragments, parseTargetString } from '@nrwl/devkit';
import { Workspaces } from '@nrwl/tao/src/shared/workspace';
import { Workspaces } from 'nx/src/shared/workspace';
import { existsSync } from 'fs';
import { merge } from 'webpack-merge';
import { resolveCustomWebpackConfig } from '../utilities/webpack';
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/executors/utilities/tailwindcss.ts
@@ -1,5 +1,5 @@
import { logger } from '@nrwl/devkit';
import { appRootPath } from '@nrwl/tao/src/utils/app-root';
import { appRootPath } from 'nx/src/utils/app-root';
import { existsSync } from 'fs';
import { join, relative } from 'path';
import * as postcssImport from 'postcss-import';
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/generators/library/library.spec.ts
Expand Up @@ -9,7 +9,7 @@ import {
} from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { Linter } from '@nrwl/linter';
import { toNewFormat } from '@nrwl/tao/src/shared/workspace';
import { toNewFormat } from 'nx/src/shared/workspace';
import { createApp } from '../../utils/nx-devkit/testing';
import { UnitTestRunner } from '../../utils/test-runners';
import {
Expand Down
@@ -1,5 +1,5 @@
import { readProjectConfiguration, Tree, updateJson } from '@nrwl/devkit';
import { appRootPath } from '@nrwl/tao/src/utils/app-root';
import { appRootPath } from 'nx/src/utils/app-root';
import { getNewProjectName } from '@nrwl/workspace/src/generators/move/lib/utils';
import { join, relative } from 'path';
import { Schema } from '../schema';
Expand Down
Expand Up @@ -6,7 +6,7 @@ import { storybookVersion } from '@nrwl/storybook';
import {
overrideCollectionResolutionForTesting,
wrapAngularDevkitSchematic,
} from '@nrwl/tao/src/commands/ngcli-adapter';
} from 'nx/src/commands/ngcli-adapter';
import { findNodes } from '@nrwl/workspace/src/utils/ast-utils';
import * as ts from 'typescript';
import { SyntaxKind } from 'typescript';
Expand Down
Expand Up @@ -7,7 +7,7 @@ describe('opt-out-testbed-teardown migration', () => {

beforeEach(() => {
tree = createTreeWithEmptyWorkspace(2);
jest.doMock('@nrwl/tao/src/utils/app-root', () => ({ appRootPath: '' }));
jest.doMock('nx/src/utils/app-root', () => ({ appRootPath: '' }));
});

it('should warn when the jestConfig property is not configured', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/utils/mfe-webpack.ts
@@ -1,7 +1,7 @@
import { readTsConfig } from '@nrwl/workspace';
import { existsSync, readFileSync } from 'fs';
import { NormalModuleReplacementPlugin } from 'webpack';
import { appRootPath as rootPath } from '@nrwl/tao/src/utils/app-root';
import { appRootPath as rootPath } from 'nx/src/utils/app-root';
import { normalizePath, joinPathFragments } from '@nrwl/devkit';
import { dirname } from 'path';
import { ParsedCommandLine } from 'typescript';
Expand Down
68 changes: 1 addition & 67 deletions packages/cli/bin/nx.ts
@@ -1,68 +1,2 @@
#!/usr/bin/env node
import { findWorkspaceRoot } from '../lib/find-workspace-root';
const workspace = findWorkspaceRoot(process.cwd());
if (workspace && workspace.type === 'nx') {
require('v8-compile-cache');
}
// polyfill rxjs observable to avoid issues with multiple version fo Observable installed in node_modules
// https://twitter.com/BenLesh/status/1192478226385428483?s=20
if (!(Symbol as any).observable)
(Symbol as any).observable = Symbol('observable polyfill');
import * as chalk from 'chalk';
import { initLocal } from '../lib/init-local';
import { output } from '../lib/output';
import { detectPackageManager } from '@nrwl/tao/src/shared/package-manager';

if (!workspace) {
output.log({
title: `The current directory isn't part of an Nx workspace.`,
bodyLines: [
`To create a workspace run:`,
chalk.bold.white(`npx create-nx-workspace@latest <workspace name>`),
],
});

output.note({
title: `For more information please visit https://nx.dev/`,
});
process.exit(1);
}

// Make sure that a local copy of Nx exists in workspace
let localNx: string;
try {
localNx = require.resolve('@nrwl/cli/bin/nx.js', {
paths: [workspace.dir],
});
} catch {
output.error({
title: `Could not find Nx modules in this workspace.`,
bodyLines: [`Have you run ${chalk.bold.white(`npm/yarn install`)}?`],
});
process.exit(1);
}

if (localNx === require.resolve('@nrwl/cli/bin/nx.js')) {
initLocal(workspace);
} else {
const packageManager = detectPackageManager();
if (packageManager === 'pnpm') {
const tip =
process.platform === 'win32'
? 'doskey pnx=pnpm nx -- $*'
: `alias pnx="pnpm nx --"`;
output.warn({
title: `Running global Nx CLI with PNPM may have issues.`,
bodyLines: [
`Prefer to use "pnpm" (https://pnpm.io/cli/exec) to execute commands in this workspace.`,
`${chalk.reset.inverse.bold.cyan(
' TIP '
)} create a shortcut such as: ${chalk.bold.white(tip)}`,
``,
],
});
}

// Nx is being run from globally installed CLI - hand off to the local
require(localNx);
}
require('nx/bin/nx');
17 changes: 1 addition & 16 deletions packages/cli/lib/decorate-cli.ts
@@ -1,16 +1 @@
import { readFileSync, writeFileSync } from 'fs';

export function decorateCli() {
const path = 'node_modules/@angular/cli/lib/cli/index.js';
const angularCLIInit = readFileSync(path, 'utf-8');
const start = angularCLIInit.indexOf(`(options) {`) + 11;

const newContent = `${angularCLIInit.substr(0, start)}
if (!process.env['NX_CLI_SET']) {
require('@nrwl/cli/bin/nx');
return new Promise(function(res, rej) {});
}
${angularCLIInit.substring(start)}
`;
writeFileSync(path, newContent);
}
require('nx/src/cli/decorate-cli');
6 changes: 1 addition & 5 deletions packages/cli/package.json
Expand Up @@ -28,10 +28,6 @@
},
"homepage": "https://nx.dev",
"dependencies": {
"yargs-parser": "20.0.0",
"@nrwl/tao": "*",
"chalk": "4.1.0",
"enquirer": "~2.3.6",
"v8-compile-cache": "2.3.0"
"nx": "*"
}
}
10 changes: 5 additions & 5 deletions packages/create-nx-plugin/bin/create-nx-plugin.ts
@@ -1,9 +1,9 @@
#!/usr/bin/env node

// we can't import from '@nrwl/workspace' because it will require typescript
import { getPackageManagerCommand } from '@nrwl/tao/src/shared/package-manager';
import type { NxJsonConfiguration } from '@nrwl/tao/src/shared/nx';
import { readJsonFile, writeJsonFile } from '@nrwl/tao/src/utils/fileutils';
import { getPackageManagerCommand } from 'nx/src/shared/package-manager';
import type { NxJsonConfiguration } from 'nx/src/shared/nx';
import { readJsonFile, writeJsonFile } from 'nx/src/utils/fileutils';
import { output } from '@nrwl/workspace/src/utilities/output';
import { execSync } from 'child_process';
import { removeSync } from 'fs-extra';
Expand Down Expand Up @@ -38,7 +38,7 @@ function createSandbox(packageManager: string) {
writeJsonFile(path.join(tmpDir, 'package.json'), {
dependencies: {
'@nrwl/workspace': nxVersion,
'@nrwl/tao': cliVersion,
nx: cliVersion,
typescript: tsVersion,
prettier: prettierVersion,
},
Expand Down Expand Up @@ -77,7 +77,7 @@ function createWorkspace(
execSync(
`${
pmc.exec
} tao ${command}/generators.json --nxWorkspaceRoot="${process.cwd()}"`,
} nx ${command}/generators.json --nxWorkspaceRoot="${process.cwd()}"`,
{
stdio: [0, 1, 2],
cwd: tmpDir,
Expand Down
5 changes: 2 additions & 3 deletions packages/create-nx-workspace/bin/create-nx-workspace.ts
Expand Up @@ -481,7 +481,7 @@ async function createSandbox(packageManager: string) {
JSON.stringify({
dependencies: {
'@nrwl/workspace': nxVersion,
'@nrwl/tao': cliVersion,
nx: cliVersion,
typescript: tsVersion,
prettier: prettierVersion,
},
Expand Down Expand Up @@ -540,8 +540,7 @@ async function createApp(
nxWorkspaceRoot = `\\"${nxWorkspaceRoot.slice(1, -1)}\\"`;
}
}
const fullCommandWithoutWorkspaceRoot = `${pmc.exec} tao ${command}/generators.json --cli=${cli}`;

const fullCommandWithoutWorkspaceRoot = `${pmc.exec} nx ${command}/generators.json --cli=${cli}`;
let workspaceSetupSpinner = ora('Creating your workspace').start();

try {
Expand Down

1 comment on commit 6f038e7

@vercel
Copy link

@vercel vercel bot commented on 6f038e7 Mar 14, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.