Skip to content

Commit 383ff9d

Browse files
authoredApr 28, 2020
refactor: 'from' option (#450)
BREAKING CHANGE: the 'from' option now can only be a string, if you use `{ from: { glob: 'directory/**', dot: false } }` changed it to `{ from: 'directory/**', globOptions: { dot: false } }`
1 parent 57f3e61 commit 383ff9d

6 files changed

+26
-140
lines changed
 

‎src/options.json

+2-9
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,8 @@
44
"type": "object",
55
"properties": {
66
"from": {
7-
"anyOf": [
8-
{
9-
"type": "string",
10-
"minLength": 1
11-
},
12-
{
13-
"type": "object"
14-
}
15-
]
7+
"type": "string",
8+
"minLength": 1
169
},
1710
"to": {
1811
"type": "string"

‎src/preProcessPattern.js

+4-18
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import globParent from 'glob-parent';
55

66
import normalize from './utils/normalize';
77
import isTemplateLike from './utils/isTemplateLike';
8-
import isObject from './utils/isObject';
98
import { stat } from './utils/promisify';
109

1110
/* eslint-disable no-param-reassign */
@@ -32,8 +31,7 @@ export default function preProcessPattern(globalRef, pattern) {
3231
pattern.context = path.join(context, pattern.context);
3332
}
3433

35-
const isFromGlobPatten =
36-
(isObject(pattern.from) && pattern.from.glob) || pattern.globOptions;
34+
const isFromGlobPatten = pattern.globOptions;
3735
// Todo remove this in next major
3836
const isToDirectory =
3937
path.extname(pattern.to) === '' || pattern.to.slice(-1) === path.sep;
@@ -67,21 +65,9 @@ export default function preProcessPattern(globalRef, pattern) {
6765

6866
pattern.fromType = 'glob';
6967

70-
const globOptions = Object.assign(
71-
{},
72-
pattern.globOptions ? pattern.globOptions : pattern.from
73-
);
74-
delete globOptions.glob;
75-
76-
pattern.absoluteFrom = path.resolve(
77-
pattern.context,
78-
pattern.globOptions ? pattern.from : pattern.from.glob
79-
);
80-
pattern.glob = normalize(
81-
pattern.context,
82-
pattern.globOptions ? pattern.from : pattern.from.glob
83-
);
84-
pattern.globOptions = globOptions;
68+
pattern.absoluteFrom = path.resolve(pattern.context, pattern.from);
69+
pattern.glob = normalize(pattern.context, pattern.from);
70+
pattern.globOptions = pattern.globOptions || {};
8571

8672
return Promise.resolve(pattern);
8773
}

‎test/CopyPlugin.test.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,7 @@ describe('apply function', () => {
240240
expectedAssetKeys: ['directory/nested/nestedfile.txt'],
241241
patterns: [
242242
{
243-
from: {
244-
glob: 'directory\\nested\\*',
245-
},
243+
from: 'directory\\nested\\*',
246244
},
247245
],
248246
})
@@ -255,9 +253,7 @@ describe('apply function', () => {
255253
expectedAssetKeys: ['directory/nested/nestedfile.txt'],
256254
patterns: [
257255
{
258-
from: {
259-
glob: 'directory/nested\\*',
260-
},
256+
from: 'directory/nested\\*',
261257
},
262258
],
263259
})
@@ -370,9 +366,7 @@ describe('apply function', () => {
370366
run({
371367
patterns: [
372368
{
373-
from: {
374-
glob: 'directory/directoryfile.txt',
375-
},
369+
from: 'directory/directoryfile.txt',
376370
},
377371
],
378372
})

‎test/__snapshots__/validate-options.test.js.snap

+7-10
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,20 @@ exports[`validate options should throw an error on the "patterns" option with "[
109109
- options.patterns[0].to should be a string."
110110
`;
111111
112+
exports[`validate options should throw an error on the "patterns" option with "[{"from":{"glob":"**/*","dot":false},"to":"dir","context":"context"}]" value 1`] = `
113+
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
114+
- options.patterns[0].from should be a non-empty string."
115+
`;
116+
112117
exports[`validate options should throw an error on the "patterns" option with "[{"from":true,"to":"dir","context":"context"}]" value 1`] = `
113118
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
114-
- options.patterns[0] should be one of these:
115-
non-empty string | object { from, to?, context?, toType?, test?, force?, ignore?, flatten?, cache?, transform?, transformPath?, … }
116-
Details:
117-
* options.patterns[0].from should be one of these:
118-
non-empty string | object { … }
119-
Details:
120-
* options.patterns[0].from should be a non-empty string.
121-
* options.patterns[0].from should be an object:
122-
object { … }"
119+
- options.patterns[0].from should be a non-empty string."
123120
`;
124121
125122
exports[`validate options should throw an error on the "patterns" option with "[{}]" value 1`] = `
126123
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
127124
- options.patterns[0] misses the property 'from'. Should be:
128-
non-empty string | object {}"
125+
non-empty string"
129126
`;
130127
131128
exports[`validate options should throw an error on the "patterns" option with "{}" value 1`] = `

‎test/from-option.test.js

-81
Original file line numberDiff line numberDiff line change
@@ -470,85 +470,4 @@ describe('from option', () => {
470470
.catch(done);
471471
});
472472
});
473-
474-
describe('is a object with glob and glob options', () => {
475-
it('should move files', (done) => {
476-
runEmit({
477-
expectedAssetKeys: ['file.txt'],
478-
patterns: [
479-
{
480-
from: {
481-
glob: '*.txt',
482-
},
483-
},
484-
],
485-
})
486-
.then(done)
487-
.catch(done);
488-
});
489-
490-
it('should move files exclude dot files', (done) => {
491-
runEmit({
492-
expectedAssetKeys: ['file.txt'],
493-
patterns: [
494-
{
495-
from: {
496-
glob: '*.txt',
497-
dot: false,
498-
},
499-
},
500-
],
501-
})
502-
.then(done)
503-
.catch(done);
504-
});
505-
506-
it('should move files with dot files', (done) => {
507-
runEmit({
508-
expectedAssetKeys: ['file.txt', '.file.txt'],
509-
patterns: [
510-
{
511-
from: {
512-
glob: '*.txt',
513-
dot: true,
514-
},
515-
},
516-
],
517-
})
518-
.then(done)
519-
.catch(done);
520-
});
521-
522-
it('should move files with the "globOptions" option', (done) => {
523-
runEmit({
524-
expectedAssetKeys: ['.file.txt', 'file.txt'],
525-
patterns: [
526-
{
527-
from: '*.txt',
528-
globOptions: {
529-
dot: true,
530-
},
531-
},
532-
],
533-
})
534-
.then(done)
535-
.catch(done);
536-
});
537-
538-
it('should move files with the "globOptions" option #2', (done) => {
539-
runEmit({
540-
expectedAssetKeys: ['file.txt'],
541-
patterns: [
542-
{
543-
from: '*.txt',
544-
globOptions: {
545-
dot: false,
546-
},
547-
},
548-
],
549-
})
550-
.then(done)
551-
.catch(done);
552-
});
553-
});
554473
});

‎test/validate-options.test.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,6 @@ describe('validate options', () => {
7070
transformPath: () => {},
7171
},
7272
],
73-
[
74-
{
75-
from: {
76-
glob: '**/*',
77-
dot: false,
78-
},
79-
to: 'dir',
80-
context: 'context',
81-
globOptions: {
82-
dot: false,
83-
},
84-
},
85-
],
8673
[
8774
{
8875
from: 'test.txt',
@@ -235,6 +222,16 @@ describe('validate options', () => {
235222
transformPath: true,
236223
},
237224
],
225+
[
226+
{
227+
from: {
228+
glob: '**/*',
229+
dot: false,
230+
},
231+
to: 'dir',
232+
context: 'context',
233+
},
234+
],
238235
],
239236
},
240237
};

0 commit comments

Comments
 (0)
Please sign in to comment.