Skip to content

Commit

Permalink
Merge pull request #132 from elsoul/backend
Browse files Browse the repository at this point in the history
add skeet create --backend option
  • Loading branch information
POPPIN-FUMI authored Sep 18, 2023
2 parents 54db437 + 8e06494 commit c32274c
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 224 deletions.
427 changes: 216 additions & 211 deletions dist/index.js

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions src/cli/create/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import inquirer from 'inquirer'
import { questionList } from '@/cli/init/questionList'
import { existsSync, readFileSync, writeFileSync } from 'fs'
import { DEFAULT_FUNCTION_NAME } from '@/index'
import { SkeetTemplate } from '@/types/skeetTypes'
import { SkeetTemplate, SkeetTemplateBackend } from '@/types/skeetTypes'
import { dbGen } from '../sub/db/dbGen'
import { dbDeploy } from '../sub/db/dbDeploy'

Expand Down Expand Up @@ -88,7 +88,10 @@ export const generateInitFiles = async (appName: string, template: string) => {
if (template === SkeetTemplate.SolanaFirestore) {
await initAppJson(appName)
}
if (template === SkeetTemplate.NextJsGraphQL) {
if (
template === SkeetTemplate.NextJsGraphQL ||
template === SkeetTemplateBackend.GraphQL
) {
const env = await fileDataOf.graphqlEnv(appName)
writeFileSync(env.filePath, env.body)

Expand All @@ -99,7 +102,9 @@ export const generateInitFiles = async (appName: string, template: string) => {
await dbDeploy(false, appGraphqlPath)
}

await addAppNameToSkeetOptions(appName, DEFAULT_FUNCTION_NAME)
if (template !== 'Backend Only - GraphQL') {
await addAppNameToSkeetOptions(appName, DEFAULT_FUNCTION_NAME)
}

const eslintrcJson = await fileDataOf.eslintrcJson(appName, template)
writeFileSync(
Expand Down
50 changes: 50 additions & 0 deletions src/cli/create/createBackend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
BACKEND_FUNCTIONS_REPO_URL,
BACKEND_GRAPHQL_REPO_URL,
FUNCTIONS_PATH,
Logger,
execSyncCmd,
} from '@/lib'
import { generateInitFiles } from './create'
import { sleep } from '@/utils/time'
import { SkeetTemplateBackend } from '@/types/skeetTypes'
import { DEFAULT_FUNCTION_NAME } from '@/index'
import { existsSync } from 'fs'
import { questionList } from '../init/questionList'
import inquirer from 'inquirer'

export const createBackend = async (appName: string) => {
const { template } = await inquirer.prompt<{ template: string }>(
questionList.backendTemplateQuestions
)
const appDir = './' + appName
if (existsSync(appDir)) {
Logger.error(`Directory ${appName} already exists.`)
process.exit(0)
}
let gitCloneCmd = null
let backendRootPath = ''
if (template === SkeetTemplateBackend.Firestore) {
gitCloneCmd = ['git', 'clone', BACKEND_FUNCTIONS_REPO_URL, appName]
backendRootPath = `${appDir}/${FUNCTIONS_PATH}/${DEFAULT_FUNCTION_NAME}`
} else {
gitCloneCmd = ['git', 'clone', BACKEND_GRAPHQL_REPO_URL, appName]
backendRootPath = `${appDir}/graphql`
}

await execSyncCmd(gitCloneCmd)
const yarnApiCmd = ['yarn']
await execSyncCmd(yarnApiCmd, appDir)
await execSyncCmd(yarnApiCmd, backendRootPath)
const rmDefaultGit = ['rm', '-rf', '.git']
await execSyncCmd(rmDefaultGit, appDir)
await sleep(1000)

await generateInitFiles(appName, template)
Logger.skeetAA()
Logger.welcomText(appName, template)
const nmb = Math.floor(Math.random() * 4 + 1)
if (nmb === 4) {
Logger.cmText()
}
}
10 changes: 8 additions & 2 deletions src/cli/create/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { program } from '@/index'
import { create } from './create'
import { createBackend } from './createBackend'

export const createCommands = async () => {
program
.command('create')
.argument('<appName>', 'Name of the app')
.description('Create Skeet Framework App')
.action(async (appName: string) => {
await create(appName)
.option('-b, --backend', 'Create Backend Only', false)
.action(async (appName: string, options: { backend: boolean }) => {
if (options.backend) {
await createBackend(appName)
} else {
await create(appName)
}
})
}
21 changes: 20 additions & 1 deletion src/cli/init/questionList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import inquirer from 'inquirer'
import { regionList } from './regionList'
import chalk from 'chalk'
import { Logger } from '@/lib'
import { SkeetTemplate } from '@/types/skeetTypes'
import { SkeetTemplate, SkeetTemplateBackend } from '@/types/skeetTypes'

export module questionList {
export const requireRepoName = (value: string) => {
Expand Down Expand Up @@ -112,6 +112,25 @@ export module questionList {
},
]

export const backendTemplateQuestions = [
{
type: 'list',
message: 'Select Template of Skeet',
name: 'template',
choices: [
new inquirer.Separator(' Templates '),
{ name: SkeetTemplateBackend.GraphQL },
{ name: SkeetTemplateBackend.Firestore },
],
validate(answer: string) {
if (answer.length < 1) {
return 'You must choose at least one template.'
}
return true
},
},
]

export const needDomainQuestions = [
{
type: 'list',
Expand Down
6 changes: 6 additions & 0 deletions src/cli/sub/docker/dockerBuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { spawnSync } from 'child_process'

export const dockerBuild = async () => {
const cmd = `yarn docker:build`
spawnSync(cmd, { shell: true, stdio: 'inherit' })
}
6 changes: 6 additions & 0 deletions src/cli/sub/docker/dockerRm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { spawnSync } from 'child_process'

export const dockerRm = async () => {
const cmd = `yarn docker:rm`
spawnSync(cmd, { shell: true, stdio: 'inherit' })
}
6 changes: 6 additions & 0 deletions src/cli/sub/docker/dockerRun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { spawnSync } from 'child_process'

export const dockerRun = async () => {
const cmd = `yarn docker:run`
spawnSync(cmd, { shell: true, stdio: 'inherit' })
}
24 changes: 24 additions & 0 deletions src/cli/sub/docker/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { program } from '@/index'
import { runPsql } from './runPsql'
import { dockerLogin } from './dockerLogin'
import { dockerRun } from './dockerRun'
import { dockerBuild } from './dockerBuild'
import { dockerRm } from './dockerRm'
export * from './runPsql'
export * from './dockerLogin'
export * from './buildContainer'
Expand All @@ -22,4 +25,25 @@ export const dockerSubCommands = async () => {
.action(async () => {
await dockerLogin()
})

docker
.command('run')
.description('Run Skeet GraphQL Backend Container')
.action(async () => {
await dockerRun()
})

docker
.command('build')
.description('Build Skeet GraphQL Backend Container')
.action(async () => {
await dockerBuild()
})

docker
.command('rm')
.description('Remove Skeet GraphQL Backend Container')
.action(async () => {
await dockerRm()
})
}
10 changes: 5 additions & 5 deletions src/cli/yarn/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ export const yarn = async (
isDev: boolean = false
) => {
const functions = getFunctions()
const functionsArray: Array<{ [key: string]: string }> = [
{ name: 'webapp' },
{ name: 'root' },
]
let functionsArray: Array<{ [key: string]: string }> = []
const { app } = await importConfig()
if (app?.template?.includes('GraphQL')) {
functionsArray.push({ name: 'graphql' })
}

for await (const functionName of functions) {
functionsArray.push({ name: functionName })
}
if (!app?.template?.includes('Backend')) {
functionsArray.push({ name: 'webapp' }, { name: 'root' })
}

inquirer
.prompt([
{
Expand Down
5 changes: 4 additions & 1 deletion src/lib/files/getSkeetConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createHash } from 'crypto'
import { execSync } from 'child_process'
import { GRAPHQL_ENV_PRODUCTION_PATH } from '@/index'
import { readFileSync } from 'fs'
Expand All @@ -13,6 +12,10 @@ export const FUNCTIONS_REPO_URL = 'https://github.com/elsoul/skeet-functions'
export const APP_REPO_URL = 'https://github.com/elsoul/skeet-app'
export const GRAPHQL_REPO_PATH = 'https://github.com/elsoul/skeet-graphql'
export const NEXT_REPO_URL = 'https://github.com/elsoul/skeet-next'
export const BACKEND_GRAPHQL_REPO_URL =
'https://github.com/elsoul/skeet-graphql-only'
export const BACKEND_FUNCTIONS_REPO_URL =
'https://github.com/elsoul/skeet-functions-only'
export const SOLANA_REPO_URL =
'https://github.com/elsoul/skeet-solana-mobile-stack'
export const FRONT_APP_PATH = './src'
Expand Down
13 changes: 12 additions & 1 deletion src/lib/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chalk from 'chalk'
import { Spinner } from 'cli-spinner'
import { spinnerPattern } from './spinnerList'
import { questionList } from '@/cli/init/questionList'
import { SkeetTemplateBackend } from '@/types/skeetTypes'

export module Logger {
export const successHex = chalk.hex('#39A845')
Expand Down Expand Up @@ -95,8 +96,18 @@ Go To : http://127.0.0.1:4000/`
$ cd ${appName}
$ skeet s
Go To : http://127.0.0.1:4000/`
const backendGraphqlText = `
$ cd ${appName}
$ skeet docker psql
$ skeet s
Go To : http://localhost:3000/graphql
`
console.log(title)
console.log(greyHex(text))
if (template === SkeetTemplateBackend.GraphQL) {
console.log(backendGraphqlText)
} else {
console.log(greyHex(text))
}
}

export const cmText = () => {
Expand Down
5 changes: 5 additions & 0 deletions src/types/skeetTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export enum SkeetTemplate {
SolanaFirestore = 'Solana Mobile Stack (Expo) + Web (Next.js) - Firestore',
}

export enum SkeetTemplateBackend {
GraphQL = 'Backend Only - GraphQL',
Firestore = 'Backend Only - Firestore',
}

export type SkeetOptions = {
name: string
projectId: string
Expand Down

0 comments on commit c32274c

Please sign in to comment.