fix(core): store packageManager preference in angular.json

This commit is contained in:
Tasos Bekos 2020-11-23 15:35:53 +02:00 committed by Victor Savkin
parent 89826063a0
commit 7383af3fb3
4 changed files with 71 additions and 5 deletions

View File

@ -21,7 +21,7 @@ export function currentCli() {
let projName: string;
export function setCurrentProjName(name: string) {
function setCurrentProjName(name: string) {
projName = name;
return name;
}
@ -42,19 +42,25 @@ export function runCreateWorkspace(
style,
base,
packageManager,
cli,
}: {
preset: string;
appName?: string;
style?: string;
base?: string;
packageManager?: string;
cli?: string;
}
) {
setCurrentProjName(name);
const linterArg =
preset === 'angular' || preset === 'angular-nest' ? ' --linter=tslint' : '';
let command = `npx create-nx-workspace@${
process.env.PUBLISHED_VERSION
} ${name} --cli=${currentCli()} --preset=${preset} ${linterArg} --no-nxCloud --no-interactive`;
} ${name} --cli=${
cli || currentCli()
} --preset=${preset} ${linterArg} --no-nxCloud --no-interactive`;
if (appName) {
command += ` --appName=${appName}`;
}

View File

@ -1,4 +1,10 @@
import { runCreateWorkspace, uniq } from '@nrwl/e2e/utils';
import {
checkFilesDoNotExist,
checkFilesExist,
readJson,
runCreateWorkspace,
uniq,
} from '@nrwl/e2e/utils';
import { existsSync, mkdirSync } from 'fs-extra';
import { execSync } from 'child_process';
@ -8,6 +14,15 @@ describe('create-nx-workspace', () => {
runCreateWorkspace(wsName, {
preset: 'empty',
});
checkFilesExist(
'workspace.json',
'package.json',
'package-lock.json',
'apps/.gitkeep',
'libs/.gitkeep'
);
checkFilesDoNotExist('yarn.lock');
});
it('should be able to create an oss workspace', () => {
@ -110,4 +125,35 @@ describe('create-nx-workspace', () => {
expect(existsSync(`${tmpDir}/${wsName}/package.json`)).toBeTruthy();
});
it('should respect package manager preference', () => {
const wsName = uniq('pm');
const appName = uniq('app');
runCreateWorkspace(wsName, {
preset: 'react',
style: 'css',
appName,
packageManager: 'yarn',
});
checkFilesExist('yarn.lock');
checkFilesDoNotExist('package-lock.json');
});
it('should store package manager preference for angular cli', () => {
const wsName = uniq('pm');
const appName = uniq('app');
runCreateWorkspace(wsName, {
preset: 'angular',
appName,
style: 'css',
packageManager: 'yarn',
cli: 'angular',
});
const workspaceJson = readJson('angular.json');
expect(workspaceJson.cli.packageManager).toEqual('yarn');
checkFilesExist('yarn.lock');
checkFilesDoNotExist('package-lock.json');
});
});

View File

@ -5,14 +5,13 @@ import {
runCLI,
runCLIAsync,
runCreateWorkspace,
setCurrentProjName,
uniq,
yarnAdd,
} from '@nrwl/e2e/utils';
describe('custom workspace layout', () => {
it('should work', async () => {
const proj = setCurrentProjName(uniq('custom-layout-proj'));
const proj = uniq('custom-layout-proj');
runCreateWorkspace(proj, { preset: 'oss' });
yarnAdd('@nrwl/react @nrwl/angular @nrwl/express');

View File

@ -134,6 +134,7 @@ export function sharedNew(cli: string, options: Schema): Rule {
return chain([
schematic('workspace', { ...workspaceOpts, cli }),
cli === 'angular' ? setDefaultPackageManager(options) : noop(),
setDefaultLinter(options),
addPresetDependencies(options),
addCloudDependencies(options),
@ -327,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;
});
}