Skip to content

Commit

Permalink
Merge pull request #311 from elsoul/cli
Browse files Browse the repository at this point in the history
update get dirs
  • Loading branch information
POPPIN-FUMI authored Mar 11, 2024
2 parents 4291065 + 66f657b commit e65042d
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 43 deletions.
4 changes: 1 addition & 3 deletions common/runDiscordChangeLog.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { discordChangeLog } from '@skeet-framework/discord-utils'
import dotenv from 'dotenv'

dotenv.config()
require('dotenv').config()

const REPO_NAME = 'elsoul/skeet'

Expand Down
7 changes: 3 additions & 4 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,15 @@
"babel-loader": "9.1.3",
"copyfiles": "2.4.1",
"esbuild": "0.20.1",
"esbuild-plugin-alias-path": "^2.0.2",
"esbuild-plugin-alias-path": "2.0.2",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"nodemon": "3.1.0",
"prettier": "3.2.5",
"ts-loader": "9.5.1",
"tsconfig-paths": "4.2.0",
"tsx": "^4.7.1",
"tsx": "4.7.1",
"typescript": "5.4.2",
"vite": "^5.1.5",
"vite": "5.1.5",
"vite-tsconfig-paths": "4.3.1",
"vitest": "1.3.1"
}
Expand Down
7 changes: 1 addition & 6 deletions packages/cli/src/cli/ai/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ export async function promptUser(
// }

const skeetPrompt = skeetAiPrompt('en')
await chat(
skeetPrompt.context,
skeetPrompt.examples,
userInput.input,
aiOptions.ai,
)
await chat(skeetPrompt.context, skeetPrompt.examples, userInput.input)
await promptUser(aiOptions, logger)
}
19 changes: 16 additions & 3 deletions packages/cli/src/cli/ai/mode/prismaMode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { PRISMA_SCHEMA_PATH } from '@/index'
import chalk from 'chalk'
import { promptUser } from '../ai'
import { spawnSync } from 'child_process'
Expand All @@ -8,6 +7,13 @@ import inquirer from 'inquirer'
import { yesOrNo } from './yesOrNoMode'
import { AiLog } from '../aiLog'
import { SkeetAIOptions } from '..'
import { getSQLs } from '@/lib/files/getSQLs'
import { prismaPrompt } from '../skeetai/prisma/prompt'

type PrismaOptions = {
modelPath: string
input: string
}

export const prismaMode = async (config: SkeetAIOptions, logger: AiLog) => {
const log = logger.text() as SkeetLog
Expand All @@ -23,14 +29,21 @@ export const prismaMode = async (config: SkeetAIOptions, logger: AiLog) => {
log.prismaMode.example2 +
'\n\n' +
chalk.green(log.common.you + ':')
logger.addJson(SkeetRole.AI, inputMessage, SkeetAiMode.Firestore, model)
const answer = await inquirer.prompt([

const answer = await inquirer.prompt<PrismaOptions>([
{
type: 'list',
name: 'modelPath',
message: 'Select Model',
choices: await getSQLs(),
},
{
type: 'input',
name: 'input',
message: inputMessage,
},
])
const firstAiContent = await prismaPrompt(answer.modelPath)

const prismaSchema = (await skeetAi.prisma(answer.input)) as string
console.log(
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/lib/files/getAllApps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync } from '@skeet-framework/utils'
import path from 'path'
import { getDirectoryLastModified } from './getDirectoryLastModified'
import { getFunctions } from './getFunctions'
import { getSQLs } from './getSQLs'
Expand All @@ -10,7 +10,7 @@ export const getAllApps = async () => {
const dirs = (await getFunctions()).map((dir) => `functions/${dir}`)
const sqls = (await getSQLs()).map((dir) => `sql/${dir}`)
dirs.push('webapp')
if (await existsSync('webapp')) {
if (!path.join(process.cwd(), 'webapp')) {
await mkdir('webapp', { recursive: true })
}
dirs.push(...sqls)
Expand Down
20 changes: 14 additions & 6 deletions packages/cli/src/lib/files/getFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { mkdir, readdir } from 'fs/promises'
import path from 'path'
import { sortDirsByLastModified } from './sortDirsByLastModified'

export const getFunctions = async () => {
const functionsDir = path.join(process.cwd(), 'functions')
if (!functionsDir) {
await mkdir(functionsDir, { recursive: true })
try {
const dir = path.join(process.cwd(), 'functions')
if (!dir) {
await mkdir(dir, { recursive: true })
}
const sqlDirs = (await readdir(dir)).map((dirName) =>
path.join(dir, dirName),
)
const result = await sortDirsByLastModified(sqlDirs)
return result
} catch (error) {
console.log('getFunctions:', error)
return ['']
}
console.log(functionsDir)
const functions = await readdir(functionsDir)
return functions
}
20 changes: 14 additions & 6 deletions packages/cli/src/lib/files/getSQLs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { mkdir, readdir } from 'fs/promises'
import path from 'path'
import { sortDirsByLastModified } from './sortDirsByLastModified'

export const getSQLs = async () => {
const dir = path.join(process.cwd(), 'sql')
if (!dir) {
await mkdir(dir, { recursive: true })
try {
const dir = path.join(process.cwd(), 'sql')
if (!dir) {
await mkdir(dir, { recursive: true })
}
const sqlDirs = (await readdir(dir)).map((dirName) =>
path.join(dir, dirName),
)
const result = await sortDirsByLastModified(sqlDirs)
return result
} catch (error) {
console.log('Error getting SQL directories:', error)
return ['']
}
console.log(dir)
const apps = await readdir(dir)
return apps
}
14 changes: 14 additions & 0 deletions packages/cli/src/lib/files/sortDirsByLastModified.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getDirectoryLastModified } from './getDirectoryLastModified'

export const sortDirsByLastModified = async (dirs: string[]) => {
const dirsWithStats = []
for await (const dir of dirs) {
const lastModified = await getDirectoryLastModified(dir)
dirsWithStats.push({ name: dir, mtime: lastModified })
}

// Sort by last modified date
dirsWithStats.sort((a, b) => b.mtime.getTime() - a.mtime.getTime())
const dirNames = dirsWithStats.map((dir) => dir.name.split('/').pop())
return dirNames as string[]
}
1 change: 1 addition & 0 deletions packages/cli/tests/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('Method: getFunctions/getSQLs', () => {

it('should return SQL Apps array', async () => {
const functions = await getSQLs()
console.log(functions)

// return SQL App Name array
expect(functions).toBeInstanceOf(Array)
Expand Down
17 changes: 4 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e65042d

Please sign in to comment.