Created
January 15, 2020 10:48
-
-
Save majecty/25a936ceb1dcc1dc2334203d3218ac60 to your computer and use it in GitHub Desktop.
Update copyright using deno
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const encoder = new TextEncoder(); | |
const decoder = new TextDecoder("utf-8"); | |
const simplePat = /Copyright (201[0-9]) Kodebox/g; | |
const complexPat = /Copyright (\d+)-(\d+) Kodebox/g; | |
for (let i = 1; i < Deno.args.length; i++) { | |
let filename = Deno.args[i]; | |
if (!await isFileExist(filename)) { | |
continue; | |
} | |
let bytes = await Deno.readFile(filename); | |
const allText = decoder.decode(bytes); | |
let lines = allText.split("\n"); | |
lines = lines.map(line => | |
line.replace(simplePat, "Copyright $1-2020 Kodebox") | |
); | |
lines = lines.map(line => | |
line.replace(complexPat, "Copyright $1-2020 Kodebox") | |
); | |
await Deno.writeFile(filename, encoder.encode(lines.join("\n"))); | |
} | |
async function isFileExist(fileName: string) { | |
try { | |
const fileInfo = await Deno.lstat(fileName); | |
if (!fileInfo.isFile()) { | |
return false; | |
} | |
} catch (err) { | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment