Skip to content

Commit

Permalink
fix(postinstall): proper path when run during postinstall
Browse files Browse the repository at this point in the history
release-npm
  • Loading branch information
tobua committed Apr 22, 2024
1 parent 8a2bf28 commit ba9d363
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# zero-configuration

<img align="right" src="https://github.com/tobua/zero-configuration/raw/main/logo.png" width="30%" alt="zero-configuration Logo" />
<img align="right" src="https://github.com/tobua/zero-configuration/raw/main/logo.png" width="20%" alt="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.

Expand Down
2 changes: 1 addition & 1 deletion configuration/biome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const templates = {
},
},
files: {
ignore: ['test/fixture', 'node_modules', 'package.json'],
ignore: ['node_modules', 'package.json'],
},
formatter: {
lineWidth: 140,
Expand Down
18 changes: 12 additions & 6 deletions helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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')
Expand All @@ -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)

Expand All @@ -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`,
)
}
}

Expand All @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand Down

0 comments on commit ba9d363

Please sign in to comment.