Skip to content

Commit

Permalink
- refactor: remove method addition;
Browse files Browse the repository at this point in the history
- feature: new cli command: clean, build, dev
  • Loading branch information
mistlog committed Nov 6, 2021
1 parent bca659f commit 53e7bed
Show file tree
Hide file tree
Showing 31 changed files with 2,877 additions and 820 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@ spike.ts
build
dist
*.tgz
src/code-object/*.ts
src/core/*.ts
src/dsl/*.ts
src/plug-in/*.ts
src/transcript
96 changes: 58 additions & 38 deletions cli/cli.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,72 @@
#!/usr/bin/env node
import * as program from "commander";
import {
ComposeFile,
ComposeDirectory,
InspectDirectory,
InspectFile,
ITypeDraftConfig,
} from "./literator";
import { ComposeFile } from "./literator";
import { resolve } from "path";
import { readJSONSync, lstatSync } from "fs-extra";
import { cosmiconfig } from "cosmiconfig";
import { default as tsLoader } from "@endemolshinegroup/cosmiconfig-typescript-loader";
import { readJSONSync } from "fs-extra";
import { build, clean, dev, IDevConfig } from "cli-common";
import { withConfig } from "./cosmiconfig";

const package_json = readJSONSync(resolve(__dirname, "../../package.json"));
program.version(package_json.version);
program.option("-w, --watch", "compose file or files in directory in watch mode");
program.parse(process.argv);

const args = program.args;

if (args.length === 0) {
program.command("help").action(() => {
program.help();
} else {
const working_directory = process.cwd();
const [target] = args;
if (target) {
const path = resolve(working_directory, target);
});

// find config
const explorer = cosmiconfig("typedraft", {
searchPlaces: [`typedraft.config.ts`],
loaders: {
".ts": tsLoader,
},
program.command("dev").action(() => {
withConfig(config => {
config.Targets.forEach(({ src, dest, baseDir }) => {
const devConfig: IDevConfig = {
rename: {
extension: ".ts",
},
transform(path, code) {
if (path.endsWith(".tsx")) {
const result = ComposeFile(code, config);
return result;
} else {
return code;
}
},
};
if (baseDir) {
devConfig.baseDir = baseDir;
}
dev(src, dest, devConfig);
});
});
});

explorer.search().then(config_info => {
let config: ITypeDraftConfig = { DSLs: [], DraftPlugins: [] };
if (config_info && !config_info.isEmpty) {
config = { ...config, ...config_info.config };
program.command("build").action(() => {
withConfig(config => {
config.Targets.forEach(({ src, dest, baseDir }) => {
const devConfig: IDevConfig = {
rename: {
extension: ".ts",
},
transform(path, code) {
if (path.endsWith(".tsx")) {
const result = ComposeFile(code, config);
return result;
} else {
return code;
}
},
};
if (baseDir) {
devConfig.baseDir = baseDir;
}
build(src, dest, devConfig);
});
});
});

//
if (lstatSync(path).isDirectory()) {
program.watch ? InspectDirectory(path, config) : ComposeDirectory(path, config);
} else {
program.watch ? InspectFile(path, config) : ComposeFile(path, config);
}
program.command("clean").action(() => {
withConfig(config => {
config.Targets.forEach(({ dest }) => {
clean(dest);
});
}
}
});
});

program.parse(process.argv);
22 changes: 22 additions & 0 deletions cli/cosmiconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env node
import { ITypeDraftConfig } from "./literator";
import { cosmiconfig } from "cosmiconfig";
import { default as tsLoader } from "@endemolshinegroup/cosmiconfig-typescript-loader";

// find config
const explorer = cosmiconfig("typedraft", {
searchPlaces: [`typedraft.config.ts`],
loaders: {
".ts": tsLoader,
},
});

export function withConfig(callback: (config: ITypeDraftConfig) => void) {
explorer.search().then(configInfo => {
let config: ITypeDraftConfig = { DSLs: [], DraftPlugins: [], Targets: [] };
if (configInfo && !configInfo.isEmpty) {
config = { ...config, ...configInfo.config };
}
callback(config);
});
}
72 changes: 4 additions & 68 deletions cli/literator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import * as traverse from "filewalker";
import { default as watch } from "node-watch";
import { outputFileSync, removeSync, readFileSync } from "fs-extra";
import { MakeDefaultTranscriber, IDSL, IPlugin, Transcriber } from "../src";
import { MakeDefaultTranscriber, IDSL, IPlugin } from "../src";

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

export function MakeTranscriberWithConfig(code: string, config: ITypeDraftConfig) {
Expand All @@ -27,72 +25,10 @@ export function MakeTranscriberWithConfig(code: string, config: ITypeDraftConfig
return transcriber;
}

/**
*
*/
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)
.on("error", error => console.log(error))
.walk();
}

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, config);
} catch (error) {
console.log(error.message);
}
}
});
}

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, config);
} catch (error) {
console.log(error.message);
}
}
});
}

export function ComposeDirectory(path: string, config?: ITypeDraftConfig) {
TraverseDirectory(path, (relative: string, absolute: string) => {
if (absolute.endsWith(".tsx")) {
try {
ComposeFile(absolute, config);
} catch (error) {
console.log(`compose file failed: ${error.message}, source: ${relative}`);
}
}
});
}

export function CrossoutDirectory(path: string) {
TraverseDirectory(path, (relative: string, absolute: string) => {
if (absolute.endsWith(".tsx")) {
removeSync(absolute.replace(".tsx", ".ts"));
}
});
}

export function ComposeFile(source: string, config?: ITypeDraftConfig) {
const code = readFileSync(source, "utf8");
export function ComposeFile(code: string, config?: ITypeDraftConfig) {
const transcriber = config
? MakeTranscriberWithConfig(code, config)
: MakeDefaultTranscriber(code);
const result = transcriber.Transcribe();
outputFileSync(source.replace(".tsx", ".ts"), result, "utf8");
return result;
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,30 @@
/**
* # Export Class
* Exported class will be represented as `ExportClassCode`:
*/
import { ExportNamedDeclaration, ClassDeclaration, ClassMethod } from "@babel/types";
import { NodePath } from "@babel/core";

export class ExportClassCode {
m_Path: NodePath<ExportNamedDeclaration>;

constructor(path: NodePath<ExportNamedDeclaration>) {
this.m_Path = path;
}

get m_Code() {
return this.m_Path.node;
}

get m_Class() {
return this.m_Code.declaration as ClassDeclaration;
}

get m_Members() {
return this.m_Class.body.body;
}

get m_Name() {
return this.m_Class.id.name as string;
}
}

/**
* ## AddMember
*/
<ExportClassCode /> +
function AddMember(this: ExportClassCode, member: ClassMethod) {
AddMember(member: ClassMethod) {
this.m_Members.push(member);
};

/**
* # Trivial
*/
<ExportClassCode /> +
function constructor(this: ExportClassCode, path: NodePath<ExportNamedDeclaration>) {
this.m_Path = path;
};

import { ExportNamedDeclaration, ClassDeclaration, ClassMethod } from "@babel/types";
import { NodePath } from "@babel/core";
}
}
5 changes: 5 additions & 0 deletions draft/code-object/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./export-class";
export * from "./inline-context";
export * from "./local-context";
export * from "./method";
export * from "./module";
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import { BlockStatement, ExpressionStatement, Statement } from "@babel/types";
import { NodePath } from "@babel/traverse";
import { IDSL } from "../core/transcriber";
import { MatchDSL, __, use } from "draft-dsl-match";

export interface IInlineContext {
ToStatements: () => Array<Statement>;
}

export class InlineContext {
m_Path: NodePath<BlockStatement>;

Expand All @@ -8,45 +17,31 @@ export class InlineContext {
get m_Code() {
return this.m_Path.node as BlockStatement;
}
}

<InlineContext /> +
function constructor(this: InlineContext, path: NodePath<BlockStatement>) {
constructor(path: NodePath<BlockStatement>) {
this.m_Path = path;
};
}

<InlineContext /> +
function ToStatements(this: InlineContext) {
ToStatements() {
return this.m_Code.body;
};
}

<InlineContext /> +
function Resolve(this: InlineContext & IInlineContext, dsl: IDSL) {
Resolve(dsl: IDSL) {
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 /> +
function GetDSLName(this: InlineContext) {
GetDSLName() {
const statement = this.m_Path.node.body[0] as ExpressionStatement;
return Λ<string>("match")` ${statement}
${{ expression: { type: "StringLiteral", value: use("text") } }} -> ${(_, { text }) => {
const [use, dsl_name] = text.trim().split(" ");
return dsl_name;
}}
${__} -> ${""}
`;
};

export interface IInlineContext {
ToStatements: () => Array<Statement>;
}
}

import { BlockStatement, ExpressionStatement, Statement } from "@babel/types";
import { NodePath } from "@babel/traverse";
import { IDSL } from "../core/transcriber";
import { MatchDSL, __, use } from "draft-dsl-match";
Loading

0 comments on commit 53e7bed

Please sign in to comment.