Skip to content

Commit f19e87c

Browse files
authored
Merge pull request #18 from homer0/homer0_ignorePaths
Add option to ignore paths
2 parents a5d1cdb + a61b430 commit f19e87c

File tree

5 files changed

+143
-5
lines changed

5 files changed

+143
-5
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ The tool has a lot of different settings you can change to customize how the imp
7878
```js
7979
module.exports = {
8080
input: ['src'],
81+
ignore: [],
8182
output: 'esm',
8283
forceDirectory: null,
8384
modules: [],
@@ -109,6 +110,12 @@ The list of directories that should be transformed.
109110

110111
> Default `['src']`
111112
113+
#### .ignore
114+
115+
A list of expressions (strings that will be converted on `RegExp`) to specify files/paths that should be ignored.
116+
117+
When a path is ignored, not only doesn't it get transformed, but it also doesn't get copied to the output directory.
118+
112119
#### .output
113120

114121
The directory where the transformed code should be placed.

src/bin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
config.output,
1919
config.extension.use,
2020
config.forceDirectory,
21+
config.ignore,
2122
);
2223
await transformOutput(files, config);
2324
if (config.addModuleEntry) {

src/index.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,25 @@ const findFiles = async (directory) => {
161161
* @param {boolean} [forceDirectory=true] If `false`, the directory itself won't
162162
* be copied,
163163
* just its contents.
164+
* @param {string[]} [ignore=[]] A list of expressions for paths that
165+
* should be ignored.
164166
* @returns {Promise<CJS2ESMCopiedFile[]>}
165167
* @ignore
166168
*/
167-
const copyDirectory = async (directory, output, useExtension, forceDirectory = true) => {
169+
const copyDirectory = async (
170+
directory,
171+
output,
172+
useExtension,
173+
forceDirectory = true,
174+
ignore = [],
175+
) => {
168176
const cwd = process.cwd();
169177
const extension = `.${useExtension}`;
170178
let contents = await findFiles(directory);
179+
if (ignore.length) {
180+
const ignoreExp = ignore.map((item) => new RegExp(item));
181+
contents = contents.filter((item) => !ignoreExp.some((exp) => item.match(exp)));
182+
}
171183
contents = await Promise.all(
172184
contents.map(async (item) => {
173185
let cleanPath = item.substr(cwd.length + 1);
@@ -207,9 +219,11 @@ const copyDirectory = async (directory, output, useExtension, forceDirectory = t
207219
* instead of the directory itself; this
208220
* parameter can be used to force it and always
209221
* copy the directory.
222+
* @param {?string[]} ignore A list of expressions for paths that should be
223+
* ignored.
210224
* @returns {Promise<CJS2ESMCopiedFile[]>}
211225
*/
212-
const copyFiles = async (input, output, useExtension, forceDirectory) => {
226+
const copyFiles = async (input, output, useExtension, forceDirectory, ignore) => {
213227
let result;
214228
if (input.length === 1) {
215229
const [firstInput] = input;
@@ -218,10 +232,11 @@ const copyFiles = async (input, output, useExtension, forceDirectory) => {
218232
output,
219233
useExtension,
220234
forceDirectory === true,
235+
ignore,
221236
);
222237
} else {
223238
result = await Promise.all(
224-
input.map((item) => copyDirectory(item, output, useExtension)),
239+
input.map((item) => copyDirectory(item, output, useExtension, undefined, ignore)),
225240
);
226241

227242
result = result.reduce((acc, item) => [...acc, ...item], []);

tests/bin.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ describe('bin', () => {
4242
const config = {
4343
input: 'some-input',
4444
output: 'some-output',
45+
ignore: [],
4546
extension: {
4647
use: 'jsx',
4748
},
@@ -62,6 +63,7 @@ describe('bin', () => {
6263
config.output,
6364
config.extension.use,
6465
config.forceDirectory,
66+
config.ignore,
6567
);
6668
expect(fns.transformOutput).toHaveBeenCalledTimes(1);
6769
expect(fns.transformOutput).toHaveBeenCalledWith(files, config);
@@ -77,6 +79,7 @@ describe('bin', () => {
7779
use: 'jsx',
7880
},
7981
forceDirectory: 'maybe',
82+
ignore: [],
8083
addModuleEntry: true,
8184
};
8285
const files = ['file-a.js', 'file-b.mjs'];
@@ -94,6 +97,7 @@ describe('bin', () => {
9497
config.output,
9598
config.extension.use,
9699
config.forceDirectory,
100+
config.ignore,
97101
);
98102
expect(fns.transformOutput).toHaveBeenCalledTimes(1);
99103
expect(fns.transformOutput).toHaveBeenCalledWith(files, config);
@@ -109,6 +113,7 @@ describe('bin', () => {
109113
use: 'jsx',
110114
},
111115
forceDirectory: 'maybe',
116+
ignore: [],
112117
addPackageJson: true,
113118
};
114119
const files = ['file-a.js', 'file-b.mjs'];
@@ -126,6 +131,7 @@ describe('bin', () => {
126131
config.output,
127132
config.extension.use,
128133
config.forceDirectory,
134+
config.ignore,
129135
);
130136
expect(fns.transformOutput).toHaveBeenCalledTimes(1);
131137
expect(fns.transformOutput).toHaveBeenCalledWith(files, config);

tests/index.test.js

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ describe('index', () => {
410410
// When
411411
result = await copyFiles(input, output, useExtension, forceDirectory);
412412
// Then
413-
expect(sortResults(result)).toEqual(expectedResult);
413+
expect(sortResults(result)).toEqual(sortResults(expectedResult));
414414
expect(fs.readdir).toHaveBeenCalledTimes(3);
415415
expect(fs.readdir).toHaveBeenNthCalledWith(1, src);
416416
expect(fs.readdir).toHaveBeenNthCalledWith(2, config);
@@ -429,6 +429,115 @@ describe('index', () => {
429429
});
430430
});
431431

432+
it('should copy multiple directories and ignore others', async () => {
433+
// Given
434+
const src = path.join(cwd, 'src');
435+
const config = path.join(cwd, 'config');
436+
const input = [src, config];
437+
const output = path.join(cwd, 'esm');
438+
const ignore = ['node_modules', 'utils/@types'];
439+
const useExtension = 'js';
440+
const forceDirectory = false;
441+
const filesSrc = ['index.js', 'utils', 'README.md', '.eslintrc', 'node_modules'];
442+
const filesUtils = ['index.js', 'utils.js', '@types'];
443+
const filesUtilsTypes = ['index.js'];
444+
const filesNodeModules = ['modules.js'];
445+
const filesConfig = ['config.js', '@types'];
446+
const filesConfigTypes = ['index.js'];
447+
/* eslint-disable jsdoc/require-jsdoc */
448+
// - src
449+
fs.readdir.mockImplementationOnce(() => filesSrc);
450+
// - config
451+
fs.readdir.mockImplementationOnce(() => filesConfig);
452+
// - src/index.js
453+
fs.stat.mockImplementationOnce(() => ({ isDirectory: () => false }));
454+
// - src/utils
455+
fs.stat.mockImplementationOnce(() => ({ isDirectory: () => true }));
456+
// - src/README.md
457+
fs.stat.mockImplementationOnce(() => ({ isDirectory: () => false }));
458+
// - src/node_modules
459+
fs.stat.mockImplementationOnce(() => ({ isDirectory: () => true }));
460+
// - config/config.js
461+
fs.stat.mockImplementationOnce(() => ({ isDirectory: () => false }));
462+
// - config/@types
463+
fs.stat.mockImplementationOnce(() => ({ isDirectory: () => true }));
464+
// - src/utils
465+
fs.readdir.mockImplementationOnce(() => filesUtils);
466+
// - src/node_modules
467+
fs.readdir.mockImplementationOnce(() => filesNodeModules);
468+
// - config/@types
469+
fs.readdir.mockImplementationOnce(() => filesConfigTypes);
470+
// - src/utils/index.js
471+
fs.stat.mockImplementationOnce(() => ({ isDirectory: () => false }));
472+
// - src/utils/utils.js
473+
fs.stat.mockImplementationOnce(() => ({ isDirectory: () => false }));
474+
// - src/utils/@types
475+
fs.stat.mockImplementationOnce(() => ({ isDirectory: () => true }));
476+
// - src/node_modules/modules.js
477+
fs.stat.mockImplementationOnce(() => ({ isDirectory: () => false }));
478+
// - config/@types/index.js
479+
fs.stat.mockImplementationOnce(() => ({ isDirectory: () => false }));
480+
// - src/utils/@types
481+
fs.readdir.mockImplementationOnce(() => filesUtilsTypes);
482+
// - src/utils/@types/index.js
483+
fs.stat.mockImplementationOnce(() => ({ isDirectory: () => false }));
484+
485+
/* eslint-enable jsdoc/require-jsdoc */
486+
let result = null;
487+
const expectedResult = [
488+
{
489+
from: path.join(config, 'config.js'),
490+
to: path.join(output, 'config', 'config.js'),
491+
},
492+
{
493+
from: path.join(config, '@types', 'index.js'),
494+
to: path.join(output, 'config', '@types', 'index.js'),
495+
},
496+
{
497+
from: path.join(src, 'index.js'),
498+
to: path.join(output, 'src', 'index.js'),
499+
},
500+
{
501+
from: path.join(src, 'utils', 'index.js'),
502+
to: path.join(output, 'src', 'utils', 'index.js'),
503+
},
504+
{
505+
from: path.join(src, 'utils', 'utils.js'),
506+
to: path.join(output, 'src', 'utils', 'utils.js'),
507+
},
508+
];
509+
// When
510+
result = await copyFiles(input, output, useExtension, forceDirectory, ignore);
511+
// Then
512+
expect(sortResults(result)).toEqual(sortResults(expectedResult));
513+
expect(fs.readdir).toHaveBeenCalledTimes(6);
514+
expect(fs.readdir).toHaveBeenNthCalledWith(1, src);
515+
expect(fs.readdir).toHaveBeenNthCalledWith(2, config);
516+
expect(fs.readdir).toHaveBeenNthCalledWith(3, path.join(src, 'utils'));
517+
expect(fs.stat).toHaveBeenCalledTimes(
518+
filesSrc.length +
519+
filesUtils.length +
520+
filesUtilsTypes.length +
521+
filesNodeModules.length +
522+
filesConfig.length +
523+
filesConfigTypes.length -
524+
1, // .eslintrc
525+
);
526+
expect(fs.ensureDir).toHaveBeenCalledTimes(5);
527+
expect(fs.ensureDir).toHaveBeenNthCalledWith(1, path.join(output, 'config'));
528+
expect(fs.ensureDir).toHaveBeenNthCalledWith(
529+
2,
530+
path.join(output, 'config', '@types'),
531+
);
532+
expect(fs.ensureDir).toHaveBeenNthCalledWith(3, path.join(output, 'src'));
533+
expect(fs.ensureDir).toHaveBeenNthCalledWith(4, path.join(output, 'src', 'utils'));
534+
expect(fs.ensureDir).toHaveBeenNthCalledWith(5, path.join(output, 'src', 'utils'));
535+
expect(fs.copyFile).toHaveBeenCalledTimes(expectedResult.length);
536+
expectedResult.forEach((item, index) => {
537+
expect(fs.copyFile).toHaveBeenNthCalledWith(index + 1, item.from, item.to);
538+
});
539+
});
540+
432541
it('should copy a directory and change the extensions to .mjs', async () => {
433542
// Given
434543
const src = path.join(cwd, 'src');
@@ -465,7 +574,7 @@ describe('index', () => {
465574
// When
466575
result = await copyFiles(input, output, useExtension, forceDirectory);
467576
// Then
468-
expect(sortResults(result)).toEqual(expectedResult);
577+
expect(sortResults(result)).toEqual(sortResults(expectedResult));
469578
expect(fs.readdir).toHaveBeenCalledTimes(2);
470579
expect(fs.readdir).toHaveBeenNthCalledWith(1, src);
471580
expect(fs.readdir).toHaveBeenNthCalledWith(2, path.join(src, 'utils'));

0 commit comments

Comments
 (0)