Skip to content

Commit

Permalink
fix(core): nx lint {project} should work when cwd !== appRootPath (#216)
Browse files Browse the repository at this point in the history
Closes #215
  • Loading branch information
AgentEnder committed Oct 15, 2021
1 parent ceae237 commit 9fac321
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
11 changes: 11 additions & 0 deletions e2e/core-e2e/tests/nx-dotnet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ describe('nx-dotnet e2e', () => {
?.childNamed('OutputPath')?.val as string;
expect(outputPath).toBeTruthy();
});

it('should lint', async () => {
const app = uniq('app');
await runNxCommandAsync(
`generate @nx-dotnet/core:app ${app} --language="C#" --template="webapi"`,
);
const promise = runNxCommandAsync(`lint ${app}`, {
silenceError: true,
}).then((x) => x.stderr);
await expect(promise).resolves.toContain('WHITESPACE');
});
});

describe('nx g test', () => {
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/executors/format/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const options: FormatExecutorSchema = {
};

const root = process.cwd() + '/tmp';
jest.mock('@nrwl/tao/src/utils/app-root', () => ({
appRootPath: process.cwd() + '/tmp',
}));

jest.mock('../../../../dotnet/src/lib/core/dotnet.client');

Expand Down Expand Up @@ -46,7 +49,7 @@ describe('Format Executor', () => {
isVerbose: false,
};
dotnetClient = new DotNetClient(mockDotnetFactory());
(dotnetClient as jest.Mocked<DotNetClient>).printSdkVersion.mockReturnValue(
(dotnetClient as jest.Mocked<DotNetClient>).getSdkVersion.mockReturnValue(
Buffer.from('5.0.402'),
);
});
Expand Down Expand Up @@ -140,7 +143,7 @@ describe('Format Executor', () => {
});

it('does not install dotnet-format if SDK is 6+', async () => {
(dotnetClient as jest.Mocked<DotNetClient>).printSdkVersion.mockReturnValue(
(dotnetClient as jest.Mocked<DotNetClient>).getSdkVersion.mockReturnValue(
Buffer.from('6.0.101'),
);

Expand Down Expand Up @@ -182,7 +185,7 @@ describe('Format Executor', () => {
});

it('passes the --verify-no-changes option on .NET 6 and later', async () => {
(dotnetClient as jest.Mocked<DotNetClient>).printSdkVersion.mockReturnValue(
(dotnetClient as jest.Mocked<DotNetClient>).getSdkVersion.mockReturnValue(
Buffer.from('6.0.101'),
);

Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/executors/format/executor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ExecutorContext, readJsonFile } from '@nrwl/devkit';
import { appRootPath } from '@nrwl/tao/src/utils/app-root';

import { existsSync } from 'fs';
import { join } from 'path';

import {
DotNetClient,
Expand Down Expand Up @@ -35,7 +38,7 @@ export default async function runExecutor(
context: ExecutorContext,
dotnetClient: DotNetClient = new DotNetClient(dotnetFactory()),
) {
const sdkVersion = dotnetClient.printSdkVersion().toString();
const sdkVersion = dotnetClient.getSdkVersion().toString();
const majorVersion = parseInt(sdkVersion.split('.')[0]);
const isNet6OrHigher = majorVersion >= 6;

Expand Down Expand Up @@ -70,7 +73,8 @@ function ensureFormatToolInstalled(
return;
}

const manifestPath = `${context.cwd}/.config/dotnet-tools.json`;
const manifestPath = join(appRootPath, './.config/dotnet-tools.json');
console.log(manifestPath);
const manifest = existsSync(manifestPath)
? readJsonFile(manifestPath)
: undefined;
Expand Down
37 changes: 23 additions & 14 deletions packages/dotnet/src/lib/core/dotnet.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { LoadedCLI } from './dotnet.factory';
export class DotNetClient {
constructor(private cliCommand: LoadedCLI, public cwd?: string) {}

new(template: dotnetTemplate, parameters?: dotnetNewOptions): Buffer {
new(template: dotnetTemplate, parameters?: dotnetNewOptions): void {
let cmd = `${this.cliCommand.command} new ${template}`;
if (parameters) {
parameters = swapArrayFieldValueUsingMap(parameters, 'flag', newKeyMap);
Expand All @@ -37,7 +37,7 @@ export class DotNetClient {
return this.logAndExecute(cmd);
}

build(project: string, parameters?: dotnetBuildOptions): Buffer {
build(project: string, parameters?: dotnetBuildOptions): void {
let cmd = `${this.cliCommand.command} build ${project}`;
if (parameters) {
parameters = swapArrayFieldValueUsingMap(parameters, 'flag', buildKeyMap);
Expand Down Expand Up @@ -71,7 +71,7 @@ export class DotNetClient {
project: string,
watch?: boolean,
parameters?: dotnetTestOptions,
): Buffer | ChildProcess {
): void | ChildProcess {
let cmd = watch ? ` watch --project ${project} test` : `test ${project}`;
cmd = `${this.cliCommand.command} ${cmd}`;

Expand Down Expand Up @@ -103,7 +103,7 @@ export class DotNetClient {
project: string,
pkg: string,
parameters?: dotnetAddPackageOptions,
): Buffer {
): void {
let cmd = `${this.cliCommand.command} add ${project} package ${pkg}`;
if (parameters) {
parameters = swapArrayFieldValueUsingMap(
Expand All @@ -117,7 +117,7 @@ export class DotNetClient {
return this.logAndExecute(cmd);
}

addProjectReference(hostCsProj: string, targetCsProj: string): Buffer {
addProjectReference(hostCsProj: string, targetCsProj: string): void {
return this.logAndExecute(
`${this.cliCommand.command} add ${hostCsProj} reference ${targetCsProj}`,
);
Expand All @@ -128,7 +128,7 @@ export class DotNetClient {
parameters?: dotnetPublishOptions,
publishProfile?: string,
extraParameters?: string,
): Buffer {
): void {
let cmd = `${this.cliCommand.command} publish ${project}`;
if (parameters) {
parameters = swapArrayFieldValueUsingMap(
Expand All @@ -148,22 +148,22 @@ export class DotNetClient {
return this.logAndExecute(cmd);
}

installTool(tool: string): Buffer {
installTool(tool: string): void {
const cmd = `${this.cliCommand.command} tool install ${tool}`;
return this.logAndExecute(cmd);
}

restorePackages(project: string): Buffer {
restorePackages(project: string): void {
const cmd = `${this.cliCommand.command} restore ${project}`;
return this.logAndExecute(cmd);
}

restoreTools(): Buffer {
restoreTools(): void {
const cmd = `${this.cliCommand.command} tool restore`;
return this.logAndExecute(cmd);
}

format(project: string, parameters?: dotnetFormatOptions): Buffer {
format(project: string, parameters?: dotnetFormatOptions): void {
let cmd = `${this.cliCommand.command} format ${project}`;
if (parameters) {
parameters = swapArrayFieldValueUsingMap(
Expand All @@ -177,12 +177,21 @@ export class DotNetClient {
return this.logAndExecute(cmd);
}

printSdkVersion(): Buffer {
return this.logAndExecute('dotnet --version');
getSdkVersion(): Buffer {
const cmd = 'dotnet --version';
return this.execute(cmd);
}

private logAndExecute(cmd: string): Buffer {
printSdkVersion(): void {
this.logAndExecute('dotnet --version');
}

private logAndExecute(cmd: string): void {
console.log(`Executing Command: ${cmd}`);
return execSync(cmd, { stdio: 'inherit', cwd: this.cwd || process.cwd() });
execSync(cmd, { stdio: 'inherit', cwd: this.cwd || process.cwd() });
}

private execute(cmd: string): Buffer {
return execSync(cmd, { cwd: this.cwd || process.cwd() });
}
}
2 changes: 0 additions & 2 deletions packages/utils/src/lib/utility-functions/childprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { ChildProcess } from 'child_process';
* TypeScript typings think ChildProcess is an interface, its a class.
*/
export function isChildProcess(obj: any): obj is cp.ChildProcess {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return obj instanceof cp.ChildProcess;
}

Expand Down

0 comments on commit 9fac321

Please sign in to comment.