Skip to content

Commit

Permalink
Merge pull request #111 from pascalre/48-error-in-recursively-sort
Browse files Browse the repository at this point in the history
Enable sorting of files with unusual extension (#48)
  • Loading branch information
pascalre committed Jan 4, 2023
2 parents 6b4bfc6 + e2ea78a commit 89a7c4d
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 51 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog
## 6.0.0 - December 6, 2022

## 6.0.2 - January 4, 2023
* (refs [#48](https://github.com/pascalre/vscode-yaml-sort/issues/48)) error in Recursively sort
* New configuration `extensions` enables recursive sorting of files with unusual extension
* Update dependencies to latest versions

## 6.0.1 - December 6, 2022
* (refs [#98](https://github.com/pascalre/vscode-yaml-sort/issues/98)) Refactor project
* Update dependencies to latest versions

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This extension contributes the following settings:
| `customSortKeywords_1` | List of keywords for `Custom Sort 1` | `["apiVersion", "kind", "metadata", "spec", "data"]`|
| `customSortKeywords_2` | List of keywords for `Custom Sort 2` | - |
| `customSortKeywords_3` | List of keywords for `Custom Sort 3` | - |
| `extensions` | Extensions to be processed with command `Recursively sort YAML files` | `["yaml", "yml"]` |
| `forceQuotes` | When `true`, all non-key strings will be quoted even if they normally don't need to. | `false` |
| `indent` | Indentation width in spaces | `2` |
| `lineWidth` | Maximum line width for YAML files | `500` |
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-yaml-sort",
"displayName": "YAML Sort",
"description": "This VS Code extension exposes the possibility to sort, format and validate yaml files.",
"version": "6.0.1",
"version": "6.0.2",
"engines": {
"vscode": "^1.49.0"
},
Expand Down Expand Up @@ -92,6 +92,14 @@
"default": [],
"description": "List of keywords for Custom Sort 3."
},
"vscode-yaml-sort.extensions": {
"type": "array",
"items": {
"type": "string"
},
"default": ["yaml", "yml"],
"description": "Files with matching extensions will be processed in recursive sort"
},
"vscode-yaml-sort.forceQuotes": {
"type": "boolean",
"default": false,
Expand Down
4 changes: 2 additions & 2 deletions src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { JsYamlAdapter } from "./adapter/js-yaml-adapter"
import { Severity, VsCodeAdapter } from "./adapter/vs-code-adapter"
import { prependWhitespacesOnEachLine, removeLeadingLineBreakOfFirstElement } from "./lib"
import { Settings } from "./settings"
import { getYamlFilesInDirectory } from "./util/file-util"
import { FileUtil } from "./util/file-util"
import { getDelimiters, splitYaml, validateTextRange, YamlUtil } from "./util/yaml-util"

const settings = new Settings()
Expand Down Expand Up @@ -133,7 +133,7 @@ export function formatYamlWrapper(): vscode.TextEdit[] {
* @param {vscode.Uri} uri Base URI
*/
export function sortYamlFiles(uri: vscode.Uri): boolean {
const files = getYamlFilesInDirectory(uri.fsPath)
const files = new FileUtil().getFilesWithExtensions(uri.fsPath)
files.forEach((file: string) => {
const yaml = fs.readFileSync(file, 'utf-8').toString()
const sortedYaml = yamlutil.sortYaml(yaml, 0)
Expand Down
38 changes: 20 additions & 18 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import * as jsyaml from "js-yaml"
import * as vscode from "vscode"
import { workspace } from "vscode"
import { CLOUDFORMATION_SCHEMA } from "cloudformation-js-yaml-schema"
import { HOMEASSISTANT_SCHEMA } from "homeassistant-js-yaml-schema"

export class Settings {
// vscodeadapter = new VsCodeAdapter()
workspace = workspace

getCustomSortKeywords(index: number): string[] {
if ([1, 2, 3].includes(index))
return vscode.workspace.getConfiguration().get("vscode-yaml-sort.customSortKeywords_" + index) as string[]
return this.workspace.getConfiguration().get(`vscode-yaml-sort.customSortKeywords_${index}`) as string[]
return []
}
getEmptyLinesUntilLevel(): number {
// return this.vscodeadapter.getProperty("emptyLinesUntilLevel") as number
return vscode.workspace.getConfiguration().get("vscode-yaml-sort.emptyLinesUntilLevel") as number
return this.workspace.getConfiguration().get("vscode-yaml-sort.emptyLinesUntilLevel") as number
}
getExtensions(): string[] {
return this.workspace.getConfiguration().get("vscode-yaml-sort.extensions") as string[]
}
getForceQuotes(): boolean {
return vscode.workspace.getConfiguration().get("vscode-yaml-sort.forceQuotes") as boolean
return this.workspace.getConfiguration().get("vscode-yaml-sort.forceQuotes") as boolean
}
getIndent(): number {
return vscode.workspace.getConfiguration().get("vscode-yaml-sort.indent") as number
return this.workspace.getConfiguration().get("vscode-yaml-sort.indent") as number
}
getJsYamlSchemaFromString(schema: string): jsyaml.Schema {
switch (schema) {
Expand All @@ -38,38 +40,38 @@ export class Settings {
}
}
getLineWidth(): number {
return vscode.workspace.getConfiguration().get("vscode-yaml-sort.lineWidth") as number
return this.workspace.getConfiguration().get("vscode-yaml-sort.lineWidth") as number
}
getLocale(): string {
return vscode.workspace.getConfiguration().get("vscode-yaml-sort.locale") as string
return this.workspace.getConfiguration().get("vscode-yaml-sort.locale") as string
}
getNoArrayIndent(): boolean {
return vscode.workspace.getConfiguration().get("vscode-yaml-sort.noArrayIndent") as boolean
return this.workspace.getConfiguration().get("vscode-yaml-sort.noArrayIndent") as boolean
}
getNoCompatMode(): boolean {
return vscode.workspace.getConfiguration().get("vscode-yaml-sort.noCompatMode") as boolean
return this.workspace.getConfiguration().get("vscode-yaml-sort.noCompatMode") as boolean
}
getNotifySuccess(): boolean {
return vscode.workspace.getConfiguration().get("vscode-yaml-sort.notifySuccess") as boolean
return this.workspace.getConfiguration().get("vscode-yaml-sort.notifySuccess") as boolean
}
getQuotingType(): "'" | "\"" {
return vscode.workspace.getConfiguration().get("vscode-yaml-sort.quotingType") as "'" | "\""
return this.workspace.getConfiguration().get("vscode-yaml-sort.quotingType") as "'" | "\""
}
getSchema(): jsyaml.Schema {
const schema = vscode.workspace.getConfiguration().get("vscode-yaml-sort.schema") as string
const schema = this.workspace.getConfiguration().get("vscode-yaml-sort.schema") as string
return this.getJsYamlSchemaFromString(schema)
}
getSortOnSave(): number {
return vscode.workspace.getConfiguration().get('vscode-yaml-sort.sortOnSave') as number
return this.workspace.getConfiguration().get('vscode-yaml-sort.sortOnSave') as number
}
getUseCustomSortRecursively(): boolean {
return vscode.workspace.getConfiguration().get("vscode-yaml-sort.useCustomSortRecursively") as boolean
return this.workspace.getConfiguration().get("vscode-yaml-sort.useCustomSortRecursively") as boolean
}
getUseAsFormatter(): boolean {
return vscode.workspace.getConfiguration().get("vscode-yaml-sort.useAsFormatter") as boolean
return this.workspace.getConfiguration().get("vscode-yaml-sort.useAsFormatter") as boolean
}
getUseLeadingDashes(): boolean {
return vscode.workspace.getConfiguration().get("vscode-yaml-sort.useLeadingDashes") as boolean
return this.workspace.getConfiguration().get("vscode-yaml-sort.useLeadingDashes") as boolean
}

customSortKeywords1 = this.getCustomSortKeywords(1)
Expand Down
16 changes: 0 additions & 16 deletions src/test/suite/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
prependWhitespacesOnEachLine,
replaceTabsWithSpaces
} from "../../lib"
import { getYamlFilesInDirectory } from "../../util/file-util"
import { getDelimiters, removeTrailingCharacters } from "../../util/yaml-util"

suite("Test removeTrailingCharacters", () => {
Expand Down Expand Up @@ -230,18 +229,3 @@ spec: value
assert.strictEqual(addNewLineBeforeKeywordsUpToLevelN(2, 2, actual), expected)
})
})

suite("Test getYamlFilesInDirectory", () => {
test("should list all files with extension *.yaml or *.yml in a directory and all its subdirectories", () => {
const expected = [
'./src/test/files/getYamlFilesInDirectory/file.yaml',
'./src/test/files/getYamlFilesInDirectory/file.yml',
'./src/test/files/getYamlFilesInDirectory/file2.yaml',
'./src/test/files/getYamlFilesInDirectory/folder1/file.yaml',
'./src/test/files/getYamlFilesInDirectory/folder1/file2.yaml',
'./src/test/files/getYamlFilesInDirectory/folder2/file.yaml'
]
assert.deepStrictEqual(getYamlFilesInDirectory("./src/test/files/getYamlFilesInDirectory"), expected)
})

})
31 changes: 31 additions & 0 deletions src/test/suite/util/file-util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as assert from "assert"
import { Settings } from "../../../settings"
import { FileUtil } from "../../../util/file-util"

suite("Test FileUtil - readFile()", () => {
Expand All @@ -11,4 +12,34 @@ suite("Test FileUtil - readFile()", () => {
test("when `file` is a path to a non existing file should throw", () => {
assert.throws(() => fileutil.readFile("nonexistent-path"), Error)
})
})

suite("Test FileUtil - getFilesWithExtensions()", () => {
const settings = new Settings()
settings.getExtensions = function () {
return ["yaml", "yml", "customyaml"]
}
const fileutil = new FileUtil(settings)

test("should return all files", () => {
const expected = [
"./src/test/suite/util/resources/foo.yaml",
"./src/test/suite/util/resources/subfolder/foo2.yaml",
"./src/test/suite/util/resources/bar.yml",
"./src/test/suite/util/resources/subfolder/.customyaml",
]
assert.deepStrictEqual(fileutil.getFilesWithExtensions("./src/test/suite/util/resources"), expected)
})

test("should list all files with extension *.yaml or *.yml in a directory and all its subdirectories", () => {
const expected = [
'./src/test/files/getYamlFilesInDirectory/file.yaml',
'./src/test/files/getYamlFilesInDirectory/file2.yaml',
'./src/test/files/getYamlFilesInDirectory/folder1/file.yaml',
'./src/test/files/getYamlFilesInDirectory/folder1/file2.yaml',
'./src/test/files/getYamlFilesInDirectory/folder2/file.yaml',
'./src/test/files/getYamlFilesInDirectory/file.yml'
]
assert.deepStrictEqual(fileutil.getFilesWithExtensions("./src/test/files/getYamlFilesInDirectory"), expected)
})
})
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
26 changes: 16 additions & 10 deletions src/util/file-util.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import * as fs from "fs"
import * as glob from "glob"
import { sync } from "glob"
import { Settings } from "../settings"

export class FileUtil {
settings: Settings
encoding: BufferEncoding = "utf-8"
globOptions = {dot: true}

constructor(settings = new Settings()) {
this.settings = settings
}

getFilesWithExtensions(path: string): string[] {
let files: string[] = []
for (const extension of this.settings.getExtensions()) {
files = files.concat(sync(`${path}/**/*.${extension}`, this.globOptions))
}
return files
}

readFile(file: string) {
return fs.readFileSync(file, this.encoding).toString()
}
}

/**
* Returns all files in a directory and its subdirectories with extension .yml or .yaml
* @param {string} uri Base URI
* @returns {string[]} List of Yaml files
*/
export function getYamlFilesInDirectory(uri: string): string[] {
return glob.sync(uri + "/**/**.y?(a)ml")
}
2 changes: 1 addition & 1 deletion src/util/sort-util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Settings } from "../settings";
import { Settings } from "../settings"

export class SortUtil {
settings: Settings
Expand Down

0 comments on commit 89a7c4d

Please sign in to comment.