-
Notifications
You must be signed in to change notification settings - Fork 0
/
less-compiler-cli.ts
66 lines (53 loc) · 2 KB
/
less-compiler-cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { dirname, join } from "https://deno.land/std@0.188.0/path/mod.ts"
import less from "./less/less-node/index.js"
// --------------------------------------------------------------------------------
interface RenderError {
column: number
extract: string[]
filename: string
index: number
line: number
message: string
type: string
}
interface RenderOutput {
css: string
map: string
imports: string[]
}
async function prompt(message: string) {
const buf = new Uint8Array(1024)
await Deno.stdout.write(new TextEncoder().encode(message))
const n = <number>await Deno.stdin.read(buf)
return new TextDecoder().decode(buf.subarray(0, n)).trim()
}
(async () => {
const lessPath = await prompt('Standalone LESS compiler v1.0.0\n\nSet source file location: ')
const outfileDir = await prompt('Set outfile folder: ')
const outfileName = await prompt('Set outfile name: ')
let lessFilePath: string
try {
lessFilePath = await Deno.readTextFile(lessPath)
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
return await Deno.stderr.write(new TextEncoder().encode('Deno error: The file was not found'))
} else {
throw error
}
}
const options = { paths: [dirname(lessPath)] }
less.render(lessFilePath, options, async (error: RenderError, output: RenderOutput | undefined) => {
if (error || !output)
return await Deno.stderr.write(new TextEncoder().encode(`${error.message} in ${error.filename} on line ${error.line}`))
try {
await Deno.writeFile(join(outfileDir, outfileName) + '.css', new TextEncoder().encode(output.css))
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
return await Deno.stderr.write(new TextEncoder().encode('Deno: Error wring the file'))
} else {
throw error
}
}
await prompt('Done!\n\nPress any key to exit...')
})
})()