Skip to content

Commit 735b3aa

Browse files
authored
Merge pull request #363 from Fdawgs/chore/tidy
2 parents 37b420e + b148274 commit 735b3aa

12 files changed

+105
-166
lines changed

.github/dependabot.yml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ updates:
2424
- "@commitlint*"
2525
eslint:
2626
patterns:
27+
- "@eslint-community*"
2728
- "eslint*"
2829
ignore:
2930
# Below are dependencies that have migrated to ESM

.github/workflows/cd.yml

+6-5
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ jobs:
5353
- name: Publish to NPM
5454
env:
5555
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
56-
# Build docs and TS definitions, and remove dev values
56+
# Build docs/definitions, and remove dev values
5757
# from package.json before publishing to reduce package size
5858
run: |
5959
npm i --ignore-scripts
60-
npm run build
60+
npm run build --if-present
6161
npm pkg delete commitlint devDependencies jest scripts
6262
npm publish --access public --ignore-scripts --provenance
6363
@@ -66,6 +66,7 @@ jobs:
6666
needs: release
6767
if: needs.release.outputs.release_created == 'true'
6868
runs-on: ubuntu-latest
69+
environment: main
6970
permissions:
7071
contents: read
7172
id-token: write
@@ -85,16 +86,16 @@ jobs:
8586

8687
- name: Scope package
8788
run: |
88-
pkgName=$(npm pkg get name | tr -d '"')
89+
pkgName=$(node -p "require('./package.json').name")
8990
npm pkg set name="@fdawgs/$pkgName"
9091
9192
- name: Publish to GitHub Packages
9293
env:
9394
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
94-
# Build docs and TS definitions, and remove dev values
95+
# Build docs/definitions, and remove dev values
9596
# from package.json before publishing to reduce package size
9697
run: |
9798
npm i --ignore-scripts
98-
npm run build
99+
npm run build --if-present
99100
npm pkg delete commitlint devDependencies jest scripts
100101
npm publish --access public --ignore-scripts --provenance

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ jobs:
6969
- name: Run License Checker
7070
run: npm run lint:licenses
7171

72-
- name: Compile TypeScript Definition File
73-
run: npm run build
72+
- name: Run Build
73+
run: npm run build --if-present
7474

7575
commit-lint:
7676
name: Lint Commit Messages

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ dist
135135
# Clinic.js
136136
.clinic
137137

138+
# Auto generated files
139+
types/
140+
138141
# lock files
139142
bun.lockb
140143
package-lock.json

.prettierignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,8 @@ dist
137137

138138
# Auto generated files
139139
package.json
140-
API.md
141140
CHANGELOG.md
142-
/types/index.d.ts
141+
types/
143142

144143
# lock files
145144
bun.lockb

API.md

-59
This file was deleted.

README.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,9 @@ For macOS, the binary can be installed with [Homebrew](https://brew.sh/):
4545
brew install unrtf
4646
```
4747

48-
## API
48+
## Example usage
4949

50-
```js
51-
const { UnRTF } = require("node-unrtf");
52-
```
53-
54-
[**API Documentation can be found here**](https://github.com/Fdawgs/node-unrtf/blob/main/API.md)
55-
56-
## Examples
50+
Please refer to the [JSDoc comments in the source code](./src/index.js) or the [generated type definitions](https://www.npmjs.com/package/node-unrtf?activeTab=code) for information on the available options.
5751

5852
### Async Await
5953

package.json

+7-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"node": ">=18.0.0"
3939
},
4040
"scripts": {
41-
"build": "tsc && jsdoc2md src/index.js > API.md",
41+
"build": "tsc",
4242
"jest": "jest",
4343
"jest:coverage": "jest --coverage",
4444
"lint": "eslint . --cache --ext js,jsx --ignore-path .gitignore",
@@ -74,27 +74,26 @@
7474
"testTimeout": 10000
7575
},
7676
"devDependencies": {
77-
"@commitlint/cli": "^19.0.3",
78-
"@commitlint/config-conventional": "^19.0.3",
77+
"@commitlint/cli": "^19.1.0",
78+
"@commitlint/config-conventional": "^19.1.0",
7979
"@eslint-community/eslint-plugin-eslint-comments": "^4.1.0",
8080
"@types/jest": "^29.5.12",
81-
"eslint": "^8.56.0",
81+
"eslint": "^8.57.0",
8282
"eslint-config-airbnb-base": "^15.0.0",
8383
"eslint-config-prettier": "^9.1.0",
8484
"eslint-plugin-import": "^2.29.1",
8585
"eslint-plugin-jest": "^27.9.0",
86-
"eslint-plugin-jsdoc": "^48.1.0",
86+
"eslint-plugin-jsdoc": "^48.2.1",
8787
"eslint-plugin-promise": "^6.1.1",
88-
"eslint-plugin-regexp": "^2.2.0",
88+
"eslint-plugin-regexp": "^2.3.0",
8989
"eslint-plugin-security": "^2.1.1",
9090
"husky": "^9.0.11",
9191
"is-html": "^2.0.0",
9292
"jest": "^29.7.0",
93-
"jsdoc-to-markdown": "^8.0.1",
9493
"license-checker": "^25.0.1",
9594
"prettier": "^3.2.5",
9695
"spdx-copyleft": "^1.0.0",
97-
"typescript": "^5.3.3"
96+
"typescript": "^5.4.2"
9897
},
9998
"dependencies": {
10099
"semver": "^7.6.0",

src/index.js

+50-28
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ const unrtfPathRegex = /(.+)unrtf/u;
1515
// UnRTF version output is inconsistent between versions but always starts with the semantic version number
1616
const unrtfVersionRegex = /^(\d{1,2}\.\d{1,2}\.\d{1,2})/u;
1717

18+
/** @typedef {{[key: string]: {arg: string, type: string, minVersion: string, maxVersion?: string}}} UnRTFAcceptedOptions */
19+
1820
/**
1921
* @author Frazer Smith
2022
* @description Checks each option provided is valid, of the correct type, and can be used by specified
2123
* version of binary.
2224
* @ignore
23-
* @param {object} acceptedOptions - Object containing accepted options.
24-
* @param {object} options - Object containing options to pass to binary.
25-
* @param {string} [version] - Semantic version of binary.
25+
* @param {UnRTFAcceptedOptions} acceptedOptions - Object containing accepted options.
26+
* @param {{[key: string]: any}} options - Object containing options to pass to binary.
27+
* @param {string} version - Semantic version of binary.
2628
* @returns {string[]} Array of CLI arguments.
2729
* @throws If invalid arguments provided.
2830
*/
@@ -48,23 +50,15 @@ function parseOptions(acceptedOptions, options, version) {
4850
);
4951
}
5052

51-
if (
52-
acceptedOptions[key].minVersion &&
53-
version &&
54-
lt(version, acceptedOptions[key].minVersion)
55-
) {
53+
/* istanbul ignore next: unable to test due to https://github.com/jestjs/jest/pull/14297 */
54+
if (lt(version, acceptedOptions[key].minVersion)) {
5655
invalidArgs.push(
5756
`Invalid option provided for the current version of the binary used. '${key}' was introduced in v${acceptedOptions[key].minVersion}, but received v${version}`
5857
);
5958
}
6059

61-
/* istanbul ignore next: requires incredibly old version of UnRTF to test */
62-
if (
63-
acceptedOptions[key].maxVersion &&
64-
version &&
65-
// @ts-ignore: type checking is done above
66-
gt(version, acceptedOptions[key].maxVersion)
67-
) {
60+
/* istanbul ignore next: unable to test due to https://github.com/jestjs/jest/pull/14297 */
61+
if (gt(version, acceptedOptions[key].maxVersion || version)) {
6862
invalidArgs.push(
6963
`Invalid option provided for the current version of the binary used. '${key}' is only present up to v${acceptedOptions[key].maxVersion}, but received v${version}`
7064
);
@@ -80,6 +74,10 @@ function parseOptions(acceptedOptions, options, version) {
8074
}
8175

8276
class UnRTF {
77+
#unrtfPath;
78+
79+
#unrtfVersion;
80+
8381
/**
8482
* @param {string} [binPath] - Path of UnRTF binary.
8583
* If not provided, the constructor will attempt to find the binary
@@ -89,10 +87,12 @@ class UnRTF {
8987
* if a local installation is not found.
9088
*/
9189
constructor(binPath) {
90+
this.#unrtfPath = "";
91+
9292
/* istanbul ignore else: requires specific OS */
9393
if (binPath) {
9494
/** @type {string|undefined} */
95-
this.unrtfPath = binPath;
95+
this.#unrtfPath = binPath;
9696
} else {
9797
const { platform } = process;
9898

@@ -102,10 +102,10 @@ class UnRTF {
102102
const unrtfPath = unrtfPathRegex.exec(which)?.[1];
103103

104104
if (unrtfPath) {
105-
this.unrtfPath = unrtfPath;
105+
this.#unrtfPath = unrtfPath;
106106
}
107107
if (platform === "win32" && !unrtfPath) {
108-
this.unrtfPath = joinSafe(
108+
this.#unrtfPath = joinSafe(
109109
__dirname,
110110
"lib",
111111
"win32",
@@ -115,24 +115,29 @@ class UnRTF {
115115
}
116116
}
117117

118-
if (!this.unrtfPath) {
118+
/* istanbul ignore next: unable to test due to https://github.com/jestjs/jest/pull/14297 */
119+
if (!this.#unrtfPath) {
119120
throw new Error(
120121
`Unable to find ${process.platform} UnRTF binaries, please pass the installation directory as a parameter to the UnRTF instance.`
121122
);
122123
}
123-
this.unrtfPath = normalizeTrim(this.unrtfPath);
124+
this.#unrtfPath = normalizeTrim(this.#unrtfPath);
124125

125126
/**
126127
* Get version of UnRTF binary for use in `convert` function.
127128
* UnRTF outputs the version into stderr.
128129
*/
129-
const version = spawnSync(joinSafe(this.unrtfPath, "unrtf"), [
130+
const version = spawnSync(joinSafe(this.#unrtfPath, "unrtf"), [
130131
"--version",
131132
]).stderr.toString();
132-
/** @type {string|undefined} */
133-
this.unrtfVersion = unrtfVersionRegex.exec(version)?.[1];
133+
this.#unrtfVersion = unrtfVersionRegex.exec(version)?.[1] || "";
134134

135-
/** @type {object} */
135+
/* istanbul ignore next: unable to test due to https://github.com/jestjs/jest/pull/14297 */
136+
if (!this.#unrtfVersion) {
137+
throw new Error("Unable to determine UnRTF version.");
138+
}
139+
140+
/** @type {UnRTFAcceptedOptions} */
136141
this.unrtfAcceptedOptions = {
137142
noPictures: {
138143
arg: "--nopict",
@@ -178,6 +183,22 @@ class UnRTF {
178183
};
179184
}
180185

186+
/**
187+
* @description Returns the path of the UnRTF binary.
188+
* @returns {string} Path of UnRTF binary.
189+
*/
190+
get path() {
191+
return this.#unrtfPath;
192+
}
193+
194+
/**
195+
* @description Returns the version of the UnRTF binary.
196+
* @returns {string} Version of UnRTF binary.
197+
*/
198+
get version() {
199+
return this.#unrtfVersion;
200+
}
201+
181202
/**
182203
* @author Frazer Smith
183204
* @description Converts an RTF file to HTML/LaTeX/RTF/TXT.
@@ -222,12 +243,12 @@ class UnRTF {
222243
const args = parseOptions(
223244
this.unrtfAcceptedOptions,
224245
options,
225-
this.unrtfVersion
246+
this.#unrtfVersion
226247
);
227248
args.push(normalizeTrim(file));
228249

229250
return new Promise((resolve, reject) => {
230-
const child = spawn(joinSafe(this.unrtfPath, "unrtf"), args);
251+
const child = spawn(joinSafe(this.#unrtfPath, "unrtf"), args);
231252

232253
let stdOut = "";
233254
let stdErr = "";
@@ -247,6 +268,7 @@ class UnRTF {
247268
} else if (stdErr === "") {
248269
reject(
249270
new Error(
271+
// @ts-ignore: Second operand used if code is not in errorMessages
250272
errorMessages[code] ||
251273
`unrtf ${args.join(
252274
" "
@@ -261,5 +283,5 @@ class UnRTF {
261283
}
262284
}
263285

264-
module.exports.UnRTF = UnRTF;
265-
module.exports.default = UnRTF;
286+
module.exports.default = UnRTF; // ESM default export
287+
module.exports.UnRTF = UnRTF; // TypeScript and named export

0 commit comments

Comments
 (0)