nx/packages/cli/lib/init-local.ts
Craigory Coppola 79cf69b4e4
feat(core): consolidate nx.json and workspace.json (#6642)
* feat(core): consolidate settings between workspace.json + nx.json

workspace.json (and linked project.json files) now contain all project specific settings.
nx.json now contains all settings that affect the whole workspace.

* chore(core): fix angular unit tests w/ new config

* chore(core): fix failing tests

* chore(core): fix formatting

* chore(core): fix more tests

* chore(core): normalize-nx-json feedback

* chore(core): Fix more unit tests

* chore(core): fix remaining tests in workspace

* chore(linter): fix remaining linter tests

* chore(core): fix remaining spec + build issues

* chore(core): formatting fixes

* feat(core): migration script to move config options to new locations

* chore(core): fix e2e tests

* chore(core): run format

* chore(react-native): fix failing tests

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* feat(core): move properties to new location during format step

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* feat(core): initial pass on ngcli-adapter for property consolidation

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* chore(misc): fix tests

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* docs(core): update docs with changes

* chore(misc): fix tests

* chore(core): code review changes

updateWorkspaceJson -> updateWorkspace, no longer uses updater function

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* chore(core): fix bug in ngcli impl

* fix(core): fix bug in ngcli-adapter

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* fix(core): fix ngcli-adapter

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* chore(core): fix workspace e2e

* chore(core): fix nx-plugin e2e

* fix(core): move defaultProject to nx.json

* chore(core): fix broken workspace test

* chore(core): formatting

* chore(core): fix workspace format

* chore(core): add nxJson to ExecutorContext

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* chore(core): remove all references ot `NxProjectConfiguration` from our code

* chore(core): Review Changes

* fix(core): update new config locations v13 migration
2021-10-14 10:42:47 -04:00

109 lines
3.4 KiB
TypeScript

import * as path from 'path';
import { Workspace } from './workspace';
import { parseRunOneOptions } from './parse-run-one-options';
import { performance } from 'perf_hooks';
import { readJsonFile } from '@nrwl/tao/src/utils/fileutils';
/**
* Nx is being run inside a workspace.
*
* @param workspace Relevant local workspace properties
*/
process.env.NX_CLI_SET = 'true';
export function initLocal(workspace: Workspace) {
performance.mark('init-local');
//nx-ignore-next-line
require('@nrwl/workspace/src/utilities/perf-logging');
//nx-ignore-next-line
const supportedNxCommands =
require('@nrwl/workspace/src/command-line/supported-nx-commands').supportedNxCommands;
const runOpts = runOneOptions(workspace);
const running = runOpts !== false;
if (supportedNxCommands.includes(process.argv[2])) {
// required to make sure nrwl/workspace import works
//nx-ignore-next-line
require('@nrwl/workspace/src/command-line/nx-commands').commandsObject.argv;
} else if (running) {
//nx-ignore-next-line
require('@nrwl/workspace/src/command-line/run-one').runOne(runOpts);
} else if (generating()) {
loadCli(workspace, '@nrwl/tao/index.js');
} else {
if (workspace.type === 'nx') {
loadCli(workspace, '@nrwl/tao/index.js');
} else {
if (
process.argv[2] === 'update' &&
process.env.FORCE_NG_UPDATE != 'true'
) {
console.log(
`Nx provides a much improved version of "ng update". It runs the same migrations, but allows you to:`
);
console.log(`- rerun the same migration multiple times`);
console.log(`- reorder migrations, skip migrations`);
console.log(`- fix migrations that "almost work"`);
console.log(`- commit a partially migrated state`);
console.log(
`- change versions of packages to match organizational requirements`
);
console.log(
`And, in general, it is lot more reliable for non-trivial workspaces. Read more at: https://nx.dev/latest/angular/workspace/update`
);
console.log(
`Run "nx migrate latest" to update to the latest version of Nx.`
);
console.log(
`Running "ng update" can still be useful in some dev workflows, so we aren't planning to remove it.`
);
console.log(
`If you need to use it, run "FORCE_NG_UPDATE=true ng update".`
);
} else {
loadCli(workspace, '@angular/cli/lib/init.js');
}
}
}
}
function loadCli(workspace: Workspace, cliPath: string) {
try {
const cli = require.resolve(cliPath, { paths: [workspace.dir] });
require(cli);
} catch (e) {
console.error(`Could not find ${cliPath} module in this workspace.`, e);
process.exit(1);
}
}
function runOneOptions(
workspace: Workspace
): false | { project; target; configuration; parsedArgs } {
try {
const workspaceConfigJson = readJsonFile(
path.join(
workspace.dir,
workspace.type === 'nx' ? 'workspace.json' : 'angular.json'
)
);
const nxJson = readJsonFile(path.join(workspace.dir, 'nx.json'));
return parseRunOneOptions(
workspace.dir,
workspaceConfigJson,
nxJson,
process.argv.slice(2)
);
} catch {
return false;
}
}
function generating(): boolean {
const command = process.argv.slice(2)[0];
return command === 'g' || command === 'generate';
}