-
-
Notifications
You must be signed in to change notification settings - Fork 676
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): refactor and impl function actions(#647)
* feat(cli): reactor cli and impl function&auth&app * feat(cli): fix auth error and add emoji
- Loading branch information
Showing
45 changed files
with
2,829 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"singleQuote": true, | ||
"trailingComma": "all", | ||
"semi": false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Oops, something went wrong.