Skip to content

Commit

Permalink
enh: use type guard to target (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
HaecheonLee committed May 1, 2024
1 parent 0a44764 commit 7dec693
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import { render as reactRender } from './renderers/react.js'
import { render as astroRender } from './renderers/astro.js'

type Extension = '.tsx' | '.astro'
type Target = 'react' | 'hono' | 'astro';

function createFile(mist: string, target: string, ext: Extension) {
function createFile(mist: string, target: Target, ext: Extension) {
try {
const data = parse(fs.readFileSync(mist, 'utf8'))
const name = path.basename(mist, '.mist.css')
Expand Down Expand Up @@ -79,15 +80,16 @@ if (!(await fsPromises.stat(dir)).isDirectory()) {
process.exit(1)
}

if (['react', 'hono', 'astro'].includes(values.target!) === false) {
const { target } = values;
if (target !== 'react' && target !== 'hono' && target !== 'astro') {
console.error('Invalid render option')
usage()
process.exit(1)
}

// Set extension
let ext: Extension
switch (values.target) {
switch (target) {
case 'react':
ext = '.tsx'
console.log('Rendering React components')
Expand Down Expand Up @@ -115,15 +117,15 @@ if (values.watch) {
console.log('Watching for changes')
chokidar
.watch('**/*.mist.css')
.on('change', (file) => createFile(file, values.target!, ext))
.on('change', (file) => createFile(file, target, ext))
.on('unlink', (file) => {
fsPromises.unlink(file.replace(/\.css$/, ext)).catch(() => false)
})
}

// Build out files
;(await globby('**/*.mist.css')).forEach((mist) =>
createFile(mist, values.target!, ext),
createFile(mist, target, ext),
)

// Clean out files without a matching mist file
Expand Down

0 comments on commit 7dec693

Please sign in to comment.