/
nx-project.ts
87 lines (81 loc) · 2.37 KB
/
nx-project.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { workspaceRoot } from '@nrwl/devkit';
import {
getPackageManagerCommand,
readJsonFile,
writeJsonFile,
} from '@nrwl/devkit';
import { execSync } from 'child_process';
import { dirname } from 'path';
import { ensureDirSync } from 'fs-extra';
import { tmpProjPath } from './paths';
import { cleanup } from './utils';
function runNxNewCommand(args?: string, silent?: boolean) {
const localTmpDir = dirname(tmpProjPath());
return execSync(
`node ${require.resolve(
'nx'
)} new proj --nx-workspace-root=${localTmpDir} --no-interactive --skip-install --collection=@nrwl/workspace --npmScope=proj --preset=empty ${
args || ''
}`,
{
cwd: localTmpDir,
...(silent && false ? { stdio: ['ignore', 'ignore', 'ignore'] } : {}),
}
);
}
export function patchPackageJsonForPlugin(
npmPackageName: string,
distPath: string
) {
const path = tmpProjPath('package.json');
const json = readJsonFile(path);
json.devDependencies[npmPackageName] = `file:${workspaceRoot}/${distPath}`;
writeJsonFile(path, json);
}
/**
* Generate a unique name for running CLI commands
* @param prefix
*
* @returns `'<prefix><random number>'`
*/
export function uniq(prefix: string) {
return `${prefix}${Math.floor(Math.random() * 10000000)}`;
}
/**
* Run the appropriate package manager install command in the e2e directory
* @param silent silent output from the install
*/
export function runPackageManagerInstall(silent: boolean = true) {
const pmc = getPackageManagerCommand();
const install = execSync(pmc.install, {
cwd: tmpProjPath(),
...(silent ? { stdio: ['ignore', 'ignore', 'ignore'] } : {}),
});
return install ? install.toString() : '';
}
/**
* Creates a new nx project in the e2e directory
*
* @param npmPackageName package name to test
* @param pluginDistPath dist path where the plugin was outputted to
*/
export function newNxProject(
npmPackageName: string,
pluginDistPath: string
): void {
cleanup();
runNxNewCommand('', true);
patchPackageJsonForPlugin(npmPackageName, pluginDistPath);
runPackageManagerInstall();
}
/**
* Ensures that a project has been setup in the e2e directory
* It will also copy `@nrwl` packages to the e2e directory
*/
export function ensureNxProject(
npmPackageName?: string,
pluginDistPath?: string
): void {
ensureDirSync(tmpProjPath());
newNxProject(npmPackageName, pluginDistPath);
}