Skip to content

Commit

Permalink
init repo
Browse files Browse the repository at this point in the history
  • Loading branch information
mistlog committed Jan 30, 2020
1 parent 3c177c9 commit e8105c6
Show file tree
Hide file tree
Showing 40 changed files with 7,190 additions and 2 deletions.
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 }}
12 changes: 12 additions & 0 deletions .gitignore
@@ -0,0 +1,12 @@
node_modules
coverage
spike.ts
build
dist
.vscode
*.tgz
/src/dsl/*.ts
/src/plug-in/*.ts
/src/section/*.ts
/src/generator/*.ts
/src/transcriber/*.ts
20 changes: 18 additions & 2 deletions README.md
@@ -1,2 +1,18 @@
# svelte-draft
Develop svelte app in typedraft
# SvelteDraft · ![Build Status](https://github.com/mistlog/svelte-draft/workflows/build/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/mistlog/svelte-draft/badge.svg)](https://coveralls.io/github/mistlog/svelte-draft)

Develop svelte app in typedraft, with possibly complete typescript support.

Currently it's working in progress, reference materials:

* [starter project](https://github.com/mistlog/svelte-draft-template)

* [TodoMVC implementation](https://github.com/mistlog/svelte-draft-todo-mvc)

* unit test: see all supported features

## License

SvelteDraft is [MIT licensed](https://github.com/mistlog/svelte-draft/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";
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";

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.help();
}
else
{
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);
}
else
{
program.watch ? InspectFile(path) : ComposeFile(path);
}
}
}


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

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

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

export function ComposeFile(source: string)
{
//
const code = readFileSync(source, "utf8");
const transcriber = new SvelteTranscriber(code);
config.dsls.forEach(dsl => transcriber.AddDSL(dsl.name, dsl.dsl));
const { import_section, script_section, template_section } = transcriber.TranscribeToSections();

//
const style = source.replace(".tsx", ".css");
const style_section = existsSync(style) ? readFileSync(style, "utf8") : "";

//
const component = [
"<script>",
import_section,
"\n",
script_section,
"</script>",
"\n",
template_section,
"\n",
"<style>",
style_section,
"</style>"
].join("\n");
outputFileSync(source.replace(".tsx", ".svelte"), component, "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 e8105c6

Please sign in to comment.