forked from MarkJamesHoward/TWLit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
59 lines (48 loc) · 1.55 KB
/
index.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
#!/usr/bin/env node
import * as fs from 'fs';
import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';
interface Args {
input: string;
output: string;
watch: boolean;
}
const parseArguments = (): Args => {
try {
const argv = yargs(hideBin(process.argv)).options({
'input': { type: 'string', default: './TailwindGenerated.css' },
'output': { type: 'string', default: './ReadyForLitimport.js' },
'watch': { type: 'boolean', default: false },
}).parseSync();
return { input: argv.input, output: argv.output, watch: argv.watch };
} catch (e) {
console.warn(`Error reading input/output parameters ${e}`);
process.exit(1);
}
};
const processFile = (input: string, output: string): void => {
try {
const contents = fs.readFileSync(input, 'utf8');
let cleanContents = contents.replaceAll('`', '');
cleanContents = cleanContents.replaceAll('\\', '\\\\');
const litContents = `
import { css } from 'lit';
export const TWStyles = css\` ${cleanContents} \`
`;
fs.writeFileSync(output, litContents);
console.log(`TWLit - wrote to file ${output}`);
} catch (err) {
console.log(`Failed to read file ${input}. Might just not be created yet? retrying..`);
}
};
const main = (): void => {
const { input, output, watch } = parseArguments();
console.log(`Reading from file ${input}`);
console.log(`Writing to ${output}`);
if (watch) {
fs.watchFile(input, { interval: 1000 }, () => processFile(input, output));
} else {
processFile(input, output);
}
};
main();