Skip to content

Commit

Permalink
docs(rm): ESLint files
Browse files Browse the repository at this point in the history
  • Loading branch information
mazeyqian committed Oct 30, 2023
1 parent b95f5a5 commit 1f61b30
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 33 deletions.
4 changes: 4 additions & 0 deletions scripts/build-shortcuts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +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

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

## cmd

### startup
Expand Down
49 changes: 49 additions & 0 deletions scripts/eslint-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# ESLint Files

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.

Here's a basic guide on how to use this script.

## Command Line Flags

The script accepts several command line flags:

| 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 |

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.

## Examples of Usage

To run the script, you would use a command like this:

1\. Simplest command:

```bash
go run eslint-files.go -files="file1.js,file2.js"
```

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

2\. Complex command:

```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"
```

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.

You can adjust these parameters as needed to suit your specific use case.
Empty file.
54 changes: 21 additions & 33 deletions scripts/list-files/main.go → scripts/eslint-files/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func find(absRoot, root, ext string) []string {
if e != nil {
return e
}
// log.Printf("find s: %s", s)
if filepath.Ext(d.Name()) == ext {
a = append(a, s)
}
Expand All @@ -34,17 +33,19 @@ func main() {
count := 0
files := flag.String("files", "", "Files Formated") // "file1,file2,file3"
folders := flag.String("folders", "", "Folders Formated") // "folder1,folder2,folder3"
root := flag.String("root", "", "Root of Folders") // "src"
esConf := flag.String("esConf", "", "Eslint Config Path") // ".eslintrc.js"
esCom := flag.String("esCom", "--fix", "Eslint Command") // ""
root := flag.String("root", "", "Root of Folders") // "src"
ext := flag.String("ext", "", "File Ext") // ".vue"
filesRang := flag.String("filesRang", "", "Files Range") // ""
befCom := flag.String("befCom", "", "Before Commands") // ""
aftCom := flag.String("aftCom", "", "After Commands") // ""
filesRang := flag.String("filesRang", "", "Files Range") // ""
flag.Parse()
log.Printf("Files: %s", *files)
log.Printf("Folders: %s", *folders)
log.Printf("Root: %s", *root)
log.Printf("ESLint Config Path: %s", *esConf)
log.Printf("ESLint Command: %s", *esCom)
log.Printf("File Ext: %s", *ext)
log.Printf("Files Range: %s", *filesRang)
log.Printf("Before Commands: %s", *befCom)
Expand All @@ -62,36 +63,28 @@ func main() {
if *folders != "" {
folderArr = strings.Split(*folders, ",")
}
// log.Printf("fileArr Length: %d", len(fileArr))
// log.Printf("folderArr Length: %d", len(folderArr))
for _, folder := range folderArr {
// log.Printf("folder: %s", folder)
fileArr = append(fileArr, find(*root, folder, *ext)...)
}
// log.Printf("fileArr Length: %d", len(fileArr))
rootCom := ""
if *root != "" {
rootCom = fmt.Sprintf("cd %s;", *root)
}
if *befCom != "" {
befStr := fmt.Sprintf("%s%s", rootCom, *befCom)
// log.Printf("befStr: %s", befStr)
befCmd := exec.Command("/bin/bash", "-c", befStr) // *befCom)
befCmd := exec.Command("/bin/bash", "-c", befStr)
_, err := befCmd.CombinedOutput()
if err != nil {
log.Println("Shell Error:", err)
}
// fmt.Printf("Before Result: %s", befResult)
}
for _, file := range fileArr {
log.Printf("File: %s", file)
cmdLines := ""
if *root != "" {
cmdLines += fmt.Sprintf("cd %s;", *root)
}
cmdLines += fmt.Sprintf("npx eslint %s --fix -c %s;", file, *esConf)
// cmdLines += fmt.Sprintf("exit %d;", 0)
// log.Printf("cmdLines: %s", cmdLines)
cmdLines += fmt.Sprintf("npx eslint %s %s -c %s;", file, *esCom, *esConf)
cmd := exec.Command("/bin/bash", "-c", cmdLines)
result, err := cmd.CombinedOutput()
if err != nil {
Expand All @@ -106,35 +99,30 @@ func main() {
}
if *aftCom != "" {
aftStr := fmt.Sprintf("%s%s", rootCom, *aftCom)
// log.Printf("aftStr: %s", aftStr)
aftCmd := exec.Command("/bin/bash", "-c", aftStr)
_, err := aftCmd.CombinedOutput()
if err != nil {
log.Println("Shell Error:", err)
}
// log.Printf("After Result: %s", aftResult)
}
log.Printf("Worked Count: %d", count)
otherFiles := []string{}
totalFiles := []string{}
if *filesRang != "" {
totalFiles = find("", *filesRang, *ext)
// for _, file := range totalFiles {
// log.Printf("Total File: %s", file)
// }
log.Printf("Total Count: %d", len(totalFiles))
otherFiles = lo.Filter(totalFiles, func(s string, index int) bool {
totalFiles := find("", *filesRang, *ext)
otherFiles := lo.Filter(totalFiles, func(s string, index int) bool {
return !lo.Contains(fileArr, s)
})
log.Printf("Worked Count: %d", count)
log.Printf("Other Count: %d", len(otherFiles))
}
for _, file := range fileArr {
log.Printf("Worked File: %s", file)
}
for _, file := range otherFiles {
log.Printf("Other File: %s", file)
}
for _, file := range totalFiles {
log.Printf("Total File: %s", file)
log.Printf("Total Count: %d", len(totalFiles))
for _, file := range fileArr {
log.Printf("Worked File: %s", file)
}
for _, file := range otherFiles {
log.Printf("Other File: %s", file)
}
// for _, file := range totalFiles {
// log.Printf("Total File: %s", file)
// }
} else {
log.Printf("Worked Count: %d", count)
}
}

0 comments on commit 1f61b30

Please sign in to comment.