Skip to content

Commit

Permalink
fix(core): default to NPM package manager
Browse files Browse the repository at this point in the history
* Simplify the determination & detection of the desired package manager.
* Add the `package-manager` option on plugin creation.
* Nx Cloud installation uses the appropriate package manager.
  • Loading branch information
bekos authored and vsavkin committed Nov 11, 2020
1 parent 3753cf3 commit 8676c1a
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 259 deletions.
23 changes: 1 addition & 22 deletions e2e/workspace/src/create-nx-workspace.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import {
cli,
forEachCli,
readJson,
runCreateWorkspace,
setCurrentProjName,
uniq,
workspaceConfigName,
} from '@nrwl/e2e/utils';
import { cli, forEachCli, runCreateWorkspace, uniq } from '@nrwl/e2e/utils';
import { existsSync, mkdirSync } from 'fs-extra';
import { execSync } from 'child_process';

Expand Down Expand Up @@ -119,18 +111,5 @@ forEachCli(() => {

expect(existsSync(`${tmpDir}/${wsName}/package.json`)).toBeTruthy();
});

it('should store `packageManager` preference', () => {
const wsName = uniq('empty');
setCurrentProjName(wsName);

runCreateWorkspace(wsName, {
preset: 'empty',
packageManager: 'yarn',
});

const workspaceJson = readJson(`${workspaceConfigName()}`);
expect(workspaceJson.cli.packageManager).toEqual('yarn');
});
});
});
6 changes: 3 additions & 3 deletions packages/create-nx-plugin/bin/create-nx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import * as path from 'path';
import { execSync } from 'child_process';
import * as inquirer from 'inquirer';
import yargsParser = require('yargs-parser');
import { determinePackageManager, showNxWarning } from './shared';
import { showNxWarning } from './shared';

const tsVersion = 'TYPESCRIPT_VERSION';
const cliVersion = 'NX_VERSION';
const nxVersion = 'NX_VERSION';
const prettierVersion = 'PRETTIER_VERSION';

const parsedArgs = yargsParser(process.argv, {
string: ['pluginName'],
string: ['pluginName', 'packageManager'],
alias: {
pluginName: 'plugin-name',
},
Expand Down Expand Up @@ -199,7 +199,7 @@ if (parsedArgs.help) {
process.exit(0);
}

const packageManager = determinePackageManager();
const packageManager = parsedArgs.packageManager || 'npm';
determineWorkspaceName(parsedArgs).then((workspaceName) => {
return determinePluginName(parsedArgs).then((pluginName) => {
const tmpDir = createSandbox(packageManager);
Expand Down
45 changes: 0 additions & 45 deletions packages/create-nx-plugin/bin/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,3 @@ export function showNxWarning(workspaceName: string) {
});
}
}

export function determinePackageManager() {
let packageManager = getPackageManagerFromAngularCLI();
if (packageManager === 'npm' || isPackageManagerInstalled(packageManager)) {
return packageManager;
}

if (isPackageManagerInstalled('yarn')) {
return 'yarn';
}

if (isPackageManagerInstalled('pnpm')) {
return 'pnpm';
}

return 'npm';
}

function getPackageManagerFromAngularCLI(): string {
// If you have Angular CLI installed, read Angular CLI config.
// If it isn't installed, default to 'yarn'.
try {
return execSync('ng config -g cli.packageManager', {
stdio: ['ignore', 'pipe', 'ignore'],
timeout: 500,
})
.toString()
.trim();
} catch (e) {
return 'yarn';
}
}

function isPackageManagerInstalled(packageManager: string) {
let isInstalled = false;
try {
execSync(`${packageManager} --version`, {
stdio: ['ignore', 'ignore', 'ignore'],
});
isInstalled = true;
} catch (e) {
/* do nothing */
}
return isInstalled;
}
4 changes: 2 additions & 2 deletions packages/create-nx-workspace/bin/create-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as inquirer from 'inquirer';
import * as path from 'path';
import { dirSync } from 'tmp';
import * as yargsParser from 'yargs-parser';
import { determinePackageManager, showNxWarning } from './shared';
import { showNxWarning } from './shared';

enum Preset {
Empty = 'empty',
Expand Down Expand Up @@ -95,7 +95,7 @@ if (parsedArgs.help) {
showHelp();
process.exit(0);
}
const packageManager = determinePackageManager(parsedArgs.packageManager);
const packageManager = parsedArgs.packageManager || 'npm';
determineWorkspaceName(parsedArgs).then((name) => {
determinePreset(parsedArgs).then((preset) => {
return determineAppName(preset, parsedArgs).then((appName) => {
Expand Down
42 changes: 0 additions & 42 deletions packages/create-nx-workspace/bin/shared.spec.ts

This file was deleted.

38 changes: 0 additions & 38 deletions packages/create-nx-workspace/bin/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,3 @@ export function showNxWarning(workspaceName: string) {
});
}
}

export function determinePackageManager(preferredPackageManager?: string) {
return (
[
preferredPackageManager,
getPackageManagerFromAngularCLI(),
'yarn',
'pnpm',
].find((pm) => pm && isPackageManagerInstalled(pm)) || 'npm'
);
}

function getPackageManagerFromAngularCLI(): string {
// If you have Angular CLI installed, read Angular CLI config.
try {
return execSync('ng config -g cli.packageManager', {
stdio: ['ignore', 'pipe', 'ignore'],
timeout: 500,
})
.toString()
.trim();
} catch (e) {
return;
}
}

function isPackageManagerInstalled(packageManager: string) {
let isInstalled = false;
try {
execSync(`${packageManager} --version`, {
stdio: ['ignore', 'ignore', 'ignore'],
});
isInstalled = true;
} catch (e) {
/* do nothing */
}
return isInstalled;
}
56 changes: 6 additions & 50 deletions packages/tao/src/shared/detect-package-manager.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,11 @@
import { execSync } from 'child_process';
import { readFileSync, existsSync } from 'fs';
import * as stripJsonComments from 'strip-json-comments';

function isPackageManagerInstalled(packageManager: string) {
try {
execSync(`${packageManager} --version`, {
stdio: ['ignore', 'ignore', 'ignore'],
});
return true;
} catch (e) {
return false;
}
}
import { existsSync } from 'fs';

export function detectPackageManager() {
const workspaceJsonPath = [`workspace.json`, `angular.json`].find((p) =>
existsSync(p)
);

if (workspaceJsonPath) {
const workspaceJson = JSON.parse(
stripJsonComments(readFileSync(workspaceJsonPath).toString())
);
if (workspaceJson.cli && workspaceJson.cli.packageManager) {
return workspaceJson.cli.packageManager;
}
}

if (existsSync('yarn.lock')) {
return 'yarn';
}

if (existsSync('pnpm-lock.yaml')) {
return 'pnpm';
}

if (existsSync('package-lock.json')) {
return 'npm';
}

// If we get here, there are no lock files,
// so lets check for package managers in our preferred order
if (isPackageManagerInstalled('yarn')) {
return 'yarn';
}

if (isPackageManagerInstalled('pnpm')) {
return 'pnpm';
}

return 'npm';
return existsSync('yarn.lock')
? 'yarn'
: existsSync('pnpm-lock.yaml')
? 'pnpm'
: 'npm';
}

export function getPackageManagerInstallCommand(
Expand Down
23 changes: 14 additions & 9 deletions packages/workspace/src/command-line/prompt-for-nx-cloud.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as inquirer from 'inquirer';
import { readNxJson } from '../core/file-utils';
import { output } from '../utils/output';
import { detectPackageManager } from '../utils/detect-package-manager';
import {
detectPackageManager,
getPackageManagerExecuteCommand,
getPackageManagerInstallCommand,
} from '../utils/detect-package-manager';
import { execSync } from 'child_process';

export async function promptForNxCloud(scan: boolean) {
Expand All @@ -16,14 +20,15 @@ export async function promptForNxCloud(scan: boolean) {
const res = await askAboutNxCloud();
if (res) {
const pm = detectPackageManager();
if (pm === 'yarn') {
execSync('yarn add -D @nrwl/nx-cloud@latest');
} else {
execSync('npm install --save-dev @nrwl/nx-cloud@latest');
}
execSync(`npx nx g @nrwl/nx-cloud:init`, {
stdio: [0, 1, 2],
});
execSync(
`${getPackageManagerInstallCommand(pm, true)} @nrwl/nx-cloud@latest`
);
execSync(
`${getPackageManagerExecuteCommand(pm)} nx g @nrwl/nx-cloud:init`,
{
stdio: [0, 1, 2],
}
);
} else {
output.log({ title: 'Executing the command without --scan' });
}
Expand Down
15 changes: 0 additions & 15 deletions packages/workspace/src/schematics/shared-new/shared-new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ export function sharedNew(cli: string, options: Schema): Rule {

return chain([
schematic('workspace', { ...workspaceOpts, cli }),
setDefaultPackageManager(options),
setDefaultLinter(options),
addPresetDependencies(options),
addCloudDependencies(options),
Expand Down Expand Up @@ -328,17 +327,3 @@ function setTSLintDefault() {
return json;
});
}

function setDefaultPackageManager({ packageManager }: Schema) {
if (!packageManager) {
return noop();
}

return updateWorkspaceInTree((json) => {
if (!json.cli) {
json.cli = {};
}
json.cli['packageManager'] = packageManager;
return json;
});
}
32 changes: 16 additions & 16 deletions packages/workspace/src/utils/detect-package-manager.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import { execSync } from 'child_process';
import { fileExists } from './fileutils';

export function detectPackageManager(): string {
try {
const output = execSync(`nx config cli.packageManager`, {
stdio: ['ignore', 'pipe', 'ignore'],
})
.toString()
.trim()
.split('\n');
return output[output.length - 1].trim();
} catch (e) {
return fileExists('yarn.lock')
? 'yarn'
: fileExists('pnpm-lock.yaml')
? 'pnpm'
: 'npm';
}
return fileExists('yarn.lock')
? 'yarn'
: fileExists('pnpm-lock.yaml')
? 'pnpm'
: 'npm';
}

export function getPackageManagerExecuteCommand(
Expand All @@ -32,3 +21,14 @@ export function getPackageManagerExecuteCommand(

return `npx`;
}

export function getPackageManagerInstallCommand(
packageManager = detectPackageManager(),
isDevDependency = false
) {
if (packageManager === 'yarn') {
return `yarn add${isDevDependency ? ' --dev' : ''}`;
}

return `${packageManager} install${isDevDependency ? ' --save-dev' : ''}`;
}
Loading

0 comments on commit 8676c1a

Please sign in to comment.