Zip Files & Folders With No Base Directory - node.js

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();

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.

How to copy only folders from a directory into another

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.

Jest test to see if *.spec.tsx files exist

I have a TypeScript project that contains a bunch of files in different directories all located under src/.
Is it possible to write a Jest test so that it only returns success if each *.tsx file found under the parent directory (src/) has a corresponding *.spec.tsx test file?
In other words if I have the following 3 files:
src/index.tsx
src/foo.tsx
src/folder/component.tsx
The Jest test would fail until such time that the following files are created:
src/index.spec.tsx
src/foo.spec.tsx
src/folder/component.spec.tsx
I ended up using
import fs from 'fs';
and then using fs.readdir and fs.statSync to determine if a file is a file or a directory so that I can recursively look in every location from my src/ path.

How create node.js command line tool for adding new files to the specific folder?

I've created the web app using node.js and I want to create the command line tool which will be adding exists files to the specific directories.
I have following project structure of my web app:
-Scripts
-Styles
-Images
-Index.html
-package.json
...
I want to add exists files to the scripts files - some *.js files and to the styles folder some *.css files.
I've found this example - http://blog.npmjs.org/post/118810260230/building-a-simple-command-line-tool-with-npm
- Here is the example - how create simple command line but I don't know how work with the files? Where can I store template files and how can I copy these files to the specific project folders?
I have tried this: cli.js
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
var shell = require("shelljs");
shell.exec("npm install ..."); //install dependencies
//Now I want to copy .js files to the scripts directory and .css files to the style directory
//And where store these template files?

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'));
}

Resources