diff --git a/packages/pylon-cli/package.json b/packages/pylon-cli/package.json index 5f73850..db17ceb 100644 --- a/packages/pylon-cli/package.json +++ b/packages/pylon-cli/package.json @@ -19,6 +19,7 @@ "microbundle": "^0.15.1" }, "dependencies": { + "@gqty/cli": "4.1.0-canary-20240708145747.43f0b4262b24cc7de046a0d5b04777bd9ac1eb21", "@getcronit/pylon": "*", "@getcronit/pylon-builder": "*", "@getcronit/pylon-server": "*", diff --git a/packages/pylon-cli/src/commands/develop.ts b/packages/pylon-cli/src/commands/develop.ts index e17fec9..4e9be4f 100644 --- a/packages/pylon-cli/src/commands/develop.ts +++ b/packages/pylon-cli/src/commands/develop.ts @@ -1,5 +1,6 @@ import {build} from '@getcronit/pylon-builder' import {makeApp, runtime} from '@getcronit/pylon-server' +import {fetchSchema, generateClient} from '@gqty/cli' import path from 'path' import {Server} from 'bun' @@ -60,6 +61,29 @@ export default async (options: {port: string}) => { await configureServer(server) } + // Load the package.json file + const packageJson = await importFresh( + path.resolve(process.cwd(), 'package.json') + ) + + console.log('packageJson', packageJson) + + const clientPath = packageJson.pylon?.gqty + + if (clientPath) { + const endpoint = `http://localhost:${server.port}/graphql` + + console.log('Generating client...', {endpoint, clientPath}) + + const schema = await fetchSchema(endpoint) + + await generateClient(schema, { + endpoint, + destination: clientPath, + react: true + }) + } + runtime.server = server } diff --git a/packages/pylon-cli/src/commands/new.ts b/packages/pylon-cli/src/commands/new.ts index fbe8793..e1483f0 100644 --- a/packages/pylon-cli/src/commands/new.ts +++ b/packages/pylon-cli/src/commands/new.ts @@ -9,6 +9,7 @@ export default async ( template: string, options: { name: string + clientPath?: string } ) => { const name = options.name @@ -53,5 +54,14 @@ export default async ( stdio: 'inherit' }) + // Insert the client path into the package.json file (gqty key) + if (options.clientPath) { + execSync( + `cd "${projectDir}" && npx json -q -I -f package.json -e 'this.pylon = this.pylon || {}; this.pylon.gqty="${options.clientPath}"'` + ) + + console.log('Inserted client path into package.json') + } + console.log(`Project ${name} created successfully at ${projectDir}.`) } diff --git a/packages/pylon-cli/src/index.ts b/packages/pylon-cli/src/index.ts index 90e1f28..cc051f0 100644 --- a/packages/pylon-cli/src/index.ts +++ b/packages/pylon-cli/src/index.ts @@ -77,8 +77,15 @@ if (!process.argv.slice(2).length) { default: false }) + let clientPath: string | undefined = undefined + if (useClient) { + clientPath = await input({ + message: 'Path to the client (relative to the pylon project)', + default: '../src/client/index.ts' + }) + } + await commands.new(rootPath, template, {name, clientPath}) - await commands.new(rootPath, template, {name}) } else if (answer === 'develop') { const port = await input({ message: 'Port to run the server on',