Skip to content

Commit

Permalink
fix: set limited type to ext variable (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
HaecheonLee committed Apr 29, 2024
1 parent dbcf399 commit 0dcd857
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import chokidar from 'chokidar'
import { globby } from 'globby'

import { createFile } from './writer.js'
import { Extension } from './types/types.js'

function usage() {
console.log(`Usage: mistcss <directory> [options]
Expand Down Expand Up @@ -51,7 +52,7 @@ if (['react', 'hono', 'astro'].includes(values.target!) === false) {
process.exit(1)
}

let ext = ''
let ext: Extension
switch (values.target) {
case 'react':
ext = '.tsx'
Expand All @@ -65,6 +66,10 @@ switch (values.target) {
ext = '.astro'
console.log('Rendering Astro components')
break
default:
console.error('Invalid render option')
usage()
process.exit(1)
}

// Change directory
Expand Down
1 change: 1 addition & 0 deletions src/types/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Extension = '.tsx' | '.astro'
8 changes: 5 additions & 3 deletions src/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ import path from 'node:path'
import { parse } from './parser.js'
import { render as reactRender } from './renderers/react.js'
import { render as astroRender } from './renderers/astro.js'
import { Extension } from './types/types.js'

// Create a file from a mist file
export function createFile(mist: string, target: string, ext: string) {
export function createFile(mist: string, target: string, ext: Extension) {
try {
const data = parse(fs.readFileSync(mist, 'utf8'))
const name = path.basename(mist, '.mist.css')
if (data[0]) {
let result = ''
switch (target) {
case 'react':
// .tsx
result = reactRender(name, data[0])
break
case 'hono':
ext = '.tsx'
// .tsx
result = reactRender(name, data[0], true)
break
case 'astro':
ext = '.astro'
// .astro
result = astroRender(name, data[0])
break
}
Expand Down

0 comments on commit 0dcd857

Please sign in to comment.