Last active
September 25, 2020 15:32
-
-
Save keelii/d95492873f35f96d95f3a169bee934c6 to your computer and use it in GitHub Desktop.
deno ext processer.
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
import { join } from "https://deno.land/std/path/mod.ts"; | |
import { readLines } from "https://deno.land/std/io/bufio.ts"; | |
type ProcessType = "remove" | "restore"; | |
function getResultContent(type: ProcessType, m: string) { | |
const hasExtRE = /\.ts";$/; | |
if (type === "remove") { | |
if (hasExtRE.test(m)) { | |
return m.replace(hasExtRE, '";'); | |
} | |
} | |
if (type === "restore") { | |
if (!hasExtRE.test(m)) { | |
return m.replace(/";$/, '.ts";'); | |
} | |
} | |
return m; | |
} | |
export async function process(type: ProcessType, items: string[]) { | |
for await (let item of items) { | |
const filename = join(Deno.cwd(), item) | |
let fileReader = await Deno.open(filename); | |
let content = await Deno.readTextFile(filename); | |
for await (let line of readLines(fileReader)) { | |
if (line === '') break; | |
if (line.indexOf("from") < 0) continue; | |
if (!/";$/.test(line)) continue; | |
content = content.replace(line, getResultContent(type, line)) | |
} | |
console.log(`Processing ${type} [${filename}]`); | |
await Deno.writeTextFile(filename, content); | |
} | |
} | |
function showHelp() { | |
console.log("") | |
console.log("Remove or restore [.ts] suffix from your import stmt in deno project.") | |
console.log("") | |
console.log("Usage:") | |
console.log(" deno_ext remove <files>...") | |
console.log(" deno_ext restore <files>...") | |
console.log("Examples:") | |
console.log(" deno_ext remove **/*.ts") | |
console.log(" deno_ext restore src/*.ts") | |
} | |
const [command, ...files] = Deno.args | |
if (Deno.args.length > 1) { | |
if (command === "remove" || command === "restore") { | |
await process(command, files); | |
} else { | |
console.error("✘ error with command."); | |
showHelp() | |
} | |
} else { | |
console.error("✘ error with command."); | |
showHelp() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
安装脚本
注意点
~/.deno/bin/
deno fmt
格式化使用: