Skip to content

Commit

Permalink
Improve CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiriVulpes committed Sep 28, 2024
1 parent 432bfa4 commit 26f3382
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 28 deletions.
5 changes: 1 addition & 4 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
"type": "npm",
"script": "play",
"isBackground": true,
"group": {
"kind": "build",
"isDefault": true,
}
"group": "build"
},
],
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"main": "out/chiri.js",
"types": "out/chiri.d.ts",
"scripts": {
"play": "node tsc2.js && concurrently --prefix none \"node tsc2.js --watch\" \"chiri playground -w\""
"play": "node tsc2.js && concurrently --prefix none \"node tsc2.js --watch\" \"chiri playground -w\"",
"watch": "node tsc2.js --watch"
},
"dependencies": {
"source-map": "0.7.4",
Expand Down
21 changes: 21 additions & 0 deletions src/args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const args: Record<string, string | true> = {}
const allArgs: string[] = []

for (let i = 2; i < process.argv.length; i++) {
const arg = process.argv[i]
if (arg[0] === "-" && (arg[2] || arg[1] !== "-")) {
if (arg[1] === "-") {
args[arg.slice(2)] = process.argv[++i]
continue
}

args[arg.slice(1)] = true
continue
}

allArgs.push(arg)
}

export default args
export { allArgs }

6 changes: 6 additions & 0 deletions src/chc/write/CSSWriter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from "path"
import args from "../../args"
import type { ChiriAST } from "../read/ChiriReader"
import type { ChiriProperty } from "../read/consume/consumePropertyOptional"
import type { ChiriWord } from "../read/consume/consumeWord"
Expand Down Expand Up @@ -25,6 +27,10 @@ export default class CSSWriter extends Writer {
super(ast, dest, { extension: ".css", ...config })
}

override createDestPath (outFile: string): string {
return typeof args["out-css"] === "string" ? path.resolve(args["out-css"], outFile) : super.createDestPath(outFile)
}

writingTo (writingTo: "default" | "root", dowhile: () => any) {
if (this.writingToType === writingTo)
return
Expand Down
6 changes: 6 additions & 0 deletions src/chc/write/DTSWriter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from "path"
import args from "../../args"
import type { ChiriAST } from "../read/ChiriReader"
import type ChiriCompiler from "./ChiriCompiler"
import type { ChiriWriteConfig } from "./Writer"
Expand All @@ -9,6 +11,10 @@ export default class DTSWriter extends Writer {
super(ast, dest, { extension: ".d.ts", ...config })
}

override createDestPath (outFile: string): string {
return typeof args["out-dts"] === "string" ? path.resolve(args["out-dts"], outFile) : super.createDestPath(outFile)
}

override onCompileStart (compiler: ChiriCompiler): void {
this.writeLineStartBlock("declare const _default: {")
}
Expand Down
6 changes: 6 additions & 0 deletions src/chc/write/ESWriter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from "path"
import args from "../../args"
import type { ChiriAST } from "../read/ChiriReader"
import type ChiriCompiler from "./ChiriCompiler"
import type { ChiriWriteConfig } from "./Writer"
Expand Down Expand Up @@ -27,6 +29,10 @@ export default class ESWriter extends Writer {
super(ast, dest, { extension: ".js", ...config })
}

override createDestPath (outFile: string): string {
return typeof args["out-es"] === "string" ? path.resolve(args["out-es"], outFile) : super.createDestPath(outFile)
}

override onCompileStart (compiler: ChiriCompiler): void {
this.writeLineStartBlock(UMD_PREFIX)
this.writeLineStartBlock("exports.default = {")
Expand Down
14 changes: 12 additions & 2 deletions src/chc/write/Writer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import fsp from "fs/promises"
import path from "path"
import { SourceMapGenerator } from "source-map"
import ansi from "../../ansi"
import args from "../../args"
import type { ChiriAST, ChiriPosition } from "../read/ChiriReader"
import type { ChiriDocumentation } from "../read/consume/consumeDocumentationOptional"
import type { ChiriValueText } from "../read/consume/consumeValueText"
import type { ChiriWord } from "../read/consume/consumeWord"
import relToCwd from "../util/relToCwd"
import stringifyText from "../util/stringifyText"
import type ChiriCompiler from "./ChiriCompiler"

Expand Down Expand Up @@ -49,13 +52,19 @@ export default class Writer {
}

constructor (ast: ChiriAST, dest: string, public readonly config: ChiriWriteConfig) {
this.dest = dest + config.extension
this.dest = this.createDestPath(relToCwd(dest)) + config.extension

this.map = new SourceMapGenerator({ file: this.dest })
for (const [filename, source] of Object.entries(ast.source))
this.map.setSourceContent(filename, source)
}

createDestPath (outFile: string): string {
if (typeof args.out === "string")
outFile = path.join(args.out, outFile)
return path.resolve(outFile)
}

indent (amount = 1) {
this.#indent += amount
}
Expand All @@ -67,7 +76,7 @@ export default class Writer {
this.currentWrite.output = this.currentWrite.output.slice(0, -1)
}

writeFile () {
async writeFile () {
this.output = ""
for (const queued of this.queue) {
if (queued.mapping) {
Expand All @@ -82,6 +91,7 @@ export default class Writer {
this.output += queued.output
}

await fsp.mkdir(path.dirname(this.dest), { recursive: true })
return fsp.writeFile(this.dest, this.output)
}

Expand Down
29 changes: 8 additions & 21 deletions src/chiri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import dotenv from "dotenv"
import path from "path"
import sourceMapSupport from "source-map-support"
import ansi from "./ansi"
import args, { allArgs } from "./args"
import type ChiriReaderType from "./chc/read/ChiriReader"
import prefixError from "./chc/util/prefixError.js"
import relToCwd from "./chc/util/relToCwd.js"
import type streamJsonType from "./chc/util/streamJson"
import type ChiriCompilerType from "./chc/write/ChiriCompiler"
import { CHC_ROOT, LIB_ROOT } from "./constants"
import { CHC_ROOT, LIB_ROOT, PACKAGE_ROOT } from "./constants"

dotenv.config()

Expand All @@ -20,22 +21,10 @@ Error.stackTraceLimit = Math.max(Error.stackTraceLimit, +process.env.CHIRI_STACK
if (process.env.CHIRI_ENV === "dev")
sourceMapSupport.install()

const args: Record<string, string | true> = {}
const allArgs: string[] = []

for (let i = 2; i < process.argv.length; i++) {
const arg = process.argv[i]
if (arg[0] === "-" && (arg[2] || arg[1] !== "-")) {
if (arg[1] === "-") {
args[arg.slice(2)] = process.argv[++i]
continue
}

args[arg.slice(1)] = true
continue
}

allArgs.push(arg)
if (args.v) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
console.log(require(path.join(PACKAGE_ROOT, "package.json")).version)
process.exit()
}

let compilationPromise: Promise<any> | undefined = undefined
Expand Down Expand Up @@ -109,16 +98,14 @@ async function compile (filename: string) {
.catch(e => { throw prefixError(e, "Failed to write AST JSON file") })
}

const outFile = reader.basename

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const ChiriCompilerClass = rerequire<typeof ChiriCompilerType>("./chc/write/ChiriCompiler.js")
const compiler = ChiriCompilerClass(ast, outFile)
const compiler = ChiriCompilerClass(ast, reader.basename)
compiler.compile()
await compiler.writeFiles()

const elapsed = performance.now() - start
console.log(ansi.label + "chiri", ansi.path + relToCwd(reader.filename), ansi.label + "=>", ansi.path + relToCwd(outFile), ansi.label + formatElapsed(elapsed))
console.log(ansi.label + "chiri", ansi.path + relToCwd(reader.filename), ansi.label + formatElapsed(elapsed))
}

function formatElapsed (elapsed: number) {
Expand Down

0 comments on commit 26f3382

Please sign in to comment.