Skip to content
This repository was archived by the owner on Apr 26, 2022. It is now read-only.

Commit 8b8bb3c

Browse files
committed
fixes issue with append action when unique is used with a string pattern
1 parent f518e91 commit 8b8bb3c

File tree

6 files changed

+31
-13
lines changed

6 files changed

+31
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-plop",
3-
"version": "0.20.0",
3+
"version": "0.21.0",
44
"description": "programmatic plopping for fun and profit",
55
"main": "lib/index.js",
66
"types": "index.d.ts",

src/actions/append.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const doAppend = function*(data, cfg, plop, fileData) {
1515
// if the appended string should be unique (default),
1616
// remove any occurence of it (but only if pattern would match)
1717

18-
if (cfg.unique !== false && new RegExp(cfg.pattern).test(fileData)) {
18+
if (cfg.unique !== false) {
1919
// only remove after "pattern", so that we remove not too much accidentally
2020
const parts = fileData.split(cfg.pattern);
2121
const lastPart = parts[parts.length - 1];

tests/append-mock/plop-templates/list-item.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/append-mock/plop-templates/list.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ Don't remove me: Plop
22

33
-- APPEND ITEMS HERE --
44

5+
/* APPEND OTHER ITEMS HERE */
6+
57
+++++++++++++++++++++++++++++++++++++++

tests/append-mock/plopfile.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ module.exports = function (plop) {
5353
type: 'append',
5454
path: 'src/{{listName}}.txt',
5555
pattern: /-- APPEND ITEMS HERE --/gi,
56-
templateFile: 'plop-templates/list-item.txt',
56+
template: '😻 name: {{name}}1',
57+
unique: !allowDuplicates
58+
},
59+
{
60+
type: 'append',
61+
path: 'src/{{listName}}.txt',
62+
pattern: '/* APPEND OTHER ITEMS HERE */',
63+
template: '🔥 name: {{name}}2',
5764
unique: !allowDuplicates
5865
}
5966
]

tests/append.ava.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ test('Check if entry will be appended', co.wrap(function* (t) {
2121
const filePath = path.resolve(testSrcPath, 'list1.txt');
2222
const content = fs.readFileSync(filePath).toString();
2323

24-
t.is((content.match(/Marco/g) || []).length, 1);
25-
t.is((content.match(/Polo/g) || []).length, 1);
24+
t.is((content.match(/Marco1/g) || []).length, 1);
25+
t.is((content.match(/Polo1/g) || []).length, 1);
26+
t.is((content.match(/Marco2/g) || []).length, 1);
27+
t.is((content.match(/Polo2/g) || []).length, 1);
2628
}));
2729

2830
test('Check if duplicates get filtered', co.wrap(function* (t) {
@@ -34,8 +36,10 @@ test('Check if duplicates get filtered', co.wrap(function* (t) {
3436
const filePath = path.resolve(testSrcPath, 'list2.txt');
3537
const content = fs.readFileSync(filePath).toString();
3638

37-
t.is((content.match(/Marco/g) || []).length, 1);
38-
t.is((content.match(/Polo/g) || []).length, 1);
39+
t.is((content.match(/Marco1/g) || []).length, 1);
40+
t.is((content.match(/Polo1/g) || []).length, 1);
41+
t.is((content.match(/Marco2/g) || []).length, 1);
42+
t.is((content.match(/Polo2/g) || []).length, 1);
3943
}));
4044

4145
test('Check if duplicates are kept, if allowed', co.wrap(function* (t) {
@@ -46,8 +50,10 @@ test('Check if duplicates are kept, if allowed', co.wrap(function* (t) {
4650
const filePath = path.resolve(testSrcPath, 'list3.txt');
4751
const content = fs.readFileSync(filePath).toString();
4852

49-
t.is((content.match(/Marco/g) || []).length, 2);
50-
t.is((content.match(/Polo/g) || []).length, 1);
53+
t.is((content.match(/Marco1/g) || []).length, 2);
54+
t.is((content.match(/Polo1/g) || []).length, 1);
55+
t.is((content.match(/Marco2/g) || []).length, 2);
56+
t.is((content.match(/Polo2/g) || []).length, 1);
5157
}));
5258

5359
test('Check if duplicates are only removed below the pattern', co.wrap(function* (t) {
@@ -59,6 +65,11 @@ test('Check if duplicates are only removed below the pattern', co.wrap(function*
5965
const filePath = path.resolve(testSrcPath, 'list4.txt');
6066
const content = fs.readFileSync(filePath).toString();
6167

62-
t.is((content.match(/Plop/g) || []).length, 2);
63-
t.is((content.match(/Polo/g) || []).length, 1);
68+
t.is((content.match(/Plop1/g) || []).length, 1);
69+
t.is((content.match(/Polo1/g) || []).length, 1);
70+
t.is((content.match(/Plop2/g) || []).length, 1);
71+
t.is((content.match(/Polo2/g) || []).length, 1);
72+
73+
// there's a plop at the top
74+
t.is((content.match(/Plop/g) || []).length, 3);
6475
}));

0 commit comments

Comments
 (0)