Skip to content

Commit

Permalink
feat: add upload cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
c0dedance committed Dec 25, 2023
1 parent 6b790df commit 86596ea
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 21 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"format:docs": "prettier --write \"docs/**/*.md\"",
"dev:docs": "r-press dev docs",
"build:docs": "r-press build docs",
"preview": "r-press preview docs"
"preview": "r-press preview docs",
"upload": "r-press upload docs"
},
"bin": {
"r-press": "bin/r-press.js"
Expand Down Expand Up @@ -99,6 +100,7 @@
"@mdx-js/rollup": "2.1.3",
"@vitejs/plugin-react": "^2.2.0",
"acorn": "^8.10.0",
"axios": "^1.6.2",
"cac": "^6.7.14",
"compression": "^1.7.4",
"esbuild": "^0.19.5",
Expand All @@ -110,7 +112,7 @@
"markdown-it": "^14.0.0",
"markdown-it-highlightjs": "^4.0.1",
"mdast-util-mdxjs-esm": "^1.3.0",
"ora": "^7.0.1",
"ora": "^6.1.2",
"polka": "^0.5.2",
"r-press": "link:",
"react": "^18.2.0",
Expand Down
95 changes: 76 additions & 19 deletions pnpm-lock.yaml

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

23 changes: 23 additions & 0 deletions src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import { cac } from 'cac'
import { build } from './build'
import { preview } from './preview'
import { upload } from './upload'
import { version } from '../../package.json'
import { resolveConfig } from './config'

Expand Down Expand Up @@ -46,4 +47,26 @@ cli
}
})

cli
.command(
'upload [root]',
'upload docs to backend and generate the knowledge base.'
)
// .option('-t, --token <token>', 'access token')
// .option('-b, --backend <backend>', 'upload url')
// .option('-i, --include <include>', 'include docs')
// .option('-e, --exclude <exclude>', 'exclude docs')
.action(async (root: string) => {
try {
root = path.resolve(root)
const config = await resolveConfig(root, 'build', 'production')
await upload({
root,
...config.siteData.aiConfig,
})
} catch (e) {
console.log(e)
}
})

cli.parse()
98 changes: 98 additions & 0 deletions src/node/upload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import path from 'node:path'
import glob from 'fast-glob'
import fs from 'fs-extra'
import ora from 'ora'
import axios from 'axios'

import type { AiConfig } from 'shared/types'

const handleError = (spinnies, name, error) => {
let message = ''
if (error && error.response && error.response.data) {
message = error.response.data.error
} else {
message = error.message
}
spinnies.fail(name, { text: `Failed: ${message}`, successColor: 'redBright' })
return
}

const uploadFiles = async (uploadUrl, files) => {
const uploadSpinner = ora({
text: `Uploading files: 0/${files.length}`,
color: 'blue',
}).start()
try {
let uploadedFilesNum = 0

for (const file of files) {
uploadSpinner.text = `Uploading files: ${uploadedFilesNum}/${files.length} ${file.fileName}`

await axios.post(uploadUrl, {
operation: 'add',
path: file.fileName,
content: file.content,
})

uploadedFilesNum += 1
}

uploadSpinner.succeed(`${files.length} files uploaded.`)
} catch (error) {
return handleError(uploadSpinner, 'uploading', error)
}

const genSpinner = ora({
text: 'Generating knowledge base...',
color: 'blue',
}).start()

try {
await axios.post(uploadUrl, {
operation: 'generate',
})
genSpinner.succeed('Knowledge base generated.')
} catch (error) {
return handleError(genSpinner, 'generating', error)
}

console.log('\nDone.')
console.log('Now you can try to Ask AI in you doc site.')
}

const readFilesByFileNames = async (rootPath, fileNames) => {
const results = []
for (const fileName of fileNames) {
const content = await fs.readFile(path.join(rootPath, fileName), 'utf8')
if (content.length !== 0) {
results.push({ fileName, content })
}
}
return results
}

const getFilesContents = async (config: AiConfig) => {
let ignore = []
if (config.exclude) {
ignore = Array.isArray(config.exclude) ? config.exclude : [config.exclude]
}
ignore.push('node_modules/**')

const include = config.include || '**'

const fileNames = await glob(include, { ignore, cwd: config.root })

const files = await readFilesByFileNames(config.root, fileNames)

return files
}
export async function upload(config: AiConfig) {
console.log('Start uploading files to backend.\n')
const uploadUrl = config.server.upload
if (!uploadUrl) {
throw new Error('The parameter `server.upload` is required.')
}

const filesContents = await getFilesContents(config)
await uploadFiles(uploadUrl, filesContents)
}

0 comments on commit 86596ea

Please sign in to comment.