Skip to content

Commit

Permalink
chore: harmonise lint configuration (#1688)
Browse files Browse the repository at this point in the history
* chore: harmonise eslint configuration

* chore: add husky,stylelint,editorconfig

---------

Co-authored-by: maslow <[email protected]>
  • Loading branch information
0fatal and maslow authored Nov 16, 2023
1 parent b73acea commit fcab606
Show file tree
Hide file tree
Showing 270 changed files with 32,819 additions and 24,775 deletions.
1 change: 1 addition & 0 deletions packages/database-ql/.editorconfig → .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ root = true
[!{node_modules}/**]
end_of_line = lf
charset = utf-8
insert_final_newline = true

[{*.js,*.ts}]
indent_style = space
Expand Down
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run lint-staged
1 change: 0 additions & 1 deletion .prettierignore

This file was deleted.

4 changes: 3 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"streetsidesoftware.code-spell-checker",
"pomdtr.excalidraw-editor",
"Tim-Koehler.helm-intellisense",
"lokalise.i18n-ally"
"lokalise.i18n-ally",
"stylelint.vscode-stylelint",
"EditorConfig.EditorConfig"
]
}
19 changes: 14 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,25 @@
"withs",
"xmlparser",
"zcube",
"zustand"
"zustand",
"stylelint"
],
"i18n-ally.localesPaths": "web/public/locales",
"i18n-ally.enabledParsers": ["json"],
"i18n-ally.enabledFrameworks": ["react", "i18next", "general"],
"i18n-ally.enabledParsers": [
"json"
],
"i18n-ally.enabledFrameworks": [
"react",
"i18next",
"general"
],
"i18n-ally.sourceLanguage": "zh-CN",
"i18n-ally.displayLanguage": "en,zh",
"i18n-ally.namespace": false,
"i18n-ally.pathMatcher": "{locale}/translation.json",
"i18n-ally.keystyle": "nested",
"i18n-ally.keysInUse": ["description.part2_whatever"],
"i18n-ally.keysInUse": [
"description.part2_whatever"
],
"jest.rootPath": "e2e"
}
}
4 changes: 4 additions & 0 deletions cli/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"root": true,
"extends": "laf/cli"
}
7 changes: 0 additions & 7 deletions cli/.prettierrc

This file was deleted.

9 changes: 6 additions & 3 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"dev": "tsc -w",
"watch": "tsc -w",
"build": "tsc",
"prettier": "npx prettier --write ./src",
"prepublishOnly": "npm run build"
"prepublishOnly": "npm run build",
"lint": "eslint . --fix --ext .ts --ext .js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -44,5 +44,8 @@
"typescript": "^4.7.4",
"urlencode": "^1.1.0",
"yaml": "^2.1.3"
},
"lint-staged": {
"*.{ts,js}": "eslint --fix"
}
}
}
11 changes: 5 additions & 6 deletions cli/src/action/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function list() {
})
const apps = await applicationControllerFindAll()
const regionMap = await getRegionMap()
for (let item of apps) {
for (const item of apps) {
table.push([
item.appid,
item.name,
Expand All @@ -50,13 +50,13 @@ export async function list() {
async function getRegionMap(): Promise<Map<string, any>> {
const regionMap = new Map<string, any>()
const regions = await regionControllerGetRegions()
for (let region of regions) {
for (const region of regions) {
regionMap.set(region._id, region)
}
return regionMap
}

export async function init(appid: string, options: { sync: boolean, basicMode: boolean }) {
export async function init(appid: string, options: { sync: boolean; basicMode: boolean }) {
if (AppSchema.exist()) {
console.log(
`${getEmoji(
Expand All @@ -69,7 +69,7 @@ export async function init(appid: string, options: { sync: boolean, basicMode: b
const app = await applicationControllerFindOne(appid)

// init app schema
let timestamp = Date.parse(new Date().toString()) / 1000
const timestamp = Date.parse(new Date().toString()) / 1000
const appSchema: AppSchema = {
name: app.name,
appid: app.appid,
Expand All @@ -93,9 +93,8 @@ export async function init(appid: string, options: { sync: boolean, basicMode: b
return
}


if (!ProjectSchema.exist()) {
// init project schema
// init project schema
const projectSchema: ProjectSchema = {
version: '1.0.0',
name: app.name,
Expand Down
14 changes: 7 additions & 7 deletions cli/src/action/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { databaseControllerExport, databaseControllerImport } from "../../api/custom";
import { AppSchema } from "../../schema/app";
import { databaseControllerExport, databaseControllerImport } from '../../api/custom'
import { AppSchema } from '../../schema/app'
import * as fs from 'fs'
import { getEmoji } from "../../util/print";
import { getEmoji } from '../../util/print'
import * as path from 'path'
import { exist } from "../../util/file";
import { exist } from '../../util/file'
import * as FormData from 'form-data'

export async function exportDB(exportPath: string): Promise<void> {
Expand All @@ -21,14 +21,14 @@ export async function importDB(sourceAppid, importPath: string): Promise<void> {

// get absolute path
const absPath = path.resolve(importPath)
if (!exist(absPath)) {
if (!exist(absPath)) {
console.log(`${getEmoji('❌')} import database file not exist`)
return
}

const formData = new FormData();
const formData = new FormData()
formData.append('file', fs.createReadStream(absPath), 'database.gz')
formData.append('sourceAppid', sourceAppid)
await databaseControllerImport(appSchema.appid, formData)
console.log(`${getEmoji('✅')} database import success`)
}
}
12 changes: 6 additions & 6 deletions cli/src/action/dependency/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { AppSchema } from '../../schema/app'
import { ProjectSchema } from '../../schema/project'
import { getBaseDir } from '../../util/sys'

export async function add(dependencyName: string, options: { targetVersion: string, remote: boolean }) {
export async function add(dependencyName: string, options: { targetVersion: string; remote: boolean }) {
const appSchema = AppSchema.read()
const dependencyDto: CreateDependencyDto = {
name: dependencyName,
Expand All @@ -40,16 +40,16 @@ export async function pull() {
console.log(`${getEmoji('👉')} please run 'npm install' install dependencies`)
}

async function pullOne(updateYaml: boolean = true) {
async function pullOne(updateYaml = true) {
const appSchema = AppSchema.read()

const dependencies = await dependencyControllerGetDependencies(appSchema.appid)

const packagePath = path.resolve(getBaseDir(), PACKAGE_FILE)
let packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf-8'))
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf-8'))
const devDependencies = {}
const localDependencies = {}
for (let dependency of dependencies) {
for (const dependency of dependencies) {
devDependencies[dependency.name] = dependency.spec

// add a non-built-in dependency
Expand All @@ -73,13 +73,13 @@ export async function push(options: { updatePackage: boolean }) {

const serverDependencies = await dependencyControllerGetDependencies(appSchema.appid)
const serverDependenciesMap = new Map<string, boolean>()
for (let item of serverDependencies) {
for (const item of serverDependencies) {
serverDependenciesMap.set(item.name, true)
}

const projectSchema = ProjectSchema.read()

for (let key in projectSchema.spec.dependencies) {
for (const key in projectSchema.spec.dependencies) {
if (!serverDependenciesMap.has(key)) {
const createDependencyDto: CreateDependencyDto = {
name: key,
Expand Down
6 changes: 3 additions & 3 deletions cli/src/action/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function deploy() {
bucketMap.set(bucket.shortName, bucket.name)
})

for (let bucket of deploySchema?.resources?.buckets) {
for (const bucket of deploySchema?.resources?.buckets) {
if (!bucketMap.has(bucket.name)) {
await createBucket(bucket.name, { policy: bucket.policy })
} else {
Expand All @@ -51,7 +51,7 @@ export async function deploy() {
websiteMap.set(website.bucketName, true)
})

for (let website of deploySchema?.resources?.websites) {
for (const website of deploySchema?.resources?.websites) {
if (!websiteMap.has(website.bucketName) && !websiteMap.has(appSchema.appid + '-' + website.bucketName)) {
await createWebsite(website.bucketName, {})
} else {
Expand All @@ -62,7 +62,7 @@ export async function deploy() {

if (deploySchema?.actions?.buckets) {
// push all buckets
for (let bucket of deploySchema?.actions?.buckets) {
for (const bucket of deploySchema?.actions?.buckets) {
await pushBucket(bucket.bucketName, bucket.srcDir, { force: true, detail: false })
}
}
Expand Down
34 changes: 19 additions & 15 deletions cli/src/action/function/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function list() {
const table = new Table({
head: ['name', 'desc', 'websocket', 'methods', 'tags', 'updatedAt'],
})
for (let func of funcs) {
for (const func of funcs) {
table.push([
func.name,
func.description,
Expand Down Expand Up @@ -95,14 +95,14 @@ export async function pullAll(options: { force: boolean }) {
const appSchema = AppSchema.read()
const funcs = await functionControllerFindAll(appSchema.appid)
const serverFuncMap = new Map<string, boolean>()
for (let func of funcs) {
for (const func of funcs) {
await pull(func.name)
console.log(`${getEmoji('✅')} function ${func.name} pulled`)
serverFuncMap.set(func.name, true)
}
// remove remote not exist function
const localFuncs = getLocalFuncs()
for (let item of localFuncs) {
for (const item of localFuncs) {
if (!serverFuncMap.has(item)) {
if (options.force) {
removeFunction(item)
Expand All @@ -115,7 +115,6 @@ export async function pullAll(options: { force: boolean }) {
}
}
}

}
}

Expand Down Expand Up @@ -153,22 +152,22 @@ export async function pushAll(options: { force: boolean }) {
const appSchema = AppSchema.read()
const serverFuncs = await functionControllerFindAll(appSchema.appid)
const serverFuncMap = new Map<string, FunctionSchema>()
for (let func of serverFuncs) {
for (const func of serverFuncs) {
serverFuncMap.set(func.name, func)
}
const localFuncs = getLocalFuncs()
for (let item of localFuncs) {
for (const item of localFuncs) {
await push(item, !serverFuncMap.has(item))
console.log(`${getEmoji('✅')} function ${item} pushed`)
}

const localFuncMap = new Map<string, boolean>()
for (let item of localFuncs) {
for (const item of localFuncs) {
localFuncMap.set(item, true)
}

// delete server functions
for (let item of serverFuncs) {
for (const item of serverFuncs) {
if (!localFuncMap.has(item.name)) {
if (options.force) {
await functionControllerRemove(appSchema.appid, urlencode(item.name))
Expand All @@ -192,7 +191,7 @@ export async function pushOne(funcName: string) {
const appSchema = AppSchema.read()
const serverFuncs = await functionControllerFindAll(appSchema.appid)
let isCreate = true
for (let func of serverFuncs) {
for (const func of serverFuncs) {
if (func.name === funcName) {
isCreate = false
break
Expand Down Expand Up @@ -276,7 +275,7 @@ async function printLog(appid: string, requestId: string) {
page: '1',
pageSize: '100',
})
for (let log of data.list) {
for (const log of data.list) {
console.log(`[${formatDate(log.createdAt)}] ${log.data}`)
}
}
Expand All @@ -289,26 +288,31 @@ function getLocalFuncs(): string[] {

function getLocalFunction(dir: string, prefix: string): string[] {
const files = fs.readdirSync(dir)
const funcNames: string[] =[]
files.forEach((file) => {
const funcNames: string[] = []
files.forEach((file) => {
const filePath = path.join(dir, file)
const stat = lstatSync(filePath)
if (stat.isDirectory()) {
funcNames.push(...getLocalFunction(filePath, path.join(prefix || '', file)))
}
if (stat.isFile() && file.endsWith('.ts')) {
funcNames.push(path.join(prefix || '', file).replace(/\\/g, '/').replace(/\.ts$/, ''))
funcNames.push(
path
.join(prefix || '', file)
.replace(/\\/g, '/')
.replace(/\.ts$/, ''),
)
}
})
return funcNames
}

function removeFunction(name: string) {
function removeFunction(name: string) {
if (FunctionSchema.exist(name)) {
FunctionSchema.delete(name)
}
const funcPath = path.join(getBaseDir(), 'functions', name + '.ts')
if (exist(funcPath)) {
remove(funcPath)
}
}
}
Loading

0 comments on commit fcab606

Please sign in to comment.