unimported
Version:
Scans your nodejs project folder and shows obsolete files and modules
109 lines (108 loc) • 5.39 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.printResults = exports.printDeleteResult = exports.formatMetaTable = exports.formatList = void 0;
const term_size_1 = __importDefault(require("term-size"));
const chalk_1 = __importDefault(require("chalk"));
const { columns } = (0, term_size_1.default)();
function formatList(caption, records) {
const gutterWidth = Math.max(4, `${records.length + 1}`.length + 1);
const colWidth = columns - gutterWidth - 2;
const lines = [
chalk_1.default.grey('─'.repeat(gutterWidth + 1) + '┬' + '─'.repeat(colWidth)),
chalk_1.default.grey(' '.repeat(gutterWidth + 1) + '│ ') + caption,
chalk_1.default.grey('─'.repeat(gutterWidth + 1) + '┼' + '─'.repeat(colWidth)),
...records.map((file, idx) => chalk_1.default.grey(`${idx + 1}`.padStart(gutterWidth, ' ') + ' │ ') + file),
chalk_1.default.grey('─'.repeat(gutterWidth + 1) + '┴' + '─'.repeat(colWidth)),
];
return `\n${lines.join('\n')}\n`;
}
exports.formatList = formatList;
function formatMetaTable(caption, data, context) {
const entryFiles = context.config.entryFiles;
const records = [
...entryFiles.map((entry, idx) => [
`entry file ${entryFiles.length > 1 ? idx + 1 : ''}`,
entry.label ? `${entry.file} — ${entry.label}` : entry.file,
]),
['', ''],
['unresolved imports', data.unresolved.length],
['unused dependencies', data.unused.length],
['unimported files', data.unimported.length],
];
const space = ' '.repeat(6);
const width = records.reduce((max, next) => Math.max(max, next[0].length), 0);
const divider = chalk_1.default.grey('─'.repeat(columns));
const { preset, version } = context.config;
const lines = [
`${space} ${caption} ${chalk_1.default.grey('unimported v' + version + (preset ? ` (${preset})` : ''))}`,
divider,
...records.reduce((acc, [label, value]) => {
acc.push(label ? `${space} ${label.padEnd(width, ' ')} : ${value}` : '');
return acc;
}, []),
];
return `\n${lines.join('\n')}\n`;
}
exports.formatMetaTable = formatMetaTable;
function printDeleteResult({ removedDeps, deletedFiles, }) {
if (removedDeps.length === 0 && deletedFiles.length === 0) {
console.log(chalk_1.default.greenBright(`✓`) + ' There are no unused files or dependencies.');
return;
}
if (removedDeps.length === 0) {
console.log(chalk_1.default.greenBright(`✓`) + ' There are no unused dependencies.');
console.log(formatList(chalk_1.default.redBright(`${deletedFiles.length} unused files removed`), deletedFiles));
return;
}
if (deletedFiles.length === 0) {
console.log(chalk_1.default.greenBright(`✓`) + ' There are no unused files.');
console.log(formatList(chalk_1.default.redBright(`${removedDeps.length} unused dependencies removed`), removedDeps));
return;
}
console.log(formatList(chalk_1.default.redBright(`${removedDeps.length} unused dependencies removed`), removedDeps));
console.log(formatList(chalk_1.default.redBright(`${deletedFiles.length} unused files removed`), deletedFiles));
}
exports.printDeleteResult = printDeleteResult;
function printResults(result, context) {
if (result.clean) {
console.log(chalk_1.default.greenBright(`✓`) + " There don't seem to be any unimported files.");
return;
}
const { showUnresolved, showUnused, showUnimported } = chooseResults(context);
const { unresolved, unused, unimported } = result;
// render
console.log(formatMetaTable(chalk_1.default.greenBright(`summary`), { unresolved, unused, unimported }, context));
if (showUnresolved && unresolved.length > 0) {
console.log(formatList(chalk_1.default.redBright(`${unresolved.length} unresolved imports`), unresolved.map(([item, sources]) => {
return `${item} ${chalk_1.default.gray(`at ${sources.join(', ')}`)}`;
})));
}
if (showUnused && unused.length > 0) {
console.log(formatList(chalk_1.default.blueBright(`${unused.length} unused dependencies`), unused));
}
if (showUnimported && unimported.length > 0) {
console.log(formatList(chalk_1.default.cyanBright(`${unimported.length} unimported files`), unimported));
}
console.log(`\n Inspect the results and run ${chalk_1.default.greenBright('npx unimported -u')} to update ignore lists`);
}
exports.printResults = printResults;
function chooseResults(context) {
const { showUnresolvedImports, showUnusedDeps, showUnusedFiles } = context;
const showAllResults =
// when all three flags are used
(showUnresolvedImports && showUnusedDeps && showUnusedFiles) ||
// when none flag is used
(!showUnresolvedImports && !showUnusedDeps && !showUnusedFiles);
const showUnresolved = showUnresolvedImports || showAllResults;
const showUnused = showUnusedDeps || showAllResults;
const showUnimported = showUnusedFiles || showAllResults;
return {
showAllResults,
showUnresolved,
showUnused,
showUnimported,
};
}
;