Skip to content

Commit

Permalink
Move lint util functions to bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Sep 27, 2023
1 parent 1c2c6fd commit fa7c321
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 52 deletions.
104 changes: 52 additions & 52 deletions pkg/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,58 +47,6 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {
const [module, modulePkgPath] = getPublishedField(rootPkg, 'module')
const [exports, exportsPkgPath] = getPublishedField(rootPkg, 'exports')

/**
* @param {string} path file path to read
* @param {string[]} [pkgPath] current path that tries to read this file.
* pass `undefined` to prevent error reporting if the file is missing.
* @param {string[]} tryExtensions list of extensions to try before giving up
* @returns {Promise<string | false>}
*/
async function readFile(path, pkgPath = undefined, tryExtensions = []) {
try {
const content = await vfs.readFile(path)
if (pkgPath && _packedFiles && !_packedFiles.includes(path)) {
fileNotPublished(pkgPath)
}
return content
} catch {
for (let ext of tryExtensions) {
// remove duplicated slashes
if (ext[0] === '/' && path[path.length - 1] === '/') {
ext = ext.slice(1)
}
try {
const content = await vfs.readFile(path + ext)
if (pkgPath && _packedFiles && !_packedFiles.includes(path)) {
fileNotPublished(pkgPath)
}
return content
} catch {}
}
if (pkgPath) {
messages.push({
code: 'FILE_DOES_NOT_EXIST',
args: {},
path: pkgPath,
type: 'error'
})
}
return false
}
}

/**
* @param {string[]} pkgPath
*/
function fileNotPublished(pkgPath) {
messages.push({
code: 'FILE_NOT_PUBLISHED',
args: {},
path: pkgPath,
type: 'error'
})
}

// Relies on default node resolution
// https://nodejs.org/api/modules.html#all-together
// LOAD_INDEX(X)
Expand Down Expand Up @@ -347,6 +295,58 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {

return { messages }

/**
* @param {string} path file path to read
* @param {string[]} [pkgPath] current path that tries to read this file.
* pass `undefined` to prevent error reporting if the file is missing.
* @param {string[]} tryExtensions list of extensions to try before giving up
* @returns {Promise<string | false>}
*/
async function readFile(path, pkgPath = undefined, tryExtensions = []) {
try {
const content = await vfs.readFile(path)
if (pkgPath && _packedFiles && !_packedFiles.includes(path)) {
fileNotPublished(pkgPath)
}
return content
} catch {
for (let ext of tryExtensions) {
// remove duplicated slashes
if (ext[0] === '/' && path[path.length - 1] === '/') {
ext = ext.slice(1)
}
try {
const content = await vfs.readFile(path + ext)
if (pkgPath && _packedFiles && !_packedFiles.includes(path)) {
fileNotPublished(pkgPath)
}
return content
} catch {}
}
if (pkgPath) {
messages.push({
code: 'FILE_DOES_NOT_EXIST',
args: {},
path: pkgPath,
type: 'error'
})
}
return false
}
}

/**
* @param {string[]} pkgPath
*/
function fileNotPublished(pkgPath) {
messages.push({
code: 'FILE_NOT_PUBLISHED',
args: {},
path: pkgPath,
type: 'error'
})
}

/**
* @param {string | Record<string, any>} fieldValue
* @param {string[]} currentPath
Expand Down
1 change: 1 addition & 0 deletions site/src/pages/Package.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
</script>
<svelte:head>
<!-- prettier-ignore -->
<title>{npmPkgName} - {npmPkgVersion ? npmPkgVersion + ' - ' : ''} publint</title>
</svelte:head>
Expand Down

0 comments on commit fa7c321

Please sign in to comment.