diff --git a/package.json b/package.json index 3e62668..120f17b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/bin.ts b/src/bin.ts index 755e6c7..a210b4e 100755 --- a/src/bin.ts +++ b/src/bin.ts @@ -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: { @@ -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), + ) } diff --git a/src/parser.test.ts b/src/parser.test.ts index 8712ad2..a8d66ee 100644 --- a/src/parser.test.ts +++ b/src/parser.test.ts @@ -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'] diff --git a/src/parser.ts b/src/parser.ts index b5415bf..be5af2d 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -3,7 +3,7 @@ import { compile, Element } from 'stylis' // Components in a MistCSS file export type Components = Record -export type Component = { +export interface Component { tag: string data: Record className?: string @@ -16,7 +16,7 @@ const booleanDataAttributeRegex = /\[data-(?[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 diff --git a/src/renderer.test.ts b/src/renderer.test.ts index df53a27..9a34520 100644 --- a/src/renderer.test.ts +++ b/src/renderer.test.ts @@ -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' diff --git a/src/renderer.ts b/src/renderer.ts index c8a5669..c5fd12e 100644 --- a/src/renderer.ts +++ b/src/renderer.ts @@ -1,4 +1,4 @@ -import { Components, Component } from './parser.js' +import { Component,Components } from './parser.js' function renderProps(component: Component): string { return Object.entries({ diff --git a/src/writer.test.ts b/src/writer.test.ts index 6d9a95f..d7c717c 100644 --- a/src/writer.test.ts +++ b/src/writer.test.ts @@ -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') diff --git a/src/writer.ts b/src/writer.ts index 1b9f57b..23f363b 100644 --- a/src/writer.ts +++ b/src/writer.ts @@ -1,5 +1,6 @@ import fs from 'node:fs' import path from 'node:path' + import { parseInput } from './parser.js' import { render } from './renderer.js'