Skip to content

Commit f51a006

Browse files
committed
object_arg_parser: replacing file paths by URLs
in the hope to improve windows support, possibly addressing #186
1 parent 6d0c659 commit f51a006

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

lib/object_arg_parser.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { existsSync } from 'node:fs'
12
import { red } from '#lib/chalk'
23
import { readJsonFile } from '#lib/json'
3-
import { isFilePathSync, isJsonString, getAbsoluteFilePath, validateTemplateCommand } from '#lib/utils'
4+
import { isJsonString, getAbsoluteFileUrl, validateTemplateCommand } from '#lib/utils'
45
import { parseGuid } from './parse_command_utils.js'
56
import program from './program.js'
67
import validateFunctionArgs from './validate_function_args.js'
@@ -20,22 +21,22 @@ const getData = args => {
2021
return JSON.parse(arg)
2122
}
2223

23-
const filepath = getAbsoluteFilePath(arg)
24+
const fileUrl = getAbsoluteFileUrl(arg)
2425

25-
if (!isFilePathSync(filepath)) {
26+
if (!existsSync(fileUrl)) {
2627
console.error(red('the argument should be a valid JSON or a JSON file path or a JS function file path'))
2728
console.error("- it doesn't look like inline JSON")
28-
console.error(`couldn't parse arguments: ${filepath} is not the path to an existing file`)
29+
console.error(`couldn't parse arguments: ${fileUrl} is not the path to an existing file`)
2930
process.exit(1)
3031
}
3132

3233
try {
3334
// Try to parse it as a JSON file
34-
return readJsonFile(filepath)
35+
return readJsonFile(fileUrl)
3536
} catch (err1) {
3637
// Try to parse it as a JS module
3738
try {
38-
return getDataFromJsModule(filepath, args)
39+
return getDataFromJsModule(fileUrl, args)
3940
} catch (err2) {
4041
if (err2 === 'SyntaxError') {
4142
console.error(red('the argument should be a valid JSON or a JSON file path or a JS function file path'))
@@ -50,8 +51,8 @@ const getData = args => {
5051
}
5152
}
5253

53-
async function getDataFromJsModule (filepath, args) {
54-
const { default: jsModule } = await import(filepath)
54+
async function getDataFromJsModule (fileUrl, args) {
55+
const { default: jsModule } = await import(fileUrl)
5556
if (typeof jsModule === 'function') {
5657
const inputArgs = args.slice(1)
5758
validateFunctionArgs(jsModule, inputArgs, jsModule)

lib/utils.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
import { existsSync } from 'node:fs'
22
import path from 'node:path'
3+
import { pathToFileURL } from 'node:url'
34
import errors_ from './errors.js'
45

56
export const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
67

78
export const sum = (a, b) => a + b
89

9-
export const average = values => {
10+
export function average (values) {
1011
if (values.length > 0) return values.reduce(sum, 0) / values.length
1112
else return 0
1213
}
1314

14-
export const getAbsoluteFilePath = filepath => path.resolve(process.cwd(), filepath)
15+
export const getAbsoluteFileUrl = filepath => pathToFileURL(path.resolve(process.cwd(), filepath))
1516

16-
export const isFilePathSync = arg => {
17-
const possibleFilePath = getAbsoluteFilePath(arg)
18-
return existsSync(possibleFilePath)
17+
export function isFilePathSync (arg) {
18+
const possibleFileUrl = getAbsoluteFileUrl(arg)
19+
return existsSync(possibleFileUrl)
1920
}
2021

21-
export const isJsonString = str => {
22+
export function isJsonString (str) {
2223
if (typeof str !== 'string') return false
2324
else return (str.trim()[0] === '{' || str.trim()[0] === '[')
2425
}
2526

26-
export const validateTemplateCommand = ({ commandName, validCommands }) => {
27+
export function validateTemplateCommand ({ commandName, validCommands }) {
2728
if (!validCommands) return
2829
if (validCommands && !validCommands.includes(commandName)) {
2930
throw errors_.exitMessage('wrong command for this template', { commandName, validCommands })

0 commit comments

Comments
 (0)