Skip to content

Commit

Permalink
feat(configuration): multiple file support and ignore file for prettier
Browse files Browse the repository at this point in the history
release-npm
  • Loading branch information
tobua committed May 21, 2024
1 parent ed42b1b commit 5419355
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Many web development projects often contain numerous configuration files in the project's root directory, with little to no actual source code. While many plugins nowadays require configuration files, this plugin aims to generate them without the necessity of committing anything to the source code.

- No configuration files in your source code.
- Support for **gitignore**, **TypeScript**, **ESLint**, **Prettier**, **Biome**, **VS Code**, **Playwright**, **Cypress** and **Vitest**.
- Support for **gitignore**, **TypeScript**, **ESLint**, **Prettier**, **Biome**, **VS Code**, **Playwright**, **Cypress**, **Tailwind**, **PostCSS**, **Vercel** and **Vitest**.
- Quickly configure bundlers like **Vite** and **Rsbuild**.
- Generate boilerplate before publishing: **LICENSE.md**.
- JSON based configuration in `package.json`.
Expand Down Expand Up @@ -71,7 +71,7 @@ export const typescript = true | 'recommended' | 'plugin' | 'web' | { extends: '
export const tsconfig // Alias for typescript
export const biome = true | 'recommended' | { extends: 'recommended', files: { ignore: ['demo'] } }
export const eslint = true | 'recommended' | [{ rules: { semi: 'error' } }]
export const prettier = true | 'recommended' | { extends: 'recommended', printWidth: 140 }
export const prettier = true | 'recommended' | { extends: 'recommended', printWidth: 140, ignore: ['test/fixture'] }
export const vscode = true | 'biome' | 'prettier-eslint' | { 'editor.defaultFormatter': 'biomejs.biome' }
export const playwright = object | File
export const vite = object | File
Expand Down
21 changes: 18 additions & 3 deletions configuration/prettier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@ export const templates: Template<object> = {
semi: false,
singleQuote: true,
printWidth: 120,
ignore: ['dist'],
},
}

export function createFile(configuration: object) {
// TODO use prettier to format the whole file.
return { name: 'prettier.config.js', contents: `export default ${JSON.stringify(configuration, null, 2)}` }
// biome-ignore lint/suspicious/noExplicitAny: Various configuration options.
export function createFile(configuration: Record<string, any>) {
const ignores = configuration.ignore

// biome-ignore lint/performance/noDelete: Not valid prettier property.
delete configuration.ignore

const files = [
// TODO use prettier to format the whole file.
{ name: 'prettier.config.js', contents: `export default ${JSON.stringify(configuration, null, 2)}` },
]

if (Array.isArray(ignores) && ignores.length) {
files.push({ name: '.prettierignore', contents: ignores.join('\n') })
}

return files
}
6 changes: 6 additions & 0 deletions helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { z } from 'zod'
import { configurations, ignore } from './configuration'
import { log } from './log'
import { root, state } from './state'
import type { File } from './types'

const keys = Object.fromEntries(configurations.map((current) => [current.name, z.union([z.string(), z.object({}), z.boolean()])]))

Expand Down Expand Up @@ -175,3 +176,8 @@ export function installLocalDependencies() {
}
}
}

export async function writeFile(file: File) {
await Bun.write(root(file.name), file.contents)
return file.name
}
21 changes: 14 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env bun
import Bun from 'bun'
import { configurations } from './configuration'
import { findConfiguration, getWorkspaces, installLocalDependencies, writeGitIgnore } from './helper'
import { findConfiguration, getWorkspaces, installLocalDependencies, writeFile, writeGitIgnore } from './helper'
import { log } from './log'
import { parse } from './parse'
import { reset, root, state } from './state'
import { reset, state } from './state'
import type { File } from './types'

async function configureProject() {
const ignores: string[] = []
Expand All @@ -14,10 +14,17 @@ async function configureProject() {
for (const { name, alias, configuration } of configurations) {
const value = state.options[name] ?? (alias && state.options[alias])
if (!value) continue
const file = await parse(value, configuration)
if (!file) continue
await Bun.write(root(file.name), file.contents)
ignores.push(file.name)
const files = await parse(value, configuration)
if (!files) continue
if (Array.isArray(files)) {
for (const file of files.filter((item) => item?.name)) {
const name = await writeFile(file as File)
ignores.push(name)
}
} else {
const name = await writeFile(files as File)
ignores.push(name)
}
}

const gitUserConfigured = await writeGitIgnore(ignores)
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
},
"vscode": "biome",
"typescript": {
"compilerOptions": {
"target": "ES2022",
"lib": ["DOM", "ES2022"]
},
"extends": "plugin",
"files": [
"index.ts"
Expand Down
6 changes: 6 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test('Adds configuration files for basic package setup.', () => {
})

expect(existsSync(join(fixturePath, 'prettier.config.js'))).toBe(true)
expect(existsSync(join(fixturePath, '.prettierignore'))).toBe(true)
expect(existsSync(join(fixturePath, 'biome.json'))).toBe(true)
expect(existsSync(join(fixturePath, 'LICENSE.md'))).toBe(true)
})
Expand All @@ -28,6 +29,7 @@ test('Adds configuration files for basic file setup.', async () => {
})

expect(existsSync(join(fixturePath, 'prettier.config.js'))).toBe(true)
expect(existsSync(join(fixturePath, '.prettierignore'))).toBe(true)
expect(existsSync(join(fixturePath, 'playwright.config.ts'))).toBe(true)
expect(existsSync(join(fixturePath, 'biome.json'))).toBe(true)
expect(existsSync(join(fixturePath, 'vitest.config.ts'))).toBe(true)
Expand All @@ -40,6 +42,10 @@ test('Adds configuration files for basic file setup.', async () => {
const cypressConfig = await import(join('..', fixturePath, 'cypress.config.ts'))
// Configuration not serialized.
expect(typeof cypressConfig.default.e2e.dynamic === 'function').toBe(true)

const prettierIgnoreFile = await Bun.file(join(fixturePath, '.prettierignore')).text()
expect(prettierIgnoreFile).toContain('dist')
expect(prettierIgnoreFile).toContain('test')
})

test('Also parses JavaScript configuration.', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/file/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// NOTE imports will fail in fixture.

export const prettier = 'recommended'
export const prettier = { extends: 'recommended', ignore: ['test'] }

export const playwright = {
fullyParallel: true,
Expand Down
4 changes: 3 additions & 1 deletion types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ export type PackageJson = {
configuration?: { [key: string]: string | object | string[] }
}

export type File = { name: string; contents: string }

export type Configuration = {
name: ConfigurationKeys
alias?: ConfigurationKeys
configuration: {
templates?: Template<string | object | string[]>
// biome-ignore lint/suspicious/noExplicitAny: Will be specified in file explicitly.
createFile: (value?: any) => { name: string; contents: string } | undefined
createFile: (value?: any) => File | (File | undefined)[] | undefined
extension?: (path: string) => object
}
}
Expand Down

0 comments on commit 5419355

Please sign in to comment.