Skip to content

Commit 38af8ec

Browse files
committed
Add copy once functionality, tests
Resolve vladshcherbin#20
1 parent 1b0334c commit 38af8ec

File tree

5 files changed

+123
-6
lines changed

5 files changed

+123
-6
lines changed

jest.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
],
66
watchPathIgnorePatterns: [
77
'<rootDir>/tests/fixtures/build',
8-
'<rootDir>/tests/fixtures/dist'
8+
'<rootDir>/tests/fixtures/dist',
9+
'<rootDir>/tests/fixtures/src/index.js'
910
]
1011
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"eslint-config-airbnb-base": "^13.1.0",
3030
"eslint-plugin-import": "^2.17.2",
3131
"jest": "^24.8.0",
32+
"replace-in-file": "^4.1.0",
3233
"rimraf": "^2.6.3",
3334
"rollup": "^1.11.3",
3435
"rollup-plugin-auto-external": "^2.0.0",

src/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,22 @@ function generateCopyTarget(src, dest, rename) {
2929

3030
export default function copy(options = {}) {
3131
const {
32+
copyOnce = false,
3233
hook = 'buildEnd',
3334
targets = [],
3435
verbose = false,
3536
...restPluginOptions
3637
} = options
3738

39+
let copied = false
40+
3841
return {
3942
name: 'copy',
4043
[hook]: async () => {
44+
if (copyOnce && copied) {
45+
return
46+
}
47+
4148
const copyTargets = []
4249

4350
if (Array.isArray(targets) && targets.length) {
@@ -90,6 +97,8 @@ export default function copy(options = {}) {
9097
} else if (verbose) {
9198
console.log(yellow('no items to copy'))
9299
}
100+
101+
copied = true
93102
}
94103
}
95104
}

tests/index.test.js

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import { rollup } from 'rollup'
1+
import { rollup, watch } from 'rollup'
22
import fs from 'fs-extra'
3+
import replace from 'replace-in-file'
34
import { bold, yellow, green } from 'colorette'
45
import copy from '../src'
56

67
process.chdir(`${__dirname}/fixtures`)
78

9+
function sleep(ms) {
10+
return new Promise(resolve => setTimeout(resolve, ms))
11+
}
12+
813
afterEach(async () => {
914
await fs.remove('build')
1015
await fs.remove('dist')
@@ -289,6 +294,50 @@ describe('Options', () => {
289294
expect(await fs.pathExists('dist/css/css-2.css')).toBe(true)
290295
})
291296

297+
test('Copy once', async () => {
298+
const watcher = watch({
299+
input: 'src/index.js',
300+
output: {
301+
dir: 'build',
302+
format: 'esm'
303+
},
304+
plugins: [
305+
copy({
306+
targets: [
307+
{ src: 'src/assets/asset-1.js', dest: 'dist' }
308+
],
309+
copyOnce: true
310+
})
311+
]
312+
})
313+
314+
await sleep(1000)
315+
316+
expect(await fs.pathExists('dist/asset-1.js')).toBe(true)
317+
318+
await fs.remove('dist')
319+
320+
expect(await fs.pathExists('dist/asset-1.js')).toBe(false)
321+
322+
await replace({
323+
files: 'src/index.js',
324+
from: 'hey',
325+
to: 'ho'
326+
})
327+
328+
await sleep(1000)
329+
330+
expect(await fs.pathExists('dist/asset-1.js')).toBe(false)
331+
332+
watcher.close()
333+
334+
await replace({
335+
files: 'src/index.js',
336+
from: 'ho',
337+
to: 'hey'
338+
})
339+
})
340+
292341
test('Rest options', async () => {
293342
await expect(build({
294343
targets: [

yarn.lock

+61-4
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,15 @@ cliui@^4.0.0:
13171317
strip-ansi "^4.0.0"
13181318
wrap-ansi "^2.0.0"
13191319

1320+
cliui@^5.0.0:
1321+
version "5.0.0"
1322+
resolved "https://registry.yarnpkg.com/cliui/-/cliui-5.0.0.tgz#deefcfdb2e800784aa34f46fa08e06851c7bbbc5"
1323+
integrity sha512-PYeGSEmmHM6zvoef2w8TPzlrnNpXIjTipYK780YswmIP9vjxmd6Y2a3CB2Ks6/AU8NHjZugXvo8w3oWM2qnwXA==
1324+
dependencies:
1325+
string-width "^3.1.0"
1326+
strip-ansi "^5.2.0"
1327+
wrap-ansi "^5.1.0"
1328+
13201329
co@^4.6.0:
13211330
version "4.6.0"
13221331
resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
@@ -2103,6 +2112,11 @@ get-caller-file@^1.0.1:
21032112
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.3.tgz#f978fa4c90d1dfe7ff2d6beda2a515e713bdcf4a"
21042113
integrity sha512-3t6rVToeoZfYSGd8YoLFR2DJkiQrIiUrGcjvFX2mDw3bn6k2OtwHN0TNCLbBO+w8qTvimhDkv+LSscbJY1vE6w==
21052114

2115+
get-caller-file@^2.0.1:
2116+
version "2.0.5"
2117+
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
2118+
integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==
2119+
21062120
get-stream@^4.0.0:
21072121
version "4.1.0"
21082122
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-4.1.0.tgz#c1b255575f3dc21d59bfc79cd3d2b46b1c3a54b5"
@@ -3630,7 +3644,7 @@ os-homedir@^1.0.0:
36303644
resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3"
36313645
integrity sha1-/7xJiDNuDoM94MFox+8VISGqf7M=
36323646

3633-
os-locale@^3.0.0:
3647+
os-locale@^3.0.0, os-locale@^3.1.0:
36343648
version "3.1.0"
36353649
resolved "https://registry.yarnpkg.com/os-locale/-/os-locale-3.1.0.tgz#a802a6ee17f24c10483ab9935719cef4ed16bf1a"
36363650
integrity sha512-Z8l3R4wYWM40/52Z+S265okfFj8Kt2cC2MKY+xNi3kFs+XGI7WXu/I309QQQYbRW4ijiZ+yxs9pqEhJh0DqW3Q==
@@ -4055,6 +4069,15 @@ repeat-string@^1.6.1:
40554069
resolved "https://registry.yarnpkg.com/repeat-string/-/repeat-string-1.6.1.tgz#8dcae470e1c88abc2d600fff4a776286da75e637"
40564070
integrity sha1-jcrkcOHIirwtYA//Sndihtp15jc=
40574071

4072+
replace-in-file@^4.1.0:
4073+
version "4.1.0"
4074+
resolved "https://registry.yarnpkg.com/replace-in-file/-/replace-in-file-4.1.0.tgz#07846245a4b98a8bc5f5f9c3e3e368fa12b18bf7"
4075+
integrity sha512-5IANGPAQZVO9PHEjaRz3EHs7eQulv1cvOu2hRi7+Ern+zXIzdslSGX9MIfcI1anaGCpICK2l4R4YOOdThsFryQ==
4076+
dependencies:
4077+
chalk "^2.4.2"
4078+
glob "^7.1.3"
4079+
yargs "^13.2.2"
4080+
40584081
40594082
version "1.1.2"
40604083
resolved "https://registry.yarnpkg.com/request-promise-core/-/request-promise-core-1.1.2.tgz#339f6aababcafdb31c799ff158700336301d3346"
@@ -4498,7 +4521,7 @@ string-width@^1.0.1:
44984521
is-fullwidth-code-point "^2.0.0"
44994522
strip-ansi "^4.0.0"
45004523

4501-
string-width@^3.0.0:
4524+
string-width@^3.0.0, string-width@^3.1.0:
45024525
version "3.1.0"
45034526
resolved "https://registry.yarnpkg.com/string-width/-/string-width-3.1.0.tgz#22767be21b62af1081574306f69ac51b62203961"
45044527
integrity sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==
@@ -4528,7 +4551,7 @@ strip-ansi@^4.0.0:
45284551
dependencies:
45294552
ansi-regex "^3.0.0"
45304553

4531-
strip-ansi@^5.0.0, strip-ansi@^5.1.0:
4554+
strip-ansi@^5.0.0, strip-ansi@^5.1.0, strip-ansi@^5.2.0:
45324555
version "5.2.0"
45334556
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-5.2.0.tgz#8c9a536feb6afc962bdfa5b104a5091c1ad9c0ae"
45344557
integrity sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==
@@ -4917,6 +4940,15 @@ wrap-ansi@^2.0.0:
49174940
string-width "^1.0.1"
49184941
strip-ansi "^3.0.1"
49194942

4943+
wrap-ansi@^5.1.0:
4944+
version "5.1.0"
4945+
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-5.1.0.tgz#1fd1f67235d5b6d0fee781056001bfb694c03b09"
4946+
integrity sha512-QC1/iN/2/RPVJ5jYK8BGttj5z83LmSKmvbvrXPNCLZSEb32KKVDJDl/MOt2N01qU2H/FkzEa9PKto1BqDjtd7Q==
4947+
dependencies:
4948+
ansi-styles "^3.2.0"
4949+
string-width "^3.0.0"
4950+
strip-ansi "^5.0.0"
4951+
49204952
wrappy@1:
49214953
version "1.0.2"
49224954
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
@@ -4950,7 +4982,7 @@ xml-name-validator@^3.0.0:
49504982
resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-3.0.0.tgz#6ae73e06de4d8c6e47f9fb181f78d648ad457c6a"
49514983
integrity sha512-A5CUptxDsvxKJEU3yO6DuWBSJz/qizqzJKOMIfUJHETbBw/sFaDxgd6fxm1ewUaM0jZ444Fc5vC5ROYurg/4Pw==
49524984

4953-
"y18n@^3.2.1 || ^4.0.0":
4985+
"y18n@^3.2.1 || ^4.0.0", y18n@^4.0.0:
49544986
version "4.0.0"
49554987
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.0.tgz#95ef94f85ecc81d007c264e190a120f0a3c8566b"
49564988
integrity sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==
@@ -4968,6 +5000,14 @@ yargs-parser@^11.1.1:
49685000
camelcase "^5.0.0"
49695001
decamelize "^1.2.0"
49705002

5003+
yargs-parser@^13.1.0:
5004+
version "13.1.1"
5005+
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-13.1.1.tgz#d26058532aa06d365fe091f6a1fc06b2f7e5eca0"
5006+
integrity sha512-oVAVsHz6uFrg3XQheFII8ESO2ssAf9luWuAd6Wexsu4F3OtIW0o8IribPXYrD4WC24LWtPrJlGy87y5udK+dxQ==
5007+
dependencies:
5008+
camelcase "^5.0.0"
5009+
decamelize "^1.2.0"
5010+
49715011
yargs@^12.0.2:
49725012
version "12.0.5"
49735013
resolved "https://registry.yarnpkg.com/yargs/-/yargs-12.0.5.tgz#05f5997b609647b64f66b81e3b4b10a368e7ad13"
@@ -4985,3 +5025,20 @@ yargs@^12.0.2:
49855025
which-module "^2.0.0"
49865026
y18n "^3.2.1 || ^4.0.0"
49875027
yargs-parser "^11.1.1"
5028+
5029+
yargs@^13.2.2:
5030+
version "13.2.4"
5031+
resolved "https://registry.yarnpkg.com/yargs/-/yargs-13.2.4.tgz#0b562b794016eb9651b98bd37acf364aa5d6dc83"
5032+
integrity sha512-HG/DWAJa1PAnHT9JAhNa8AbAv3FPaiLzioSjCcmuXXhP8MlpHO5vwls4g4j6n30Z74GVQj8Xa62dWVx1QCGklg==
5033+
dependencies:
5034+
cliui "^5.0.0"
5035+
find-up "^3.0.0"
5036+
get-caller-file "^2.0.1"
5037+
os-locale "^3.1.0"
5038+
require-directory "^2.1.1"
5039+
require-main-filename "^2.0.0"
5040+
set-blocking "^2.0.0"
5041+
string-width "^3.0.0"
5042+
which-module "^2.0.0"
5043+
y18n "^4.0.0"
5044+
yargs-parser "^13.1.0"

0 commit comments

Comments
 (0)