Skip to content

Commit

Permalink
Merge remote-tracking branch 'bp/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingsong committed Aug 15, 2024
2 parents 96f504c + 9370ca7 commit aad0f93
Show file tree
Hide file tree
Showing 131 changed files with 5,502 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coverage:
status:
default:
target: 80%
threshold: 3%
if_ci_failed: error
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs
# Editor configuration, see http://editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
max_line_length = off
trim_trailing_whitespace = false
27 changes: 27 additions & 0 deletions .githooks/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh
#
# 根据 commitmsg 以及 branch name 判断是否需要 commitlint
#
# Author: waiting
# Date: 2023.07.21
#

if [[ -z $NVM_DIR ]]; then
if [[ -f $HOME/.nvm/nvm.sh ]]; then
. $HOME/.nvm/nvm.sh
fi
fi

branch=$( git branch | grep \* | cut -d ' ' -f2 )
# ./.githooks/init-repo/is-skip-commitlint.ts $1 "$branch"

# code=$?
code=0
#echo -e 'node exitCode:' $code

if [[ -f ./node_modules/.bin/commitlint ]]; then
if [[ $code = 0 ]] && [[ -d node_modules ]] ; then
./node_modules/.bin/commitlint -e ./.git/COMMIT_EDITMSG
fi
fi

79 changes: 79 additions & 0 deletions .githooks/gen-file-from-example.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env tsx
/**
* 搜索指定目录以 file.example 文件为基础生成不带后缀的文件为不带 .example 后缀的文件
*/
import { dirname, join, sep } from 'node:path'
import { cp, mkdir, readdir } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'

import { folderArr, globalConfigFileArr } from './init.config.js'
import { genFileFromExample } from './init-example-file.js'


const currentURL = import.meta.url
const currentPath = fileURLToPath(currentURL)
const currentDir = dirname(currentPath)
const currentFileName = currentPath.split(sep).pop()
const rootDir = join(currentDir, '..')
console.log({ rootDir, currentFileName })

const pkgEntryName = 'packages'
const pkgBase = join(rootDir, pkgEntryName)

const files = await cpGlobalConfigsToPkgs(rootDir, globalConfigFileArr, pkgBase)
console.log('Sync config:', files)

const files2 = await genFileFromExample(rootDir, folderArr)
console.info(`生成基础文件:${rootDir}`, files2)

const dirs = await readdir(pkgBase)
const arr: string[] = []

for (const name of dirs) {
const pkgPath = join(pkgBase, name)
const files3 = await genFileFromExample(pkgPath, folderArr)
files3.forEach(file => arr.push(`${pkgEntryName}/${name}/${file}`))
console.info(`生成包文件:`, arr)
}


// ---------------------------------------------

async function cpGlobalConfigsToPkgs(
baseDir: string,
configPaths: string[],
pkgBase: string,
): Promise<string[]> {

const pkgs = await readdir(pkgBase)
const ret: string[] = []

for (const pkg of pkgs) {
for (const path of configPaths) {
try {
const dst = `${pkg}/${path}`
const dstDir = dirname(join(pkgBase, dst))
await mkdir(dstDir, { recursive: true })
await cp(
join(baseDir, path),
join(pkgBase, dst),
)
ret.push(dst)
}
catch (ex: any) {
console.log(ex.message)
}
}
}

return ret
}

/**
* Generate random integer
*
* @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math/random
*/
export function genRandomInt(max: number): number {
return Math.floor(Math.random() * Math.floor(max))
}
73 changes: 73 additions & 0 deletions .githooks/init-example-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 搜索指定目录以 file.example 文件为基础生成 去除结尾 .example
*/
import { join } from 'node:path'
import { cp, readdir, stat } from 'node:fs/promises'


export async function genFileFromExample(rootDir: string, list: string[]): Promise<string[]> {
const copied: string[] = []

for (const dir of list) {
const srcPath = join(rootDir, dir.replace(/\.{2,}/, '/'))

try {
const pathStat = await stat(srcPath)
if (! pathStat.isDirectory) { continue }
}
catch {
continue
}

const files = await readdir(srcPath)

for (const file of files) {
if (! hasExampleSuffix(file)) {
continue
}
const source = join(srcPath, file)
const stripped = stripExampleSuffix(file)
const target = join(srcPath, stripped)

let targetStat
try {
targetStat = await stat(target)
}
catch {
void 0
}
if (! targetStat) {
await cp(source, target, { force: false })
copied.push(`${dir}/${stripped}`)
}
}
}

return copied
}

function hasExampleSuffix(name: string): boolean {
if (!name) {
return false
}
if (name === '.example') {
return false
}
const arr = name.split('.')

if (arr.length > 1 && arr[arr.length - 1] === 'example') { // 排除 '.example'
return true
}
else {
return false
}
}

function stripExampleSuffix(name: string): string {
const arr = name.split('.')

if (arr.length > 1 && arr[arr.length - 1] === 'example') {
return arr.slice(0, arr.length - 1).join('.')
}
return name
}
35 changes: 35 additions & 0 deletions .githooks/init-repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
#
# 初始化 git 仓库
#
# Author: waiting
# Date: 2019.01.21
#
set -e

git init
git config --global i18n.commitencoding utf-8
git config --local core.autocrlf input
git config --local core.eol lf

if [ -z "$CI" ]; then
git config --local core.filemode false
git config --local core.hooksPath ./.githooks
git config --local core.ignorecase false
git config --local core.precomposeUnicode true
git config --local fetch.prune true
git config --local pull.rebase true
git config --local push.autoSetupRemote true
git config --local push.followTags true
git config --local rebase.autoStash true
git config --local remote.origin.prune true
git config --local remote.origin.tagopt --tags
git config --local remote.pushdefault origin
fi;

echo It may going for a long time. Plese wait...
.githooks/gen-file-from-example.mts

lerna list
set +e
echo init done
29 changes: 29 additions & 0 deletions .githooks/init.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 搜索指定目录以 file.example 文件为基础生成不带后缀的文件为不带 .example 后缀的文件
*/

export const folderArr: string[] = [
'./',
'.vscode',
'.settings',
'resource',
'configs',
'config', // egg
'src/config', // midway
]

export const globalConfigFileArr: string[] = [
'.vscode/tasks.json.example',
'.vscode/launch.json.example',
'.vscode/settings.json.example',
'.vscode/ci.code-snippets.example',
'.vscode/common.code-snippets.example',
'.vscode/md.code-snippets.example',
'.vscode/midway.code-snippets.example',
'.vscode/promise.code-snippets.example',
'./tsconfig.base.json',
'./tsconfig.eslint.json',
'./rollup.config.js',
'./bin-hashbang.js',
]

17 changes: 17 additions & 0 deletions .githooks/is-skip-commitlint-rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Rules for skipping commintlint or not
* when some test passed
* protect has high priority than skip
*/

export const protectBranch = <RegExp[]> [
/^(master|main|release)$/,
/^develop$/,
/^v.+/,
/^release-.+/,
]

export const skipMsg = <RegExp[]> [
/^Merge remote-tracking branch/,
/test foo/,
]
45 changes: 45 additions & 0 deletions .githooks/pre-commit.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/sh
#
# commit前eslint校验提交文件中js文件
# 若校验有error则阻止commit
# 命令行执行设置 git config --global core.hooksPath ./.githooks
#
# Author: waiting
# Date: 2017.02.09
#

if [[ -z $NVM_DIR ]]; then
if [[ -f $HOME/.nvm/nvm.sh ]]; then
. $HOME/.nvm/nvm.sh
fi
fi

if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

branch=$( git branch | grep \* | cut -d ' ' -f2- )
if [[ $branch != "main" ]]; then
exit 0
fi

# ------------- parse ts
files=$(git diff --cached --name-only --diff-filter=ACMR $against|grep '\.ts$')
if [[ $files != "" ]] ; then
# echo $files | xargs eslint --fix
npm run lint
rc=$?
if [[ $rc != 0 ]] ; then
echo -e "拒绝: ESLint 语法校验失败!"
echo -e "ESLint错误代码信息参考 https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin/ "
echo -e ""
exit $rc
fi
git add $files
fi

exit 0
4 changes: 4 additions & 0 deletions .githooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

sh .githooks/set-scripts-executable.sh

51 changes: 51 additions & 0 deletions .githooks/pre-push.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/sh
#
# push前校验变更js/ts文件
# 若校验有error则阻止push
#
# Author: waiting
# Date: 2017.02.03
#

remote="$1"
url="$2"

if [[ -z $NVM_DIR ]]; then
if [[ -f $HOME/.nvm/nvm.sh ]]; then
. $HOME/.nvm/nvm.sh
fi
fi

z40=0000000000000000000000000000000000000000

while read local_ref local_sha remote_ref remote_sha
do
if [ "$local_sha" = $z40 ]
then
# Handle delete
:
else
if [ "$remote_sha" = $z40 ]
then
# New branch, examine all commits
:
else
# ------------- parse ts
files=$(git diff --name-only --diff-filter=ACMR $remote_sha $local_sha| grep '\.ts$')

if [[ $files != "" ]] ; then
npm run lint:nofix
rc=$?
if [[ $rc != 0 ]] ; then
echo -e "拒绝: ESLint 语法校验失败!"
echo -e "ESLint错误代码信息参考 https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin/ "
echo -e ""
exit $rc
fi
git add $files
fi
fi
fi
done

exit 0
Loading

0 comments on commit aad0f93

Please sign in to comment.