forked from asciidoctor/asciidoctor-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.ts
69 lines (58 loc) · 1.89 KB
/
file.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*---------------------------------------------------------------------------------------------
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode'
import * as ospath from 'path'
import * as fs from 'fs'
export function isAsciidocFile (document: vscode.TextDocument) {
return document.languageId === 'asciidoc'
}
export class FileInfo {
file: string;
isFile: boolean;
constructor (path: string, file: string) {
this.file = file
this.isFile = fs.statSync(ospath.join(path, file)).isFile()
}
}
/**
* @param fileName {string} current filename the look up is done. Absolute path
* @param text {string} text in import string. e.g. './src/'
*/
export function getPathOfFolderToLookupFiles (
fileName: string,
text: string | undefined,
rootPath?: string
): string {
const normalizedText = ospath.normalize(text || '')
const isPathAbsolute = normalizedText.startsWith(ospath.sep)
let rootFolder = ospath.dirname(fileName)
const pathEntered = normalizedText
if (isPathAbsolute) {
rootFolder = rootPath || ''
}
return ospath.join(rootFolder, pathEntered)
}
export async function getChildrenOfPath (path: string) {
try {
const files: string[] = await new Promise((resolve, reject) => {
fs.readdir(path, (err, files) => {
if (err) {
reject(err)
} else {
resolve(files)
}
})
})
const filesDbg = files
.map((f) => new FileInfo(path, f))
return filesDbg
} catch (error) {
return []
}
}
export const sortFilesAndDirectories = (filesAndDirs: FileInfo[]): FileInfo[] => {
const dirs = filesAndDirs.filter((f) => f.isFile !== true)
const files = filesAndDirs.filter((f) => f.isFile === true)
return [...dirs, ...files]
}