Skip to content

Commit a1833d1

Browse files
committed
overwrite file contents
1 parent 74d6028 commit a1833d1

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

tasks/rename_files.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ task('rename-files', 'Batch replace text in local filenames')
1111

1212
const files = (
1313
await Promise.all(
14-
directories.map(async (dir) => {
15-
const files = await fs.promises.readdir(dir, { recursive: true });
16-
return files.map((file) => path.resolve(dir, file));
17-
}),
14+
directories.map(async (dir) =>
15+
(await fs.promises.readdir(dir, { recursive: true })).map((file) =>
16+
path.resolve(dir, file),
17+
),
18+
),
1819
)
1920
)
2021
.flat()
@@ -28,19 +29,34 @@ task('rename-files', 'Batch replace text in local filenames')
2829

2930
console.log(`Found ${files.length} files to rename:`);
3031

32+
const fileContents: { [file: string]: string } = {};
33+
34+
await Promise.all(
35+
files.map(
36+
async (f) =>
37+
(fileContents[f] = (await fs.promises.readFile(f)).toString()),
38+
),
39+
);
40+
3141
for (const oldName of files) {
3242
const newName = path.resolve(
3343
path.dirname(oldName),
3444
path.basename(oldName).replace(args.oldText, args.newText),
3545
);
3646

47+
const newContents = fileContents[oldName].replaceAll(
48+
args.oldText,
49+
args.newText,
50+
);
51+
3752
console.log(
3853
path.relative('.', oldName),
3954
'=>',
4055
path.relative('.', newName),
4156
);
4257

4358
if (args.write) {
59+
await fs.promises.writeFile(oldName, newContents);
4460
await fs.promises.rename(oldName, newName);
4561
}
4662
}

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"compilerOptions": {
33
"target": "es2020",
4+
"lib": ["es2021"],
45
"module": "commonjs",
56
"strict": true,
67
"esModuleInterop": true,

0 commit comments

Comments
 (0)