diff --git a/cli/commands/unlink.mjs b/cli/commands/unlink.mjs new file mode 100644 index 00000000..a14061a2 --- /dev/null +++ b/cli/commands/unlink.mjs @@ -0,0 +1,39 @@ +import { consola } from 'consola' +import { colors } from 'consola/utils' +import { isCancel, confirm } from '@clack/prompts' +import { defineCommand, runCommand } from 'citty' +import { fetchUser, projectPath, writeProjectConfig, loadProjectConfig, fetchProject } from '../utils/index.mjs' +import login from './login.mjs' + +export default defineCommand({ + meta: { + name: 'link', + description: 'Unlink a local directory from a NuxtHub project.', + }, + async setup() { + let user = await fetchUser() + if (!user) { + consola.info('Please login to unlink your project.') + await runCommand(login, {}) + user = await fetchUser() + } + let project = await fetchProject() + if (!project) { + consola.warn('This directory is not linked to any NuxtHub project.') + return + } + const shouldUnlink = await confirm({ + message: `Do you want to unlink ${colors.blue(projectPath())} from NuxtHub project ${colors.blue(project.slug)}?`, + initialValue: false + }) + if (!shouldUnlink || isCancel(shouldUnlink)) { + return consola.log('Cancelled.') + } + + const config = loadProjectConfig() + delete config.hub.projectId + writeProjectConfig(config) + + consola.success(`Project \`${project.slug}\` unlinked.`) + }, +}) diff --git a/cli/index.mjs b/cli/index.mjs index 06924c7e..9edd7a66 100755 --- a/cli/index.mjs +++ b/cli/index.mjs @@ -3,6 +3,7 @@ import { defineCommand, runMain } from 'citty' import consola from 'consola' import { colors } from 'consola/utils' import link from './commands/link.mjs' +import unlink from './commands/unlink.mjs' import login from './commands/login.mjs' import logout from './commands/logout.mjs' import whoami from './commands/whoami.mjs' @@ -21,6 +22,7 @@ const main = defineCommand({ subCommands: { deploy, link, + unlink, login, logout, whoami diff --git a/cli/utils/config.mjs b/cli/utils/config.mjs index f61ee987..e46c0fc4 100644 --- a/cli/utils/config.mjs +++ b/cli/utils/config.mjs @@ -1,5 +1,5 @@ import isDocker from 'is-docker' -import { updateUser, readUser, writeUser, read, update } from 'rc9' +import { updateUser, readUser, writeUser, read, update, write } from 'rc9' import { homedir } from 'os' export const INITIAL_CONFIG = loadUserConfig() @@ -20,6 +20,9 @@ export function loadProjectConfig () { export function updateProjectConfig (config) { return update(config, '.nuxtrc') } +export function writeProjectConfig (config) { + return write(config, '.nuxtrc') +} export function isHeadless() { return isDocker() || Boolean(process.env.SSH_CLIENT || process.env.SSH_TTY)