Skip to content

Commit

Permalink
Check for npm package errors (closes #6)
Browse files Browse the repository at this point in the history
  • Loading branch information
knutkirkhorn committed Jul 7, 2023
1 parent 3501588 commit 22ec850
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ async function checkGithubActions(directoryPath) {
return ['GitHub Actions: missing workflow file in `.github/workflows`'];
}

async function checkNpmPackage(directoryPath) {
const packageJson = JSON.parse(await fs.readFile(path.join(directoryPath, 'package.json'), 'utf8'));
const isNotPrivate = packageJson.private !== true;

if (!isNotPrivate) return [];

const npmPackageErrors = [];

const hasLockFile = await fileExists(path.join(directoryPath, 'package-lock.json'));
if (hasLockFile) npmPackageErrors.push('npm package: should not have a lockfile (`package-lock.json`)');

const hasNpmrc = await fileExists(path.join(directoryPath, '.npmrc'));
if (!hasNpmrc) npmPackageErrors.push('npm package: should have a `.npmrc` file');

const isTypeScriptPackage = packageJson.devDependencies && packageJson.devDependencies.typescript !== undefined;
const hasIndexFile = await fileExists(path.join(directoryPath, 'index.js'));

if (!isTypeScriptPackage && hasIndexFile) {
const hasTypeDefinitions = await fileExists(path.join(directoryPath, 'index.d.ts'));
if (!hasTypeDefinitions) npmPackageErrors.push('npm package: should have type definitions (`index.d.ts`)');

const hasTypeDefinitionTests = await fileExists(path.join(directoryPath, 'index.test-d.ts'));
if (!hasTypeDefinitionTests) npmPackageErrors.push('npm package: should have type definition tests (`index.test-d.ts`)');
}

return npmPackageErrors;
}

async function checkDirectoryFiles(directoryPath) {
const filesToCheck = [...defaultFilesToCheck];
const isJavascriptDirectory = await hasPackageJson(directoryPath);
Expand All @@ -139,6 +167,9 @@ async function checkDirectoryFiles(directoryPath) {
}

if (isJavascriptDirectory) {
const npmPackageErrors = await checkNpmPackage(directoryPath);
errors.push(...npmPackageErrors);

const eslintErrors = await checkEslintConfig(directoryPath);
errors.push(...eslintErrors);
}
Expand Down

0 comments on commit 22ec850

Please sign in to comment.