Skip to content

Commit

Permalink
fix: fail gracefully when getting npm info
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Aug 23, 2021
1 parent 2f40fd9 commit 515b85f
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/repos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,22 @@ export class Repos extends ConfigFile<RepoIndex> {

private async addAdditionalInfo(repo: Repository): Promise<Repository> {
const location = repo.location || path.join(this.directory.name, repo.org, repo.name);
const pkgJsonPath = path.join(location, 'package.json');
const pkgJson = JSON.parse(await readFile(pkgJsonPath, 'utf-8')) as { name: string };
repo.npm = { name: pkgJson.name };

const npmInfoRaw = exec(`npm view ${pkgJson.name} --json`, { silent: true }).stdout;
const npmInfo = JSON.parse(npmInfoRaw) as {
'dist-tags': Record<string, string>;
versions: string[];
};
repo.npm.version = npmInfo['dist-tags']['latest'] ?? npmInfo.versions.reverse()[0];
repo.npm.tags = npmInfo['dist-tags'];
repo.location = location;
const pkgJsonPath = path.join(location, 'package.json');
try {
const pkgJson = JSON.parse(await readFile(pkgJsonPath, 'utf-8')) as { name: string };
repo.npm = { name: pkgJson.name };

const npmInfoRaw = exec(`npm view ${pkgJson.name} --json`, { silent: true }).stdout;
const npmInfo = JSON.parse(npmInfoRaw) as {
'dist-tags': Record<string, string>;
versions: string[];
};
repo.npm.version = npmInfo['dist-tags']['latest'] ?? npmInfo.versions.reverse()[0];
repo.npm.tags = npmInfo['dist-tags'];
} catch {
// likely not an npm package, which is okay
}
return repo;
}
}

0 comments on commit 515b85f

Please sign in to comment.