Skip to content
This repository has been archived by the owner on Jul 29, 2021. It is now read-only.

Commit

Permalink
Added createPath flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Marek Sierociński committed Jan 31, 2019
1 parent 5eab913 commit 7547442
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#### 1.1.0 (2019-01-31)

* Added `createPath` flag for `item`


#### 1.0.1 (2019-01-31)

* Module fixing
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ export default [

Destination path

* `createPath`

_Optional, Default `false`_

If path for destination file doesn't exist - create it.


* `hookOn`

Expand Down
35 changes: 35 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const path = require('path');

function Copier (options) {
if (options == null) {
Expand Down Expand Up @@ -26,6 +27,7 @@ function Copier (options) {
* @typedef {Object} ToCopy
* @property {string} src - Source
* @property {string} dest - Destination
* @property {boolean=false} createPath - Create path if doesn't exist
*/

/**
Expand Down Expand Up @@ -54,8 +56,16 @@ function Copier (options) {
*/
this[this._hookOn] = function() {
var promises = that._items.map(function(item) {
item.createPath = item.createPath == null ? false : item.createPath;

return new Promise(function(resolve, reject) {
that._debug(`${item.src}${item.dest}`);

if (!that._itemDestinationExists(item)) {
reject(`Destination for file ${item.dest} doesn't exist`);
return;
}

fs.copyFile(item.src, item.dest, function(err) {
if (err) {
reject(err);
Expand All @@ -80,6 +90,31 @@ function Copier (options) {
}
};

/**
* Item destination exists
* @param item {ToCopy}
* @private
*/
this._itemDestinationExists = function(item) {
if (fs.existsSync(path.dirname(item.dest))) {
return true;
} else if (item.createPath) {
mkDirP(item.dest);
return true;
} else {
return false;
}
};

}

function mkDirP(dest) {
var dirName = path.dirname(dest);

if (!fs.existsSync(dirName)) {
mkDirP(dirName);
fs.mkdirSync(dirName);
}
}

function plugin (options) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rollup-plugin-copier",
"version": "1.0.1",
"version": "1.1.0",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
87 changes: 82 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ const copier = require('../index');

const TEST_RUN_DIR = 'test/_run';

function clearEnv() {
fs.readdirSync(TEST_RUN_DIR).forEach(function(name) {
fs.unlinkSync(`${TEST_RUN_DIR}/${name}`);
function clearEnv(path = TEST_RUN_DIR) {
fs.readdirSync(path).forEach(function(name) {
var filePath = `${path}/${name}`;
var stat = fs.statSync(filePath);
if(stat.isDirectory()) {
clearEnv(filePath);
} else {
fs.unlinkSync(filePath);
}
});
fs.rmdirSync(TEST_RUN_DIR);
fs.rmdirSync(path);
}



describe('rollup-plugin-copier', function() {
describe('Preparing environment', function () {
it('Delete previous environment if exists', function() {
Expand Down Expand Up @@ -176,6 +181,78 @@ describe('rollup-plugin-copier', function() {
});
});

describe('createPath flag', function() {
it('Should not copy file when createPath is not set', function() {
return new Promise(function(resolve, reject) {
rollup.rollup({
input: 'index.js',
plugins: [
copier({
items: [
{
src: `${TEST_RUN_DIR}/enFile.txt`,
dest: `${TEST_RUN_DIR}/sub-dir/enFile-test-createPath.txt`
}
]
})
]
}).then(reject).catch(function(err) {
expect(err.plugin).to.be.equal('rollup-plugin-copier');
expect(err.message).to.be.equal("Destination for file test/_run/sub-dir/enFile-test-createPath.txt doesn't exist");
resolve();
});
});
});

it('Should not copy file when createPath is set to false', function() {
return new Promise(function(resolve, reject) {
rollup.rollup({
input: 'index.js',
plugins: [
copier({
items: [
{
src: `${TEST_RUN_DIR}/enFile.txt`,
dest: `${TEST_RUN_DIR}/sub-dir/enFile-test-createPath.txt`,
createPath: false
}
]
})
]
}).then(reject).catch(function(err) {
expect(err.plugin).to.be.equal('rollup-plugin-copier');
expect(err.message).to.be.equal("Destination for file test/_run/sub-dir/enFile-test-createPath.txt doesn't exist");
resolve();
});
});
});

it('Should copy file when createPath is set to true', function() {
return new Promise(function(resolve, reject) {
rollup.rollup({
input: 'index.js',
plugins: [
copier({
items: [
{
src: `${TEST_RUN_DIR}/enFile.txt`,
dest: `${TEST_RUN_DIR}/sub-dir/enFile-test-createPath.txt`,
createPath: true
}
]
})
]
}).then(function() {
expect(fs.existsSync(`${TEST_RUN_DIR}/sub-dir/enFile-test-createPath.txt`)).to.be.true;
const buffSrc = fs.readFileSync(`${TEST_RUN_DIR}/enFile.txt`);
const buffDest = fs.readFileSync(`${TEST_RUN_DIR}/sub-dir/enFile-test-createPath.txt`);
expect(buffDest.equals(buffSrc)).to.be.true;
resolve();
}).catch(reject);
});
});
});

describe('Cleaning environment', function() {
it('Delete environment', function() {
clearEnv();
Expand Down

0 comments on commit 7547442

Please sign in to comment.