Skip to content

Commit

Permalink
fix(index): return promise objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Frazer Smith committed Nov 10, 2020
1 parent 59bb581 commit 863d9f7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 40 deletions.
63 changes: 33 additions & 30 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
export class UnRTF {
/**
* @param {string=} binPath - Path of UnRTF binary.
* Useful for Linux users who have UnRTF binary already installed.
*/
constructor(binPath?: string | undefined);
unrtfPath: string;
/**
* @author Frazer Smith
* @description Converts an RTF file to HTML/LaTeX/RTF/TXT.
* UnRTF will use the directory of the original file to store embedded pictures.
* @param {string} file - Filepath of the RTF file to read.
* @param {object=} options - Object containing options to pass to binary.
* @param {boolean=} options.noPictures - Disable the automatic storing of embedded
* pictures to the directory of the original file.
* @param {boolean=} options.noRemap - Disable charset conversion (only works for 8-bit charsets)
* (UnRTF v0.20.5 or later only).
* @param {boolean=} options.outputHtml - Generate HTML output.
* @param {boolean=} options.outputLatex - Generate LaTeX output.
* @param {boolean=} options.outputPs - Generate PostScript (PS) output (UnRTF v0.19.4 or earlier only).
* @param {boolean=} options.outputRtf - Generate RTF output. (UnRTF v0.21.3 or later only).
* @param {boolean=} options.outputText - Generate ASCII text output.
* @param {boolean=} options.outputVt - Generate text output with VT100 escape codes.
* @param {boolean=} options.outputWpml - Generate WPML output (UnRTF v0.19.4 or earlier only).
* @param {boolean=} options.printVersionInfo - Print copyright and version info.
* @param {boolean=} options.quiet - Do not print any leading comments in output (UnRTF v0.21.3 or later only).
* @returns {Promise<string|Error>} Promise of stdout string on resolve, or Error object on rejection.
*/
convert(file: string, options?: object | undefined): Promise<string | Error>;
}
export class UnRTF {
/**
* @param {string=} binPath - Path of UnRTF binary.
* Useful for Linux users who have UnRTF binary already installed.
*/
constructor(binPath?: string | undefined);
unrtfPath: string;
/**
* @author Frazer Smith
* @description Converts an RTF file to HTML/LaTeX/RTF/TXT.
* UnRTF will use the directory of the original file to store embedded pictures.
* @param {string} file - Filepath of the RTF file to read.
* @param {object=} options - Object containing options to pass to binary.
* @param {boolean=} options.noPictures - Disable the automatic storing of embedded
* pictures to the directory of the original file.
* @param {boolean=} options.noRemap - Disable charset conversion (only works for 8-bit charsets)
* (UnRTF v0.20.5 or later only).
* @param {boolean=} options.outputHtml - Generate HTML output.
* @param {boolean=} options.outputLatex - Generate LaTeX output.
* @param {boolean=} options.outputPs - Generate PostScript (PS) output (UnRTF v0.19.4 or earlier only).
* @param {boolean=} options.outputRtf - Generate RTF output. (UnRTF v0.21.3 or later only).
* @param {boolean=} options.outputText - Generate ASCII text output.
* @param {boolean=} options.outputVt - Generate text output with VT100 escape codes.
* @param {boolean=} options.outputWpml - Generate WPML output (UnRTF v0.19.4 or earlier only).
* @param {boolean=} options.printVersionInfo - Print copyright and version info.
* @param {boolean=} options.quiet - Do not print any leading comments in output (UnRTF v0.21.3 or later only).
* @returns {Promise<string|Error>} Promise of stdout string on resolve, or Error object on rejection.
*/
convert(
file: string,
options?: object | undefined
): Promise<string | Error>;
}
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,22 @@ class UnRTF {
printVersionInfo: { arg: '--version', type: 'boolean' }
};

// UnRTF still attempts to convert empty strings/files, so catch them here before
if (file === undefined || fs.existsSync(file) === false) {
throw new Error('File missing');
}

try {
// UnRTF still attempts to convert empty strings/files, so catch them here before
if (file === undefined || fs.existsSync(file) === false) {
throw new Error('File missing');
}

const args = await parseOptions(acceptedOptions, options);
args.push(file);

const { stdout } = await execFileAsync(
path.join(this.unrtfPath, 'unrtf'),
args
);
return stdout;
return Promise.resolve(stdout);
} catch (err) {
return err;
return Promise.reject(err);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ describe('convert function', () => {
outputHtml: 'sure'
};

expect.assertions(1);
await unRtf.convert(file, options).catch((err) => {
expect(err.message).toEqual(
"Invalid value type provided for option 'outputHtml', expected boolean but recieved string"
Expand All @@ -118,6 +119,7 @@ describe('convert function', () => {
outputMp3: true
};

expect.assertions(1);
await unRtf.convert(file, options).catch((err) => {
expect(err.message).toEqual("Invalid option provided 'outputMp3'");
});
Expand All @@ -130,10 +132,9 @@ describe('convert function', () => {
outputHtml: 'sure'
};

expect.assertions(1);
await unRtf.convert(undefined, options).catch((err) => {
expect(err.message).toEqual(
"File missing"
);
expect(err.message).toEqual('File missing');
});
});
});

0 comments on commit 863d9f7

Please sign in to comment.