Skip to content

Commit

Permalink
feat(uniconfig-plugin-api-file): check error, use static import for fs (
Browse files Browse the repository at this point in the history
#478)

BREAKING CHANGE: fs is now required statically in uniconfig-plugin-api-file
  • Loading branch information
oljekechoro authored Sep 18, 2023
1 parent 5c7bdc0 commit bcdd30a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/uniconfig-plugin-api-file/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"types": "target/es5/index.d.ts",
"scripts": {
"clean": "rm -rf target",
"jest": "NODE_ENV=test BAZ=baz run -T jest -w 1 --config jest.config.json",
"jest": "NODE_ENV=test BAZ=baz jest -w 1 --config jest.config.json",
"test": "yarn run jest",
"build:es5": "mkdir -p target/es5 && run -T tsc -p tsconfig.build.json --target ES5 --outDir target/es5 -m CommonJS",
"build:es6": "mkdir -p target/es6 && run -T tsc -p tsconfig.build.json --target ES6 --outDir target/es6",
Expand Down
21 changes: 15 additions & 6 deletions packages/uniconfig-plugin-api-file/src/main/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import {
INamedPipe,
IContext,
} from '@qiwi/uniconfig-core'
import {readFile, readFileSync, promises} from 'node:fs'

export type IFsOpts = {
encoding: string,
encoding: BufferEncoding,
flag?: string
}

Expand All @@ -25,9 +26,13 @@ export const pipe: INamedPipe = {
if (Array.isArray(target)) {
for (const path of target) {
try {
return require('fs').readFileSync(path, processOpts(opts))
return readFileSync(path, processOpts(opts))
}
catch (e: any) {
if (e.code !== 'ENOENT') {
throw e
}
}
catch { /* noop */ }
}
throw new Error(`All targets are unreachable (${target})`)
}
Expand All @@ -38,15 +43,19 @@ export const pipe: INamedPipe = {
if (Array.isArray(target)) {
for (const path of target) {
try {
resolve(await require('fs/promises').readFile(path, processOpts(opts)))
resolve(await promises.readFile(path, processOpts(opts)))
return
}
catch (e) { /* noop */ }
catch (e: any) {
if (e.code !== 'ENOENT') {
throw e
}
}
}
reject(new Error(`All targets are unreachable (${target})`))
return
}
require('fs').readFile(target, processOpts(opts), (err: IAny | null, data: IAny) => {
readFile(target, processOpts(opts), (err: IAny | null, data: IAny) => {
if (err) {
reject(err)
}
Expand Down

0 comments on commit bcdd30a

Please sign in to comment.