Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"build": "rm -rf lib && tsc",
"format": "prettier --write .",
"lint": "eslint .",
"lint": "eslint src",
"fixtures:type-check": " tsc -p ./fixtures/tsconfig.json",
"fixtures:lint-css": "stylelint './fixtures/**/*.css'",
"test": "npm run build && node --test && npm run fixtures:type-check && npm run fixtures:lint-css",
Expand Down
46 changes: 24 additions & 22 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ import {
safeCreateFile,
} from './writer.js'

function handleMistFileUpdate(cwd: string, filename: string) {
safeCreateFile(path.join(cwd, filename))
}

function handleMistFileDelete(cwd: string, filename: string) {
const genFilename = mistToGenFilename(filename)
if (fs.existsSync(path.join(cwd, genFilename))) {
fs.unlinkSync(path.join(cwd, genFilename))
}
}

function handleGenFileWithoutMistFile(cwd: string, filename: string) {
const mistFilename = genToMistFilename(filename)
if (!fs.existsSync(path.join(cwd, mistFilename))) {
fs.unlinkSync(path.join(cwd, filename))
}
}

const { values, positionals } = parseArgs({
options: {
watch: {
Expand Down Expand Up @@ -44,35 +62,19 @@ if (fs.statSync(fileOrDir).isFile()) {
const mistFiles = await globby(mistGlob, { cwd })
const genFiles = await globby(genGlob, { cwd })

function handleMistFileUpdate(filename: string) {
safeCreateFile(path.join(cwd, filename))
}

function handleMistFileDelete(filename: string) {
const genFilename = mistToGenFilename(filename)
if (fs.existsSync(path.join(cwd, genFilename))) {
fs.unlinkSync(path.join(cwd, genFilename))
}
}

function handleGenFileWithoutMistFile(filename: string) {
const mistFilename = genToMistFilename(filename)
if (!fs.existsSync(path.join(cwd, mistFilename))) {
fs.unlinkSync(path.join(cwd, filename))
}
}

// Watch files
if (values.watch) {
chokidar
.watch(mistGlob, { cwd })
.on('change', handleMistFileUpdate)
.on('unlink', handleMistFileDelete)
.on('change', (filename) => handleMistFileUpdate(cwd, filename))
.on('unlink', (filename) => handleMistFileDelete(cwd, filename))
}

// Re-generate all files
mistFiles.forEach(handleMistFileUpdate)
mistFiles.forEach((filename) => handleMistFileUpdate(cwd, filename))

// Clean up generated files without a corresponding mist file
genFiles.forEach((genFilename) => handleGenFileWithoutMistFile(genFilename))
genFiles.forEach((genFilename) =>
handleGenFileWithoutMistFile(cwd, genFilename),
)
}
6 changes: 3 additions & 3 deletions src/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import assert from 'node:assert'
import fs from 'node:fs'
import test from 'node:test'

import { type Components, parseInput, camelCase, pascalCase } from './parser.js'
import { camelCase, type Components, parseInput, pascalCase } from './parser.js'

// Fixtures
const mistCSS: string = fs.readFileSync('fixtures/Foo.mist.css', 'utf-8')

test('toCamelCase', () => {
void test('toCamelCase', () => {
const arr = ['foo', 'foo-bar', 'f', 'f-b']
const actual = arr.map(camelCase)
const expected = ['foo', 'fooBar', 'f', 'fB']
assert.deepStrictEqual(actual, expected)
})

test('toPascalCase', () => {
void test('toPascalCase', () => {
const arr = ['foo', 'foo-bar', 'f', 'f-b']
const actual = arr.map(pascalCase)
const expected = ['Foo', 'FooBar', 'F', 'FB']
Expand Down
4 changes: 2 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { compile, Element } from 'stylis'
// Components in a MistCSS file
export type Components = Record<string, Component>

export type Component = {
export interface Component {
tag: string
data: Record<string, string[] | boolean>
className?: string
Expand All @@ -16,7 +16,7 @@ const booleanDataAttributeRegex = /\[data-(?<attribute>[a-z-]+)(?=\])/g
const pascalCaseRegex = /(?:^|-)([a-z])/g

export function pascalCase(str: string): string {
return str.replace(pascalCaseRegex, (_, g) => g.toUpperCase())
return str.replace(pascalCaseRegex, (_, g: string) => g.toUpperCase())
}

const camelCaseRegex = /-([a-z])/g
Expand Down
1 change: 1 addition & 0 deletions src/renderer.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'node:assert'
import fs from 'node:fs'
import test from 'node:test'

import { Components, parseInput } from './parser.js'
import { render } from './renderer.js'

Expand Down
2 changes: 1 addition & 1 deletion src/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Components, Component } from './parser.js'
import { Component,Components } from './parser.js'

function renderProps(component: Component): string {
return Object.entries({
Expand Down
10 changes: 5 additions & 5 deletions src/writer.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import assert from 'node:assert'
import fs from 'node:fs'
import test from 'node:test'
import os from 'node:os'
import path from 'node:path'
import test from 'node:test'

import {
genToMistFilename,
mistToGenFilename,
safeCreateFile,
} from './writer.js'
import path from 'node:path'

test('genToMistFilename', () => {
void test('genToMistFilename', () => {
const actual = genToMistFilename('Foo.mist.tsx')
const expected = 'Foo.mist.css'
assert.strictEqual(actual, expected)
})

test('mistToGenFilename', () => {
void test('mistToGenFilename', () => {
const actual = mistToGenFilename('Foo.mist.css')
const expected = 'Foo.mist.tsx'
assert.strictEqual(actual, expected)
})

test('safeCreateFile', () => {
void test('safeCreateFile', () => {
const dir = os.tmpdir()
const mistCSS = path.join(dir, 'Foo.mist.css')
const mistTSX = path.join(dir, 'Foo.mist.tsx')
Expand Down
1 change: 1 addition & 0 deletions src/writer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'node:fs'
import path from 'node:path'

import { parseInput } from './parser.js'
import { render } from './renderer.js'

Expand Down