Skip to content

Commit

Permalink
Merge pull request #9 from mistlog/refactor/cli
Browse files Browse the repository at this point in the history
refactor cli: remove config
  • Loading branch information
mistlog authored Jun 20, 2020
2 parents 8847f42 + 7233572 commit b75596d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 35 deletions.
20 changes: 2 additions & 18 deletions cli/cli.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#!/usr/bin/env node
import * as program from "commander";
import { ComposeFile, ComposeDirectory, InspectDirectory, InspectFile } from "./literator";
import { resolve, join } from "path";
import { lstatSync } from "fs";
import { readJsonSync, readJSONSync } from "fs-extra";
import { config } from "./config.js";
import { resolve } from "path";
import { readJSONSync, lstatSync } from "fs-extra";

const package_json = readJSONSync(resolve(__dirname, "../../package.json"));
program.version(package_json.version);
Expand All @@ -19,20 +17,6 @@ if (args.length === 0) {
const working_directory = process.cwd();
const [target] = args;
if (target) {
//
const project_package = readJsonSync(join(working_directory, "package.json"), {
throws: false,
}) || { devDependencies: {} };

const dsl_names = Object.keys(project_package.devDependencies).filter(key =>
key.startsWith("draft-dsl")
);
const dsls = dsl_names.map(name =>
require(`${join(working_directory, "node_modules", name)}`)?.MakeDSL()
);
config.dsls = dsls;

//
const path = resolve(working_directory, target);
if (lstatSync(path).isDirectory()) {
program.watch ? InspectDirectory(path) : ComposeDirectory(path);
Expand Down
9 changes: 0 additions & 9 deletions cli/config.ts

This file was deleted.

14 changes: 6 additions & 8 deletions cli/literator.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { readFileSync } from "fs";
import { outputFileSync, removeSync } from "fs-extra";
import * as traverse from "filewalker";
import * as watch from "node-watch";
import { default as watch } from "node-watch";
import { outputFileSync, removeSync, readFileSync } from "fs-extra";
import { MakeDefaultTranscriber } from "../src";
import { config } from "./config";

function TraverseDirectory(path: string, callback: (name: string, path: string) => void) {
const action = (relative: string, stats, absolute: string) => callback(relative, absolute);
traverse(path).on("file", action).walk();
traverse(path)
.on("file", action)
.on("error", error => console.log(error))
.walk();
}

export function InspectDirectory(path: string) {
ComposeDirectory(path);

//@ts-ignore
watch(path, { recursive: true }, (event, name: string) => {
if (name.endsWith(".tsx")) {
console.log(event, name);
Expand All @@ -29,7 +29,6 @@ export function InspectDirectory(path: string) {
export function InspectFile(path: string) {
ComposeFile(path);

//@ts-ignore
watch(path, (event, name: string) => {
if (name.endsWith(".tsx")) {
console.log(event, name);
Expand Down Expand Up @@ -65,7 +64,6 @@ export function CrossoutDirectory(path: string) {
export function ComposeFile(source: string) {
const code = readFileSync(source, "utf8");
const transcriber = MakeDefaultTranscriber(code);
config.dsls.forEach(dsl => transcriber.AddDSL(dsl.name, dsl.dsl));
const result = transcriber.Transcribe();
outputFileSync(source.replace(".tsx", ".ts"), result, "utf8");
}

0 comments on commit b75596d

Please sign in to comment.