Skip to content

Instantly share code, notes, and snippets.

@keelii
Last active September 25, 2020 15:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keelii/d95492873f35f96d95f3a169bee934c6 to your computer and use it in GitHub Desktop.
Save keelii/d95492873f35f96d95f3a169bee934c6 to your computer and use it in GitHub Desktop.
deno ext processer.
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()
}
@keelii
Copy link
Author

keelii commented Jun 28, 2020

安装脚本

deno install --allow-read --allow-write -f -n deno_ext https://gist.githubusercontent.com/keelii/d95492873f35f96d95f3a169bee934c6/raw/6951c3c5b3b17200ac374f43a8c3926ac586ac84/deno_ext.ts

注意点

  1. 请确保你的环境变量里面包含:~/.deno/bin/
  2. 执行前请先使用 deno fmt 格式化

使用:

➜ deno_ext

Remove or restore [.tx] suffix from your import stmt in deno project.

Usage:
  deno_ext remove <files>...
  deno_ext restore <files>...
Examples:
  deno_ext remove **/*.ts
  deno_ext restore src/*.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment