feat(cli): add error prompt & opt init project (#1235)

This commit is contained in:
skyoct 2023-06-10 09:16:51 +08:00 committed by GitHub
parent 5d12068980
commit 1f1f7ba1c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 35 deletions

View File

@ -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 existcreate 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)

View File

@ -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

View File

@ -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'

View File

@ -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)
}
}

View File

@ -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)
}