Skip to content

Commit 56875dd

Browse files
author
Caleb Barnes
committed
cleanup
1 parent aefd8b7 commit 56875dd

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

src/commands/database/database.ts

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { OptionValues } from 'commander'
44
import BaseCommand from '../base-command.js'
55

66
import openBrowser from '../../utils/open-browser.js'
7-
import { getExtension, getExtensionInstallations, installExtension } from './utils.js'
7+
import { carefullyWriteFile, getExtension, getExtensionInstallations, installExtension } from './utils.js'
88
import { getToken } from '../../utils/command-helpers.js'
99
import inquirer from 'inquirer'
1010
import { NetlifyAPI } from 'netlify'
@@ -69,25 +69,24 @@ const init = async (_options: OptionValues, command: BaseCommand) => {
6969
})
7070
}
7171

72+
if (!command.netlify.api.accessToken) {
73+
throw new Error(`No access token found, please login with netlify login`)
74+
}
75+
7276
let site: Awaited<ReturnType<typeof command.netlify.api.getSite>>
7377
try {
7478
// @ts-expect-error -- feature_flags is not in the types
7579
site = await command.netlify.api.getSite({ siteId: command.siteId, feature_flags: 'cli' })
7680
} catch (e) {
77-
console.error(`Error getting site, make sure you are logged in with netlify login`, e)
78-
return
81+
throw new Error(`Error getting site, make sure you are logged in with netlify login`, {
82+
cause: e,
83+
})
7984
}
8085
if (!site.account_id) {
81-
console.error(`Error getting site, make sure you are logged in with netlify login`)
82-
return
83-
}
84-
if (!command.netlify.api.accessToken) {
85-
console.error(`You must be logged in with netlify login to initialize a database.`)
86-
return
86+
throw new Error(`No account id found for site ${command.siteId}`)
8787
}
8888

8989
const netlifyToken = command.netlify.api.accessToken.replace('Bearer ', '')
90-
9190
const extension = await getExtension({
9291
accountId: site.account_id,
9392
token: netlifyToken,
@@ -132,14 +131,13 @@ const init = async (_options: OptionValues, command: BaseCommand) => {
132131
})
133132

134133
if (siteEnv.key === 'NETLIFY_DATABASE_URL') {
135-
console.error(`Database already initialized for site: ${command.siteId}, skipping.`)
136-
return
134+
throw new Error(`Database already initialized for site: ${command.siteId}`)
137135
}
138136
} catch {
139137
// no op, env var does not exist, so we just continue
140138
}
141139

142-
console.log('Initializing a new database for site:', command.siteId)
140+
console.log('Initializing a new database for site: ', command.siteId)
143141

144142
const initEndpoint = new URL(
145143
'/cli-db-init',
@@ -276,20 +274,3 @@ export const db = drizzle({
276274
schema
277275
});
278276
`
279-
280-
const carefullyWriteFile = async (filePath: string, data: string) => {
281-
if (fs.existsSync(filePath)) {
282-
const answers = await inquirer.prompt([
283-
{
284-
type: 'confirm',
285-
name: 'overwrite',
286-
message: `Overwrite existing ${path.basename(filePath)}?`,
287-
},
288-
])
289-
if (answers.overwrite) {
290-
fs.writeFileSync(filePath, data)
291-
}
292-
} else {
293-
fs.writeFileSync(filePath, data)
294-
}
295-
}

src/commands/database/utils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import fs from 'fs'
2+
import inquirer from 'inquirer'
3+
import path from 'path'
4+
15
const JIGSAW_URL = 'https://jigsaw.services-prod.nsvcs.net'
26

37
export const getExtensionInstallations = async ({
@@ -81,3 +85,20 @@ export const installExtension = async ({
8185

8286
return installExtensionData
8387
}
88+
89+
export const carefullyWriteFile = async (filePath: string, data: string) => {
90+
if (fs.existsSync(filePath)) {
91+
const answers = await inquirer.prompt([
92+
{
93+
type: 'confirm',
94+
name: 'overwrite',
95+
message: `Overwrite existing ${path.basename(filePath)}?`,
96+
},
97+
])
98+
if (answers.overwrite) {
99+
fs.writeFileSync(filePath, data)
100+
}
101+
} else {
102+
fs.writeFileSync(filePath, data)
103+
}
104+
}

0 commit comments

Comments
 (0)