|
11 | 11 | const fs = require('fs');
|
12 | 12 | const path = require('path');
|
13 | 13 |
|
14 |
| -const [search, replace, file] = process.argv.slice(2); |
| 14 | +const [search, replace, ...files] = process.argv.slice(2); |
15 | 15 |
|
16 |
| -if (!search || !replace || !file) { |
| 16 | +if (!search || !replace || !files.length) { |
17 | 17 | // The path of the script relative to the directory where the user ran the
|
18 | 18 | // script to be used in the error message demonstrating the usage.
|
19 | 19 | const scriptPath = path.relative(process.cwd(), __filename);
|
20 | 20 |
|
21 | 21 | console.error('Missing arguments.');
|
22 |
| - console.table({ search, replace, file }); |
| 22 | + console.table({ search, replace, files }); |
23 | 23 |
|
24 |
| - console.error(`Usage: node ${scriptPath} <search> <replace> <file>`); |
| 24 | + console.error(`Usage: node ${scriptPath} <search> <replace> <files...>`); |
25 | 25 | process.exit(1);
|
26 | 26 | }
|
27 | 27 |
|
28 |
| -try { |
29 |
| - const filePath = path.resolve(process.cwd(), file); |
| 28 | +for (const file of files) { |
| 29 | + try { |
| 30 | + const filePath = path.resolve(process.cwd(), file); |
30 | 31 |
|
31 |
| - const fileContent = fs.readFileSync(filePath, 'utf8'); |
| 32 | + const fileContent = fs.readFileSync(filePath, 'utf8'); |
32 | 33 |
|
33 |
| - const newContent = fileContent.replaceAll(search, replace); |
| 34 | + const newContent = fileContent.replaceAll(search, replace); |
34 | 35 |
|
35 |
| - fs.writeFileSync(filePath, newContent); |
36 |
| -} catch (error) { |
37 |
| - console.error('An error occurred while replacing the content of the file.'); |
38 |
| - console.error(error); |
39 |
| - process.exit(1); |
| 36 | + fs.writeFileSync(filePath, newContent); |
| 37 | + } catch (error) { |
| 38 | + console.error(`An error occurred while replacing the content of the file: ${file}.`); |
| 39 | + console.error(error); |
| 40 | + process.exit(1); |
| 41 | + } |
40 | 42 | }
|
0 commit comments