-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathnx.ts
More file actions
105 lines (97 loc) · 2.74 KB
/
nx.ts
File metadata and controls
105 lines (97 loc) · 2.74 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import {
type ExecutorContext,
type NxJsonConfiguration,
type PluginConfiguration,
type ProjectConfiguration,
type ProjectGraph,
type Tree,
updateJson,
} from '@nx/devkit';
import { libraryGenerator } from '@nx/js';
import type { LibraryGeneratorSchema } from '@nx/js/src/generators/library/schema';
import path from 'node:path';
import { createTreeWithEmptyWorkspace } from 'nx/src/generators/testing-utils/create-tree-with-empty-workspace';
import { executeProcess } from '@code-pushup/utils';
export function executorContext<
T extends { projectName: string; cwd?: string },
>(nameOrOpt: string | T): ExecutorContext {
const { projectName, cwd = process.cwd() } =
typeof nameOrOpt === 'string' ? { projectName: nameOrOpt } : nameOrOpt;
return {
cwd,
isVerbose: false,
projectName,
root: '.',
projectsConfigurations: {
projects: {
[projectName]: {
name: projectName,
root: `libs/${projectName}`,
},
},
version: 1,
},
nxJsonConfiguration: {},
projectGraph: { nodes: {}, dependencies: {} } satisfies ProjectGraph,
};
}
export async function generateWorkspaceAndProject(
options:
| string
| (Omit<Partial<LibraryGeneratorSchema>, 'name'> & {
name: string;
}),
) {
const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
const { name, ...normalizedOptions } =
typeof options === 'string' ? { name: options } : options;
await libraryGenerator(tree, {
name,
directory: path.join('libs', name),
tags: 'scope:plugin',
linter: 'none',
unitTestRunner: 'none',
testEnvironment: 'node',
buildable: false,
publishable: false,
...normalizedOptions,
});
return tree;
}
export function registerPluginInWorkspace(
tree: Tree,
configuration: PluginConfiguration,
pluginConfig?: Record<string, unknown>,
) {
const normalizedPluginConfiguration =
typeof configuration === 'string'
? {
plugin: configuration,
}
: configuration;
const pluginName =
typeof configuration === 'string' ? configuration : configuration.plugin;
updateJson(tree, 'nx.json', (json: NxJsonConfiguration) => ({
...json,
plugins: [...(json.plugins ?? []), normalizedPluginConfiguration],
...(pluginConfig
? {
pluginsConfig: {
...json.pluginsConfig,
[pluginName]: pluginConfig,
},
}
: {}),
}));
}
export async function nxShowProjectJson<T extends ProjectConfiguration>(
cwd: string,
project: string,
) {
const { code, stderr, stdout } = await executeProcess({
command: 'npx',
args: ['nx', 'show', `project --json ${project}`],
cwd,
});
return { code, stderr, projectJson: JSON.parse(stdout) as T };
}