Skip to content

Commit

Permalink
feat(core): support packageManager argument
Browse files Browse the repository at this point in the history
  • Loading branch information
bekos committed Oct 30, 2020
1 parent 1849e6b commit 34d156d
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 20 deletions.
6 changes: 6 additions & 0 deletions e2e/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ export function runCreateWorkspace(
appName,
style,
base,
packageManager,
}: {
preset: string;
appName?: string;
style?: string;
base?: string;
packageManager?: string;
}
) {
const linterArg =
Expand All @@ -105,6 +107,10 @@ export function runCreateWorkspace(
command += ` --defaultBase="${base}"`;
}

if (packageManager) {
command += ` --package-manager=${packageManager}`;
}

const create = execSync(command, {
cwd: `./tmp/${cli}`,
stdio: [0, 1, 2],
Expand Down
23 changes: 22 additions & 1 deletion e2e/workspace/src/create-nx-workspace.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { cli, forEachCli, runCreateWorkspace, uniq } from '@nrwl/e2e/utils';
import {
cli,
forEachCli,
readJson,
runCreateWorkspace,
setCurrentProjName,
uniq,
workspaceConfigName,
} from '@nrwl/e2e/utils';
import { existsSync, mkdirSync } from 'fs-extra';
import { execSync } from 'child_process';

Expand Down Expand Up @@ -111,5 +119,18 @@ 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');
});
});
});
12 changes: 10 additions & 2 deletions packages/create-nx-workspace/bin/create-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ const angularCliVersion = 'ANGULAR_CLI_VERSION';
const prettierVersion = 'PRETTIER_VERSION';

const parsedArgs = yargsParser(process.argv, {
string: ['cli', 'preset', 'appName', 'style', 'linter', 'defaultBase'],
string: [
'cli',
'preset',
'appName',
'style',
'linter',
'defaultBase',
'packageManager',
],
alias: {
appName: 'app-name',
nxCloud: 'nx-cloud',
Expand All @@ -87,7 +95,7 @@ if (parsedArgs.help) {
showHelp();
process.exit(0);
}
const packageManager = determinePackageManager();
const packageManager = determinePackageManager(parsedArgs.packageManager);
determineWorkspaceName(parsedArgs).then((name) => {
determinePreset(parsedArgs).then((preset) => {
return determineAppName(preset, parsedArgs).then((appName) => {
Expand Down
42 changes: 42 additions & 0 deletions packages/create-nx-workspace/bin/shared.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { determinePackageManager } from './shared';

let INSTALLED_PACKAGE_MANAGERS = ['npm', 'yarn', 'pnpm'];

jest.mock('child_process', () => {
return {
execSync: (command) => {
if (command.endsWith(`--version`)) {
const pm = command.split(' ')[0];
if (INSTALLED_PACKAGE_MANAGERS.includes(pm)) {
return;
}
throw Error();
}
},
};
});

describe('determinePackageManager', () => {
it('will prefer Yarn if installed and there is no preference', () => {
const pm = determinePackageManager();
expect(pm).toEqual('yarn');
});

it('will use preferred one if installed', () => {
expect(determinePackageManager('pnpm')).toEqual('pnpm');
expect(determinePackageManager('yarn')).toEqual('yarn');
expect(determinePackageManager('npm')).toEqual('npm');
});

it('will not use preferred one if not installed', () => {
INSTALLED_PACKAGE_MANAGERS = ['npm', 'pnpm'];
const pm = determinePackageManager('yarn');
expect(pm).toEqual('pnpm');
});

it('will fallback to NPM', () => {
INSTALLED_PACKAGE_MANAGERS = [];
const pm = determinePackageManager();
expect(pm).toEqual('npm');
});
});
27 changes: 10 additions & 17 deletions packages/create-nx-workspace/bin/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,19 @@ 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';
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.
// If it isn't installed, default to 'yarn'.
try {
return execSync('ng config -g cli.packageManager', {
stdio: ['ignore', 'pipe', 'ignore'],
Expand All @@ -50,7 +43,7 @@ function getPackageManagerFromAngularCLI(): string {
.toString()
.trim();
} catch (e) {
return 'yarn';
return;
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/workspace/src/schematics/ng-new/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
"type": "string",
"enum": ["tslint", "eslint"],
"default": "eslint"
},
"packageManager": {
"description": "The package manager used to install dependencies.",
"type": "string",
"enum": ["npm", "yarn", "pnpm"]
}
}
}
16 changes: 16 additions & 0 deletions packages/workspace/src/schematics/shared-new/shared-new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface Schema {
defaultBase?: string;
nxWorkspaceRoot?: string;
linter: 'tslint' | 'eslint';
packageManager?: string;
}

class RunPresetTask {
Expand Down Expand Up @@ -133,6 +134,7 @@ 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 @@ -326,3 +328,17 @@ 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;
});
}
5 changes: 5 additions & 0 deletions packages/workspace/src/schematics/tao-new/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
"type": "string",
"enum": ["tslint", "eslint"],
"default": "eslint"
},
"packageManager": {
"description": "The package manager used to install dependencies.",
"type": "string",
"enum": ["npm", "yarn", "pnpm"]
}
}
}

0 comments on commit 34d156d

Please sign in to comment.