Skip to content

Commit

Permalink
chore: Work of project create command
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed Jun 4, 2024
1 parent 56e9171 commit 9e0c07f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 205 deletions.
2 changes: 0 additions & 2 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { Command } from 'commander';
import { configCommand } from './src/commands/config';
import { initCommand } from './src/commands/init';
import { cleanCommand } from './src/commands/clean';
import { environmentCommand } from './src/commands/environment';
import { composeCommand } from './src/commands/compose';
Expand All @@ -26,7 +25,6 @@ cli
`))
.addCommand(projectCommand())
.addCommand(configCommand())
// .addCommand(initCommand())
// .addCommand(gitCommand())
// .addCommand(cleanCommand())
// .addCommand(environmentCommand())
Expand Down
14 changes: 0 additions & 14 deletions src/commands/init/cli.ts

This file was deleted.

163 changes: 0 additions & 163 deletions src/commands/init/index.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/commands/init/options.ts

This file was deleted.

72 changes: 72 additions & 0 deletions src/commands/project/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { join } from 'node:path'
import { confirm, input } from '@inquirer/prompts'
import { Command } from 'commander'
import { CONFIG_PATH } from '../../config/cli.config'
import { projectConfig } from '../../config/project.config'
import logger from '../../utils/log'
import { createPath } from '../../utils/file-system'

export function createProjectCommand() {
return new Command()
.name('create')
.alias('new')
.summary('Create a new project in DEMS')
.option('-n, --name', 'Project name')
.option('-r, --repo [repos...]', 'Project repositories')
.option('-d, --dockerfile', 'Dockerfile used for local services')
.option('-g, --git-ref', 'Git ref to clone repositories')
.action(async (options) => {
const newProject = projectConfig.default()

if (options.projectName) {
newProject.projectName = options.projectName
} else {
newProject.projectName = await input({
message: 'What is the name of the project?',
})
}

newProject.projectRootPath = join(CONFIG_PATH, newProject.projectName)
newProject.configFile = join(newProject.projectRootPath, 'config.json')

if (options.gitOrg) {
newProject.git.org = options.gitOrg
} else {
newProject.git.org = await input({
message: 'What is the GitHub owner (org/user)?',
})
}

newProject.repositories = {}
if (options.repos) {
} else {
while (true) {
const repo = await input({
message: 'What is the name of the repository?',
})
newProject.repositories[repo] =
`git@github.com:${newProject.git.org}/${repo}.git`
if (
!(await confirm({ message: 'Do you want to add another one?' }))
) {
break
}
}
}

if (options.dockerfile) newProject.dockerfile = options.dockerfile
if (options.gitRef) newProject.git.defaultRef = options.gitRef

console.log(JSON.stringify(newProject, null, 2))
let confirmProject = false
confirmProject = await confirm({
message: 'Proceed to create project with above config?',
})

if (confirmProject) {
createPath({ path: newProject.projectRootPath })
projectConfig.save(newProject)
logger.info('New project created successfully.')
}
})
}
2 changes: 2 additions & 0 deletions src/commands/project/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Command } from 'commander'
import { activeProjectCommand } from './active'
import { projectConfigCommand } from './config'
import { removeProjectCommand } from './config/remove'
import { createProjectCommand } from './create'
import { setActiveProjectCommand } from './set'

export function projectCommand() {
Expand All @@ -11,6 +12,7 @@ export function projectCommand() {
.description('Manage the current project state of DEMS')
.addCommand(activeProjectCommand())
.addCommand(setActiveProjectCommand())
.addCommand(createProjectCommand())
.addCommand(projectConfigCommand())
.addCommand(removeProjectCommand())
return command
Expand Down
8 changes: 4 additions & 4 deletions src/config/project.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const PROJECT_CONFIG_FILE = join(PROJECT_CONFIG_ROOT, 'config.json')

interface ProjectConfigSpec {
// The project name determines how docker compose services will be prefixed,
// thus maintaining complete isolation with other services deployed locally.
// thus maintaining compslete isolation with other services deployed locally.
// It also servers as a unique reference across DEMS tasks.
projectName: string

Expand Down Expand Up @@ -57,7 +57,7 @@ export const projectConfig = {
dockerfile: 'Dockerfile',
repositories: {
'demo-api': join(homedir(), 'repos', 'demo', 'demo-api'),
'demo-webapp': join(homedir(), 'repos', 'demo', 'demo-api'),
'demo-webapp': join(homedir(), 'repos', 'demo', 'demo-web'),
},
git: {
org: 'gbh-tech',
Expand Down Expand Up @@ -85,10 +85,10 @@ export const projectConfig = {
},

save(config: ProjectConfigSpec) {
createPath({ path: PROJECT_CONFIG_ROOT })
createPath({ path: config.projectRootPath })
createFile({
file: `${config.projectRootPath}/config.json`,
content: JSON.stringify(config),
content: JSON.stringify(config, null, 2),
})
},

Expand Down

0 comments on commit 9e0c07f

Please sign in to comment.