diff --git a/README.md b/README.md index 2204472..6b99bc3 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,52 @@ This tool is created to transform JavaScript based on definitions written in a c This is a DSL created to define proposal transformations using templates. A transformation is defined by two templates, a template of what code snippets to search for **applicable to** and a template of how to transform those snippets **transform to**. Examples of definitions for proposals can be found in [dsl_files](./dsl_files/) + +## Running this project + +This project exposes an api in the file [transform/transfrom.ts](./src/transform/transform.ts), which is meant to be used as this library's entry point. + +To run this code as a standalone project, a file [index.ts](./index.ts) exists to facilitate running this project. + +This project is created using TypeScript, and it is recommended to use [Bun](https://bun.sh/) to compile and run it. + +### Building Langium required files + +Firstly, building langium is required, this is done by the following: + +```sh +# cd into JSTQL +cd JSTQL + +# Install dependencies +npm i + +# Build langium generated files +npm run langium:generate + +# Build final JS source +npm run build +``` + +### Running a transformation + +Now we are ready to run this project with the pipeline on a test JS file: + +```sh +# cd into top level +cd .. + +# Install dependencies +bun i + +# Run an example transformation +bun run index.ts dsl_files/pipeline.jstql test_files/test.js +``` + +### Arguments of index.ts + +Arguments + +- First positional is path to JSTQL +- Second positional is path to JS file to transform +- -o is optional output path, if not given path is **./out.js** diff --git a/bun.lockb b/bun.lockb index 03950d9..b1da9fc 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/src/index.ts b/index.ts similarity index 67% rename from src/index.ts rename to index.ts index e733ca2..bbf1565 100644 --- a/src/index.ts +++ b/index.ts @@ -1,32 +1,40 @@ //import * as babelparser from "../babel/packages/babel-parser"; import * as babelparser from "@babel/parser"; //import core from "../babel/packages/babel-core"; -import { parse_with_plugins } from "./parser/parse"; +import { parse_with_plugins } from "./src/parser/parse"; import { SelfHostedRecipe, TransformRecipe, transform, -} from "./transform/transform"; +} from "./src/transform/transform"; import { readdir } from "node:fs/promises"; -import { parseJSTQL } from "./langium/langiumRunner"; +import { parseJSTQL } from "./src/langium/langiumRunner"; import { parseArgs } from "util"; -const options = { - o: { - type: "string", - }, -}; +const options = {}; const { values: argVals, tokens: positional } = parseArgs({ - options, + options: { + o: { + type: "string", + }, + }, tokens: true, allowPositionals: true, }); const main = async () => { //transform(selfHostedTransformExampleMultiStmt, codeFromFile); console.log(positional); - if (!positional[0] || !positional[0].value.endsWith(".jstql")) { + console.log(argVals); + if (!positional) { + throw new Error("Something is wrong with args"); + } + if ( + !positional[0] && + positional[0].kind === "positional" && + !positional[0].value.endsWith(".jstql") + ) { throw new Error("First positional argument is current JSTQL file"); } if (!positional[1] || !positional[1].value.endsWith(".js")) { @@ -44,12 +52,13 @@ const main = async () => { for (let proposal of parsedJSTQL) { let [resultString, matches] = transform(proposal.cases, codeString); - - let path = "./"; + console.log(resultString); + let path = "./out.js"; if (argVals["o"]) { path = argVals["o"]; } - Bun.write(path, resultString); + console.log(path); + await Bun.write(path, resultString); } }; diff --git a/tsconfig.json b/tsconfig.json index f5dba31..98668a6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,6 @@ "moduleResolution": "node", "types": ["bun-types"] }, - "include": ["src"], + "include": ["src", "index.ts"], "exclude": ["babel"] }