Skip to content

Commit

Permalink
Merge pull request #19 from sieem/feature/node-module-resolution
Browse files Browse the repository at this point in the history
feat: use node module resolution to find packages
  • Loading branch information
robrechtme authored Sep 12, 2023
2 parents 31245b5 + 1481bf6 commit adfbb28
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 53 deletions.
17 changes: 8 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
#! /usr/bin/env node
"use strict";

var _fs = _interopRequireDefault(require("fs"));
var _promises = _interopRequireDefault(require("fs/promises"));
var _path = _interopRequireDefault(require("path"));
var _util = require("util");
var _licenseUtils = require("./licenseUtils");
var _util2 = require("./util");
var _util = require("./util");
var _packageUtils = require("./packageUtils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
async function readFromLicenseFile(basePath) {
const matches = await (0, _util.promisify)(_fs.default.readdir)(basePath);
const matches = await _promises.default.readdir(basePath);
const validLicenseFiles = matches.filter(match => match.toUpperCase().startsWith('LICENSE'));
if (validLicenseFiles.length < 1) {
console.log(`No valid license files found in ${basePath}`);
return null;
}
const licenseFile = _path.default.join(basePath, validLicenseFiles[0]);
const contents = (await (0, _util.promisify)(_fs.default.readFile)(licenseFile)).toString();
const contents = (await _promises.default.readFile(licenseFile)).toString();
const file = licenseFile.split('node_modules/')[1];
return {
file,
Expand Down Expand Up @@ -46,15 +45,15 @@ async function getPackageDetails(dep) {
};
}
(async () => {
const flags = (0, _util2.parseCLIFlags)(process.argv);
const flags = (0, _util.parseCLIFlags)(process.argv);
let [dependencies, devDependencies] = await (0, _packageUtils.getDependencies)(Boolean(flags.includeDev));
if (devDependencies !== null) {
dependencies = [...dependencies, ...devDependencies];
}
if (flags.exclude) {
if (Array.isArray(flags.exclude) && flags.exclude.length > 1) {
// Get a diff between excluded and current if multiple provided
dependencies = (0, _util2.diff)(dependencies, flags.exclude);
dependencies = (0, _util.diff)(dependencies, flags.exclude);
} else if (typeof flags.exclude === 'string') {
// If only one dependency provided, exclude it from current dependencies
dependencies = dependencies.filter(dep => dep !== flags.exclude);
Expand All @@ -66,12 +65,12 @@ async function getPackageDetails(dep) {
if (flags.export) {
if (typeof flags.export === 'string') {
const filename = _path.default.join(process.cwd(), flags.export);
(0, _util.promisify)(_fs.default.writeFile)(filename, `${JSON.stringify(resolvedDeps, null, 2)}\n`);
_promises.default.writeFile(filename, `${JSON.stringify(resolvedDeps, null, 2)}\n`);
console.log(`Licenses exported to ${filename}`);
} else if (typeof flags.export === 'boolean') {
// Export to default licenses.json if no exported file name provided
const filename = _path.default.join(process.cwd(), 'licenses.json');
(0, _util.promisify)(_fs.default.writeFile)(filename, `${JSON.stringify(resolvedDeps, null, 2)}\n`);
_promises.default.writeFile(filename, `${JSON.stringify(resolvedDeps, null, 2)}\n`);
console.log(`Licenses exported to ${filename}`);
}
} else {
Expand Down
25 changes: 17 additions & 8 deletions dist/packageUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,24 @@ async function getDependencies(includeDev) {
}

/**
* Returns descriptor (basepath and package.json content) for a package
* Returns descriptor (basePath and package.json content) for a package
* Emulating node js module resolution (see: https://medium.com/outbrain-engineering/node-js-module-resolution-af46715784ef)
*/
async function getPackageDescriptor(dep) {
const basePath = _path.default.join(process.cwd(), 'node_modules', dep);
const pkg = await (0, _util.readJSONFile)(_path.default.join(basePath, 'package.json'));
return {
basePath,
pkg
};
async function getPackageDescriptor(dep, basePath = process.cwd(), basePathTries = []) {
try {
const pkg = await (0, _util.readJSONFile)(_path.default.join(basePath, 'node_modules', dep, 'package.json'), false);
return {
pkg,
basePath: _path.default.join(basePath, 'node_modules', dep)
};
} catch (error) {
basePathTries = [...basePathTries, _path.default.join(basePath, 'node_modules', dep)];
if (basePath === '/') {
console.error(`Could not find package: '${dep}', tried: \n\t${basePathTries.join('\n\t')}`);
return process.exit(1);
}
return getPackageDescriptor(dep, _path.default.join(basePath, '..'), basePathTries);
}
}

/**
Expand Down
11 changes: 6 additions & 5 deletions dist/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Object.defineProperty(exports, "__esModule", {
exports.diff = diff;
exports.parseCLIFlags = parseCLIFlags;
exports.readJSONFile = readJSONFile;
var _fs = _interopRequireDefault(require("fs"));
var _util = require("util");
var _promises = _interopRequireDefault(require("fs/promises"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Creates a nice object of flags from Node's process.argv
Expand Down Expand Up @@ -40,12 +39,14 @@ function diff(a, b) {
/**
* Safely read a file and parse it as JSON
*/
async function readJSONFile(filePath) {
async function readJSONFile(filePath, outputFail = true) {
try {
const fileBuffer = await (0, _util.promisify)(_fs.default.readFile)(filePath);
const fileBuffer = await _promises.default.readFile(filePath);
return JSON.parse(fileBuffer.toString());
} catch (error) {
console.error('Could not read JSON file:', error);
if (outputFail) {
console.error('Could not read JSON file:', error);
}
throw error;
}
}
14 changes: 0 additions & 14 deletions dist/whitelist.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@inthepocket/npm-license-scraper",
"version": "2.3.1",
"version": "2.3.2",
"description": "Dead simple license scraper and validator with zero dependencies.",
"main": "dist/index.js",
"bin": {
Expand Down
11 changes: 5 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#! /usr/bin/env node

import fs from 'fs';
import fs from 'fs/promises';
import path from 'path';
import { promisify } from 'util';

import { isValidLicense } from './licenseUtils';
import { parseCLIFlags, diff } from './util';
import { getPackageDescriptor, getPackageInfo, getDependencies } from './packageUtils';


async function readFromLicenseFile(basePath: string) {
const matches = await promisify(fs.readdir)(basePath);
const matches = await fs.readdir(basePath);
const validLicenseFiles = matches.filter(match => match.toUpperCase().startsWith('LICENSE'));

if (validLicenseFiles.length < 1) {
Expand All @@ -19,7 +18,7 @@ async function readFromLicenseFile(basePath: string) {
}

const licenseFile = path.join(basePath, validLicenseFiles[0]);
const contents = (await promisify(fs.readFile)(licenseFile)).toString();
const contents = (await fs.readFile(licenseFile)).toString();

const file = licenseFile.split('node_modules/')[1];

Expand Down Expand Up @@ -77,12 +76,12 @@ async function getPackageDetails(dep: string) {
if (flags.export) {
if (typeof flags.export === 'string') {
const filename = path.join(process.cwd(), flags.export);
promisify(fs.writeFile)(filename, `${JSON.stringify(resolvedDeps, null, 2)}\n`);
fs.writeFile(filename, `${JSON.stringify(resolvedDeps, null, 2)}\n`);
console.log(`Licenses exported to ${filename}`);
} else if (typeof flags.export === 'boolean') {
// Export to default licenses.json if no exported file name provided
const filename = path.join(process.cwd(), 'licenses.json');
promisify(fs.writeFile)(filename, `${JSON.stringify(resolvedDeps, null, 2)}\n`);
fs.writeFile(filename, `${JSON.stringify(resolvedDeps, null, 2)}\n`);
console.log(`Licenses exported to ${filename}`);
}
} else {
Expand Down
32 changes: 27 additions & 5 deletions src/packageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,34 @@ export async function getDependencies(
}

/**
* Returns descriptor (basepath and package.json content) for a package
* Returns descriptor (basePath and package.json content) for a package
* Emulating node js module resolution (see: https://medium.com/outbrain-engineering/node-js-module-resolution-af46715784ef)
*/
export async function getPackageDescriptor(dep: string) {
const basePath = path.join(process.cwd(), 'node_modules', dep);
const pkg = await readJSONFile(path.join(basePath, 'package.json'));
return { basePath, pkg };
export async function getPackageDescriptor(
dep: string,
basePath = process.cwd(),
basePathTries: string[] = []
): Promise<{
basePath: string;
pkg: any;
}> {
try {
const pkg = await readJSONFile(path.join(basePath, 'node_modules', dep, 'package.json'), false);

return {
pkg,
basePath: path.join(basePath, 'node_modules', dep),
};
} catch (error) {
basePathTries = [...basePathTries, path.join(basePath, 'node_modules', dep)];

if (basePath === '/') {
console.error(`Could not find package: '${dep}', tried: \n\t${basePathTries.join('\n\t')}`);
return process.exit(1);
}

return getPackageDescriptor(dep, path.join(basePath, '..'), basePathTries);
}
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'fs';
import { promisify } from "util";
import fs from 'fs/promises';

/**
* Creates a nice object of flags from Node's process.argv
Expand Down Expand Up @@ -36,12 +35,14 @@ export function diff(a: any[], b: any[]) {
/**
* Safely read a file and parse it as JSON
*/
export async function readJSONFile(filePath: string) {
export async function readJSONFile(filePath: string, outputFail = true) {
try {
const fileBuffer = await promisify(fs.readFile)(filePath);
const fileBuffer = await fs.readFile(filePath);
return JSON.parse(fileBuffer.toString());
} catch (error) {
console.error('Could not read JSON file:', error);
if (outputFail) {
console.error('Could not read JSON file:', error);
}
throw error;
}
}

0 comments on commit adfbb28

Please sign in to comment.