Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"object-curly-newline": ["error", { "multiline": true, "consistent": true }],
"semi": ["error", "never"],
"object-shorthand": ["off"],
"no-console": ["off"],
"no-await-in-loop": ["off"],
"prettier/prettier": ["error"]
},
"env": {
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
node_js:
- 12
- 10
script:
- jest --ci --coverage && codecov
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rollup-plugin-copy-merge",
"description": "Copy & Merge files and folders using Rollup",
"version": "0.1.0",
"version": "0.2.0",
"author": "syJSdev <whitedragon226@gmail.com>",
"repository": "syJSdev/rollup-plugin-copy-merge",
"main": "dist/index.commonjs.js",
Expand All @@ -19,7 +19,6 @@
"@types/fs-extra": "^8.0.1",
"colorette": "^1.1.0",
"fs-extra": "^8.1.0",
"fs-jetpack": "^4.1.0",
"globby": "10.0.1",
"is-plain-object": "^3.0.0"
},
Expand Down Expand Up @@ -63,8 +62,7 @@
]
},
"engines": {
"npm": ">=6.0.0",
"node": ">=12.0.0"
"node": ">=10.12"
},
"keywords": [
"rollup",
Expand Down
42 changes: 22 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
/* eslint-disable no-await-in-loop, no-console, no-restricted-syntax */
import path from 'path'
import fsJet from 'fs-jetpack'
import fs from 'fs-extra'
import isObject from 'is-plain-object'
import globby from 'globby'
import { bold, green, yellow } from 'colorette'

import { ensureTrailingNewLine, stringify } from './utils'

async function isFile(filePath) {
const fileStats = await fsJet.inspectAsync(filePath)
const fileStats = await fs.stat(filePath)

return fileStats.type === 'file'
return fileStats.isFile()
}

function renameTarget(target, rename) {
Expand Down Expand Up @@ -42,7 +41,7 @@ async function generateCopyTarget(src, dest, file, { flatten, rename, transform
}
let contents
if (file || transform) {
contents = await fsJet.readAsync(src)
contents = await fs.readFile(src, 'utf-8')
if (transform) {
contents = await transform(contents)
}
Expand Down Expand Up @@ -112,7 +111,8 @@ export default function copy(options = {}) {
const copyTargets = []

if (Array.isArray(targets) && targets.length) {
for (const target of targets) {
for (let index = 0; index < targets.length; index += 1) {
const target = targets[index]
if (!isObject(target)) {
throw new Error(`${stringify(target)} target must be an object`)
}
Expand Down Expand Up @@ -147,15 +147,16 @@ export default function copy(options = {}) {
})
)
)
copyTargets.push(...targetsList.flat(1))
targetsList.forEach((ts) => {
copyTargets.push(...ts)
})
} else {
copyTargets.push(
...(await generateCopyTargets(matchedPaths, dest, file, {
flatten,
rename,
transform
}))
)
const ts = await generateCopyTargets(matchedPaths, dest, file, {
flatten,
rename,
transform
})
copyTargets.push(...ts)
}
}
}
Expand All @@ -166,15 +167,16 @@ export default function copy(options = {}) {
console.log(green('copied:'))
}

for (const copyTarget of copyTargets) {
for (let index = 0; index < copyTargets.length; index += 1) {
const copyTarget = copyTargets[index]
const { src, contents, transformed, merge, merged, dest } = copyTarget

if (transformed || merged) {
if (merge || transformed) {
await fsJet.writeAsync(dest, contents, restPluginOptions)
}
if (transformed) {
await fs.outputFile(dest, contents)
} else if (merged) {
if (merge) await fs.outputFile(dest, contents)
} else {
await fsJet.copyAsync(src, dest, { overwrite: true, ...restPluginOptions })
await fs.copy(src, dest, restPluginOptions)
}

if (verbose) {
Expand Down
13 changes: 6 additions & 7 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ describe('Concat', () => {
})

expect(await fs.pathExists('dist/asset-all.js')).toBe(true)
const contents = await fs.readFile('dist/asset-all.js', 'utf8')
const contents1 = await fs.readFile('src/assets/asset-1.js', 'utf8')
const contents2 = await fs.readFile('src/assets/asset-2.js', 'utf8')
const contents = await readFile('dist/asset-all.js')
const contents1 = await readFile('src/assets/asset-1.js')
const contents2 = await readFile('src/assets/asset-2.js')
expect(contents === ensureTrailingNewLine(contents1).concat(contents2)).toBe(true)
})

Expand All @@ -297,9 +297,9 @@ describe('Concat', () => {
})

expect(await fs.pathExists('dist/css-all.css')).toBe(true)
const contents = await fs.readFile('dist/css-all.css', 'utf8')
const contents1 = await fs.readFile('src/assets/css/css-1.css', 'utf8')
const contents2 = await fs.readFile('src/assets/css/css-2.css', 'utf8')
const contents = await readFile('dist/css-all.css')
const contents1 = await readFile('src/assets/css/css-1.css')
const contents2 = await readFile('src/assets/css/css-2.css')
expect(contents === ensureTrailingNewLine(contents1).concat(contents2)).toBe(true)
})

Expand All @@ -313,7 +313,6 @@ describe('Concat', () => {
})

describe('Options', () => {
/* eslint-disable no-console */
test('Verbose, copy files', async () => {
console.log = jest.fn()

Expand Down
10 changes: 1 addition & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2470,14 +2470,6 @@ fs-extra@^8.1.0:
jsonfile "^4.0.0"
universalify "^0.1.0"

fs-jetpack@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/fs-jetpack/-/fs-jetpack-4.1.0.tgz#d693fcffd3cedbd8829226967866b9e89f290f0f"
integrity sha512-h4nHLIcCaxnXfUWhwP+mLnar03R2DBlqicNvKJG44TJob8RV6GB8EKNwJgSaBeDAfqWhqq01y+Ao96vRwpXlPw==
dependencies:
minimatch "^3.0.2"
rimraf "^2.6.3"

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
Expand Down Expand Up @@ -3823,7 +3815,7 @@ mimic-fn@^2.1.0:
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-2.1.0.tgz#7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b"
integrity sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==

minimatch@^3.0.2, minimatch@^3.0.4:
minimatch@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==
Expand Down