Skip to content

Commit

Permalink
feat: add support for package.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbayley authored and iiroj committed Nov 11, 2023
1 parent e023c2a commit 0ac8e91
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Options:

_Lint-staged_ can be configured in many ways:

- `lint-staged` object in your `package.json`
- `lint-staged` object in your `package.json`, or [`package.yaml`](https://github.com/pnpm/pnpm/pull/1799)
- `.lintstagedrc` file in JSON or YML format, or you can be explicit with the file extension:
- `.lintstagedrc.json`
- `.lintstagedrc.yaml`
Expand Down
19 changes: 18 additions & 1 deletion lib/loadConfig.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/** @typedef {import('./index').Logger} Logger */

import { basename } from 'node:path'

import debug from 'debug'
import { lilconfig } from 'lilconfig'
import YAML from 'yaml'
Expand All @@ -16,6 +18,8 @@ const PACKAGE_JSON = 'package.json'
* from, in the declared order.
*/
export const searchPlaces = [
'package.yaml',
'package.yml',
PACKAGE_JSON,
'.lintstagedrc',
'.lintstagedrc.json',
Expand All @@ -42,7 +46,20 @@ const jsonParse = (path, content) => {
}
}

const yamlParse = (path, content) => YAML.parse(content)
const yamlParse = (path, content) => {
const isPackageFile = searchPlaces.slice(0, 2).includes(basename(path))
try {
const yaml = YAML.parse(content)
return isPackageFile ? yaml['lint-staged'] : yaml
} catch (error) {
if (isPackageFile) {
debugLog('Ignoring invalid package file `%s` with content:\n%s', path, content)
return undefined
}

throw error
}
}

/**
* `lilconfig` doesn't support yaml files by default,
Expand Down
2 changes: 2 additions & 0 deletions test/unit/__mocks__/package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lint-staged:
"*": mytask
46 changes: 46 additions & 0 deletions test/unit/loadConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,50 @@ describe('loadConfig', () => {

await fs.rm(configFile)
})

it('should read "lint-staged" key from package.yaml', async () => {
expect.assertions(1)

const configFile = path.join(__dirname, '__mocks__', 'package.yaml')

await fs.writeFile(configFile, 'lint-staged:\n "*": mytask')

const { config } = await loadConfig({ configPath: configFile }, logger)

expect(config).toMatchInlineSnapshot(`
{
"*": "mytask",
}
`)
})

it('should read "lint-staged" key from package.yml', async () => {
expect.assertions(1)

const configFile = path.join(__dirname, '__mocks__', 'package.yml')

await fs.writeFile(configFile, 'lint-staged:\n "*": mytask')

const { config } = await loadConfig({ configPath: configFile }, logger)

expect(config).toMatchInlineSnapshot(`
{
"*": "mytask",
}
`)
})

it('should return null config when package.yaml file is invalid', async () => {
expect.assertions(1)

const configFile = path.join(__dirname, '__mocks__', 'package.yaml')

await fs.writeFile(configFile, '{')

const { config } = await loadConfig({ configPath: configFile }, logger)

expect(config).toBeUndefined()

await fs.rm(configFile)
})
})

0 comments on commit 0ac8e91

Please sign in to comment.