mirror of
https://github.com/labring/laf.git
synced 2026-02-01 16:57:03 +00:00
feat(cli): add error prompt & opt init project (#1235)
This commit is contained in:
parent
5d12068980
commit
1f1f7ba1c4
@ -9,7 +9,7 @@ import { AppSchema } from '../../schema/app'
|
||||
|
||||
import {
|
||||
DEBUG_TOKEN_EXPIRE,
|
||||
FUNCTION_SCHEMA_DIRCTORY,
|
||||
FUNCTION_SCHEMA_DIRECTORY,
|
||||
GITIGNORE_FILE,
|
||||
GLOBAL_FILE,
|
||||
PACKAGE_FILE,
|
||||
@ -60,25 +60,13 @@ export async function init(appid: string, options: { sync: boolean }) {
|
||||
console.log(
|
||||
`${getEmoji(
|
||||
'❌',
|
||||
)} The laf.yaml file already exists in the current directory. Please change the directory or delete the laf.yaml file`,
|
||||
)} The .app.yaml file already exists in the current directory. Please change the directory or delete the .app.yaml file`,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
const app = await applicationControllerFindOne(appid)
|
||||
|
||||
// init project schema
|
||||
if (!ProjectSchema.exist()) {
|
||||
const projectSchema: ProjectSchema = {
|
||||
version: '1.0.0',
|
||||
name: app.name,
|
||||
spec: {
|
||||
runtime: app.runtime.name,
|
||||
},
|
||||
}
|
||||
ProjectSchema.write(projectSchema)
|
||||
}
|
||||
|
||||
// init app schema
|
||||
let timestamp = Date.parse(new Date().toString()) / 1000
|
||||
const appSchema: AppSchema = {
|
||||
@ -99,11 +87,23 @@ export async function init(appid: string, options: { sync: boolean }) {
|
||||
}
|
||||
AppSchema.write(appSchema)
|
||||
|
||||
// init function
|
||||
initFunction()
|
||||
if (!ProjectSchema.exist()) {
|
||||
// init project schema
|
||||
const projectSchema: ProjectSchema = {
|
||||
version: '1.0.0',
|
||||
name: app.name,
|
||||
spec: {
|
||||
runtime: app.runtime.name,
|
||||
},
|
||||
}
|
||||
ProjectSchema.write(projectSchema)
|
||||
|
||||
// init policy
|
||||
initPolicy()
|
||||
// init function
|
||||
initFunction()
|
||||
|
||||
// init policy
|
||||
initPolicy()
|
||||
}
|
||||
|
||||
if (options.sync) {
|
||||
// pull dependencies
|
||||
@ -118,7 +118,7 @@ export async function init(appid: string, options: { sync: boolean }) {
|
||||
|
||||
function initFunction() {
|
||||
// if not exist,create functions directory
|
||||
ensureDirectory(path.join(process.cwd(), FUNCTION_SCHEMA_DIRCTORY))
|
||||
ensureDirectory(path.join(process.cwd(), FUNCTION_SCHEMA_DIRECTORY))
|
||||
|
||||
const typeDir = path.resolve(process.cwd(), TYPE_DIR)
|
||||
ensureDirectory(typeDir)
|
||||
|
||||
@ -16,7 +16,7 @@ import { invokeFunction } from '../../api/debug'
|
||||
import { exist, remove } from '../../util/file'
|
||||
import { getEmoji } from '../../util/print'
|
||||
import { getAppPath } from '../../util/sys'
|
||||
import { FUNCTION_SCHEMA_DIRCTORY } from '../../common/constant'
|
||||
import { FUNCTION_SCHEMA_DIRECTORY } from '../../common/constant'
|
||||
import { confirm } from '../../common/prompts'
|
||||
import { AppSchema } from '../../schema/app'
|
||||
import { FunctionSchema } from '../../schema/function'
|
||||
@ -261,7 +261,7 @@ async function printLog(appid: string, requestId: string) {
|
||||
}
|
||||
|
||||
function getLocalFuncs() {
|
||||
const funcDir = path.join(getAppPath(), FUNCTION_SCHEMA_DIRCTORY)
|
||||
const funcDir = path.join(getAppPath(), FUNCTION_SCHEMA_DIRECTORY)
|
||||
const files = fs.readdirSync(funcDir)
|
||||
const funcs = files.filter((file) => file.endsWith('.ts')).map((file) => file.replace('.ts', ''))
|
||||
return funcs
|
||||
|
||||
@ -23,7 +23,7 @@ export const GITIGNORE_FILE = 'gitignore'
|
||||
export const APP_SCHEMA_NAME = '.app.yaml'
|
||||
export const PROJECT_SCHEMA_NAME = 'laf.yaml'
|
||||
export const USER_SCHEMA_NAME = 'user.yaml'
|
||||
export const FUNCTION_SCHEMA_DIRCTORY = 'functions'
|
||||
export const FUNCTION_SCHEMA_DIRECTORY = 'functions'
|
||||
export const FUNCTION_SCHEMA_SUFFIX = '.yaml'
|
||||
export const DEPLOY_SCHEMA_NAME = 'deploy.yaml'
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import * as path from 'path'
|
||||
import { FUNCTION_SCHEMA_DIRCTORY, FUNCTION_SCHEMA_SUFFIX } from '../common/constant'
|
||||
import { FUNCTION_SCHEMA_DIRECTORY, FUNCTION_SCHEMA_SUFFIX } from '../common/constant'
|
||||
import { exist, loadYamlFile, remove, writeYamlFile } from '../util/file'
|
||||
|
||||
export class FunctionSchema {
|
||||
@ -9,22 +9,22 @@ export class FunctionSchema {
|
||||
methods: string[]
|
||||
|
||||
static read(name: string): FunctionSchema {
|
||||
const funcConfigPath = path.join(process.cwd(), FUNCTION_SCHEMA_DIRCTORY, name + FUNCTION_SCHEMA_SUFFIX)
|
||||
const funcConfigPath = path.join(process.cwd(), FUNCTION_SCHEMA_DIRECTORY, name + FUNCTION_SCHEMA_SUFFIX)
|
||||
return loadYamlFile(funcConfigPath)
|
||||
}
|
||||
|
||||
static write(name: string, schema: FunctionSchema): void {
|
||||
const funcConfigPath = path.join(process.cwd(), FUNCTION_SCHEMA_DIRCTORY, name + FUNCTION_SCHEMA_SUFFIX)
|
||||
const funcConfigPath = path.join(process.cwd(), FUNCTION_SCHEMA_DIRECTORY, name + FUNCTION_SCHEMA_SUFFIX)
|
||||
return writeYamlFile(funcConfigPath, schema)
|
||||
}
|
||||
|
||||
static exist(name: string): boolean {
|
||||
const funcConfigPath = path.join(process.cwd(), FUNCTION_SCHEMA_DIRCTORY, name + FUNCTION_SCHEMA_SUFFIX)
|
||||
const funcConfigPath = path.join(process.cwd(), FUNCTION_SCHEMA_DIRECTORY, name + FUNCTION_SCHEMA_SUFFIX)
|
||||
return exist(funcConfigPath)
|
||||
}
|
||||
|
||||
static delete(name: string) {
|
||||
const funcConfigPath = path.join(process.cwd(), FUNCTION_SCHEMA_DIRCTORY, name + FUNCTION_SCHEMA_SUFFIX)
|
||||
const funcConfigPath = path.join(process.cwd(), FUNCTION_SCHEMA_DIRECTORY, name + FUNCTION_SCHEMA_SUFFIX)
|
||||
remove(funcConfigPath)
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,21 +61,21 @@ request.interceptors.response.use(
|
||||
process.exit(1)
|
||||
} else {
|
||||
// handle error code
|
||||
const { statusCode, data } = error.response.data
|
||||
if (statusCode === 400) {
|
||||
const { status, data } = error.response
|
||||
if (status === 400) {
|
||||
console.log('Bad request!')
|
||||
console.log(data.message)
|
||||
process.exit(1)
|
||||
} else if (statusCode === 401) {
|
||||
console.log('please first login')
|
||||
} else if (status === 401) {
|
||||
console.log('User not logged in or expired, please log in again')
|
||||
process.exit(1)
|
||||
} else if (statusCode == 403) {
|
||||
console.log('Forbidden resource!')
|
||||
} else if (status == 403) {
|
||||
console.log('Unauthorized resource request')
|
||||
process.exit(1)
|
||||
} else if (statusCode === 500) {
|
||||
} else if (status === 500) {
|
||||
console.log('Internal server error!')
|
||||
process.exit(1)
|
||||
} else if (statusCode === 503) {
|
||||
} else if (status === 503) {
|
||||
console.log('The server is abnormal, please contact the administrator!')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user