Skip to content

Commit

Permalink
fix(cli): fix path sep error in win, add docs, fix code styles
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jul 29, 2022
1 parent c41c1a2 commit 5c944e8
Show file tree
Hide file tree
Showing 25 changed files with 75 additions and 215 deletions.
16 changes: 13 additions & 3 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ npm i laf-cli -g

## Usage

#### 1. Generate a env file template for laf-cli: `.env`
### 1. login your account

```bash
laf init --access-key xxxxx --access-secret xxxxxx --bucket xxxxxx
laf login -u maslow -p password -r https://console.lafyun.com
```

#### 2. run `laf-cli sync ./dist` command to sync files to OSS bucket
### 2. show your applications list

```bash
laf list
```

### 3. init in your local project

```bash
laf init -s YOUR_APPID
```

## Upgrade laf-cli

Expand Down
26 changes: 11 additions & 15 deletions packages/cli/src/actions/app.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
import * as Table from 'cli-table2'

import {appList} from '../api/apps'
import * as Table from 'cli-table2'
import { appList } from '../api/apps'

/**
* apps list
* @returns
*/

export async function handleAppListCommand() {

// get list
const response = await appList()

//init table
const table = new Table({
head: ['appid', 'name','status'],
});
head: ['appid', 'name', 'status'],
})

// user create app
if(response.data.created){
if (response.data.created) {
response.data.created.forEach(app => {
table.push([app.appid,app.name,app.status])
});
table.push([app.appid, app.name, app.status])
})
}

// user join app
if(response.data.joined){
if (response.data.joined) {
response.data.joined.forEach(app => {
table.push([app.appid,app.name,app.status])
});
table.push([app.appid, app.name, app.status])
})
}

// show table
console.log(table.toString())

}
7 changes: 1 addition & 6 deletions packages/cli/src/actions/function-invoke.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { PROJECT_DIR } from '../utils/constants'

import * as path from 'node:path'
import * as fs from 'node:fs'
import { compileTs2js } from '../utils/util-lang'

import { PROJECT_DIR } from '../utils/constants'
import { FUNCTIONS_DIR, FUNCTIONS_FILE } from '../utils/constants'

import { debugFunction } from '../api/functions'

/**
Expand All @@ -15,7 +12,6 @@ import { debugFunction } from '../api/functions'
* @param {object} param
* @returns
*/

export async function handleInvokeFunctionCommand(appid: string, functionName: string, param: object) {

// get local code
Expand All @@ -35,5 +31,4 @@ export async function handleInvokeFunctionCommand(appid: string, functionName: s

const res = await debugFunction(functionName, obj)
console.log(res)

}
3 changes: 1 addition & 2 deletions packages/cli/src/actions/function-pull-list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { pullFunction } from "../api/functions"
import * as path from 'node:path'

import { pullFunction } from "../api/functions"
import { FUNCTIONS_DIR, PROJECT_DIR } from '../utils/constants'
import { ensureDirectory } from '../utils/util'
import { getLocalList, getRemoteList } from "../utils/function-lists"
Expand Down
4 changes: 1 addition & 3 deletions packages/cli/src/actions/function-pull-one.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { pullFunction } from "../api/functions"
import * as path from 'node:path'
import * as fs from 'node:fs'
import { FUNCTIONS_DIR, PROJECT_DIR } from '../utils/constants'
import { ensureDirectory } from '../utils/util'

import { pullFunction } from "../api/functions"
import { createfn, updatefn } from "../utils/function-pull"


Expand Down Expand Up @@ -31,5 +30,4 @@ export async function handlePullOneCommand(appid: string, functionName: string)
createfn(response.data[0])
}
}

}
10 changes: 1 addition & 9 deletions packages/cli/src/actions/function-push-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,22 @@ export async function handlePushListCommand(appid: string, options: any) {

// functions dir
const functionsDir = path.resolve(PROJECT_DIR, FUNCTIONS_DIR)

ensureDirectory(functionsDir)

const response = await pullFunction(appid, '')


if (!response.data) {
return false
}

const remoteList = getRemoteList(response.data)

const localList = await getLocalList(functionsDir)
const localList = getLocalList(functionsDir)

// create list
const createList = getCreateList(remoteList, localList)
if (createList.length > 0) {
createList.forEach(async (item) => {
await createRemoteFn(appid, item.key)
})

}

//update list
Expand All @@ -45,7 +40,6 @@ export async function handlePushListCommand(appid: string, options: any) {
updateList.forEach(async (item2) => {
await updateRemoteFn(appid, item2.key)
})

}

// delete list
Expand All @@ -56,7 +50,5 @@ export async function handlePushListCommand(appid: string, options: any) {
await deleteRemoteFn(appid, item3.value._id)
})
}

}

}
13 changes: 0 additions & 13 deletions packages/cli/src/actions/funtcion-push-one.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
import { getFunctionByName, } from "../api/functions"

import { createRemoteFn, updateRemoteFn } from "../utils/function-push"



/**
* pull function
* @param { appid} string
* @param { functionName } string
* @returns
*/
export async function handlePushOneCommand(appid: string, functionName: string) {



// get remote function
const record = await getFunctionByName(appid, functionName)


if (!record.data) {

createRemoteFn(appid, functionName)
} else {

updateRemoteFn(appid, functionName)

}

}
28 changes: 14 additions & 14 deletions packages/cli/src/actions/init.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { GLOBAL_FILE, PACKAGE_FILE, PROJECT_DIR, RESPONSE_FILE, TEMPLATES_DIR, TSCONFIG_FILE, TYPE_DIR } from './../utils/constants'
import * as path from 'node:path'
import * as fs from 'node:fs'
import { GLOBAL_FILE, PACKAGE_FILE, PROJECT_DIR, RESPONSE_FILE, TEMPLATES_DIR, TSCONFIG_FILE, TYPE_DIR } from './../utils/constants'
import { LAF_CONFIG_FILE } from '../utils/constants'
import { ensureDirectory } from '../utils/util'

import { handlePullListCommand } from './function-pull-list'
import * as path from 'node:path'


/**
Expand All @@ -13,9 +12,12 @@ import * as path from 'node:path'
* @param {string} endpoint
* @param {string} oss_endpoint
*/

export async function handleInitAppCommand(appid: string, endpoint: string, oss_endpoint: string) {
// add laf.json to project
fs.writeFileSync(LAF_CONFIG_FILE, JSON.stringify({ appid: appid, endpoint: endpoint, oss_endpoint: oss_endpoint }))

// add config file
addConfigFromTemplates()
}


Expand All @@ -26,35 +28,33 @@ export async function handleInitAppCommand(appid: string, endpoint: string, oss_

export async function handleSyncAppCommand(appid: string) {
ensureDirectory(PROJECT_DIR)
// pull function
await handlePullListCommand(appid, [])
// add config file
addConfigFromTemplates()

// pull functions
await handlePullListCommand(appid, [])
}

function addConfigFromTemplates() {

ensureDirectory(TYPE_DIR)
//from templates dir

// from templates dir
const templates_dir = path.resolve(__dirname, '../../', TEMPLATES_DIR)

// global
// generate global.d.ts
const from_globle_file = path.resolve(templates_dir, GLOBAL_FILE)
const out_globe_file = path.resolve(TYPE_DIR, GLOBAL_FILE)
fs.writeFileSync(out_globe_file, fs.readFileSync(from_globle_file, 'utf-8'))

// response
// generate response.d.ts
const from_response_file = path.resolve(templates_dir, RESPONSE_FILE)
const out_response_file = path.resolve(TYPE_DIR, RESPONSE_FILE)
fs.writeFileSync(out_response_file, fs.readFileSync(from_response_file, 'utf-8'))

// package
// generate package.json
const from_package_file = path.resolve(templates_dir, PACKAGE_FILE)
const out_package_file = path.resolve(PROJECT_DIR, PACKAGE_FILE)
fs.writeFileSync(out_package_file, fs.readFileSync(from_package_file, 'utf-8'))

// tsconfig
// generate tsconfig.json
const from_tsconfig_file = path.resolve(templates_dir, TSCONFIG_FILE)
const out_tsconfig_file = path.resolve(PROJECT_DIR, TSCONFIG_FILE)
fs.writeFileSync(out_tsconfig_file, fs.readFileSync(from_tsconfig_file, 'utf-8'))
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/actions/oss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import * as fs from 'node:fs'
import * as path from 'node:path'
import { createHash } from 'node:crypto'
import * as mime from 'mime'
import { ensureDirectory, getS3Client } from '../utils/util'
import axios from 'axios'
import { pipeline } from 'node:stream/promises'
import { ensureDirectory, getS3Client } from '../utils/util'

/**
* push files
Expand All @@ -31,7 +31,7 @@ export async function handlePushCommand(credentials: any, options: any) {
// get source files
const sourceFiles = readdirRecursive(source).map(file => {
return {
key: path.relative(abs_source, file),
key: path.relative(abs_source, file).split(path.sep).join('/'),
abs_path: path.resolve(file),
}
})
Expand All @@ -48,7 +48,7 @@ export async function handlePushCommand(credentials: any, options: any) {
Bucket: options.bucketName,
Key: file.key,
Body: fs.readFileSync(path.resolve(source, file.abs_path)),
ContentType: mime.getType(file.key)
ContentType: mime.getType(file.key),
}).promise()

console.log(path.resolve(source, file.abs_path))
Expand Down
18 changes: 8 additions & 10 deletions packages/cli/src/actions/user.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {loginApi} from '../api/user'
import * as fs from 'node:fs'
import {AUTH_FILE} from '../utils/constants'
import { AUTH_FILE } from '../utils/constants'
import { checkCredentialsDir } from '../utils/util'
import { loginApi } from '../api/user'

/**
* login
Expand All @@ -10,23 +10,21 @@ import { checkCredentialsDir } from '../utils/util'
* @param {string} password
* @returns
*/

export async function handleLoginCommand(remote:string,username:string,password:string) {
export async function handleLoginCommand(remote: string, username: string, password: string) {

// check auth dir
checkCredentialsDir()

// login
const result = await loginApi(remote,{username,password})

if(!result){
const result = await loginApi(remote, { username, password })
if (!result) {
console.error('username or password is wrong')
process.exit(1)
}
const content = {access_token:result.access_token,expire_time:result.expire_time,remote:remote}


const content = { access_token: result.access_token, expire_time: result.expire_time, remote: remote }

// write to file
fs.writeFileSync(AUTH_FILE, JSON.stringify(content))
console.log(`Generated: ${AUTH_FILE}`)

}
15 changes: 5 additions & 10 deletions packages/cli/src/api/apps.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import { requestData } from "./request"


/**
* 根据 appid 获取应用
* @param {string} appid
* @returns 返回应用数据
*/
export async function getApplicationByAppid(appid:string) {
export async function getApplicationByAppid(appid: string) {
const res = await requestData({
url: `/sys-api/apps/${appid}`,
method: 'get'
url: `/sys-api/apps/${appid}`,
method: 'get'
})

return res.data
}

return res.data
}

/**
* 获取应用列表
* @returns 返回应用数据
*/

export async function appList() {
const url = `/sys-api/apps/my`
const obj = {
Expand All @@ -36,7 +33,6 @@ export async function appList() {
* @param {string} appid
* @returns
*/

export async function appStop(appid: string) {
const url = `/sys-api/apps/${appid}/instance/stop`
const obj = {
Expand Down Expand Up @@ -69,7 +65,6 @@ export async function appStart(appid: string) {
* @param {string} appid
* @returns
*/

export async function appRestart(appid: string) {
const url = `/sys-api/apps/${appid}/instance/start`
const obj = {
Expand Down
Loading

0 comments on commit 5c944e8

Please sign in to comment.