Skip to content
This repository has been archived by the owner on Sep 2, 2020. It is now read-only.

Commit

Permalink
Merge branch 'release/init-repo'
Browse files Browse the repository at this point in the history
  • Loading branch information
mistlog committed Dec 21, 2019
2 parents 3338181 + be0288d commit cb83f29
Show file tree
Hide file tree
Showing 24 changed files with 5,568 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 }}
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
node_modules
coverage
build
dist
spike.ts
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",
}
]
}
13 changes: 11 additions & 2 deletions README.md
@@ -1,2 +1,11 @@
# source-view
A simple tool for turning code into document
# Source View · ![Build Status](https://github.com/mistlog/source-view/workflows/build/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/mistlog/source-view/badge.svg)](https://coveralls.io/github/mistlog/source-view)

Source view is a simple tool for turning code into document.

## Documentation

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

## License

Source view is [MIT licensed](https://github.com/mistlog/source-view/blob/master/LICENSE).
32 changes: 32 additions & 0 deletions cli/cli.ts
@@ -0,0 +1,32 @@
#!/usr/bin/env node
import * as program from "commander";
//@ts-ignore
import * as package_json from "../../package.json";
import { resolve, join } from "path";
import { lstatSync } from "fs";
import { GenerateDocument, GenerateDocumentWatch } from "./utility";

program.version(package_json.version)
program.option("-w, --watch", "generate document 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 path = resolve(working_directory, target);
const output_path = join(working_directory,"source-view");
if (lstatSync(path).isDirectory())
{
program.watch ? GenerateDocumentWatch(path, output_path) : GenerateDocument(path, output_path);
}
}
}
48 changes: 48 additions & 0 deletions cli/utility.ts
@@ -0,0 +1,48 @@
import { readFileSync } from "fs";
import * as traverse from "filewalker";
import { CodeToMarkdown } from "../src/source-view";
import { outputFileSync, removeSync } from "fs-extra";
import { relative, join } from "path";
import { resolve } from "url";
import * as watch from "node-watch";

export function GenerateDocument(source: string, target: string)
{
removeSync(target);
traverse(source).on("file", (relative: string, stats: object, absolute: string) =>
{
if (absolute.endsWith(".tsx"))
{
const code = readFileSync(absolute, "utf8");
const markdown = CodeToMarkdown(code);
const output = relative.replace(".tsx", ".md")
outputFileSync(join(target, output), markdown);
}
}).walk();
}

export function GenerateDocumentWatch(source: string, target: string)
{
GenerateDocument(source, target);

//@ts-ignore
watch(source, { recursive: true }, (event, path: string) =>
{
if (path.endsWith(".tsx"))
{
try
{
const code = readFileSync(path, "utf8");
const markdown = CodeToMarkdown(code);
const relative_path = relative(source, path);
const target_path = join(target, relative_path).replace(".tsx", ".md");
outputFileSync(target_path, markdown, "utf8");
console.log(event, target_path);
}
catch (error)
{
console.log(error.message);
}
}
});
}
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 cb83f29

Please sign in to comment.