Skip to content

Commit

Permalink
feat(configuration): add validation, build tools and license & "extends"
Browse files Browse the repository at this point in the history
release-npm
  • Loading branch information
tobua committed Apr 20, 2024
1 parent 2d5c0d0 commit bcdf754
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 49 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Many web development projects often contain numerous configuration files in the

- No configuration files in your source code.
- Support for **gitignore**, **TypeScript**, **ESLint**, **Prettier**, **Biome**, **VS Code** and **Playwright**.
- Quickly configure bundlers like **Vite** and **Rsbuild**.
- Generate boilerplate before publishing: **LICENSE.md**.
- JSON based configuration in `package.json`.
- Optional typed programmatic interface in `configuration.ts`.
- Recommended configurations to easily extend.
Expand Down
53 changes: 53 additions & 0 deletions configuration/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { Configuration } from '../types'
import * as biome from './biome'
import * as eslint from './eslint'
import * as ignore from './gitignore'
import * as license from './license'
import * as playwright from './playwright'
import * as prettier from './prettier'
import * as rsbuild from './rsbuild'
import * as typescript from './typescript'
import * as vite from './vite'
import * as vscode from './vscode'

export { ignore }

export const configurations: Configuration[] = [
{
name: 'typescript',
alias: 'tsconfig',
configuration: typescript,
},
{
name: 'biome',
configuration: biome,
},
{
name: 'eslint',
configuration: eslint,
},
{
name: 'prettier',
configuration: prettier,
},
{
name: 'vscode',
configuration: vscode,
},
{
name: 'playwright',
configuration: playwright,
},
{
name: 'vite',
configuration: vite,
},
{
name: 'rsbuild',
configuration: rsbuild,
},
{
name: 'license',
configuration: license,
},
]
45 changes: 45 additions & 0 deletions configuration/license.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { PackageJson, Template } from '../types'

const getNameFromPackageJson = (packageJson: PackageJson) => {
if (typeof packageJson.author === 'string') {
return packageJson.author
}

if (typeof packageJson.author === 'object' && typeof packageJson.author.name === 'string') {
return packageJson.author.name
}

return packageJson.name
}

const mitLicense = (packageJson: PackageJson) => `MIT License
Copyright (c) ${new Date().getFullYear()} ${getNameFromPackageJson(packageJson)}
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.`

export const templates: Template<(packageJson: PackageJson) => string> = {
mit: mitLicense,
// biome-ignore lint/style/useNamingConvention: Alias for upper case typed license.
MIT: mitLicense,
}

export function createFile(content: string) {
return { name: 'LICENSE.md', contents: content }
}
5 changes: 5 additions & 0 deletions configuration/rsbuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const extension = (path: string) => ({ extends: path })

export function createFile(configuration: object) {
return { name: 'rsbuild.config.ts', contents: `export default ${JSON.stringify(configuration, null, 2)}` }
}
5 changes: 5 additions & 0 deletions configuration/vite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const extension = (path: string) => ({ extends: path })

export function createFile(configuration: object) {
return { name: 'vite.config.js', contents: `export default ${JSON.stringify(configuration, null, 2)}` }
}
23 changes: 23 additions & 0 deletions helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { create } from 'logua'
import { z } from 'zod'
import { configurations } from './configuration'

export const log = create('zero-configuration', 'blue')

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

for (const configuration of configurations) {
if (configuration.alias) {
keys[configuration.alias] = z.union([z.string(), z.object({}), z.boolean()])
}
}

const schema = z.object(keys).partial().strip()

export const validate = (configuration: unknown) => {
try {
return schema.parse(configuration)
} catch (error) {
log(`Invalid configuration provided: ${error}`, 'error')
}
}
48 changes: 5 additions & 43 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,13 @@ import { existsSync } from 'node:fs'
import { join } from 'node:path'
import { it } from 'avait'
import Bun from 'bun'
import { create } from 'logua'
import * as biome from './configuration/biome'
import * as eslint from './configuration/eslint'
import * as ignore from './configuration/gitignore'
import * as playwright from './configuration/playwright'
import * as prettier from './configuration/prettier'
import * as typescript from './configuration/typescript'
import * as vscode from './configuration/vscode'
import { configurations, ignore } from './configuration'
import { log, validate } from './helper'
import { parse } from './parse'
import type { Configuration } from './types'

const log = create('zero-configuration', 'blue')

// TODO validate inputs with zod.

const configurations: Configuration[] = [
{
name: 'typescript',
alias: 'tsconfig',
configuration: typescript,
},
{
name: 'biome',
configuration: biome,
},
{
name: 'eslint',
configuration: eslint,
},
{
name: 'prettier',
configuration: prettier,
},
{
name: 'vscode',
configuration: vscode,
},
{
name: 'playwright',
configuration: playwright,
},
]

const ignores: string[] = []

const packageJson = await Bun.file('./package.json').json()
// @ts-ignore
const { value: moduleContents } = await it(import(join(process.cwd(), './configuration.ts')))

if (!(moduleContents || Object.hasOwn(packageJson, 'configuration'))) {
Expand All @@ -58,10 +18,12 @@ if (!(moduleContents || Object.hasOwn(packageJson, 'configuration'))) {

const userConfiguration = packageJson.configuration ?? moduleContents

validate(userConfiguration)

for (const { name, alias, configuration } of configurations) {
const value = userConfiguration[name] ?? (alias && userConfiguration[alias])
if (!value) continue
const file = await parse(value, configuration)
const file = await parse(value, configuration, packageJson)
if (!file) continue
await Bun.write(file.name, file.contents)
ignores.push(file.name)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
},
"dependencies": {
"avait": "^1.0.0",
"logua": "^3.0.3"
"logua": "^3.0.3",
"zod": "^3.22.5"
},
"devDependencies": {
"@biomejs/biome": "^1.7.0",
Expand Down
9 changes: 5 additions & 4 deletions parse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync } from 'node:fs'
import { join } from 'node:path'
import { it } from 'avait'
import type { Configuration } from './types'
import type { Configuration, PackageJson } from './types'

const isExtension = async (value: string) => {
// NOTE dynamic import in tests will resolve relative to project node_modules and not fixture.
Expand All @@ -13,10 +13,11 @@ const isExtension = async (value: string) => {
return false
}

export async function parse(value: string | object | boolean, configuration: Configuration['configuration']) {
export async function parse(value: string | object | boolean, configuration: Configuration['configuration'], packageJson: PackageJson) {
// Template.
if (typeof value === 'string' && Object.hasOwn(configuration.templates, value)) {
const configurationTemplate = configuration.templates[value as keyof typeof configuration.templates]
if (typeof value === 'string' && configuration.templates && Object.hasOwn(configuration.templates, value)) {
const template = configuration.templates[value as keyof typeof configuration.templates]
const configurationTemplate = typeof template === 'function' ? (template as (value: PackageJson) => string)(packageJson) : template
return configuration.createFile(configurationTemplate)
}

Expand Down
1 change: 1 addition & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ test('Adds configuration files for basic package setup.', () => {

expect(existsSync(join(fixturePath, 'prettier.config.js'))).toBe(true)
expect(existsSync(join(fixturePath, 'biome.json'))).toBe(true)
expect(existsSync(join(fixturePath, 'LICENSE.md'))).toBe(true)
})

test('Adds configuration files for basic file setup.', () => {
Expand Down
4 changes: 4 additions & 0 deletions test/fixture/package/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"name": "package",
"author": {
"name": "Matthias Giger"
},
"configuration": {
"eslint": true,
"prettier": "recommended",
"biome": "recommended",
"playwright": true,
"license": "MIT",
"tsconfig": {
"compilerOptions": {
"target": "ES6"
Expand Down
21 changes: 21 additions & 0 deletions test/unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect, test } from 'bun:test'
import { validate } from '../helper'

test('Configuration is validated properly.', () => {
const configuration = {
biome: 'recommended',
prettier: true,
typescript: 'test/tsconfig.json',
tsconfig: 'my-module/tsconfig.json',
removed: 'not-available',
}

const validated = validate(configuration)

expect(validated).toBeDefined()
expect(typeof validated).toBe('object')
// Strips missing entries.
expect(validated && Object.hasOwn(validated, 'removed')).toBe(false)
// Keeps aliases.
expect(typeof validated?.tsconfig).toBe('string')
})
4 changes: 3 additions & 1 deletion types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export type Template<T = object> = { [key: string]: T }

export type PackageJson = { name: string; author?: string | { name: string } }

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

0 comments on commit bcdf754

Please sign in to comment.