Skip to content

Commit

Permalink
Group unpublished files in folders (#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
Drarig29 authored Nov 2, 2023
1 parent 1cb22e0 commit c31c2bc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion source/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const checkNewFilesAndDependencies = async (pkg, rootDir) => {

const messages = [];
if (newFiles.unpublished.length > 0) {
messages.push(`The following new files will not be part of your published package:\n${util.joinList(newFiles.unpublished)}\n\nIf you intended to publish them, add them to the \`files\` field in package.json.`);
messages.push(`The following new files will not be part of your published package:\n${util.groupFilesInFolders(newFiles.unpublished)}\n\nIf you intended to publish them, add them to the \`files\` field in package.json.`);
}

if (newFiles.firstTime.length > 0) {
Expand Down
22 changes: 22 additions & 0 deletions source/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ export const getTagVersionPrefix = pMemoize(async options => {

export const joinList = list => chalk.reset(list.map(item => `- ${item}`).join('\n'));

export const groupFilesInFolders = (files, groupingMinimumDepth = 1, groupingThresholdCount = 5) => {
const groups = {};
for (const file of files) {
const groupKey = path.join(...file.split(path.sep).slice(0, groupingMinimumDepth));
groups[groupKey] = [...groups[groupKey] ?? [], file];
}

const lines = [];
for (const [folder, filesInFolder] of Object.entries(groups)) {
if (filesInFolder.length > groupingThresholdCount) {
lines.push(`- ${folder}/* ${chalk.bold.white(`(${filesInFolder.length} files)`)}`);
continue;
}

for (const file of filesInFolder) {
lines.push(`- ${file}`);
}
}

return chalk.reset(lines.join('\n'));
};

export const getNewFiles = async rootDir => {
const listNewFiles = await git.newFilesSinceLastRelease(rootDir);
const listPkgFiles = await npm.getFilesToBePacked(rootDir);
Expand Down
33 changes: 33 additions & 0 deletions test/util/auto-group-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import test from 'ava';
import stripAnsi from 'strip-ansi';
import {groupFilesInFolders} from '../../source/util.js';

const testJoinList = test.macro((t, {list, expected}) => {
const output = groupFilesInFolders(list);
t.is(stripAnsi(output), expected);
});

test('one item', testJoinList, {
list: [
'scripts/a.sh',
],
expected: '- scripts/a.sh',
});

test('mix of collapsed and expanded folders', testJoinList, {
list: [
'scripts/a.sh',
'scripts/b.sh',
'scripts/c.sh',
'test/_utils-1.js',
'test/_utils-2.js',
'test/_utils-3.js',
'test/_utils-4.js',
'test/_utils-5.js',
'test/_utils-6.js',
],
expected: `- scripts/a.sh
- scripts/b.sh
- scripts/c.sh
- test/* (6 files)`,
});

0 comments on commit c31c2bc

Please sign in to comment.