Skip to content

Commit

Permalink
Merge pull request #10 from mistlog/feature/plugin-mechanism
Browse files Browse the repository at this point in the history
Feature/plugin mechanism
  • Loading branch information
mistlog authored Jun 25, 2020
2 parents b75596d + e99f275 commit 766fc83
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 49 deletions.
22 changes: 19 additions & 3 deletions cli/cli.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#!/usr/bin/env node
import * as program from "commander";
import { ComposeFile, ComposeDirectory, InspectDirectory, InspectFile } from "./literator";
import {
ComposeFile,
ComposeDirectory,
InspectDirectory,
InspectFile,
ITypeDraftConfig,
} from "./literator";
import { resolve } from "path";
import { readJSONSync, lstatSync } from "fs-extra";
import { cosmiconfigSync } from "cosmiconfig";

const package_json = readJSONSync(resolve(__dirname, "../../package.json"));
program.version(package_json.version);
Expand All @@ -18,10 +25,19 @@ if (args.length === 0) {
const [target] = args;
if (target) {
const path = resolve(working_directory, target);

// find config
const config_info = cosmiconfigSync("typedraft").search();
let config: ITypeDraftConfig = { DSLs: [], DraftPlugins: [] };
if (config_info && !config_info.isEmpty) {
config = { ...config, ...config_info.config };
}

//
if (lstatSync(path).isDirectory()) {
program.watch ? InspectDirectory(path) : ComposeDirectory(path);
program.watch ? InspectDirectory(path, config) : ComposeDirectory(path, config);
} else {
program.watch ? InspectFile(path) : ComposeFile(path);
program.watch ? InspectFile(path, config) : ComposeFile(path, config);
}
}
}
51 changes: 40 additions & 11 deletions cli/literator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
import * as traverse from "filewalker";
import { default as watch } from "node-watch";
import { outputFileSync, removeSync, readFileSync } from "fs-extra";
import { MakeDefaultTranscriber } from "../src";
import { MakeDefaultTranscriber, IDSL, IPlugin, Transcriber } from "../src";

/**
*
*/
export interface ITypeDraftConfig {
DSLs: Array<{ name: string; dsl: () => IDSL }>;
DraftPlugins: Array<IPlugin & Function>;
}

export function MakeTranscriberWithConfig(code: string, config: ITypeDraftConfig) {
const transcriber = MakeDefaultTranscriber(code);

config.DSLs.forEach(({ name, dsl }) => {
transcriber.AddDSL(name, dsl());
});

if (config.DraftPlugins.length !== 0) {
transcriber.m_Plugins = config.DraftPlugins.map(PluginConstructor =>
Reflect.construct(PluginConstructor, [transcriber])
);
}

return transcriber;
}

/**
*
*/
function TraverseDirectory(path: string, callback: (name: string, path: string) => void) {
const action = (relative: string, stats, absolute: string) => callback(relative, absolute);
traverse(path)
Expand All @@ -11,41 +38,41 @@ function TraverseDirectory(path: string, callback: (name: string, path: string)
.walk();
}

export function InspectDirectory(path: string) {
ComposeDirectory(path);
export function InspectDirectory(path: string, config?: ITypeDraftConfig) {
ComposeDirectory(path, config);

watch(path, { recursive: true }, (event, name: string) => {
if (name.endsWith(".tsx")) {
console.log(event, name);
try {
ComposeFile(name);
ComposeFile(name, config);
} catch (error) {
console.log(error.message);
}
}
});
}

export function InspectFile(path: string) {
ComposeFile(path);
export function InspectFile(path: string, config?: ITypeDraftConfig) {
ComposeFile(path, config);

watch(path, (event, name: string) => {
if (name.endsWith(".tsx")) {
console.log(event, name);
try {
ComposeFile(name);
ComposeFile(name, config);
} catch (error) {
console.log(error.message);
}
}
});
}

export function ComposeDirectory(path: string) {
export function ComposeDirectory(path: string, config?: ITypeDraftConfig) {
TraverseDirectory(path, (relative: string, absolute: string) => {
if (absolute.endsWith(".tsx")) {
try {
ComposeFile(absolute);
ComposeFile(absolute, config);
} catch (error) {
console.log(`compose file failed: ${error.message}, source: ${relative}`);
}
Expand All @@ -61,9 +88,11 @@ export function CrossoutDirectory(path: string) {
});
}

export function ComposeFile(source: string) {
export function ComposeFile(source: string, config?: ITypeDraftConfig) {
const code = readFileSync(source, "utf8");
const transcriber = MakeDefaultTranscriber(code);
const transcriber = config
? MakeTranscriberWithConfig(code, config)
: MakeDefaultTranscriber(code);
const result = transcriber.Transcribe();
outputFileSync(source.replace(".tsx", ".ts"), result, "utf8");
}
29 changes: 8 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"dependencies": {
"@babel/core": "^7.6.2",
"commander": "^4.0.1",
"cosmiconfig": "^6.0.0",
"filewalker": "^0.1.3",
"fs-extra": "^8.1.0",
"node-watch": "^0.6.3",
Expand Down
6 changes: 5 additions & 1 deletion src/code-object/inline-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export class InlineContext {

<InlineContext /> +
function Resolve(this: InlineContext & IInlineContext, dsl: IDSL) {
this.m_Code.body = dsl.Transcribe(this.ToStatements(), this.m_Path);
if (dsl.m_Merge) {
this.m_Path.replaceWithMultiple(dsl.Transcribe(this.ToStatements(), this.m_Path));
} else {
this.m_Code.body = dsl.Transcribe(this.ToStatements(), this.m_Path);
}
};

<InlineContext /> +
Expand Down
18 changes: 7 additions & 11 deletions src/core/transcriber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ export class Transcriber {
/**
* # Utility
* Other methods are just utilities.
*
* ## Plugin
*/

<Transcriber /> + function PreparePlugins(this: Transcriber & ITranscriber) {};

/**
* ## DSL
*/
Expand All @@ -52,8 +48,6 @@ export class Transcriber {
return this.m_DSLMap.get(name);
};

<Transcriber /> + function PrepareDSLs(this: Transcriber) {};

/**
* ## Context and Class
*/
Expand Down Expand Up @@ -94,25 +88,26 @@ export class Transcriber {
this.m_ContextMap = new Map<string, LocalContext>();
this.m_InlineContextMap = new Map<Symbol, InlineContext>();
this.m_DSLMap = new Map<string, IDSL>();

this.PrepareDSLs();
this.PreparePlugins();
this.m_Plugins = new Array<IPlugin>();
};

export interface IDSL {
Transcribe(
block: Array<Statement>,
path?: NodePath<FunctionDeclaration> | NodePath<BlockStatement>
): Array<Statement>;

m_Merge?: boolean;
}

export interface IPlugin {
Transcribe(): void;
}

export interface ITranscriber {
PrepareDSLs: () => void;
PreparePlugins: () => void;
Transcribe: () => string;

AddDSL: (name: string, dsl: IDSL) => void;

GetDSL: (name: string) => IDSL;
GetClass: (name: string) => ExportClassCode;
Expand All @@ -133,6 +128,7 @@ export interface ITranscriber {
m_InlineContextMap: Map<Symbol, InlineContext>;

m_DSLMap: Map<string, IDSL>;
m_Plugins: Array<IPlugin>;
}

export type TraverseLocalContextCallback = (context: LocalContext, name: string) => void;
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ export * from "./common/utility";
/**
*
*/
import { Transcriber } from "./core/transcriber";
import { Transcriber, ITranscriber } from "./core/transcriber";
import { RefreshDraftPlugin } from "./plug-in/draft-plugin-refresh";
import { DSLPlugin } from "./plug-in/draft-plugin-dsl";
import { LocalContextPlugin } from "./plug-in/draft-plugin-local-context";
import { ClassPlugin } from "./plug-in/draft-plugin-class";
import { FilterPlugin } from "./plug-in/draft-plugin-filter";

export function MakeDefaultTranscriber(_module: string) {
export function MakeDefaultTranscriber(_module: string): ITranscriber {
const transcriber = new Transcriber(_module);
transcriber.m_Plugins = [
new RefreshDraftPlugin(transcriber),
Expand Down
8 changes: 8 additions & 0 deletions test/plug-in/__snapshots__/dsl.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ exports[`inline context 1`] = `
}"
`;

exports[`inline context: merge 1`] = `
"export function Main() {
console.log(\\"hello\\");
console.log(\\"current\\");
console.log(\\"world\\");
}"
`;

exports[`local context added after dsl resolved 1`] = `
"export function Main() {
if (value === \\"a\\") {
Expand Down
Loading

0 comments on commit 766fc83

Please sign in to comment.