Skip to content

Commit

Permalink
feat: sentry for cli and server / add getLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
schettn committed Jul 11, 2024
1 parent c28a743 commit 9bb1fb1
Show file tree
Hide file tree
Showing 13 changed files with 423 additions and 270 deletions.
1 change: 1 addition & 0 deletions packages/pylon-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dependencies": {
"@gqty/cli": "4.1.0-canary-20240708145747.43f0b4262b24cc7de046a0d5b04777bd9ac1eb21",
"@inquirer/prompts": "^5.1.2",
"@sentry/bun": "^8.17.0",
"commander": "^10.0.0"
},
"peerDependencies": {
Expand Down
16 changes: 9 additions & 7 deletions packages/pylon-cli/src/commands/develop.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {build} from '@getcronit/pylon-builder'
import {$, Subprocess} from 'bun'
import {$, Subprocess, spawnSync} from 'bun'

import {sfiBuildPath, sfiSourcePath} from '../constants.js'
import path from 'path'
import {fetchSchema, generateClient} from '@gqty/cli'
import path from 'path'
import {sfiBuildPath, sfiSourcePath} from '../constants.js'

export default async (options: {port: string}) => {
const {stdout} = await $`npx -p which which pylon-server`
const {stdout} = spawnSync(['npx', '-p', 'which', 'which', 'pylon-server'])

const binPath = stdout.toString().trim()

Expand All @@ -21,7 +21,11 @@ export default async (options: {port: string}) => {

currentProc = Bun.spawn({
cmd: ['bun', 'run', binPath, '--port', options.port],
stdout: 'inherit'
stdout: 'inherit',
env: {
...process.env,
NODE_ENV: 'development'
}
})
}

Expand All @@ -32,8 +36,6 @@ export default async (options: {port: string}) => {
if (clientPath) {
const endpoint = `http://localhost:${options.port}/graphql`

console.log('Generating client...', {endpoint, clientPath})

const schema = await fetchSchema(endpoint)

await generateClient(schema, {
Expand Down
114 changes: 76 additions & 38 deletions packages/pylon-cli/src/commands/new.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {execSync} from 'child_process'
import {existsSync, mkdirSync} from 'fs'
import {join} from 'path'

import {setPackageName} from '../utils/set-package-name'
import {getLogger} from '@getcronit/pylon'
import {access} from 'fs/promises'
import {mkdir} from 'fs/promises'
import {constants} from 'fs/promises'

const logger = getLogger()

export default async (
rootPath: string,
Expand All @@ -12,56 +16,90 @@ export default async (
clientPath?: string
}
) => {
await new Promise(resolve => setTimeout(resolve, 100))
const name = options.name

console.log(
`Creating project ${name} at ${rootPath} from template ${template}`
)
logger.info(`🚀 Starting project creation: ${name}`)
logger.info(`📁 Destination: ${rootPath}`)
logger.info(`🔖 Template: ${template}`)

await new Promise(resolve => setTimeout(resolve, 100))

// Check if the project directory already exists
const projectDir = join(process.cwd(), rootPath)
if (existsSync(projectDir)) {
console.error(
`Error: Project directory "${name}" already exists in "${rootPath}".`
// await new Promise(resolve => setTimeout(resolve, 100))

if (options.clientPath) {
logger.info(
`🔧 Client path will be inserted into package.json: ${options.clientPath}`
)
process.exit(1)
} else {
mkdirSync(rootPath, {recursive: true})
}

// Clone the template repository into the project directory
execSync(`git clone ${template} "${projectDir}"`)
try {
const projectDir = join(process.cwd(), rootPath)

// Remove the .git directory from the project directory
execSync(`rm -rf "${join(projectDir, '.git')}"`)
try {
await access(projectDir, constants.F_OK)

// Set the project name in the package.json file
setPackageName(projectDir, name)
throw new Error(
`Project directory "${name}" already exists in "${rootPath}".`
)
} catch (err: any) {
if (err.code !== 'ENOENT') {
throw err // Re-throw unexpected errors
}
// Directory does not exist, continue
}

// Initialize a new git repository in the project directory
execSync(`git init "${projectDir}"`)
await mkdir(rootPath, {recursive: true})

// Add all files to the git repository
execSync(`git -C "${projectDir}" add .`)
logger.info(`Created directory: ${rootPath}`)

// Create an initial commit
execSync(`git -C "${projectDir}" commit -m "Initial commit"`)
// Clone the template repository into the project directory
logger.info(`Cloning template from ${template} into ${projectDir}`)
await Bun.$`git clone ${template} "${projectDir}"`

console.log('Installing project dependencies...')
// Remove the .git directory from the project directory
logger.info('Removing existing .git directory')
await Bun.$`rm -rf "${join(projectDir, '.git')}"`

// Bun install the project dependencies
execSync(`cd "${projectDir}" && bun install`, {
stdio: 'inherit'
})
// Set the project name in the package.json file
logger.info('Setting project name in package.json')
setPackageName(projectDir, name)

// 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}"'`
)
// Initialize a new git repository in the project directory
logger.info('Initializing new git repository')
await Bun.$`git init "${projectDir}"`

console.log('Inserted client path into package.json')
}
// Add all files to the git repository
logger.info('Adding files to git repository')
await Bun.$`git -C "${projectDir}" add .`

// Create an initial commit
logger.info('Creating initial commit')
await Bun.$`git -C "${projectDir}" commit -m "Initial commit"`

console.log(`Project ${name} created successfully at ${projectDir}.`)
logger.info('Installing project dependencies...')
// Bun install the project dependencies
await Bun.$`cd "${projectDir}" && bun install`

// Insert the client path into the package.json file (gqty key)
if (options.clientPath) {
logger.info('Inserting client path into package.json')
await Bun.$`cd "${projectDir}" && npx json -q -I -f package.json -e 'this.pylon = this.pylon || {}; this.pylon.gqty="${options.clientPath}"'`

logger.info('Inserted client path into package.json')

// Add @gqty/react and gqty to the cwd package.json and prompt the user to install them
await Bun.$`npx json -q -I -f package.json -e 'this.devDependencies = this.devDependencies || {}; this.devDependencies["@gqty/react"] = "*"'`

await Bun.$`npx json -q -I -f package.json -e 'this.devDependencies = this.devDependencies || {}; this.devDependencies["gqty"] = "*"'`

logger.info(
'Added @gqty/react and gqty to package.json. Run "install" to install them'
)
}

logger.info(`🎉 Project ${name} created successfully at ${projectDir}.`)
} catch (err: any) {
throw new Error(`Error creating project: ${err.message}`)
}
}
21 changes: 16 additions & 5 deletions packages/pylon-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {input, select, confirm} from '@inquirer/prompts'
import packageJson from './utils/package-json.js'
import * as commands from './commands/index.js'
import {cliName} from './constants.js'
import {logger} from '@getcronit/pylon'
import * as Sentry from '@sentry/bun'

// Set development environment
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
Expand All @@ -13,6 +15,13 @@ export const program = new Command()

program.name(cliName).description('Pylon CLI').version(packageJson.version)

Sentry.init({
dsn: 'https://cb8dd08e25022c115327258343ffb657@sentry.cronit.io/9',
environment: process.env.NODE_ENV,
normalizeDepth: 10,
tracesSampleRate: 1
})

program
.command('develop')
.description('Start the development server')
Expand Down Expand Up @@ -49,13 +58,13 @@ program
.action(commands.new)

if (!process.argv.slice(2).length) {
;(async () => {
try {
const answer = await select({
message: 'What action would you like to take?',
choices: [
{name: 'Create a new pylon', value: 'new'},
{name: 'Start development server', value: 'develop'},
{name: 'Build the pylon', value: 'build'}
{name: 'New', value: 'new'},
{name: 'Develop', value: 'develop'},
{name: 'Build', value: 'build'}
]
})

Expand Down Expand Up @@ -100,7 +109,9 @@ if (!process.argv.slice(2).length) {
} else if (answer === 'build') {
await commands.build({})
}
})()
} catch (e: any) {
logger.error(e.toString())
}
} else {
program.parse()
}
3 changes: 1 addition & 2 deletions packages/pylon-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
],
"license": "UNLICENSED",
"dependencies": {
"@hono/sentry": "^1.0.1",
"@sentry/bun": "^7.106.0",
"@sentry/bun": "^8.17.0",
"commander": "^11.0.0",
"graphql": "^16.6.0",
"graphql-yoga": "^5.3.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/pylon-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Set default env variables
process.env.NODE_ENV = process.env.NODE_ENV || 'production'

import {logger} from '@getcronit/pylon'
import {makeApp, runtime} from '@getcronit/pylon-server'
import {Command} from 'commander'
import fs from 'fs'
Expand Down Expand Up @@ -80,4 +81,4 @@ configureServer?.(server)

runtime.server = server

console.log(`Listening on localhost:`, server.port)
logger.info(`Server listening on port ${args.port}`)
Loading

0 comments on commit 9bb1fb1

Please sign in to comment.