-
-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support conversion from standard input #4
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6f5f574
Support pipe from stdin with refactor File class
yhatt bc3c131
Fix unavailable tests
yhatt 161bb29
Increase global jest timeout seconds to 10sec
yhatt 71e7673
Revert "Increase global jest timeout seconds to 10sec"
yhatt a278339
Refactor File class
yhatt f2a0f5f
Update .circleci/config.yml to be able to clear cache
yhatt 53b700c
Improve test mock of CLI interface to work on CircleCI's 6.14.2 build
yhatt 505592d
Stop mocking process.exit on help option
yhatt dd1fdf4
Use mockImplementationOnce instead of mockImplementation on mocking p…
yhatt 77ba7f8
Use runInBand option while running test on CircleCI
yhatt 115e9ef
Add test of stdin input
yhatt 981bf2b
Update CHANGELOG.md
yhatt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import fs from 'fs' | ||
import getStdin from 'get-stdin' | ||
import globby from 'globby' | ||
import path from 'path' | ||
|
||
const markdownExtensions = ['*.md', '*.mdown', '*.markdown', '*.markdn'] | ||
|
||
export enum FileType { | ||
File, | ||
StandardIO, | ||
} | ||
|
||
export class File { | ||
buffer?: Buffer | ||
type: FileType = FileType.File | ||
readonly path: string | ||
|
||
constructor(filepath: string) { | ||
this.path = filepath | ||
} | ||
|
||
get absolutePath() { | ||
return path.resolve(this.path) | ||
} | ||
|
||
convert(output: string | undefined, extension: string): File { | ||
if (output === undefined) | ||
return File.initialize( | ||
this.convertExtension(extension), | ||
f => (f.type = this.type) | ||
) | ||
|
||
if (output === '-') | ||
return File.initialize('-', f => (f.type = FileType.StandardIO)) | ||
|
||
return File.initialize(output) | ||
} | ||
|
||
async load() { | ||
this.buffer = | ||
this.buffer || | ||
(await new Promise<Buffer>((resolve, reject) => | ||
fs.readFile(this.path, (e, buf) => (e ? reject(e) : resolve(buf))) | ||
)) | ||
|
||
return this.buffer | ||
} | ||
|
||
relativePath(from: string = process.cwd()) { | ||
return path.relative(from, this.absolutePath) | ||
} | ||
|
||
async save() { | ||
switch (this.type) { | ||
case FileType.File: | ||
await new Promise<void>((resolve, reject) => | ||
fs.writeFile(this.path, this.buffer, e => (e ? reject(e) : resolve())) | ||
) | ||
break | ||
case FileType.StandardIO: | ||
process.stdout.write(this.buffer!) | ||
} | ||
} | ||
|
||
private convertExtension(extension: string): string { | ||
return path.join( | ||
path.dirname(this.path), | ||
`${path.basename(this.path, path.extname(this.path))}.${extension}` | ||
) | ||
} | ||
|
||
private static stdinBuffer?: Buffer | ||
|
||
static async find(...pathes: string[]): Promise<File[]> { | ||
return (await globby(pathes, { | ||
absolute: true, | ||
expandDirectories: { files: markdownExtensions }, | ||
})).map(path => new File(path)) | ||
} | ||
|
||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By this change, we no longer restrict to output PDF into stdout. The default output path will decide by where came input from.