Skip to content

Commit

Permalink
feat: add client generation (gqty)
Browse files Browse the repository at this point in the history
  • Loading branch information
schettn committed Jul 9, 2024
1 parent 27ac0ff commit 57d8f55
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/pylon-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand Down
24 changes: 24 additions & 0 deletions packages/pylon-cli/src/commands/develop.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 10 additions & 0 deletions packages/pylon-cli/src/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default async (
template: string,
options: {
name: string
clientPath?: string
}
) => {
const name = options.name
Expand Down Expand Up @@ -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}.`)
}
9 changes: 8 additions & 1 deletion packages/pylon-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 57d8f55

Please sign in to comment.