Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group unpublished files in folders #716

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)`,
});