Skip to content

Commit

Permalink
feat(cli): refactor and impl function actions(#647)
Browse files Browse the repository at this point in the history
* feat(cli): reactor cli and impl function&auth&app

* feat(cli): fix auth error and add emoji
  • Loading branch information
skyoct authored Jan 18, 2023
1 parent 51d73a3 commit d49b15e
Show file tree
Hide file tree
Showing 45 changed files with 2,829 additions and 342 deletions.
5 changes: 5 additions & 0 deletions cli/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"trailingComma": "all",
"semi": false
}
58 changes: 43 additions & 15 deletions cli/package-lock.json

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

3 changes: 2 additions & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"url": "https://github.com/labring/laf/issues"
},
"devDependencies": {
"@types/cli-table2": "^0.2.3",
"@types/mime": "^2.0.3",
"@types/node": "^17.0.31"
},
Expand All @@ -34,6 +33,8 @@
"class-transformer": "^0.5.1",
"cli-table3": "^0.6.3",
"commander": "^9.3.0",
"dayjs": "^1.11.7",
"node-emoji": "^1.11.0",
"reflect-metadata": "^0.1.13",
"typescript": "^4.7.4",
"yaml": "^2.1.3"
Expand Down
93 changes: 93 additions & 0 deletions cli/src/action/application/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { applicationControllerFindAll, applicationControllerFindOne } from "../../api/v1/application"
import * as Table from 'cli-table3';
import { ApplicationConfig, existApplicationConfig, writeApplicationConfig } from "../../config/application"
import * as path from 'node:path'
import * as fs from 'node:fs'
import { FUNCTIONS_DIRECTORY_NAME, GLOBAL_FILE, PACKAGE_FILE, RESPONSE_FILE, TEMPLATE_DIR, TSCONFIG_FILE, TYPE_DIR } from "../../common/constant"
import { ensureDirectory } from "../../util/file"
import { readSystemConfig } from "../../config/system"
import { update as dependencyUpdate } from "../dependency"
import { refreshSecretConfig } from "../../config/secret"
import { getEmoji } from "../../util/print";




export async function list() {
const table = new Table({
head: ['appid', 'name', 'region', 'bundle', 'runtime', 'phase'],
})
const data = await applicationControllerFindAll();
for (let item of data) {
table.push([item.appid, item.name, item.regionName, item.bundleName, item.runtimeName, item.phase])
}
console.log(table.toString());
}

export async function init(appid: string, options: { sync: boolean }) {
if (existApplicationConfig()) {
console.log(`${getEmoji('❌')} The laf.yaml file already exists in the current directory. Please change the directory or delete the laf.yaml file`)
return
}

const data = await applicationControllerFindOne(appid);

const config: ApplicationConfig = {
name: data.name,
appid: data.appid,
regionName: data.regionName,
bundleName: data.bundleName,
runtimeName: data.runtimeName,
createdAt: data.createdAt,
}
// generate application invoke address
const systemConfig = readSystemConfig()
const invokeUrl = systemConfig.remoteServer.replace('api', config.appid)
config.invokeUrl = invokeUrl

writeApplicationConfig(config)

// init function
initFunction()

// init secret
refreshSecretConfig()

if (options.sync) {
// TODO: sync
}
console.log(`${getEmoji('🚀')} application ${data.name} init success`)
console.log(`${getEmoji('👉')} please run 'npm install' install dependencies`)
}

function initFunction() {
// if not exist,create functions directory
ensureDirectory(path.join(process.cwd(), FUNCTIONS_DIRECTORY_NAME))

const typeDir = path.resolve(process.cwd(), TYPE_DIR)
ensureDirectory(typeDir)

// from template dir
const templateDir = path.resolve(__dirname, '../../../', TEMPLATE_DIR)

// generate global.d.ts
const fromGlobalFile = path.resolve(templateDir, GLOBAL_FILE)
const outGlobalFile = path.resolve(typeDir, GLOBAL_FILE)
fs.writeFileSync(outGlobalFile, fs.readFileSync(fromGlobalFile, 'utf-8'))

// generate response.d.ts
const fromResponseFile = path.resolve(templateDir, RESPONSE_FILE)
const outResponseFile = path.resolve(TYPE_DIR, RESPONSE_FILE)
fs.writeFileSync(outResponseFile, fs.readFileSync(fromResponseFile, 'utf-8'))

// generate package.json
const fromPackageFile = path.resolve(templateDir, PACKAGE_FILE)
const outPackageFile = path.resolve(process.cwd(), PACKAGE_FILE)
fs.writeFileSync(outPackageFile, fs.readFileSync(fromPackageFile, 'utf-8'))
dependencyUpdate()

// generate tsconfig.json
const fromTsConfigFile = path.resolve(templateDir, TSCONFIG_FILE)
const outTsConfigFile = path.resolve(process.cwd(), TSCONFIG_FILE)
fs.writeFileSync(outTsConfigFile, fs.readFileSync(fromTsConfigFile, 'utf-8'))
}
36 changes: 36 additions & 0 deletions cli/src/action/auth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { authControllerPat2Token } from "../../api/v1/authentication"
import { Pat2TokenDto } from "../../api/v1/data-contracts"
import { DEFAULT_REMOTE_SERVER, TOKEN_EXPIRE } from "../../common/constant"
import { removeSystemConfig, SystemConfig, writeSystemConfig } from "../../config/system"
import { getEmoji } from "../../util/print"


export async function login(pat, options) {

let systemConfig: SystemConfig = {
remoteServer: DEFAULT_REMOTE_SERVER,
}
writeSystemConfig(systemConfig)

const patDto: Pat2TokenDto = {
pat: pat,
}
const token = await authControllerPat2Token(patDto)
const timestamp = Date.parse(new Date().toString()) / 1000
systemConfig = {
token: token,
tokenExpire: timestamp + TOKEN_EXPIRE,
pat: pat,
remoteServer: DEFAULT_REMOTE_SERVER,
}
if (options.remote) {
systemConfig.remoteServer = options.remote
}
writeSystemConfig(systemConfig)
console.log(`${getEmoji('🎉')} login success`)
}

export async function logout() {
removeSystemConfig()
console.log(`${getEmoji('👋')} logout success`)
}
21 changes: 21 additions & 0 deletions cli/src/action/dependency/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as path from "node:path"
import * as fs from "node:fs"
import { dependencyControllerGetDependencies } from "../../api/v1/application"
import { PACKAGE_FILE } from "../../common/constant"
import { readApplicationConfig } from "../../config/application"


export async function update() {
const appConfig = readApplicationConfig()
const dependencies = await dependencyControllerGetDependencies(appConfig.appid)

// TODO: update dependencies to package.json
const packagePath = path.resolve(process.cwd(), PACKAGE_FILE)
let packageJson = JSON.parse(fs.readFileSync(packagePath, "utf-8"))
const devDependencies = {}
for (let dependency of dependencies) {
devDependencies[dependency.name] = dependency.spec
}
packageJson.devDependencies = devDependencies
fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2))
}
Loading

0 comments on commit d49b15e

Please sign in to comment.