-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
file.ts
164 lines (137 loc) · 3.91 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import fs from 'fs'
import getStdin from 'get-stdin'
import globby from 'globby'
import mkdirp from 'mkdirp'
import path from 'path'
import * as url from 'url'
import { tmpName } from 'tmp'
import { promisify } from 'util'
const mkdirpPromise = promisify<string, any>(mkdirp)
const readFile = promisify(fs.readFile)
const tmpNamePromise = promisify(tmpName)
const unlink = promisify(fs.unlink)
const writeFile = promisify(fs.writeFile)
export const markdownExtensions = ['md', 'mdown', 'markdown', 'markdn']
export enum FileType {
File,
StandardIO,
Null,
}
export class File {
buffer?: Buffer
inputDir?: string
type: FileType = FileType.File
readonly path: string
constructor(filepath: string) {
this.path = filepath
}
get absolutePath(): string {
return path.resolve(this.path)
}
get absoluteFileScheme(): string {
if (url.pathToFileURL)
return url.pathToFileURL(this.absolutePath).toString()
// Fallback for Node < v10.12.0
return `file://${path.posix.resolve(this.path)}`
}
convert(output: string | false | undefined, extension: string): File {
switch (output) {
case undefined:
return File.initialize(
this.convertExtension(extension),
f => (f.type = this.type)
)
case false:
return File.initialize(this.path, f => (f.type = FileType.Null))
case '-':
return File.initialize('-', f => (f.type = FileType.StandardIO))
}
if (this.inputDir)
return File.initialize(
this.convertExtension(
extension,
path.join(output, this.relativePath(this.inputDir))
)
)
return File.initialize(output)
}
async load() {
this.buffer = this.buffer || (await readFile(this.path))
return this.buffer
}
relativePath(from: string = process.cwd()) {
return path.relative(from, this.absolutePath)
}
async save() {
switch (this.type) {
case FileType.File:
await this.saveToFile()
break
case FileType.StandardIO:
process.stdout.write(this.buffer!)
}
}
async saveTmpFile(ext?: string): Promise<File.TmpFileInterface> {
const tmp: string = await tmpNamePromise({ postfix: ext })
await this.saveToFile(tmp)
return {
cleanup: async () => {
try {
await this.cleanup(tmp)
} catch (e) {}
},
path: tmp,
}
}
private cleanup(tmpPath: string) {
return unlink(tmpPath)
}
private convertExtension(extension: string, basePath = this.path): string {
return path.join(
path.dirname(basePath),
`${path.basename(basePath, path.extname(basePath))}.${extension}`
)
}
private async saveToFile(savePath: string = this.path) {
await mkdirpPromise(path.dirname(path.resolve(savePath)))
await writeFile(savePath, this.buffer)
}
private static stdinBuffer?: Buffer
static async find(...pathes: string[]): Promise<File[]> {
return (await globby(pathes, {
absolute: true,
expandDirectories: {
extensions: [],
files: markdownExtensions.map(ext => `*.${ext}`),
},
ignore: ['**/node_modules'],
})).map(p => new File(p))
}
static async findDir(directory: string): Promise<File[]> {
const found = await this.find(directory)
found.forEach(p => (p.inputDir = path.resolve(directory)))
return found
}
static async stdin(): Promise<File | undefined> {
this.stdinBuffer = this.stdinBuffer || (await getStdin.buffer())
if (this.stdinBuffer.length === 0) return undefined
return this.initialize('-', f => {
f.buffer = this.stdinBuffer
f.type = FileType.StandardIO
})
}
private static initialize(
filepath: string,
tap: (instance: File) => void = () => {}
) {
const instance = new this(filepath)
tap(instance)
return instance
}
}
export namespace File {
export interface TmpFileInterface {
path: string
cleanup: () => Promise<void>
}
}