Skip to content

Commit

Permalink
💥 Use ES modules instead of CommonJS
Browse files Browse the repository at this point in the history
  • Loading branch information
ENT8R committed Sep 9, 2021
1 parent 00102ab commit 08f8ab4
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 160 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"root": true,
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "script",
"sourceType": "module",
"ecmaFeatures": {
"impliedStrict": true
}
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ node_modules/
package-lock.json

.coveralls.yml
.nyc_output/
coverage/
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: node_js

node_js:
- "10"
- "12"
- "14"
- "node"

cache:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $ npm install package-age --global

## Usage

<img src="carbon.png">
<img src="assets/carbon.png">

## License

Expand Down
Binary file added assets/carbon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed carbon.png
Binary file not shown.
81 changes: 81 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env node
import chalk from 'chalk';
import Table from 'cli-table3';
import { program } from 'commander';

import config from './lib/config.js';
import * as dates from './lib/dates.js';
import * as dependencies from './lib/dependencies.js';
import * as versions from './lib/versions.js';

const SHORT = {
devDependencies: chalk.blue.bold('dev'),
peerDependencies: chalk.magenta.bold('peer'),
bundledDependencies: chalk.cyan.bold('bundled'),
};

const table = new Table({
head: [
chalk.keyword('orange').underline('Name'),
chalk.keyword('orange').underline('Type'),
chalk.keyword('orange').underline('Version'),
chalk.keyword('orange').underline('Last Publish')
]
});

program
.name('package-age')
.version(config.version, '-v, --version')
.description('A CLI for detecting old dependencies used in your project')
.option('-f, --file [optional]', 'path to the package.json', 'package.json')

.option('-y, --year [optional]', 'after how much years a package should be considered old', 2)
.option('-m, --month [optional]', 'after how much months a package should be considered old', 0)

.option('-a, --all', 'parameter to get all kinds of dependencies', false)
.option('-d, --dev', 'parameter to get the devDependencies', false)
.option('-p, --peer', 'parameter to get the peerDependencies', false)
.option('-b, --bundled', 'parameter to get the bundledDependencies', false)
.parse(process.argv);

async function cli() {
const options = program.opts();
const results = await dependencies.get(Object.assign(config, {
file: options.file,
year: options.year,
month: options.month,
dependencies: {
all: options.all,
dev: options.dev,
peer: options.peer,
bundled: options.bundled
}
}));

// Print the results to the console
Object.entries(results).forEach(([key, dependencies]) => {
dependencies.forEach(dependency => {
let version;
let date;

if (dependency.valid) {
version = versions.compare(dependency.version, dependency.latest);
date = dates.compare(dependency.date, config.year, config.month);
} else {
version = chalk.bgRed.bold(`supplied invalid version: '${version}'`);
date = null;
}

table.push([
dependency.name,
SHORT[key] || null,
version,
date
]);
});
});

process.stdout.write(table.toString());
}

cli();
96 changes: 3 additions & 93 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,4 @@
#!/usr/bin/env node
import config from './lib/config.js';
import * as dependencies from './lib/dependencies.js';

const chalk = require('chalk');
const Table = require('cli-table3');
const commander = require('commander');

const dates = require('./lib/dates');
const dependencies = require('./lib/dependencies');
const versions = require('./lib/versions');

const config = {
version: require('./package.json').version,
registry: 'https://registry.npmjs.org',
file: 'package.json',
year: 2,
month: 0,
dependencies: {}
};

const SHORT = {
devDependencies: chalk.blue.bold('dev'),
peerDependencies: chalk.magenta.bold('peer'),
bundledDependencies: chalk.cyan.bold('bundled'),
};

const table = new Table({
head: [
chalk.keyword('orange').underline('Name'),
chalk.keyword('orange').underline('Type'),
chalk.keyword('orange').underline('Version'),
chalk.keyword('orange').underline('Last Publish')
]
});

commander
.version(config.version, '-v, --version')
.description('A CLI for detecting old dependencies used in your project')
.option('-f, --file [optional]', 'path to the package.json', 'package.json')

.option('-y, --year [optional]', 'after how much years a package should be considered old', 2)
.option('-m, --month [optional]', 'after how much months a package should be considered old', 0)

.option('-a, --all', 'parameter to get all kinds of dependencies', false)
.option('-d, --dev', 'parameter to get the devDependencies', false)
.option('-p, --peer', 'parameter to get the peerDependencies', false)
.option('-b, --bundled', 'parameter to get the bundledDependencies', false)
.parse(process.argv);


async function cli() {
const results = await dependencies.get(Object.assign(config, {
file: commander.file,
year: commander.year,
month: commander.month,
dependencies: {
all: commander.all,
dev: commander.dev,
peer: commander.peer,
bundled: commander.bundled
}
}));

// Print the results to the console
Object.entries(results).forEach(([key, dependencies]) => {
dependencies.forEach(dependency => {
let version;
let date;

if (dependency.valid) {
version = versions.compare(dependency.version, dependency.latest);
date = dates.compare(dependency.date, config.year, config.month);
} else {
version = chalk.bgRed.bold(`supplied invalid version: '${version}'`);
date = null;
}

table.push([
dependency.name,
SHORT[key] || null,
version,
date
]);
});
});

console.log(table.toString()); // eslint-disable-line no-console
}


if (require.main === module) {
cli();
} else {
module.exports = options => dependencies.get(Object.assign(config, options));
}
export default options => dependencies.get(Object.assign({}, config, options));
10 changes: 10 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { readPackageSync } from 'read-pkg';

const version = readPackageSync().version;
const registry = 'https://registry.npmjs.org';
const file = 'package.json';
const year = 2;
const month = 0;
const dependencies = {};

export default { version, registry, file, year, month, dependencies };
8 changes: 2 additions & 6 deletions lib/dates.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const chalk = require('chalk');
import chalk from 'chalk';

/* Compares a given date (usually the date when
a specific version of a package has been released)
with another date which is constructed by specifying
a year and a month after which a package is considered old */
function compare(date, year, month) {
export function compare(date, year, month) {
const x = new Date();
x.setFullYear(x.getFullYear() - year);
x.setMonth(x.getMonth() - month);
Expand All @@ -18,7 +18,3 @@ function compare(date, year, month) {
return chalk.bgRed.bold(date);
}
}

module.exports = {
compare
};
52 changes: 24 additions & 28 deletions lib/dependencies.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
const chalk = require('chalk');
const readPkg = require('read-pkg');

const info = require('./information');
const versions = require('./versions');

function get(config) {
return readPkg(config.file).then(async pkg => {
const result = {};
// Always read the normal dependencies if they are available
if (pkg.dependencies) {
result.dependencies = await packages(config, pkg.dependencies);
}
import chalk from 'chalk';
import { readPackage } from 'read-pkg';

if (pkg.devDependencies && (config.dependencies.dev || config.dependencies.all)) {
result.devDependencies = await packages(config, pkg.devDependencies);
}
if (pkg.peerDependencies && (config.dependencies.peer || config.dependencies.all)) {
result.peerDependencies = await packages(config, pkg.peerDependencies);
}
if (pkg.bundledDependencies && (config.dependencies.bundled || config.dependencies.all)) {
result.bundledDependencies = await packages(config, pkg.bundledDependencies);
}
import info from './information.js';
import * as versions from './versions.js';

export async function get(config) {
const pkg = await readPackage(config.file);
const result = {};

// Always read the normal dependencies if they are available
if (pkg.dependencies) {
result.dependencies = await packages(config, pkg.dependencies);
}

if (pkg.devDependencies && (config.dependencies.dev || config.dependencies.all)) {
result.devDependencies = await packages(config, pkg.devDependencies);
}
if (pkg.peerDependencies && (config.dependencies.peer || config.dependencies.all)) {
result.peerDependencies = await packages(config, pkg.peerDependencies);
}
if (pkg.bundledDependencies && (config.dependencies.bundled || config.dependencies.all)) {
result.bundledDependencies = await packages(config, pkg.bundledDependencies);
}

return result;
});
return result;
}

function packages(config, dependencies) {
Expand All @@ -49,7 +49,3 @@ function packages(config, dependencies) {

return Promise.all(information);
}

module.exports = {
get
};
6 changes: 3 additions & 3 deletions lib/information.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const request = require('./request');
const versions = require('./versions');
import request from './request.js';
import * as versions from './versions.js';

module.exports = (config, name, version) => {
export default (config, name, version) => {
return request(`${config.registry}/${name}`).then(body => {
const valid = versions.valid(version);
return {
Expand Down
4 changes: 2 additions & 2 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable consistent-return */
const https = require('https');
import https from 'https';

module.exports = url => {
export default url => {
return new Promise((resolve, reject) => {
if (typeof url === 'undefined') {
return reject(new TypeError('Please specify an URL'));
Expand Down
16 changes: 5 additions & 11 deletions lib/versions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const chalk = require('chalk');
const semver = require('semver');
import chalk from 'chalk';
import semver from 'semver';

function compare(v, l) {
export function compare(v, l) {
const compare = semver.compare(v, l);
if (compare === -1) {
return `${chalk.bgRed.bold(v)} => ${chalk.bgGreen.bold(l)}`;
Expand All @@ -10,20 +10,14 @@ function compare(v, l) {
}
}

function clean(v) {
export function clean(v) {
// TODO: maybe some more methods or even an external library are needed here
v = v.trim();
v = v.replace(/\^/g, '');
v = v.replace(/~/g, '');
return v;
}

function valid(v) {
export function valid(v) {
return semver.valid(v) !== null;
}

module.exports = {
compare,
clean,
valid
};
Loading

0 comments on commit 08f8ab4

Please sign in to comment.