-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
114 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,49 @@ | ||
/** | ||
* @typedef {import('vfile').VFile} VFile | ||
*/ | ||
|
||
/** | ||
* @typedef Info | ||
* Info. | ||
* @property {string} folder | ||
* Path to folder with `package.json`. | ||
* @property {string | undefined} license | ||
* SPDX identifier. | ||
* @property {string | undefined} name | ||
* Name of author. | ||
* @property {string | undefined} url | ||
* URL for author. | ||
*/ | ||
|
||
import fs from 'node:fs/promises' | ||
import path from 'node:path' | ||
import parse from 'parse-author' | ||
import {read} from 'to-vfile' | ||
import {findUp} from 'vfile-find-up' | ||
|
||
/** | ||
* @param {string} base | ||
* @returns {Promise<{license: string|undefined, name: string|undefined, url: string|undefined}>} | ||
* @param {VFile} from | ||
* File to resolve from. | ||
* @returns {Promise<Info | undefined>} | ||
* Info. | ||
*/ | ||
export async function findNearestPackage(base) { | ||
export async function findNearestPackage(from) { | ||
/* c8 ignore next -- else is for stdin, typically not used. */ | ||
const base = from.dirname ? path.resolve(from.cwd, from.dirname) : from.cwd | ||
const file = await findUp('package.json', base) | ||
|
||
if (file) { | ||
await read(file) | ||
file.value = await fs.readFile(file.path) | ||
/** @type {import('type-fest').PackageJson} */ | ||
const json = JSON.parse(String(file)) | ||
const author = | ||
typeof json.author === 'string' ? parse(json.author) : json.author || {} | ||
|
||
return {license: json.license, name: author.name, url: author.url} | ||
return { | ||
/* c8 ignore next -- always defined. */ | ||
folder: file.dirname ? path.resolve(file.cwd, file.dirname) : file.cwd, | ||
license: json.license, | ||
name: author.name, | ||
url: author.url | ||
} | ||
} | ||
|
||
// V8 bug on Node 12. | ||
/* c8 ignore next 2 */ | ||
return {license: undefined, name: undefined, url: undefined} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters