Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from mistlog/feature/cli
Feature/cli
- Loading branch information
Showing
7 changed files
with
215 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { default as traverse } from "filewalker"; | ||
import { default as watch } from "node-watch"; | ||
import { readFileSync } from "fs"; | ||
import { transform } from "../src" | ||
import { outputFileSync, removeSync } from "fs-extra"; | ||
|
||
function TraverseDirectory(path: string, callback: (relative: string, absolute: string) => void) { | ||
const action = (relative: string, stats, absolute: string) => callback(relative, absolute); | ||
traverse(path) | ||
.on("file", action) | ||
.on("error", error => console.log(error)) | ||
.walk(); | ||
} | ||
|
||
function transformFile(absolute: string, config = { debug: false }) { | ||
const input = readFileSync(absolute, "utf8"); | ||
const output = transform(input, config).code; | ||
outputFileSync(absolute.replace(".type", ".ts"), output, "utf8"); | ||
} | ||
|
||
export function build(dir: string, options: { watch: boolean }) { | ||
if (options.watch) { | ||
watch(dir, { recursive: true }, (event, absolute: string) => { | ||
if (absolute.endsWith(".type")) { | ||
console.log(event, absolute); | ||
debug(absolute); | ||
} | ||
}); | ||
} else { | ||
TraverseDirectory(dir, (relative, absolute) => { | ||
if (absolute.endsWith(".type")) { | ||
transformFile(absolute); | ||
} | ||
}) | ||
} | ||
} | ||
|
||
export function clean(dir: string) { | ||
TraverseDirectory(dir, (relative, absolute) => { | ||
if (absolute.endsWith(".type")) { | ||
removeSync(absolute.replace(".type", ".ts")); | ||
} | ||
}) | ||
} | ||
|
||
export function debug(absolute: string) { | ||
try { | ||
transformFile(absolute, { debug: true }); | ||
} catch (error) { | ||
console.log(`build type failed: ${absolute}`); | ||
console.log(error.message); | ||
console.log(error.backtrace); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env node | ||
import { default as program } from "commander"; | ||
import { readJSONSync, lstatSync } from "fs-extra"; | ||
import { resolve } from "path"; | ||
import { build, clean, debug } from "./cli"; | ||
|
||
const packageJSON = readJSONSync(resolve(__dirname, "../../package.json")); | ||
program.version(packageJSON.version); | ||
|
||
program | ||
.option('-w, --watch', "watch mode") | ||
|
||
program | ||
.command("build <dir>") | ||
.description("build ts type") | ||
.action((dir: string) => { | ||
const path = resolve(process.cwd(), dir); | ||
if (lstatSync(path).isDirectory()) { | ||
const options = program.opts(); | ||
build(path, { watch: options.watch }); | ||
} | ||
}); | ||
|
||
program | ||
.command("clean <dir>") | ||
.description("clean ts type") | ||
.action((dir: string) => { | ||
const path = resolve(process.cwd(), dir); | ||
if (lstatSync(path).isDirectory()) { | ||
clean(path); | ||
} | ||
}); | ||
|
||
program | ||
.command("debug <file>") | ||
.description("debug ts type") | ||
.action((file: string) => { | ||
const path = resolve(process.cwd(), file); | ||
if (!lstatSync(path).isDirectory()) { | ||
debug(path); | ||
} | ||
}); | ||
|
||
program.parse(process.argv); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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