Skip to content

Commit

Permalink
test: fix nx-melos-e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
tinesoft committed Feb 28, 2024
1 parent b7de0bd commit 320f9f1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
2 changes: 1 addition & 1 deletion e2e/nx-melos-e2e/tests/nx-melos.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('nx-melos e2e', () => {
const scripts = [
{
name: 'melos-bootstrap',
output: `NX Successfully ran target melos-bootstrap for project @test-project/source`,
output: `Successfully ran target melos-bootstrap for project @test-project/source`,
},
];

Expand Down
2 changes: 1 addition & 1 deletion packages/common-jvm/src/lib/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createProjectGraphAsync, joinPathFragments, logger, ProjectGraph, readCachedProjectGraph, Tree } from '@nx/devkit';
import { createProjectGraphAsync, logger, ProjectGraph, readCachedProjectGraph, Tree } from '@nx/devkit';
import { execSync } from 'child_process';
import { fileExists } from '@nx/workspace/src/utilities/fileutils';

Expand Down
64 changes: 52 additions & 12 deletions packages/common/testing/e2e/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JsonParseOptions, PackageManager, getPackageManagerCommand, joinPathFragments, parseJson, readJsonFile, workspaceRoot } from '@nx/devkit';
import { JsonParseOptions, PackageManager, detectPackageManager, getPackageManagerCommand, joinPathFragments, parseJson, readJsonFile, workspaceRoot } from '@nx/devkit';
import { getPackageLatestNpmVersion } from '../../src/';
import { ExecSyncOptions, execSync } from 'child_process';
import { exec, execSync } from 'child_process';
import { rmSync, mkdirSync, statSync, readFileSync, existsSync } from 'fs-extra';
import { join, dirname, isAbsolute } from 'path';

Expand Down Expand Up @@ -103,18 +103,58 @@ function exists(filePath: string): boolean {
return directoryExists(filePath) || fileExists(filePath);
}

export function runNxCommand(command: string, pkgManagerExec = 'npx', opts: ExecSyncOptions = { cwd: tmpProjPath(), env: process.env, stdio: 'pipe' }) {
return {
stdout: execSync(`${pkgManagerExec} nx ${command}`, {
cwd: opts.cwd,
env: opts.env,
stdio: opts.stdio
})?.toString()
};

//https://github.com/nrwl/nx/blob/master/e2e/utils/create-project-utils.ts#L628

/**
* Run a command asynchronously inside the e2e directory.
*
* @param command
* @param opts
*/
export function runCommandAsync(
command: string,
opts: { silenceError?: boolean; env?: NodeJS.ProcessEnv; cwd?: string } = {
silenceError: false,
}
): Promise<{ stdout: string; stderr: string }> {
return new Promise((resolve, reject) => {
exec(
command,
{
cwd: opts.cwd ?? tmpProjPath(),
env: { ...process.env, ...opts.env },
},
(err, stdout, stderr) => {
if (!opts.silenceError && err) {
reject(err);
}
resolve({ stdout, stderr });
}
);
});
}

export async function runNxCommandAsync(command: string, pkgManagerExec = 'npx', opts: ExecSyncOptions = { cwd: tmpProjPath(), env: process.env, stdio: 'pipe' }) {
return runNxCommand(command, pkgManagerExec, opts);
/**
* Run a nx command asynchronously inside the e2e directory
* @param command
* @param opts
*/
export function runNxCommandAsync(
command: string,
opts: { silenceError?: boolean; env?: NodeJS.ProcessEnv; cwd?: string } = {
silenceError: false,
}
): Promise<{ stdout: string; stderr: string }> {
const cwd = opts.cwd ?? tmpProjPath();
if (fileExists(tmpProjPath('package.json'))) {
const pmc = getPackageManagerCommand(detectPackageManager(cwd));
return runCommandAsync(`${pmc.exec} nx ${command}`, opts);
} else if (process.platform === 'win32') {
return runCommandAsync(`./nx.bat %${command}`, opts);
} else {
return runCommandAsync(`./nx %${command}`, opts);
}
}

export function readJson<T extends object = any>(path: string, options?: JsonParseOptions): T {
Expand Down

0 comments on commit 320f9f1

Please sign in to comment.