How to copy only folders from a directory into another - node.js

I'm using ncp plugin to copy some folders from a directory into another, the source directory contains some files as well so I only want to copy the folders in it and their content, and this is what I tried:
async function copyAssets(exportFolderName) {
const assets = glob.sync("**/", { cwd: distPath });
return Promise.all(
assets.map((asset) => {
return ncpPromise(path.join(distPath, asset), path.join(exportPath, exportFolderName), {
clobber: false,
});
})
);
}
What I'm doing here is I get the folder names inside distPath using glob.sync and then I copy each folder and it's content into exportPath + exportFolderName.
My source folder looks like this:
But then I get some weird results:
As you can see the folders were not copied, instead it was their content that was copied.
How can I solve this ?
Thanks in advance,

I just tried to play with 'glob' and 'ncp' modules, and this is my little understanding -
glob.sync('**/') Gets directory names recursively
glob.sync('*/') Gets directory names non recursively
Since you want to exclude files from source directory, but copy all directories as a whole, I think you should go with latter glob.sync
Now, playing with ncp -
ncp(source, destination) Copies all files/directories in source to destination directory
So, I am guessing, the following would work for you -
ncp(path.join(distPath, asset), path.join(exportPath, exportFolderName, asset))
This should create the source asset directory, and then put files into it.

Related

npm compressing module to compress two subfolders

I need a .tgz with the following folder structure:
./folder1/folder2/ "multiples files here".
The first folder will never contain files but is necessary.
I want to use the "compressing" npm module ( https://www.npmjs.com/package/compressing ).
But when I use it like in the following code, only folder2 is included. When I create another folder before folder 1 its the same, only folder2 is in there.
const compressing = require('compressing');
try{
compressing.tgz.compressDir('/folder1/folder2', 'destination.tgz')
}catch(e){
console.log(e)
}
I also tried to only specifiy the path to folder1 in the compressDir function, but then I get an error "TarStreamError: ENOENT: no such file or directory".
How can I achieve this?
You're getting the error because your path is incorrect. you are giving the function the path /folder1, meaning it tries to find the folder in the ROOT directory because of the initial /. instead use ./folder1 or just folder.

Gulp vinyl ftp - how to use clean function?

The vinyl-ftp package has a function clean() but I'm not sure how to use it right. I need to:
get all files from my build folder
put them into the target folder on my ftp server
clean files if they're not available locally
I have the following gulp task:
gulp.task('deploy', () => {
let conn = ftp.create({host:host,user:user,password: password});
return gulp.src('build/**', {base: './build/', buffer: false })
.pipe(conn.newer('/path/on/my/server/')) // only upload newer files
.pipe(conn.dest('/path/on/my/server/'))
.pipe(conn.clean('build/**', './build/'));
});
1) and 2) is OK, but the clean() function does nothing
The vinyl-ftp docs have this to say:
conn.clean( globs, local[, options] )
Globs remote files, tests if they are locally available at <local>/<remote.relative> and removes them if not.
Note that globs expects a path for the remote files on your FTP server. Since your remote files are located in /path/on/my/server/ you have to specify that path as your glob:
.pipe(conn.clean('/path/on/my/server/**', './build/'));
Since I got a lot of struggle with this, here a working peace of code. It removes all files from the server that dont exist locally except the usage folder:
var connection = ftp.create({ ... });
connection.clean([
'/*.*',
'/!(usage)*',
'/de/**',
'/en/**',
'/images/**',
'/fonts/**',
'/json/**',
'/sounds/**'
], './dist', { base: '/' });
My files are locally on the ./dist folder and remote directly in the root directory (/) (of the used ftp user).

Get matched folder from gulp src path using glob

Consider this which checks for JS files in either of two specific locations:
gulp.src( "#(Branch|Main)/*.js" ).pipe( _do-stuff_ )
How does one get which folder was matched (Main or Branch) for the current file(s)?

Gulp - Copy multiple files to seperate paths

I am working on a web app that uses Node.js. In this app, I have a Gulp file. I am using Gulp 4. During my build process, I am attempting to copy multiple files to directories at once. My directory structure looks like this:
./
dest/
source/
child/
index.js
index.bak
file.js
README.md
My real directory structure is more involved. However, I am trying to copy ./source/file.js to ./dest/file.js and ./source/child/index.js to ./dest/child/index.js. Notice that I do not want to copy README.md or index.bak over to the ./dest directory. In an attempt to do this, I have the following function:
function copy() {
let files = [
'source/file.js',
'source/child/**/*.*'
];
return gulp
.src(files)
.pipe(gulp.dest('dest'))
;
}
My problem is, everything just gets copied to the dest directory. The directory structure does not get preserved. While would be fine if I could figure out how to copy files to different directories in a single task. I tried the following:
function copy() {
return gulp
.src('source/child/index.js')
.pipe(gulp.dest('dest/child'))
.src('source/file.js')
.pipe(gulp.dest('dest'))
;
}
However, that approach just generates an error that says:
TypeError: gulp.src(...).pipe(...).src is not a function
So, I'm stuck. I'm not sure how to copy multiple files to multiple directories from a single gulp task.
You need to use the base option as mentioned here ref. It will make sure your directory is copied as it is.
function copy() {
let files = [
'source/file.js',
'source/child/**/*.*'
];
return gulp
.src(files, {base: 'source/'})
.pipe(gulp.dest('dest'));
}

Zip Files & Folders With No Base Directory

AWS Lambda requires a zip file that produces a file when it's unzipped.
However, every node.js zip library produces a zip file that contains a base folder, containing the files Lambda needs, which breaks Lambda, resulting in a 'Cannot find module' error.
For example, if I have a index.js file and a node_modules directory in the dist folder, when I use gulp-zip, I get an added root folder when the zip file is unzipped...
gulp.src(['./dist/**/*'])
.pipe(zip('dist.zip'))
.pipe(gulp.dest('./'))
// When unzipped, this results in a "dist" folder containing index.js and node_modules
I've tried 6 node zip libraries and none have a simple way of excluding the base directory.
Any thoughts?
I've used 'node-archiver', which can zip a directory to a destination directory (which I just set as an empty string).
https://github.com/archiverjs/node-archiver#directorydirpath-destpath-data
var archiver = require('archiver');
archive = archive.directory('./directoryToZip/', '' ); //option 2 is the dest
archive.pipe( outZip);
archive.finalize();

Resources