Skip to content

Commit f13f0d7

Browse files
committed
wip
1 parent 2d29088 commit f13f0d7

File tree

2 files changed

+246
-226
lines changed

2 files changed

+246
-226
lines changed
Lines changed: 165 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,175 +1,182 @@
11
import {
2-
formatFiles,
3-
generateFiles,
4-
names,
5-
readProjectConfiguration,
6-
Tree,
7-
updateJson,
8-
updateProjectConfiguration,
9-
workspaceRoot,
2+
formatFiles,
3+
generateFiles,
4+
names,
5+
readProjectConfiguration,
6+
Tree,
7+
updateJson,
8+
updateProjectConfiguration,
9+
workspaceRoot,
1010
} from '@nx/devkit';
11-
import {relative, join} from 'path';
12-
import {PluginGeneratorSchema} from './schema';
13-
import {libraryGenerator} from '@nx/js';
11+
import { relative, join } from 'path';
12+
import { PluginGeneratorSchema } from './schema';
13+
import { libraryGenerator } from '@nx/js';
1414
import e2ePluginGenerator from '../e2e-plugin/generator';
15-
import {addCodePushupConfigFile, formatObjectToFormattedJsString} from "../../internal/generate-files";
15+
import {
16+
addCodePushupConfigFile,
17+
formatObjectToFormattedJsString,
18+
} from '../../internal/generate-files';
1619

1720
export async function pluginGenerator(
18-
tree: Tree,
19-
options: PluginGeneratorSchema,
21+
tree: Tree,
22+
options: PluginGeneratorSchema,
2023
) {
21-
const {name, skipTest, skipE2e, categoryName} = options;
22-
const projectRoot = `./packages/${name}`;
23-
24-
await libraryGenerator(tree, {
25-
name: name,
26-
tags: 'scope:plugin',
27-
linter: 'eslint',
28-
unitTestRunner: skipTest ? 'none' : 'vitest',
29-
testEnvironment: 'node',
30-
bundler: 'esbuild',
31-
});
32-
33-
const pluginNames = names(name);
34-
24+
const { name, skipTest, skipE2e, categoryName } = options;
25+
const projectRoot = `./packages/${name}`;
26+
27+
await libraryGenerator(tree, {
28+
name: name,
29+
tags: 'scope:plugin',
30+
linter: 'eslint',
31+
unitTestRunner: skipTest ? 'none' : 'vitest',
32+
testEnvironment: 'node',
33+
bundler: 'esbuild',
34+
});
35+
36+
const pluginNames = names(name);
37+
38+
const pCfg = readProjectConfiguration(tree, name);
39+
await updateProjectConfiguration(tree, name, {
40+
...pCfg,
41+
targets: {
42+
...pCfg.targets,
43+
'code-pushup': {
44+
command: `npx @code-pushup/cli collect --config ${projectRoot}/code-pushup.config.ts --onlyPlugins=${pluginNames.fileName}`,
45+
},
46+
},
47+
});
48+
49+
updateJson(
50+
tree,
51+
`${projectRoot}/package.json`,
52+
({ type, main, ...rest }) => rest,
53+
);
54+
55+
// clean irrelevant files
56+
tree.delete(`${projectRoot}/src/lib/${name}.ts`);
57+
tree.delete(`${projectRoot}/src/lib/${name}.spec.ts`);
58+
59+
if (!options?.skipTest) {
60+
// delete existing vite.config.json
61+
tree.delete(`${projectRoot}/vite.config.ts`);
62+
63+
// rename existing tsconfig.spec.json
64+
tree.rename(
65+
`${projectRoot}/tsconfig.spec.json`,
66+
`${projectRoot}/tsconfig.test.json`,
67+
);
68+
// update tsconfig references for renamed tsconfig.test.json
69+
updateJson(tree, `${projectRoot}/tsconfig.json`, (json) => ({
70+
...json,
71+
compilerOptions: {
72+
...json.compilerOptions,
73+
module: 'ESNext',
74+
},
75+
references: json.references.map((ref: any) => ({
76+
...ref,
77+
path:
78+
ref.path === './tsconfig.spec.json'
79+
? './tsconfig.test.json'
80+
: ref.path,
81+
})),
82+
}));
83+
84+
// update tsconfig.test.json
85+
updateJson(tree, `${projectRoot}/tsconfig.test.json`, (json) => ({
86+
...json,
87+
include: [
88+
'vite.config.unit.ts',
89+
'vite.config.integration.ts',
90+
'mocks/**/*.ts',
91+
...json.include,
92+
],
93+
}));
94+
95+
// add test targets
3596
const pCfg = readProjectConfiguration(tree, name);
3697
await updateProjectConfiguration(tree, name, {
37-
...pCfg,
38-
targets: {
39-
...pCfg.targets,
40-
'code-pushup': {
41-
command: `npx @code-pushup/cli collect --config ${projectRoot}/code-pushup.config.ts --onlyPlugins=${pluginNames.fileName}`,
42-
},
98+
...pCfg,
99+
targets: {
100+
...Object.fromEntries(
101+
Object.entries(pCfg.targets).filter(
102+
([targetName]) => targetName !== 'test',
103+
),
104+
),
105+
build: {
106+
...pCfg.targets.build,
107+
options: {
108+
...pCfg.targets.build.options,
109+
format: ['esm'],
110+
},
111+
},
112+
'unit-test': {
113+
executor: '@nx/vite:test',
114+
options: {
115+
config: `${projectRoot}/vite.config.unit.ts`,
116+
},
117+
},
118+
'integration-test': {
119+
executor: '@nx/vite:test',
120+
options: {
121+
config: `${projectRoot}/vite.config.integration.ts`,
122+
},
43123
},
124+
},
44125
});
45126

46-
updateJson(
47-
tree,
48-
`${projectRoot}/package.json`,
49-
({type, main, ...rest}) => rest,
50-
);
51-
52-
// clean irrelevant files
53-
tree.delete(`${projectRoot}/src/lib/${name}.ts`);
54-
tree.delete(`${projectRoot}/src/lib/${name}.spec.ts`);
55-
56-
if (!options?.skipTest) {
57-
// delete existing vite.config.json
58-
tree.delete(`${projectRoot}/vite.config.ts`);
59-
60-
// rename existing tsconfig.spec.json
61-
tree.rename(
62-
`${projectRoot}/tsconfig.spec.json`,
63-
`${projectRoot}/tsconfig.test.json`,
64-
);
65-
// update tsconfig references for renamed tsconfig.test.json
66-
updateJson(tree, `${projectRoot}/tsconfig.json`, (json) => ({
67-
...json,
68-
compilerOptions: {
69-
...json.compilerOptions,
70-
module: 'ESNext',
71-
},
72-
references: json.references.map((ref: any) => ({
73-
...ref,
74-
path:
75-
ref.path === './tsconfig.spec.json'
76-
? './tsconfig.test.json'
77-
: ref.path,
78-
})),
79-
}));
80-
81-
// update tsconfig.test.json
82-
updateJson(tree, `${projectRoot}/tsconfig.test.json`, (json) => ({
83-
...json,
84-
include: [
85-
'vite.config.unit.ts',
86-
'vite.config.integration.ts',
87-
'mocks/**/*.ts',
88-
...json.include,
89-
],
90-
}));
91-
92-
// add test targets
93-
const pCfg = readProjectConfiguration(tree, name);
94-
await updateProjectConfiguration(tree, name, {
95-
...pCfg,
96-
targets: {
97-
...Object.fromEntries(
98-
Object.entries(pCfg.targets).filter(
99-
([targetName]) => targetName !== 'test',
100-
),
101-
),
102-
build: {
103-
...pCfg.targets.build,
104-
options: {
105-
...pCfg.targets.build.options,
106-
format: ['esm'],
107-
},
108-
},
109-
'unit-test': {
110-
executor: '@nx/vite:test',
111-
options: {
112-
config: `${projectRoot}/vite.config.unit.ts`,
113-
},
114-
},
115-
'integration-test': {
116-
executor: '@nx/vite:test',
117-
options: {
118-
config: `${projectRoot}/vite.config.integration.ts`,
119-
},
120-
},
121-
},
122-
});
123-
124-
// add e2e project and tests
125-
if (!options?.skipE2e) {
126-
await e2ePluginGenerator(tree, {
127-
pluginName: name,
128-
skipTest: false,
129-
});
130-
}
131-
132-
// add plugin test logic
133-
generateFiles(tree, join(__dirname, 'tests'), projectRoot, {
134-
...pluginNames,
135-
relativeToRoot: relative(projectRoot, workspaceRoot),
136-
tmpl: '',
137-
});
127+
// add e2e project and tests
128+
if (!options?.skipE2e) {
129+
await e2ePluginGenerator(tree, {
130+
pluginName: name,
131+
skipTest: false,
132+
});
138133
}
139134

140-
// add plugin logic
141-
generateFiles(tree, join(__dirname, 'files'), projectRoot, {
142-
...pluginNames,
143-
tmpl: '',
144-
});
145-
146-
// add code-pushup config file
147-
const categories = (categoryName && categoryName !== '') && [
148-
{
149-
slug: categoryName,
150-
title: categoryName,
151-
refs: [
152-
`+...${pluginNames.propertyName}AuditRefs+`
153-
]
154-
}
155-
].map(formatObjectToFormattedJsString)
156-
// remove quotes around the `spreading object` syntax
157-
.map(v => v.replace(/"\+/g, '').replace(/\+"/g, ''));
158-
addCodePushupConfigFile(tree, projectRoot, {
159-
configFile: {
160-
format: 'ts',
161-
fileImports: categoryName ? [`import { ${pluginNames.propertyName}AuditRefs } from './src';`] : [],
162-
},
163-
plugins: [
164-
{
165-
importPath: `./src`,
166-
exportName: `${pluginNames.propertyName}Plugin`
167-
}
168-
],
169-
categories
135+
// add plugin test logic
136+
generateFiles(tree, join(__dirname, 'tests'), projectRoot, {
137+
...pluginNames,
138+
relativeToRoot: relative(projectRoot, workspaceRoot),
139+
tmpl: '',
170140
});
171-
172-
await formatFiles(tree);
141+
}
142+
143+
// add plugin logic
144+
generateFiles(tree, join(__dirname, 'files'), projectRoot, {
145+
...pluginNames,
146+
tmpl: '',
147+
});
148+
149+
// add code-pushup config file
150+
const categories =
151+
categoryName &&
152+
categoryName !== '' &&
153+
[
154+
{
155+
slug: categoryName,
156+
title: categoryName,
157+
refs: [`+...${pluginNames.propertyName}AuditRefs+`],
158+
},
159+
]
160+
.map(formatObjectToFormattedJsString)
161+
// remove quotes around the `spreading object` syntax
162+
.map((v) => v.replace(/"\+/g, '').replace(/\+"/g, ''));
163+
addCodePushupConfigFile(tree, projectRoot, {
164+
configFile: {
165+
format: 'ts',
166+
fileImports: categoryName
167+
? [`import { ${pluginNames.propertyName}AuditRefs } from './src';`]
168+
: [],
169+
},
170+
plugins: [
171+
{
172+
importPath: `./src`,
173+
exportName: `${pluginNames.propertyName}Plugin`,
174+
},
175+
],
176+
categories,
177+
});
178+
179+
await formatFiles(tree);
173180
}
174181

175182
export default pluginGenerator;

0 commit comments

Comments
 (0)