Skip to content

Commit

Permalink
feat(scripts): expand exts
Browse files Browse the repository at this point in the history
  • Loading branch information
mazeyqian committed Oct 31, 2023
1 parent 1f61b30 commit 87f07f6
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 42 deletions.
3 changes: 2 additions & 1 deletion scripts/build-shortcuts.sh
Expand Up @@ -36,9 +36,10 @@ GOOS=darwin GOARCH=amd64 go build -o dist/convert-markdown-to-typedoc-mac-darwin
GOOS=linux GOARCH=amd64 go build -o dist/convert-markdown-to-typedoc-linux-amd64 scripts/convert-markdown-to-typedoc/main.go
GOOS=windows GOARCH=amd64 go build -o dist/convert-markdown-to-typedoc-windows-amd64.exe scripts/convert-markdown-to-typedoc/main.go

### eslint
### eslint-files

GOOS=darwin GOARCH=amd64 go build -o dist/eslint-files-mac-darwin-amd64 scripts/eslint-files/main.go
GOOS=linux GOARCH=amd64 go build -o dist/eslint-files-linux-amd64 scripts/eslint-files/main.go

## cmd

Expand Down
94 changes: 64 additions & 30 deletions scripts/eslint-files/README.md
@@ -1,49 +1,83 @@
# ESLint Files
# Using Scripts to Consolidate Designated Files/Folders and Execute Customized ESLint Commands

This Go script is designed to format a set of files using ESLint. ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs.
## Background

Here's a basic guide on how to use this script.
Recently, I faced a large project where I only needed to modify a specific module. It was too cumbersome to manually input commands every time, so I thought about writing a script to assist in handling these tasks.

## Command Line Flags
## Solution

The script accepts several command line flags:
Customized one-click ESLint, download the executable file at:

| Flag | Description | Example | Required |
| --- | --- | --- | --- |
| `files` | A comma-separated list of files to be formatted. | `file1.js,file2.js` | Optional |
| `folders` | A comma-separated list of folders to be formatted. | `src/utils,src/components` | Optional |
| `esConf` | The path to the ESLint configuration file. | `.eslintrc.js` | Optional |
| `esCom` | The ESLint command to run. | `--fix` | Optional |
| `root` | The root of the folders. | `src` | Optional |
| `ext` | The file extension to look for when formatting files. | `.js` | Optional |
| `befCom` | Commands to run before the formatting step. | `echo 'Starting format'` | Optional |
| `aftCom` | Commands to run after the formatting step. | `echo 'Format completed'` | Optional |
| `filesRang` | The range of files to format. | `1-10` | Optional |
<https://github.com/mazeyqian/go-gin-gee/releases/tag/v1.4.0>

1. **File and Folder Selection**: The script allows you to specify the files and folders to be formatted using the `files` and `folders` flags. If a root directory is specified, it will prepend the root to each file or folder. It will also find all files with the specified extension within each folder.
2. **Command Execution**: If any commands are specified in the `befCom` or `aftCom` flags, they will be executed before and after the formatting step, respectively.
3. **ESLint Execution**: For each file in the list, the script will execute the ESLint command with the specified configuration file. It will log any errors that occur during this process.
4. **File Range**: If a file range is specified, the script will find all files within this range with the specified extension. It will then filter out any files that were already formatted, and log the remaining files.
5. **Logging**: The script logs various information throughout its execution, including the number of files worked on, any errors that occur, and any files within the specified range that were not formatted.
### Basic Usage

## Examples of Usage
The following examples use MacOS as an example, please replace the corresponding files for other systems.

To run the script, you would use a command like this:
Example 1: Specify files `file1.js` and `file2.js` with the default configuration.

1\. Simplest command:
```bash
#!/bin/bash
./eslint-files-mac-darwin-amd64 -files="file1.js,file2.js"
```

Example 2: Specify folders `src/views` and `src/components`.

```bash
#!/bin/bash
./eslint-files-mac-darwin-amd64 -folders="/root/app/src/views,/root/app/src/components"
```

Specify folders using the root directory `root`:

```bash
#!/bin/bash
./eslint-files-mac-darwin-amd64 \
-folders="src/views,src/components" \
-root="/root/app/"
```

Example 3: Specify ESLint configuration file `custom.eslintrc.js` and command `--fix`.

```bash
go run eslint-files.go -files="file1.js,file2.js"
#!/bin/bash
./eslint-files-mac-darwin-amd64 \
-folders="/root/app/src/views" \
-esConf="custom.eslintrc.js" \
-esCom="--fix"
```

This command will format the files `file1.js` and `file2.js` using the default ESLint configuration and command.
### Complex Scenarios

2\. Complex command:
1. Specify ESLint configuration file `custom.eslintrc.js`;
2. Specify accompanying command `--fix`;
3. Specify files and folders;
4. Specify file suffix;
5. Add prefix and postfix execution commands.

```bash
go run eslint-files.go -files="file1.js,file2.js" -folders="src/utils,src/components" -esConf=".eslintrc.js" -esCom="--fix" -root="src" -ext=".js" -befCom="echo 'Starting format'" -aftCom="echo 'Format completed'" -filesRang="1-10"
#!/bin/bash
./eslint-files-mac-darwin-amd64 \
-files="file1.js,file2.js" \
-folders="src/views,src/components" \
-root="/root/app/" \
-esConf="custom.eslintrc.js" \
-esCom="--fix" \
-ext=".js,.ts,.jsx,.vue,.tsx" \
-befCom="echo 'Starting format';" \
-aftCom="echo 'Format completed';"
```

This command will format the files `file1.js` and `file2.js`, as well as all `.js` files in the `src/utils` and `src/components` folders. It will use the ESLint configuration in `.eslintrc.js` and the ESLint command `--fix`. It will also echo a start and end message, and only format files in the range 1-10.
### Parameter Description

You can adjust these parameters as needed to suit your specific use case.
| Parameter | Description | Default | Example | Required |
| --- | --- | --- | --- | --- |
| `files` | Specify files, multiple files are separated by `,`. | - | `file1.js,file2.js` | Optional |
| `folders` | Specify folders, multiple folders are separated by `,`. | - | `src/views,src/components` | Optional |
| `esConf` | Specify ESLint configuration file. | - | `custom.eslintrc.js` | Optional |
| `esCom` | Specify accompanying command. | - | `--fix` | Optional |
| `root` | Specify root directory, used with `folders`. | - | `/root/app/` | Optional |
| `ext` | Specify file suffix. | `.js` | `.js,.ts,.jsx,.vue` | Optional |
| `befCom` | Specify prefix execution command. | - | `echo 'Starting format';` | Optional |
| `aftCom` | Specify postfix execution command. | - | `echo 'Format completed';` | Optional |
| `filesRang` | Specify the range of files, count the processed and unprocessed files. | - | `/root/app/` | Optional |
83 changes: 83 additions & 0 deletions scripts/eslint-files/README_ZH.md
@@ -0,0 +1,83 @@
# 使用脚本整合指定文件/文件夹,执行定制化 ESLint 命令

## 背景

最近面对一个庞大的项目,但是只需要修改其中某个模块,每次都手搓命令太麻烦了,于是就想着能不能写个脚本来辅助处理这些事情。

## 解决方案

定制化一键 ESLint,执行文件下载地址:

<https://github.com/mazeyqian/go-gin-gee/releases/tag/v1.4.0>

### 基础使用

以下案例以 MacOS 为例,其他系统自行替换对应的文件。

案例 1:指定文件 `file1.js``file2.js`,使用默认的配置。

```bash
#!/bin/bash
./eslint-files-mac-darwin-amd64 -files="file1.js,file2.js"
```

案例 2:指定文件夹 `src/views``src/components`

```bash
#!/bin/bash
./eslint-files-mac-darwin-amd64 -folders="/root/app/src/views,/root/app/src/components"
```

配合根目录 `root` 使用指定文件夹:

```bash
#!/bin/bash
./eslint-files-mac-darwin-amd64 \
-folders="src/views,src/components" \
-root="/root/app/"
```

案例 3:指定 ESLint 配置文件 `custom.eslintrc.js` 和命令 `--fix`

```bash
#!/bin/bash
./eslint-files-mac-darwin-amd64 \
-folders="/root/app/src/views" \
-esConf="custom.eslintrc.js" \
-esCom="--fix"
```

### 复杂场景

1. 指定 ESLint 配置文件 `custom.eslintrc.js`
2. 指定附带命令 `--fix`
3. 指定文件和文件夹;
4. 指定文件后缀;
5. 添加前置和后置执行命令。

```bash
#!/bin/bash
./eslint-files-mac-darwin-amd64 \
-files="file1.js,file2.js" \
-folders="src/views,src/components" \
-root="/root/app/" \
-esConf="custom.eslintrc.js" \
-esCom="--fix" \
-ext=".js,.ts,.jsx,.vue,.tsx" \
-befCom="echo 'Starting format';" \
-aftCom="echo 'Format completed';"
```

### 参数说明

| 参数 | 说明 | 默认 | 示例 | 是否必须 |
| --- | --- | --- | --- | --- |
| `files` | 指定文件,多个文件用 `,` 分隔。 | - | `file1.js,file2.js` | 可选 |
| `folders` | 指定文件夹,多个文件夹用 `,` 分隔。 | - | `src/views,src/components` | 可选 |
| `esConf` | 指定 ESLint 配置文件。 | - | `custom.eslintrc.js` | 可选 |
| `esCom` | 指定附带命令。 | - | `--fix` | 可选 |
| `root` | 指定根目录,配合 `folders` 使用。 | - | `/root/app/` | 可选 |
| `ext` | 指定文件后缀。 | `.js` | `.js,.ts,.jsx,.vue` | 可选 |
| `befCom` | 指定前置执行命令。 | - | `echo 'Starting format';` | 可选 |
| `aftCom` | 指定后置执行命令。 | - | `echo 'Format completed';` | 可选 |
| `filesRang` | 指定文件范围,统计处理过和未处理的文件。 | - | `/root/app/` | 可选 |
55 changes: 44 additions & 11 deletions scripts/eslint-files/main.go
Expand Up @@ -17,13 +17,27 @@ func find(absRoot, root, ext string) []string {
if absRoot != "" {
root = absRoot + root
}
exts := []string{}
if ext != "" {
exts = strings.Split(ext, ",")
}
filepath.WalkDir(root, func(s string, d fs.DirEntry, e error) error {
if e != nil {
return e
}
if filepath.Ext(d.Name()) == ext {
if ext == "" {
a = append(a, s)
} else {
for _, ext := range exts {
if filepath.Ext(d.Name()) == ext {
a = append(a, s)
break
}
}
}
// else if filepath.Ext(d.Name()) == ext {
// a = append(a, s)
// }
return nil
})
return a
Expand All @@ -33,10 +47,10 @@ func main() {
count := 0
files := flag.String("files", "", "Files Formated") // "file1,file2,file3"
folders := flag.String("folders", "", "Folders Formated") // "folder1,folder2,folder3"
esConf := flag.String("esConf", "", "Eslint Config Path") // ".eslintrc.js"
esCom := flag.String("esCom", "--fix", "Eslint Command") // ""
esConf := flag.String("esConf", "", "ESLint Config Path") // ".eslintrc.js"
esCom := flag.String("esCom", "", "ESLint Command") // "--fix"
root := flag.String("root", "", "Root of Folders") // "src"
ext := flag.String("ext", "", "File Ext") // ".vue"
ext := flag.String("ext", ".js", "File Ext") // ".vue,.js,.ts,.jsx,.tsx"
befCom := flag.String("befCom", "", "Before Commands") // ""
aftCom := flag.String("aftCom", "", "After Commands") // ""
filesRang := flag.String("filesRang", "", "Files Range") // ""
Expand Down Expand Up @@ -73,37 +87,56 @@ func main() {
if *befCom != "" {
befStr := fmt.Sprintf("%s%s", rootCom, *befCom)
befCmd := exec.Command("/bin/bash", "-c", befStr)
_, err := befCmd.CombinedOutput()
result, err := befCmd.CombinedOutput()
if err != nil {
log.Println("Shell Error:", err)
log.Println("Bash Error:", err)
}
resultStr := string(result)
if resultStr == "" {
resultStr = "ok"
}
log.Printf("Bash Result: %s", resultStr)
}
for _, file := range fileArr {
log.Printf("File: %s", file)
cmdLines := ""
if *root != "" {
cmdLines += fmt.Sprintf("cd %s;", *root)
}
cmdLines += fmt.Sprintf("npx eslint %s %s -c %s;", file, *esCom, *esConf)
esConfCom := ""
if *esConf != "" {
esConfCom = fmt.Sprintf(" -c %s", *esConf)
}
esComCom := ""
if *esCom != "" {
esComCom = fmt.Sprintf(" %s", *esCom)
}
cmdLines += fmt.Sprintf("npx eslint %s%s%s;", file, esComCom, esConfCom)
// log.Printf("ESLint Command: %s", cmdLines)
cmd := exec.Command("/bin/bash", "-c", cmdLines)
result, err := cmd.CombinedOutput()
if err != nil {
log.Println("Eslint Error:", err)
log.Println("ESLint Error:", err)
}
resultStr := string(result)
if resultStr == "" {
resultStr = "ok"
}
log.Printf("Eslint Result: %s", resultStr)
log.Printf("ESLint Result: %s", resultStr)
count++
}
if *aftCom != "" {
aftStr := fmt.Sprintf("%s%s", rootCom, *aftCom)
aftCmd := exec.Command("/bin/bash", "-c", aftStr)
_, err := aftCmd.CombinedOutput()
result, err := aftCmd.CombinedOutput()
if err != nil {
log.Println("Shell Error:", err)
log.Println("Bash Error:", err)
}
resultStr := string(result)
if resultStr == "" {
resultStr = "ok"
}
log.Printf("Bash Result: %s", resultStr)
}
if *filesRang != "" {
totalFiles := find("", *filesRang, *ext)
Expand Down

0 comments on commit 87f07f6

Please sign in to comment.