Skip to content
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

chore: to replace fs.access with fs.exist #1890

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/csv-parser-discount-code/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import yargs from 'yargs'

import CsvParserDiscountCode from './main'

const doesFileExist = (filePath) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a little observation, it will be nice if we can add a simple unit test to test this function.

try {
fs.accessSync(filePath)
return true
} catch (e) {
return false
}
}

process.title = 'csvParserDiscountCode'

const args = yargs
Expand All @@ -23,7 +32,7 @@ Converts commercetools discount code data from CSV to JSON.`
.coerce('input', (arg) => {
if (arg === 'stdin') return process.stdin

if (fs.existsSync(arg)) {
if (doesFileExist(arg)) {
if (arg.match(/\.csv$/i)) return fs.createReadStream(String(arg))

throw new Error('Invalid input file format. Must be CSV file')
Expand Down
11 changes: 10 additions & 1 deletion packages/csv-parser-state/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import yargs from 'yargs'
import CsvParserState from './main'
import { description } from '../package.json'

const doesFileExist = (filePath) => {
try {
fs.accessSync(filePath)
return true
} catch (e) {
return false
}
}

process.title = 'csv-parser-state'

const args = yargs
Expand Down Expand Up @@ -44,7 +53,7 @@ Required scopes: ['view_orders']`,
.coerce('input', (arg) => {
if (arg === 'stdin') return process.stdin

if (fs.existsSync(arg)) {
if (doesFileExist(arg)) {
if (arg.match(/\.csv$/i)) return fs.createReadStream(String(arg))

throw new Error('Invalid input file format. Must be CSV file')
Expand Down
11 changes: 10 additions & 1 deletion packages/discount-code-exporter/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import yargs from 'yargs'
import DiscountCodeExport from './main'
import { description } from '../package.json'

const doesFileExist = (filePath) => {
try {
fs.accessSync(filePath)
return true
} catch (e) {
return false
}
}

process.title = 'discount-code-exporter'

const args = yargs
Expand All @@ -28,7 +37,7 @@ ${description}`
'Language used for localised fields (such as `name` and `description`) when exporting without template. This field is ignored for exports with template',
})
.coerce('template', (arg) => {
if (fs.existsSync(arg)) return fs.createReadStream(String(arg))
if (doesFileExist(arg)) return fs.createReadStream(String(arg))

throw new Error('Input file cannot be reached or does not exist')
})
Expand Down
11 changes: 10 additions & 1 deletion packages/discount-code-generator/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import flatten, { unflatten } from 'flat'
import discountCodeGenerator from './main'
import prepareInput from './utils'

const doesFileExist = (filePath) => {
try {
fs.accessSync(filePath)
return true
} catch (e) {
return false
}
}

process.title = 'discountCodeGenerator'

const args = yargs
Expand Down Expand Up @@ -47,7 +56,7 @@ Generate multiple discount codes to import to the commercetools platform.`
describe: 'Path to code options CSV or JSON file.',
})
.coerce('input', (arg) => {
if (fs.existsSync(arg)) {
if (doesFileExist(arg)) {
if (arg.match(/\.json$/i) || arg.match(/\.csv$/i)) return String(arg)

throw new Error('Invalid input file format. Must be CSV or JSON')
Expand Down
11 changes: 10 additions & 1 deletion packages/inventories-exporter/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import CONSTANTS from './constants'
import InventoryExporter from './main'
import { description } from '../package.json'

const doesFileExist = (filePath) => {
try {
fs.accessSync(filePath)
return true
} catch (e) {
return false
}
}

process.title = 'inventories-exporter'

const args = yargs
Expand Down Expand Up @@ -76,7 +85,7 @@ can be used with channelKey flag
.coerce('template', (arg) => {
const filePath = String(arg)

if (fs.existsSync(arg)) {
if (doesFileExist(arg)) {
if (arg.match(/\.csv$/i)) return fs.createReadStream(filePath)

throw new Error('Invalid file format of a CSV template. Must be CSV file')
Expand Down
11 changes: 10 additions & 1 deletion packages/price-exporter/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import yargs from 'yargs'
import PriceExporter from './main'
import { description } from '../package.json'

const doesFileExist = (filePath) => {
try {
fs.accessSync(filePath)
return true
} catch (e) {
return false
}
}

process.title = 'price-exporter'

const args = yargs
Expand All @@ -22,7 +31,7 @@ ${description}`
describe: 'Path to CSV template.',
})
.coerce('input', (arg) => {
if (fs.existsSync(arg)) return fs.createReadStream(String(arg))
if (doesFileExist(arg)) return fs.createReadStream(String(arg))

throw new Error('Input file cannot be reached or does not exist')
})
Expand Down
13 changes: 11 additions & 2 deletions packages/product-json-to-csv/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import yargs from 'yargs'
import ProductJsonToCsv from './main'
import { description } from '../package.json'

const doesFileExist = (filePath) => {
try {
fs.accessSync(filePath)
return true
} catch (e) {
return false
}
}

process.title = 'product-json-to-csv'

const args = yargs
Expand Down Expand Up @@ -44,7 +53,7 @@ Required scopes: ['view_products']`,
'CSV file containing your header that defines what you want to export.',
})
.coerce('template', (arg) => {
if (fs.existsSync(arg)) {
if (doesFileExist(arg)) {
if (arg.match(/\.csv$/i)) return fs.createReadStream(String(arg))

throw new Error('Invalid file format. Must be CSV file')
Expand All @@ -59,7 +68,7 @@ Required scopes: ['view_products']`,
.coerce('input', (arg) => {
if (arg === 'stdin') return process.stdin

if (fs.existsSync(arg)) {
if (doesFileExist(arg)) {
if (arg.match(/\.json$/i)) return fs.createReadStream(String(arg))

throw new Error('Invalid input file format. Must be JSON file')
Expand Down
13 changes: 11 additions & 2 deletions packages/product-json-to-xlsx/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import yargs from 'yargs'
import ProductJsonToXlsx from './main'
import { description } from '../package.json'

const doesFileExist = (filePath) => {
try {
fs.accessSync(filePath)
return true
} catch (e) {
return false
}
}

process.title = 'product-json-to-xlsx'

const args = yargs
Expand Down Expand Up @@ -44,7 +53,7 @@ Required scopes: ['view_products']`,
'CSV file containing your header that defines what you want to export.',
})
.coerce('template', (arg) => {
if (fs.existsSync(arg)) {
if (doesFileExist(arg)) {
if (arg.match(/\.csv$/i)) return fs.createReadStream(String(arg))

throw new Error('Invalid file format. Must be CSV file')
Expand All @@ -59,7 +68,7 @@ Required scopes: ['view_products']`,
.coerce('input', (arg) => {
if (arg === 'stdin') return process.stdin

if (fs.existsSync(arg)) {
if (doesFileExist(arg)) {
if (arg.match(/\.json$/i)) return fs.createReadStream(String(arg))

throw new Error('Invalid input file format. Must be JSON file')
Expand Down
Loading