Skip to content

Commit

Permalink
feat: add create project CLI command, and lot of commands alias
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoRCD committed Mar 31, 2024
1 parent e3beb29 commit e9cccfd
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 10 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2024 Hugo Richard
Copyright 2024-present Hugo Richard

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion apps/cli/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2024 Hugo Richard
Copyright 2024-present Hugo Richard

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
41 changes: 41 additions & 0 deletions apps/cli/src/commands/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { defineCommand, runCommand } from 'citty'
import consola from 'consola'
import { createProject } from '../utils/projects.ts'
import link from './link.ts'

export default defineCommand({
meta: {
name: 'create',
description: 'Create a new project',
},
args: {
name: {
type: 'string',
description: 'Project name',
valueHint: 'my-project',
required: true,
alias: 'n',
},
},
async setup(ctx) {
const name = ctx.args.name
consola.start(`Creating project ${ name }...`)
try {
await createProject(name)
consola.success(`Project ${ name } created successfully!`)
const linkProject = await consola.prompt('Do you want to link the project? (y/n)', {
default: 'y',
type: 'confirm',
})
if (linkProject) {
await runCommand(link, { rawArgs: [name] }).then(() => {
consola.success('Project linked successfully!')
}).catch(() => {
consola.error('Failed to link the project')
})
}
} catch (e) {
consola.error('Failed to create project')
}
},
})
3 changes: 2 additions & 1 deletion apps/cli/src/commands/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ export default defineCommand({
description: 'Name of the project to link',
valueHint: 'project-name (case-insensitive)',
default: '',
alias: 'n',
}
},
async run(ctx) {
const name = ctx.args._[0] || ctx.args.name
const name = ctx.args.name
if (name) {
consola.start(`Fetching project ${name}...`)
const project = await getProjectByName(name)
Expand Down
4 changes: 2 additions & 2 deletions apps/cli/src/commands/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export default defineCommand({
name: 'open',
description: 'Open the project in the browser',
},
async setup() {
setup() {
consola.info('Opening the project in the browser...')
const projectId = getProjectId()
if (!projectId) {
consola.error('No project linked run `shelve link` to link a project')
return
}
await open(`https://shelve.hrcd.fr/app/project/${ projectId }`)
open(`https://shelve.hrcd.fr/app/project/${ projectId }`).then(r => r)
},
})
5 changes: 3 additions & 2 deletions apps/cli/src/commands/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default defineCommand({
description: 'Environment to pull from',
valueHint: 'production|prod|preview|development|dev',
default: 'development',
alias: 'e',
},
},
async run(ctx) {
Expand All @@ -22,8 +23,8 @@ export default defineCommand({
consola.error('Project is not linked run `shelve link` to link the project')
return
}
const variables = await getProjectVariable(projectId, ctx.args._[0] || ctx.args.env)
await createEnvFile(variables)
const variables = await getProjectVariable(projectId, ctx.args.env)
createEnvFile(variables)
consola.success('Pulled successfully!')
},
})
3 changes: 2 additions & 1 deletion apps/cli/src/commands/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default defineCommand({
description: 'Environment to push to',
valueHint: 'production|prod|preview|development|dev',
default: 'development',
alias: 'e',
},
},
async run(ctx) {
Expand All @@ -22,7 +23,7 @@ export default defineCommand({
consola.error('Project is not linked run `shelve link` to link the project')
return
}
await pushProjectVariable(projectId, ctx.args._[0] || ctx.args.env)
await pushProjectVariable(projectId, ctx.args.env)
consola.success('Pushed successfully!')
},
})
4 changes: 2 additions & 2 deletions apps/cli/src/commands/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export default defineCommand({
consola.start(`Upgrading from ${version} to the latest version...`)
try {
await execa('npm', ['install', '-g', name], { stdio: 'inherit' })
consola.success(`Successfully upgraded to version ${latestVersion}`)
consola.success(`Successfully upgraded to the latest version`)
} catch (error) {
consola.error('Failed to upgrade the CLI')
consola.error(error)

}
}
},
Expand Down
11 changes: 11 additions & 0 deletions apps/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,25 @@ export const main = defineCommand({
},
subCommands: {
upgrade: () => import('./commands/upgrade').then((r) => r.default),
u: () => import('./commands/upgrade').then((r) => r.default),
create: () => import('./commands/create').then((r) => r.default),
c: () => import('./commands/create').then((r) => r.default),
link: () => import('./commands/link').then((r) => r.default),
l: () => import('./commands/link').then((r) => r.default),
unlink: () => import('./commands/unlink').then((r) => r.default),
ul: () => import('./commands/unlink').then((r) => r.default),
pull: () => import('./commands/pull').then((r) => r.default),
pl: () => import('./commands/pull').then((r) => r.default),
push: () => import('./commands/push').then((r) => r.default),
ps: () => import('./commands/push').then((r) => r.default),
whoami: () => import('./commands/whoami').then((r) => r.default),
w: () => import('./commands/whoami').then((r) => r.default),
login: () => import('./commands/login').then((r) => r.default),
li: () => import('./commands/login').then((r) => r.default),
logout: () => import('./commands/logout').then((r) => r.default),
lo: () => import('./commands/logout').then((r) => r.default),
open: () => import('./commands/open').then((r) => r.default),
o: () => import('./commands/open').then((r) => r.default),
},
async setup() {
await checkForUpdates()
Expand Down
9 changes: 9 additions & 0 deletions apps/cli/src/utils/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,12 @@ export function addDotShelveToGitignore(): void {
fs.writeFileSync('.gitignore', `${gitignore}\n# Shelve config\n.shelve`)
}
}

export async function createProject(name: string): Promise<Project> {
return await $api('/project', {
method: 'POST',
body: {
name,
}
})
}

0 comments on commit e9cccfd

Please sign in to comment.