Repository navigation
Expand file tree
/
Copy pathcli.ts
More file actions
212 lines (190 loc) Β· 4.9 KB
/
Copy pathcli.ts
File metadata and controls
212 lines (190 loc) Β· 4.9 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/// <reference no-default-lib="true"/>
/// <reference lib="deno.ns" />
/// <reference lib="dom" />
/// <reference lib="esnext" />
import { parse } from "https://deno.land/std@0.97.0/flags/mod.ts";
import { join, toFileUrl } from "https://deno.land/std@0.97.0/path/mod.ts";
import { walk } from "https://deno.land/std@0.97.0/fs/walk.ts";
import { magenta, red } from "https://deno.land/std@0.97.0/fmt/colors.ts";
import { generate, GenerateConfig, init as initTw, TwInfo } from "./mod.ts";
import { Config } from "./types.ts";
import debounce from "https://esm.sh/debounce@1.2.1";
const NAME = "twd";
const VERSION = "0.4.8";
function usage() {
console.log(`
Usage: ${NAME} [-h|-v|-i] <input files, ...> [-d][-w][-o <output>]
Options:
-i, --init Initializes 'twd.ts' config file.
-w, --watch Watches the input file. If you set this option, you also need to specify -o option.
-o, --output Specifies the output file. If not specified, it prints in stdout.
-d, --debug Output warnings during extracting tailwind classes.
-v, --version Shows the version number.
-h, --help Shows the help message.
`.trim());
}
type CliArgs = {
help: boolean;
version: boolean;
watch: boolean;
output: string;
debug: boolean;
init: boolean;
_: string[];
};
export async function main(cliArgs: string[]): Promise<number> {
const {
help,
version,
watch,
output,
debug,
init,
_: paths,
} = parse(cliArgs, {
boolean: ["help", "version", "watch", "debug", "init"],
string: ["output"],
alias: {
v: "version",
h: "help",
w: "watch",
o: "output",
d: "debug",
i: "init",
},
}) as CliArgs;
if (version) {
console.log(`${NAME}@${VERSION}`);
return 0;
}
if (help) {
usage();
return 0;
}
if (init) {
try {
await Deno.lstat("twd.ts");
console.log(red("Error: twd.ts already exists"));
return 1;
} catch (e) {
if (e.name === "NotFound") {
console.log("Creating config file 'twd.ts'");
await Deno.writeTextFile(
"twd.ts",
`
import { Config } from "https://deno.land/x/twd@v0.4.8/types.ts";
export const config: Config = {
preflight: true,
theme: {},
plugins: {},
};
`.trim() + "\n",
);
console.log("Done!");
return 0;
}
throw e;
}
}
const files: string[] = [];
for (const path of paths) {
try {
files.push(...await expandFiles(path));
} catch (e) {
if (e.name === "NotFound") {
console.log(`Error: The given path not found: '${path}'`);
return 1;
}
throw e;
}
}
if (files.length === 0) {
console.error(red("No input is given"));
usage();
return 1;
}
const config: GenerateConfig = {
...await readConfig(),
mode: debug ? "warn" : "silent",
};
const info: TwInfo = initTw(config);
if (watch) {
if (!output) {
console.log(
"Error: --output option is not specified. You need to specify --output option with --watch option",
);
return 1;
}
const perform = debounce(async (retry?: boolean) => {
try {
await writeStyles(output, files, info);
} catch (e) {
if (e.name === "NotFound" && !retry) {
await perform(true);
return;
}
throw e;
}
});
perform();
for await (const _e of Deno.watchFs(files)) {
perform();
}
return 0;
}
if (output) {
await writeStyles(output, files, info);
return 0;
}
console.log(await genStyles(files, info));
return 0;
}
async function genStyles(files: string[], info: TwInfo) {
const start = Date.now();
const styles = generate(
await Promise.all(files.map((file) => Deno.readTextFile(file))),
info,
);
console.log(`Builtin ${Date.now() - start}ms`);
return styles;
}
async function writeStyles(
output: string,
files: string[],
info: TwInfo,
) {
console.log(`Writing styles to file '${output}'`);
await Deno.writeTextFile(output, await genStyles(files, info));
}
export async function readConfig(): Promise<Config> {
let config: Config;
try {
const path = toFileUrl(join(Deno.cwd(), "twd.ts"));
config = (await import(path.href)).config;
console.error(magenta(`Using config file: '${path}'`));
} catch {
console.error(
`Using default settings. You can optionally configure it by 'twd.ts' config file.`,
);
config = {};
}
return config;
}
export async function expandFiles(path: string): Promise<string[]> {
const result: string[] = [];
const stat = await Deno.lstat(path);
if (stat.isDirectory) {
for await (const entry of walk(path)) {
const stat = await Deno.lstat(entry.path);
if (!stat.isDirectory) {
result.push(entry.path);
}
}
} else {
result.push(path);
}
return result;
}
if (import.meta.main) {
Deno.exit(await main(Deno.args));
}