How to copy multiple directories to one directory in Node - node.js

I'm looking for something like..
fs.copy([dir1,dir2], 'destDirString').then(() => {
cb();
});
Is there an easy way to copy two directories' contents to another dir in Node? I have two NCP commands now, want to condense.

you might be looking for merge-dirs
Usage:
var mergedirs = require('merge-dirs');
// copy folder/a into folder/b
mergedirs('/folder/a', '/folder/b');
// copy folder/a into folder/b with conflict resolution 'overwrite'
mergedirs('/folder/a', '/folder/b', 'overwrite');
// copy folder/a into folder/b with conflict resolution 'ask'
mergedirs('/folder/a', '/folder/b', 'ask');
// copy folder/a into folder/b with conflict resolution 'skip'
mergedirs('/folder/a', '/folder/b', 'skip');

Related

Puppet data array

I got a module that creating some directories depending of server:
class linux_sftp::sftp_mount ($sftp_mount_ip, $sftp_mount_username, $sftp_mount_password, $sftp_mount_point) {
file { "/mnt/${sftp_mount_point}":
ensure => directory,
subscribe => Exec['sftp_remount'],
}
in data.yml
sftp_mount_point: "stcontent1"
I want to add to data more folders like: stcontent2, stcontent3. Is it a way to add this and loop thru data?
sftp_mount_point:
- "stcontent1"
- "stcontent2" ...
Yes you can use lambda method (can also be invoked as functions if desired) iteration to accomplish this task. The most common for your use case is each. It can be easily invoked on type Array[String] like you have in your question.
$sftp_mount_point.each |String $mount| {
file { "/mnt/${mount}":
ensure => directory,
}
}
Note that the file type does not have a subscribable property, so subscribe is not a valid attribute and I therefore removed it above.

gulp clean is not working in correct manner

I have directory structure like this.
dist
|--client
|--assets
|--images
|--bower_components
|--server
|--.bower.json
I am trying to clean dist folder, except assets/images folder.
But when i execute this command in dryRun mode, it doesn't remove assets/images file. But after disabling it, it remove all the files and folder.
gulp.task('clean:dist', () => {
del.sync([
`${paths.dist}/!(.git*|.openshift|Procfile)**`,
`${paths.dist}/client/**`,
`!${paths.dist}/client/assets`,
`!${paths.dist}/client/assets/**`], {
//dryRun: true
});
//console.log('dELETE FIELSE ARE: ' + JSON.stringify(value));
});
value of used constant is:
${paths.dist} ='dist';
The offical del documentation states the following:
The glob pattern ** matches all children and the parent.
So this won't work:
del.sync(['public/assets/**', '!public/assets/goat.png']);
You have to explicitly ignore the parent directories too:
del.sync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);
In your case you delete dist/client/**, which includes the dist/client directory itself. If you just ignore dist/client/assets/** the dist/client directory is still deleted.
You need to explicitly ignore the dist/client directory:
gulp.task('clean:dist', () => {
del.sync([
`${paths.dist}/!(.git*|.openshift|Procfile)**`,
`${paths.dist}/client/**`,
`!${paths.dist}/client`,
`!${paths.dist}/client/assets/**`]);
});

Gulp copy min file but not js/css

I'm trying to copy all my assets into my public dir but I want all assets except JavaScript and CSS files cause they are concatenated and minified into prod.min.js and prod.min.css so I want to make two exception for min.js and min.css files.
I have tried this (only for JS for now)
gulp.src([src + '/**/*', src + '/**/*.min.js', '!' + src + '/**/*.js'])
.pipe(gulp.dest(dest))
But it results in no JavaScript files at all.
How can do this?
Create separate glob arrays for CSS and JS. Then you can be more selective in which get moved.
I am also dealing with this problem at the moment.
The way I patched it (it's a patch because it's not really an optimal solution) is by making two passes:
First pass -> move all (**/*.*) files and negate all js extensions (!**/*.js)
Second pass -> move only the minified files (**/*.min.js)
A quick task example:
gulp.task('move-min-not-src', [], function() {
var paths = [
[
'source-folder/**/*.*',
'!source-folder/**/*.js'
],
[
'source-folder/**/*.min.js'
]
];
paths.forEach(function(path) {
gulp.src(path)
.pipe(
gulp.dest('destination-folder/')
);
});
});

Flatten first directory of a FileTree in Gradle

I'm writing a task to extract a tarball into a directory. I don't control this tarball's contents.
The tarball contains a single directory which contains all the files I actually care about. I want to pull everything out of that directory and copy that into my destination.
Example:
/root/subdir
/root/subdir/file1
/root/file2
Desired:
/subdir
/subdir/file1
/file2
Here's what I tried so far, but this seems like a really goofy way of doing it:
copy {
eachFile {
def segments = it.getRelativePath().getSegments() as List
it.setPath(segments.tail().join("/"))
return it
}
from tarTree(resources.gzip('mytarfile.tar.gz'))
into destinationDir
}
For each file, I get the elements of its path, remove the first, join that with /, then set that as the file's path. And this works...sort of. The problem is that this creates the following structure as a result:
/root/subdir
/root/subdir/file1
/root/file2
/subdir
/subdir/file1
/file2
I'm fine with just removing the root directory myself as a final action of the task, but I feel like there should be a much simpler way of doing this.
AFAIK, the only way is to unpack the zip, tar, tgz file :(
There is an open issue here
Please go vote for it!
Until then, the solution isn't very pretty, but not that hard either. In the example below, I am assuming that you want to remove the 'apache-tomcat-XYZ' root-level directory from a 'tomcat' configuration that only includes the apache-tomcat zip file.
def unpackDir = "$buildDir/tmp/apache.tomcat.unpack"
task unpack(type: Copy) {
from configurations.tomcat.collect {
zipTree(it).matching {
// these would be global items I might want to exclude
exclude '**/EMPTY.txt'
exclude '**/examples/**', '**/work/**'
}
}
into unpackDir
}
def mainFiles = copySpec {
from {
// use of a closure here defers evaluation until execution time
// It might not be clear, but this next line "moves down"
// one directory and makes everything work
"${unpackDir}/apache-tomcat-7.0.59"
}
// these excludes are only made up for an example
// you would only use/need these here if you were going to have
// multiple such copySpec's. Otherwise, define everything in the
// global unpack above.
exclude '**/webapps/**'
exclude '**/lib/**'
}
task createBetterPackage(type: Zip) {
baseName 'apache-tomcat'
with mainFiles
}
createBetterPackage.dependsOn(unpack)
Using groovy's syntax, we can use a regex to eliminate the first path segment:
task myCopyTask(type: Copy) {
eachFile {
path -= ~/^.+?\//
}
from tarTree(resources.gzip('mytarfile.tar.gz'))
into destinationDir
includeEmptyDirs = false // ignore empty directories
}

Duplicate declaration error with puppet

I try to create a folder then trying to copy some files into it like below.
init.pp
$tempfolder = "D:/TempFolder/"
file { [ $tempfolder ]:
ensure => "directory",
}
file { [ $tempfolder ]:
ensure => present,
recurse => true,
source => "E:/TestFiules",
}
When I try to run this, it is giving below error
Error: Duplicate declaration: File [ D:/TempFolder/ ] is already declared.
What is the wrong in the usage?
For reference: http://docs.puppetlabs.com/guides/techniques.html#how-can-i-manage-whole-directories-of-files-without-explicitly-listing-the-files
Thus you could do
file { "$tempfolder":
ensure => directory,
recurse => true,
source => "E:/TestFiules",
}
The ensure => directory also ensures that it will be present, so you don't have to declare it again.
A node can only have one resources with the same name declared, in this case the $tempfolder.
Either the $tempfolder is created empty (your first declaration) or created and populated with your E:/TestFiule content (the second declaration).
Note that you can drop the array syntax it's often used to create multiple directory at once or ensure an order like creating a tree
I came across this message when, but the error was:
Error: Duplicate declaration: File[] is already declared in file init.pp:40; cannot redeclare at init.pp:46 on node (redacted)
The file it was looking up was not defined, because my Hiera configuration was incorrect. As a result it was declaring two files named "".
Verify that Hiera is passing values correctly.

Resources