From ba9d363f75b58f4297cbe20ad3866565e20ae3b6 Mon Sep 17 00:00:00 2001 From: Matthias Giger Date: Mon, 22 Apr 2024 20:57:26 +0200 Subject: [PATCH] fix(postinstall): proper path when run during postinstall release-npm --- README.md | 2 +- configuration/biome.ts | 2 +- helper.ts | 18 ++++++++++++------ index.ts | 4 ++-- test/basic.test.ts | 2 +- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 859a65a..ce344c8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # zero-configuration -zero-configuration Logo +zero-configuration Logo 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. diff --git a/configuration/biome.ts b/configuration/biome.ts index 38d1a10..0f4a1bd 100644 --- a/configuration/biome.ts +++ b/configuration/biome.ts @@ -18,7 +18,7 @@ export const templates = { }, }, files: { - ignore: ['test/fixture', 'node_modules', 'package.json'], + ignore: ['node_modules', 'package.json'], }, formatter: { lineWidth: 140, diff --git a/helper.ts b/helper.ts index 244ed0f..cd0d9f4 100644 --- a/helper.ts +++ b/helper.ts @@ -10,6 +10,9 @@ import { state } from './state' export const log = create('zero-configuration', 'blue') +export const root = (file: string) => + process.cwd().includes('node_modules') ? join(process.cwd(), '../..', file) : join(process.cwd(), file) + const keys = Object.fromEntries(configurations.map((current) => [current.name, z.union([z.string(), z.object({}), z.boolean()])])) for (const configuration of configurations) { @@ -29,9 +32,9 @@ export const validate = (configuration: unknown) => { } export async function findConfiguration() { - const packageJson = await Bun.file('./package.json').json() - const { value: typeScriptModuleContents } = await it(import(join(process.cwd(), './configuration.ts'))) - const { value: javaScriptModuleContents } = await it(import(join(process.cwd(), './configuration.js'))) + const packageJson = await Bun.file(root('./package.json')).json() + const { value: typeScriptModuleContents } = await it(import(root('./configuration.ts'))) + const { value: javaScriptModuleContents } = await it(import(root('./configuration.js'))) if (!(typeScriptModuleContents || javaScriptModuleContents || Object.hasOwn(packageJson, 'configuration'))) { log('No configuration found', 'error') @@ -50,7 +53,7 @@ export async function findConfiguration() { async function addAdditionalGitignoreEntries(file: { name: string; contents: string }) { const addedIgnores: string[] = [] - const existingFileContents = await Bun.file(file.name).text() + const existingFileContents = await Bun.file(root(file.name)).text() const { patterns: existingIgnores } = parse(existingFileContents) const { patterns: updatedIgnores } = parse(file.contents) @@ -61,7 +64,10 @@ async function addAdditionalGitignoreEntries(file: { name: string; contents: str } if (addedIgnores.length) { - await Bun.write(file.name, `${existingFileContents}${existingFileContents.endsWith('\n') ? '' : '\n'}${addedIgnores.join('\n')}\n`) + await Bun.write( + root(file.name), + `${existingFileContents}${existingFileContents.endsWith('\n') ? '' : '\n'}${addedIgnores.join('\n')}\n`, + ) } } @@ -81,7 +87,7 @@ export async function writeGitIgnore(ignores: string[]) { if (existsSync(file.name)) { await addAdditionalGitignoreEntries(file) } else { - await Bun.write(file.name, file.contents) + await Bun.write(root(file.name), file.contents) } return Object.hasOwn(state.options, 'ignore') || Object.hasOwn(state.options, 'gitignore') diff --git a/index.ts b/index.ts index 2e91bbd..fc46822 100644 --- a/index.ts +++ b/index.ts @@ -1,7 +1,7 @@ #!/usr/bin/env bun import Bun from 'bun' import { configurations } from './configuration' -import { findConfiguration, log, writeGitIgnore } from './helper' +import { findConfiguration, log, root, writeGitIgnore } from './helper' import { parse } from './parse' import { state } from './state' @@ -14,7 +14,7 @@ for (const { name, alias, configuration } of configurations) { if (!value) continue const file = await parse(value, configuration) if (!file) continue - await Bun.write(file.name, file.contents) + await Bun.write(root(file.name), file.contents) ignores.push(file.name) } diff --git a/test/basic.test.ts b/test/basic.test.ts index 9b3cc75..b251617 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -54,7 +54,7 @@ test('Extends existing configurations.', async () => { const biome = await Bun.file(join(fixturePath, 'biome.json')).json() - expect(biome.files.ignore[0]).toBe('test/fixture') + expect(biome.files.ignore[2]).toBe('test/fixture') expect(biome.linter.rules.all).toBe(true) expect(biome.linter.rules.style.useBlockStatements).toBe('off') })