Skip to content

Commit d883d59

Browse files
committed
feat(nx-plugin): use built in dependency management and update paths for windows
1 parent 99d8325 commit d883d59

File tree

1 file changed

+76
-80
lines changed
  • packages/nx-plugin/src/generators/openapi-client

1 file changed

+76
-80
lines changed

packages/nx-plugin/src/generators/openapi-client/index.ts

Lines changed: 76 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Tree } from '@nx/devkit';
22
import {
3+
addDependenciesToPackageJson,
34
addProjectConfiguration,
45
formatFiles,
56
generateFiles,
@@ -15,6 +16,7 @@ import { existsSync } from 'fs';
1516
import { mkdir, readFile, rm } from 'fs/promises';
1617
import { join } from 'path';
1718

19+
import packageJson from '../../../package.json';
1820
import type { UpdateApiExecutorSchema } from '../../executors/update-api/schema';
1921

2022
const tempFolder = join(process.cwd(), 'tmp');
@@ -57,7 +59,7 @@ export default async function (
5759
});
5860

5961
// Update the package.json file
60-
updatePackageJson({
62+
const installDeps = await updatePackageJson({
6163
clientType,
6264
projectName,
6365
projectRoot,
@@ -78,7 +80,8 @@ export default async function (
7880
await rm(tempFolder, { force: true, recursive: true });
7981

8082
// Return a function that installs the packages
81-
return () => {
83+
return async () => {
84+
await installDeps();
8285
installPackagesTask(tree);
8386
};
8487
}
@@ -100,9 +103,9 @@ export function normalizeOptions(
100103
options: OpenApiClientGeneratorSchema,
101104
): NormalizedOptions {
102105
const name = names(options.name).fileName;
103-
const projectDirectory = names(options.directory).fileName;
106+
const projectDirectory = names(options.directory).fileName.replace('./', '');
104107
const projectName = name.replace(new RegExp('/', 'g'), '-');
105-
const projectRoot = join(projectDirectory, projectName);
108+
const projectRoot = `${projectDirectory}/${projectName}`;
106109
const tagArray = options.tags
107110
? options.tags.split(',').map((s) => s.trim())
108111
: ['api', 'openapi'];
@@ -147,56 +150,51 @@ export function generateNxProject({
147150
};
148151

149152
// Create basic project structure
150-
addProjectConfiguration(
151-
tree,
152-
`${projectScope}/${projectName}`,
153-
{
154-
projectType: 'library',
155-
root: projectRoot,
156-
sourceRoot: `${projectRoot}/src`,
157-
tags: tagArray,
158-
targets: {
159-
build: {
160-
executor: '@nx/js:tsc',
161-
options: {
162-
additionalEntryPoints: [`${projectRoot}/src/rq.ts`],
163-
assets: [
164-
{
165-
glob: 'README.md',
166-
input: `./${projectRoot}`,
167-
output: '.',
168-
},
169-
],
170-
main: `${projectRoot}/src/index.ts`,
171-
outputPath: `dist/${projectRoot}`,
172-
tsConfig: `${projectRoot}/tsconfig.lib.json`,
173-
},
174-
outputs: ['{options.outputPath}'],
175-
},
176-
generateApi: {
177-
executor: 'nx:run-commands',
178-
options: {
179-
command: `npx @hey-api/openapi-ts -i ./api/spec.yaml -o ./src/generated -c @hey-api/client-${clientType} -p @tanstack/react-query`,
180-
cwd: projectRoot,
181-
},
153+
addProjectConfiguration(tree, `${projectScope}/${projectName}`, {
154+
projectType: 'library',
155+
root: projectRoot,
156+
sourceRoot: `${projectRoot}/src`,
157+
tags: tagArray,
158+
targets: {
159+
build: {
160+
executor: '@nx/js:tsc',
161+
options: {
162+
additionalEntryPoints: [`${projectRoot}/src/rq.ts`],
163+
assets: [
164+
{
165+
glob: 'README.md',
166+
input: `./${projectRoot}`,
167+
output: '.',
168+
},
169+
],
170+
main: `${projectRoot}/src/index.ts`,
171+
outputPath: `dist/${projectRoot}`,
172+
tsConfig: `${projectRoot}/tsconfig.lib.json`,
182173
},
183-
lint: {
184-
executor: '@nx/eslint:lint',
185-
options: {
186-
lintFilePatterns: [
187-
`${projectRoot}/**/*.ts`,
188-
`${projectRoot}/package.json`,
189-
],
190-
},
174+
outputs: ['{options.outputPath}'],
175+
},
176+
generateApi: {
177+
executor: 'nx:run-commands',
178+
options: {
179+
command: `npx @hey-api/openapi-ts -i ./api/spec.yaml -o ./src/generated -c @hey-api/client-${clientType} -p @tanstack/react-query`,
180+
cwd: projectRoot,
191181
},
192-
updateApi: {
193-
executor: './libs/openapi-generator:update-api',
194-
options: updateOptions,
182+
},
183+
lint: {
184+
executor: '@nx/eslint:lint',
185+
options: {
186+
lintFilePatterns: [
187+
`${projectRoot}/**/*.ts`,
188+
`${projectRoot}/package.json`,
189+
],
195190
},
196191
},
192+
updateApi: {
193+
executor: `${packageJson.name}:update-api`,
194+
options: updateOptions,
195+
},
197196
},
198-
false,
199-
);
197+
});
200198

201199
// Create directory structure
202200
const templatePath = join(__dirname, 'files');
@@ -269,7 +267,7 @@ export async function generateApi({
269267
/**
270268
* Updates the package.json file to add dependencies and scripts
271269
*/
272-
export function updatePackageJson({
270+
export async function updatePackageJson({
273271
clientType,
274272
projectName,
275273
projectRoot,
@@ -283,46 +281,44 @@ export function updatePackageJson({
283281
tree: Tree;
284282
}) {
285283
// Update package.json to add dependencies and scripts
286-
if (tree.exists(`${projectRoot}/package.json`)) {
287-
updateJson(tree, `${projectRoot}/package.json`, (json) => {
288-
json.scripts = {
289-
...json.scripts,
290-
generate: 'nx run ' + projectName + ':generateApi',
291-
};
292-
293-
// Add the required dependencies
294-
json.dependencies = json.dependencies || {};
295-
json.devDependencies = json.devDependencies || {};
296-
297-
// Add the client dependency
298-
if (clientType === 'fetch') {
299-
json.dependencies['@hey-api/client-fetch'] = '^0.9.0';
300-
} else if (clientType === 'axios') {
301-
json.dependencies['@hey-api/client-axios'] = '^0.7.0';
302-
json.dependencies['axios'] = '^1.6.0';
303-
}
304-
305-
// Add dev dependency for the generator
306-
json.devDependencies['@hey-api/openapi-ts'] = '^0.66.0';
307-
308-
return json;
309-
});
310-
}
284+
const deps: Record<string, string> =
285+
clientType === 'fetch'
286+
? {
287+
'@hey-api/client-fetch': '^0.9.0',
288+
}
289+
: {
290+
'@hey-api/client-axios': '^0.7.0',
291+
axios: '^1.6.0',
292+
};
293+
294+
const installDeps = addDependenciesToPackageJson(
295+
tree,
296+
deps,
297+
{},
298+
join(projectRoot, 'package.json'),
299+
);
311300

312301
const tsConfigPath = join(workspaceRoot, 'tsconfig.base.json');
313302
if (existsSync(tsConfigPath)) {
314303
updateJson(tree, 'tsconfig.base.json', (json) => {
315-
json.compilerOptions.paths[`${projectScope}/${projectName}`] = [
316-
`${projectRoot}/src/index.ts`,
304+
const paths = json.compilerOptions.paths || {};
305+
paths[`${projectScope}/${projectName}`] = [
306+
`./${projectRoot}/src/index.ts`,
317307
];
318-
json.compilerOptions.paths[`${projectScope}/${projectName}/rq`] = [
319-
`${projectRoot}/src/rq.ts`,
308+
paths[`${projectScope}/${projectName}/rq`] = [
309+
`./${projectRoot}/src/rq.ts`,
320310
];
311+
json.compilerOptions.paths = paths;
321312
return json;
322313
});
323314
} else {
324-
logger.error(`Failed to find tsconfig.base.json file in ${workspaceRoot}`);
315+
logger.error(`Failed to find tsconfig.base.json file in ${workspaceRoot}.`);
316+
throw new Error(
317+
`Failed to find tsconfig.base.json file in ${workspaceRoot}.`,
318+
);
325319
}
320+
321+
return installDeps;
326322
}
327323

328324
/**

0 commit comments

Comments
 (0)