Skip to content

Instantly share code, notes, and snippets.

@Getaji
Last active October 22, 2021 07:19
Show Gist options
  • Select an option

  • Save Getaji/695b9964d557dbc1d7274c599527e112 to your computer and use it in GitHub Desktop.

Select an option

Save Getaji/695b9964d557dbc1d7274c599527e112 to your computer and use it in GitHub Desktop.
Denoでディレクトリを監視して新しいファイルがあったらリネームするスクリプト

(ファイルが見つかりませんと言われることがある。ディレイが必要かもしれない)

// deno run --allow-read --allow-write watch-and-rename.ts
import * as path from "https://deno.land/std/path/mod.ts";
const DIR = "ここに監視対象のディレクトリパスを指定";
// ファイル名を変換
function rename(fileName: string): string {
return "renamed-" + fileName;
}
// パスを変換
function changePath(filePath: string): string {
const fileDir = path.dirname(filePath);
const fileName = path.basename(filePath);
return path.join(fileDir, rename(fileName));
}
for await (const fsEvent of Deno.watchFs(DIR)) {
if (fsEvent.kind === "create") {
fsEvent.paths.forEach((oldPath) => {
const newPath = changePath(oldPath);
if (oldPath !== newPath) {
Deno.rename(oldPath, newPath);
console.log("OLD:", oldPath);
console.log("NEW:", newPath);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment