Skip to content

Commit

Permalink
Return object as lint result
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jul 26, 2023
1 parent 86c410b commit 6b9d403
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Use `npx publint --help` for more information.
```js
import { publint } from 'publint'

const messages = await publint({
const { messages } = await publint({
/**
* Path to your package that contains a package.json file.
* Defaults to `process.cwd()` in node, `/` in browser.
Expand Down
6 changes: 5 additions & 1 deletion pkg/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,8 @@ export interface Vfs {
getExtName: (path: string) => string
}

export declare function publint(options?: Options): Promise<Message[]>
export interface Result {
messages: Message[]
}

export declare function publint(options?: Options): Promise<Result>
2 changes: 1 addition & 1 deletion pkg/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async function lintDir(pkgDir, level, strict, compact = false) {
if (!rootPkgContent) return logs
const rootPkg = JSON.parse(rootPkgContent)
const pkgName = rootPkg.name || path.basename(pkgDir)
const messages = await publint({ pkgDir, level, strict })
const { messages } = await publint({ pkgDir, level, strict })

if (messages.length) {
const suggestions = messages.filter((v) => v.type === 'suggestion')
Expand Down
10 changes: 5 additions & 5 deletions pkg/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {

/**
* @param {Options} options
* @returns {Promise<import('../index.js').Message[]>}
* @returns {Promise<import('../index.d.ts').Result>}
*/
export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {
/** @type {import('../index.d.ts').Message[]} */
Expand All @@ -39,7 +39,7 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {

const rootPkgPath = vfs.pathJoin(pkgDir, 'package.json')
const rootPkgContent = await readFile(rootPkgPath, [])
if (rootPkgContent === false) return messages
if (rootPkgContent === false) return { messages }
const rootPkg = JSON.parse(rootPkgContent)
const [main, mainPkgPath] = getPublishedField(rootPkg, 'main')
const [module, modulePkgPath] = getPublishedField(rootPkg, 'module')
Expand Down Expand Up @@ -306,12 +306,12 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {
}

if (level === 'warning') {
return messages.filter((m) => m.type !== 'suggestion')
return { messages: messages.filter((m) => m.type !== 'suggestion') }
} else if (level === 'error') {
return messages.filter((m) => m.type === 'error')
return { messages: messages.filter((m) => m.type === 'error') }
}

return messages
return { messages }

/**
* @param {string | Record<string, any>} fieldValue
Expand Down
2 changes: 1 addition & 1 deletion pkg/tests/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function testFixture(name, expectCodes, options) {
test(name, async () => {
const fixtureName = name.replace(/\(.*$/, '').trim()
const pkgDir = path.resolve(process.cwd(), 'tests/fixtures', fixtureName)
const messages = await publint({
const { messages } = await publint({
pkgDir,
level: options?.level,
strict: options?.strict
Expand Down
2 changes: 1 addition & 1 deletion site/src/utils/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ self.addEventListener('message', async (e) => {
postMessage({ type: 'status', data: 'Linting package...' })
// The tar file names have appended "package", except for `@types` packages very strangely
const pkgDir = files.length ? files[0].name.split('/')[0] : 'package'
const messages = await publint({ pkgDir, vfs })
const { messages } = await publint({ pkgDir, vfs })
const pkgJson = JSON.parse(await vfs.readFile(pkgDir + '/package.json'))

postMessage({
Expand Down

0 comments on commit 6b9d403

Please sign in to comment.