Skip to content

Commit 549f568

Browse files
committed
Add support for CSV input file
1 parent 84dd3c2 commit 549f568

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

bin/test-redirection.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { resolve } from 'path';
44
import chalk from 'chalk';
55
import { cac } from 'cac';
66
import run from '../src/index.js';
7-
import { importJson } from '../src/utils.js';
7+
import { importJson, importCsv } from '../src/utils.js';
88

99
const pkg = importJson('../package.json', import.meta.url);
1010
const PKG_NAME = pkg.name;
@@ -29,6 +29,8 @@ cli
2929
default: 100,
3030
})
3131
.option('--ignore-query-parameters', 'Ignore query parameters in the final URL.')
32+
.option('-p, --parser [parser]', 'Define which parser should be used: json or csv.')
33+
.option('--csv-delimiter [delimiter]', 'Define the delimiter of the input CSV file, can be a string or a RegExp.')
3234
.action((configPath, options) => {
3335
const resolvedConfigPath = resolve(process.cwd(), configPath);
3436

@@ -39,7 +41,16 @@ cli
3941
process.exit(1);
4042
}
4143

42-
const config = importJson(resolvedConfigPath, import.meta.url);
44+
let config;
45+
46+
if (options.parser === 'csv') {
47+
config = importCsv(resolvedConfigPath, import.meta.url, {
48+
delimiter: new RegExp(options.csvDelimiter)
49+
});
50+
} else {
51+
config = importJson(resolvedConfigPath, import.meta.url);
52+
}
53+
4354
run(config, options);
4455
});
4556

src/utils.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { readFileSync } from 'fs';
22

3+
/**
4+
* Import a file.
5+
* @param {string} path The path to the file.
6+
* @param {string} base The base path from which the import should be resolved.
7+
* @returns {string}
8+
*/
9+
export function importFile(path, base) {
10+
return readFileSync(new URL(path, base)).toString();
11+
}
12+
313
/**
414
* Import a JSON file.
515
*
@@ -8,7 +18,35 @@ import { readFileSync } from 'fs';
818
* @return {Object}
919
*/
1020
export function importJson(path, base) {
11-
return JSON.parse(readFileSync(new URL(path, base)).toString());
21+
return JSON.parse(importFile(path, base));
22+
}
23+
24+
/**
25+
* Parse a CSV file.
26+
* @param {string} csv
27+
* @returns {Array<any>}
28+
* @param {{ delimiter?: string|RegExp }} [options]
29+
* @returns {Array<{ from: string, to: string }>}
30+
*/
31+
export function csvToJson(csv, { delimiter = ' ' } = {}) {
32+
return csv.split('\n').filter(Boolean).map((line) => {
33+
const [from, to] = line.split(delimiter);
34+
return {
35+
from: from ?? '',
36+
to: to ?? '',
37+
};
38+
});
39+
}
40+
41+
/**
42+
* Import a CSV file as a config object.
43+
* @param {string} path
44+
* @param {string} base
45+
* @param {{ delimiter?: string|RegExp }} [options]
46+
* @returns {Array<{ from: string, to: string }>}
47+
*/
48+
export function importCsv(path, base, options) {
49+
return csvToJson(importFile(path, base), options);
1250
}
1351

1452
/**
@@ -18,7 +56,7 @@ export function importJson(path, base) {
1856
* @return {Promise<number>}
1957
*/
2058
export async function wait(delay = 0) {
21-
return new Promise((resolve) => {
22-
setTimeout(() => resolve(delay), delay);
23-
});
24-
}
59+
return new Promise((resolve) => {
60+
setTimeout(() => resolve(delay), delay);
61+
});
62+
}

0 commit comments

Comments
 (0)