Skip to content

Commit

Permalink
Merge branch 'release/init-repo'
Browse files Browse the repository at this point in the history
  • Loading branch information
mistlog committed Dec 22, 2019
2 parents bf8b9a1 + a1ec40f commit 65e92cc
Show file tree
Hide file tree
Showing 48 changed files with 7,954 additions and 1 deletion.
31 changes: 31 additions & 0 deletions .github/workflows/build.yml
@@ -0,0 +1,31 @@
name: build

on: [push]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [8.x, 10.x, 12.x]

steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install, build, and test
run: |
npm ci
npm run build --if-present
npm test
env:
CI: true
- name: Coveralls GitHub Action
uses: coverallsapp/github-action@v1.0.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
github-token: ${{ secrets.github_token }}
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
node_modules
coverage
spike.ts
build
dist
15 changes: 15 additions & 0 deletions .vscode/launch.json
@@ -0,0 +1,15 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Current TS File",
"type": "node",
"request": "launch",
"args": ["${relativeFile}"],
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"protocol": "inspector",
}
]
}
11 changes: 10 additions & 1 deletion README.md
@@ -1,2 +1,11 @@
# typedraft
# TypeDraft · ![Build Status](https://github.com/mistlog/typedraft/workflows/build/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/mistlog/typedraft/badge.svg)](https://coveralls.io/github/mistlog/typedraft)

TypeDraft is a superset of typescript with built-in support for DSL extension and literate programming.

## Documentation

The document is matained in [wiki](https://github.com/mistlog/typedraft/wiki).

## License

TypeDraft is [MIT licensed](https://github.com/mistlog/typedraft/blob/master/LICENSE).
47 changes: 47 additions & 0 deletions cli/cli.ts
@@ -0,0 +1,47 @@
#!/usr/bin/env node
import * as program from "commander";
//@ts-ignore
import * as package_json from "../../package.json";
import { ComposeFile, ComposeDirectory, InspectDirectory, InspectFile } from "./literator";
import { resolve, join } from "path";
import { lstatSync } from "fs";
import { readJsonSync } from "fs-extra";
import { config } from "./config.js";

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.help();
}
else
{
const working_directory = process.cwd();
const [target] = args;
if (target)
{

//
const project_package = readJsonSync(join(working_directory, "package.json")) || { 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)}`).dsl);
config.dsls = dsls;

//
const path = resolve(working_directory, target);
if (lstatSync(path).isDirectory())
{
program.watch ? InspectDirectory(path) : ComposeDirectory(path);
}
else
{
program.watch ? InspectFile(path) : ComposeFile(path);
}
}
}


10 changes: 10 additions & 0 deletions cli/config.ts
@@ -0,0 +1,10 @@
import { IDSL } from "../src";

export interface IConfig
{
dsls: Array<{ name: string, dsl: IDSL }>;
}

export const config: IConfig = {
dsls: []
}
96 changes: 96 additions & 0 deletions cli/literator.ts
@@ -0,0 +1,96 @@
import { readFileSync } from "fs";
import { outputFileSync, removeSync } from "fs-extra";
import * as traverse from "filewalker";
import * as watch from "node-watch";
import { Transcriber, } 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();
}

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

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

export function InspectFile(path: string)
{
ComposeFile(path);

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

export function ComposeDirectory(path: string)
{
TraverseDirectory(path, (relative: string, absolute: string) =>
{
if (absolute.endsWith(".tsx"))
{
try
{
ComposeFile(absolute);
} 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)
{
const code = readFileSync(source, "utf8");
const transcriber = new Transcriber(code);
config.dsls.forEach(dsl => transcriber.AddDSL(dsl.name, dsl.dsl));
const result = transcriber.Transcribe();
outputFileSync(source.replace(".tsx", ".ts"), result, "utf8");
}

5 changes: 5 additions & 0 deletions jest.config.js
@@ -0,0 +1,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
notify: true
};

0 comments on commit 65e92cc

Please sign in to comment.