|
| 1 | +import { existsSync } from 'node:fs' |
| 2 | +import * as p from '@clack/prompts' |
| 3 | +import * as execa from 'execa' |
| 4 | +import { createResolver } from '@nuxt/kit' |
| 5 | +import chalk from 'chalk' |
| 6 | + |
| 7 | +const { resolve: resolveProject } = createResolver(process.cwd()) |
| 8 | + |
| 9 | +async function up() { |
| 10 | + p.intro(chalk.bgGreen.blue(` nuxt-edgedb `)) |
| 11 | + |
| 12 | + /** |
| 13 | + * CLI Install detection |
| 14 | + */ |
| 15 | + let edgedbCliVersion: string | undefined |
| 16 | + try { |
| 17 | + edgedbCliVersion = await execa.execa(`edgedb`, [`--version`], { cwd: resolveProject() }).then(result => result.stdout.replace('EdgeDB CLI ', '')) |
| 18 | + } |
| 19 | + catch (e) {} |
| 20 | + |
| 21 | + if (!edgedbCliVersion) { |
| 22 | + const setupEdgeDbCli = await p.select({ |
| 23 | + message: 'EdgeDB CLI not found, do you want to install EdgeDB it?', |
| 24 | + options: [ |
| 25 | + { label: 'Yes', value: 'yes', hint: 'recommended' }, |
| 26 | + { label: 'No', value: 'no', hint: 'skip installation' }, |
| 27 | + ], |
| 28 | + }) |
| 29 | + |
| 30 | + if (setupEdgeDbCli === 'yes') { |
| 31 | + const spinner = p.spinner() |
| 32 | + |
| 33 | + try { |
| 34 | + spinner.start('Installing EdgeDB CLI...') |
| 35 | + await execa.$`curl https://sh.edgedb.com --proto '=https' -sSf1 | sh` |
| 36 | + edgedbCliVersion = await execa.execa(`edgedb`, ['--version'], { cwd: resolveProject() }).then(result => result.stdout.replace('EdgeDB CLI ', '')) |
| 37 | + spinner.stop(`EdgeDB CLI version ${edgedbCliVersion} installed.`) |
| 38 | + } |
| 39 | + catch (e) { |
| 40 | + spinner.stop('Failed to install EdgeDB CLI.') |
| 41 | + p.log.warn(`Try running: \`${chalk.green('curl https://sh.edgedb.com --proto \'=https\' -sSf1 | sh')}\` manually.`) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if (!edgedbCliVersion) { |
| 46 | + process.exit(0) |
| 47 | + } |
| 48 | + } |
| 49 | + else { |
| 50 | + p.log.success(`EdgeDB CLI version ${chalk.blue(edgedbCliVersion)} found.`) |
| 51 | + } |
| 52 | + |
| 53 | + const groupData = await p.group( |
| 54 | + { |
| 55 | + path: () => p.text({ message: 'Where to setup dbschema?', defaultValue: './dbschema', placeholder: './dbschema' }), |
| 56 | + interfaces: () => p.text({ message: 'Generate interfaces?', defaultValue: 'yes', placeholder: 'yes' }), |
| 57 | + queries: () => p.text({ message: 'Generate queries?', defaultValue: 'yes', placeholder: 'yes' }), |
| 58 | + queryBuilder: () => p.text({ message: 'Generate query builder?', defaultValue: 'yes', placeholder: 'yes' }), |
| 59 | + }, |
| 60 | + { |
| 61 | + onCancel: () => { |
| 62 | + p.cancel('Operation cancelled.') |
| 63 | + process.exit(0) |
| 64 | + }, |
| 65 | + }, |
| 66 | + ) |
| 67 | + |
| 68 | + const dbschemaPath = resolveProject(groupData.path) |
| 69 | + |
| 70 | + if (!existsSync(dbschemaPath)) { |
| 71 | + p.log.error(`Your ${chalk.green('dbschema')} directory does not exist, you must run \`${chalk.green('edgedb project init')}\` at least once before running this command.`) |
| 72 | + } |
| 73 | + |
| 74 | + if (groupData.interfaces === 'yes') { |
| 75 | + const spinner = p.spinner() |
| 76 | + |
| 77 | + spinner.start('Generating interfaces...') |
| 78 | + await execa.$`npx @edgedb/generate interfaces --file ${dbschemaPath}/interfaces.ts --force-overwrite` |
| 79 | + spinner.stop('Interfaces generated.') |
| 80 | + } |
| 81 | + |
| 82 | + if (groupData.queries === 'yes') { |
| 83 | + const spinner = p.spinner() |
| 84 | + |
| 85 | + spinner.start('Generating queries...') |
| 86 | + await execa.$`npx @edgedb/generate queries --file ${dbschemaPath}/queries --target=ts --force-overwrite` |
| 87 | + spinner.stop('Queries generated.') |
| 88 | + } |
| 89 | + |
| 90 | + if (groupData.queryBuilder === 'yes') { |
| 91 | + const spinner = p.spinner() |
| 92 | + |
| 93 | + spinner.start('Generating query builder...') |
| 94 | + await execa.$`npx @edgedb/generate edgeql-js --output-dir ${dbschemaPath}/query-builder --force-overwrite --target=ts` |
| 95 | + spinner.stop('Query builder generated.') |
| 96 | + } |
| 97 | + |
| 98 | + p.log.success('Done. Feel free to checkout the next steps on the README') |
| 99 | + |
| 100 | + p.log.success('https://github.com/tahul/nuxt-edgedb#readme') |
| 101 | +} |
| 102 | + |
| 103 | +up() |
0 commit comments