Skip to content

Commit

Permalink
fix: Fixed file includes
Browse files Browse the repository at this point in the history
.distinclude and .distexclude priorities weren't set properly - precedence is now set in accordance with readme
  • Loading branch information
seebeen committed Jan 16, 2024
1 parent ee36d3d commit 359040f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ You can also use the `include` option which works in the same manner.
#### Notes

* Include and exclude options are not mutually exclusive so you can use both.
* Plugin also looks for `.distinclude` and `.distexclude` files which take precedence over the options set in the plugin.
* Plugin also looks for `.distinclude` and `.distexclude` / `.distignore` files which take precedence over the options set in the plugin.
* By default we exclude a lot of build artifacts and files which are not needed in the package. You can see the full list in [constants.ts](lib/constants.ts).

### Examples
Expand Down
9 changes: 7 additions & 2 deletions lib/utils/copy-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ function remapGlobs(workDir: string, includePath: string): string {
* @returns Array of globs to include
*/
export function getInclude(workDir: string, files?: string[]): string[] {
const include = [...new Set(files ?? getFileArray(workDir, '.distinclude'))];
const include = [
...new Set(getFileArray(workDir, '.distinclude') ?? files ?? []),
];

return include.length !== 0 ? include : ['**/*'];
}
Expand All @@ -40,7 +42,10 @@ export function getIgnore(workDir: string, files?: string[]): string[] {
return [
...new Set([
...DEFAULT_EXCLUDES,
...(files ?? getFileArray(workDir, '.distignore')),
...(getFileArray(workDir, '.distexclude') ??
getFileArray(workDir, '.distignore') ??
files ??
[]),
]),
]
.filter(
Expand Down
15 changes: 9 additions & 6 deletions lib/utils/get-file-array.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import fs from 'fs-extra';
import path from 'node:path';

export function getFileArray(workDir: string, fileToRead: string): string[] {
export function getFileArray(
workDir: string,
fileToRead: string,
): null | string[] {
try {
const filesinFile = fs
.readFileSync(path.resolve(path.join(workDir, fileToRead)), 'utf8')
.split('\n')
.filter((file) => file !== '')
.map((file) => file.trim());
.readFileSync(path.resolve(path.join(workDir, fileToRead)), 'utf8') // Read the file from the workDir
.split('\n') // Split the file into an array of lines
.map((file) => file.trim()) // Trim each line
.filter((file) => file !== '' && !file.startsWith('#')); // Remove empty lines and comments

return [...new Set([...filesinFile])];
} catch (err) {
return [];
return null;
}
}
4 changes: 2 additions & 2 deletions test/0-corner-cases.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { PluginConfig } from '../lib/classes/plugin-config.class.js';
describe('Corner cases affecting releases', () => {
it('Should fail to read non-existant file', () => {
try {
const files = getFileArray('/test', 'non-existant-file.txt');
const files = getFileArray('/test', 'non-existant-file.txt') ?? [];
expect(files).toEqual([]);
} catch (err) {}
});
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('Corner cases affecting releases', () => {
'vendor',
]);
const ignore1 = DEFAULT_EXCLUDES;
const include2 = getInclude(workDir);
const include2 = getInclude(workDir, ['dist-test.php']);
const ignore2 = getIgnore(workDir);
const expected = ['dist-test.php', 'test1.php', 'vendor'].sort();

Expand Down
1 change: 0 additions & 1 deletion test/fixtures/dist-test/.distinclude

This file was deleted.

0 comments on commit 359040f

Please sign in to comment.