@skypack/package-check
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -14,3 +14,3 @@ #!/usr/bin/env node | ||
const run = cli.run || cli.cli || cli.default; | ||
run(process.argv).catch(function (error) { | ||
run(process.argv.slice(2)).catch(function (error) { | ||
console.error(` | ||
@@ -17,0 +17,0 @@ ${error.stack || error.message || error} |
@@ -1,1 +0,1 @@ | ||
export declare function cli(): Promise<void>; | ||
export declare function cli(args: string[]): Promise<void>; |
100
lib/index.js
@@ -28,2 +28,3 @@ "use strict"; | ||
const path_1 = __importDefault(require("path")); | ||
const yargs_parser_1 = __importDefault(require("yargs-parser")); | ||
const colors = __importStar(require("kleur/colors")); | ||
@@ -34,5 +35,10 @@ const get_repo_url_1 = require("./get-repo-url"); | ||
const result = pass(); | ||
if (!result) { | ||
console.error(colors.red('Check failed: '), title); | ||
console.error(colors.yellow('How to fix: '), url); | ||
if (result) { | ||
console.error(colors.green(`✓`) + colors.dim(` ${title}`)); | ||
} | ||
else if (!result) { | ||
console.error(colors.red(`✖ ${title}`)); | ||
console.error(''); | ||
console.error(colors.red('check failed:'), title); | ||
console.error(colors.red(' how to fix:'), url); | ||
process.exit(1); | ||
@@ -46,11 +52,20 @@ } | ||
} | ||
const cwd = process.cwd(); | ||
const packageJsonContents = fs_1.default.readFileSync(path_1.default.join(cwd, 'package.json'), { | ||
encoding: 'utf-8', | ||
}); | ||
const READMEContents = fs_1.default.readFileSync(path_1.default.join(cwd, 'README.md'), { | ||
encoding: 'utf-8', | ||
}); | ||
const pkg = JSON.parse(packageJsonContents); | ||
async function cli() { | ||
async function cli(args) { | ||
const cliFlags = yargs_parser_1.default(args, {}); | ||
const cwd = cliFlags.cwd ? path_1.default.resolve(cliFlags.cwd) : process.cwd(); | ||
const files = fs_1.default.readdirSync(cwd); | ||
// Check: Has a package.json | ||
runCheck({ | ||
title: 'package.json', | ||
url: 'https://docs.skypack.dev/package-authors/package-checks#esm', | ||
pass: () => { | ||
return !!files.includes('package.json'); | ||
}, | ||
}); | ||
// Load package.json | ||
const pkg = await fs_1.default.promises | ||
.readFile(path_1.default.join(cwd, 'package.json'), { | ||
encoding: 'utf-8', | ||
}) | ||
.then((packageJsonContents) => JSON.parse(packageJsonContents)); | ||
// Check: Has ESM | ||
@@ -61,8 +76,17 @@ runCheck({ | ||
pass: () => { | ||
return ((pkg.exports && | ||
!!(pkg.exports['import'] || | ||
!!Object.values(pkg.exports).find((x) => typeof x === 'object' && x.import))) || | ||
!!pkg.module || | ||
pkg.type === 'module' || | ||
(typeof pkg.main === 'string' && pkg.main.endsWith('.mjs'))); | ||
if (pkg.type === 'module') { | ||
return true; | ||
} | ||
if (pkg.module) { | ||
return true; | ||
} | ||
if (typeof pkg.main === 'string' && pkg.main.endsWith('.mjs')) { | ||
return true; | ||
} | ||
if (pkg.exports && | ||
(pkg.exports['import'] || | ||
!!Object.values(pkg.exports).find((x) => typeof x === 'object' && x.import))) { | ||
return true; | ||
} | ||
return false; | ||
}, | ||
@@ -110,10 +134,2 @@ }); | ||
}); | ||
// Check: Has "README" | ||
runCheck({ | ||
title: 'README', | ||
url: 'https://docs.skypack.dev/package-authors/package-checks#readme', | ||
pass: () => { | ||
return !!READMEContents; | ||
}, | ||
}); | ||
// Check: Has "repository url" | ||
@@ -125,6 +141,12 @@ runCheck({ | ||
let repositoryUrl; | ||
if (pkg.repository && pkg.repository.url) { | ||
repositoryUrl = get_repo_url_1.repoURL(pkg.repository.url); | ||
if (!pkg.repository) { | ||
return false; | ||
} | ||
return !!repositoryUrl; | ||
if (typeof pkg.repository === 'string') { | ||
return true; | ||
} | ||
if (pkg.repository.url) { | ||
return !!new URL(get_repo_url_1.repoURL(pkg.repository.url)); | ||
} | ||
return false; | ||
}, | ||
@@ -137,5 +159,23 @@ }); | ||
pass: () => { | ||
return !!pkg.types || !!pkg.typings; // `typings` is also valid according to TypeScript, even though `types` is preferred | ||
const isOk = !!pkg.types || !!pkg.typings || !!pkg.typesVersions; | ||
if (isOk) { | ||
return true; | ||
} | ||
if (files.includes('index.d.ts')) { | ||
console.error(colors.yellow('"./index.d.ts" file found, but package.json "types" entry is missing.')); | ||
console.error(colors.yellow('Learn more about why this is still required: https://github.com/skypackjs/package-check/issues/6#issuecomment-714840634')); | ||
return false; | ||
} | ||
return false; | ||
}, | ||
}); | ||
// Check: Has "README" | ||
runCheck({ | ||
title: 'README', | ||
url: 'https://docs.skypack.dev/package-authors/package-checks#readme', | ||
pass: () => { | ||
return !!files.find((f) => /^readme\.?/i.test(f)); | ||
}, | ||
}); | ||
console.error(''); | ||
console.error(colors.green(`[100/100] ${pkg.name} passes all quality checks.`)); | ||
@@ -142,0 +182,0 @@ } |
{ | ||
"name": "@skypack/package-check", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A quality score checker for npm packages.", | ||
@@ -38,3 +38,4 @@ "types": "lib/index.d.ts", | ||
"dependencies": { | ||
"kleur": "^4.1.3" | ||
"kleur": "^4.1.3", | ||
"yargs-parser": "^20.2.3" | ||
}, | ||
@@ -41,0 +42,0 @@ "directories": { |
@@ -7,3 +7,3 @@ # @skypack/package-check | ||
# yarn (run this in your package directory) | ||
yarn add package-check --dev | ||
yarn add @skypack/package-check --dev | ||
yarn run package-check | ||
@@ -14,4 +14,4 @@ ``` | ||
# npm (run this in your package directory) | ||
npm install package-check --dev | ||
npm install @skypack/package-check --dev | ||
npx package-check | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15933
239
2
+ Addedyargs-parser@^20.2.3
+ Addedyargs-parser@20.2.9(transitive)