javascript 如何使用 Node.js 和 npm 自动压缩文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31306180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to automatically zip files with Node.js and npm
提问by Dmitry
Is there a way to automatically zip certain files at the build time with Node.js and npm?
有没有办法在构建时使用 Node.js 和 npm 自动压缩某些文件?
For example, I have a project, that file structure looks like this:
例如,我有一个项目,该文件结构如下所示:
Project/
--lib/
--node_modules/
--test/
--index.js
--package.json
I want to be able to zip lib folder, certain modules from node_modules and index.js into some zip archive to upload it on the AWS Lambda, for example. I do not need test folder or test Node.js modules (mocha and chai) to be zipped. I have even created a bash script for generating zip file, but is there a way to automatically execute this script, when 'npm install' is called?
例如,我希望能够将 lib 文件夹、node_modules 和 index.js 中的某些模块压缩到一些 zip 存档中,以将其上传到 AWS Lambda 上。我不需要压缩测试文件夹或测试 Node.js 模块(mocha 和 chai)。我什至创建了一个用于生成 zip 文件的 bash 脚本,但是有没有办法在调用“npm install”时自动执行这个脚本?
This should be a standard problem and it should have a standard solution, but I was unable to discover such.
这应该是一个标准的问题,它应该有一个标准的解决方案,但我无法发现这样的问题。
UPDATE
更新
thanks to michael, decided to use gulp. This is my script, in case some one else will need it for AWS Lambda:
感谢 michael,决定使用 gulp。这是我的脚本,以防其他人需要它用于 AWS Lambda:
var gulp = require('gulp');
var clean = require('gulp-clean');
var zip = require('gulp-zip');
var merge = require('merge-stream');
gulp.task('clean', function () {
var build = gulp.src('build', {read: false})
.pipe(clean());
var dist = gulp.src('dist', {read: false})
.pipe(clean());
return merge(build, dist);
});
gulp.task('build', function() {
var index = gulp.src('index.js')
.pipe(gulp.dest('build'));
var lib = gulp.src('lib/**')
.pipe(gulp.dest('build/lib'));
var async = gulp.src('node_modules/async/**')
.pipe(gulp.dest('build/node_modules/async'));
var collections = gulp.src('node_modules/collections/**')
.pipe(gulp.dest('build/node_modules/collections'));
var underscore = gulp.src('node_modules/underscore/**')
.pipe(gulp.dest('build/node_modules/underscore'));
var util = gulp.src('node_modules/util/**')
.pipe(gulp.dest('build/node_modules/util'));
var xml2js = gulp.src('node_modules/xml2js/**')
.pipe(gulp.dest('build/node_modules/xml2js'));
return merge(index, lib, async, collections, underscore, util, xml2js);
});
gulp.task('zip', ['build'], function() {
return gulp.src('build/*')
.pipe(zip('archive.zip'))
.pipe(gulp.dest('dist'));
});
gulp.task('default', ['zip']);
采纳答案by baao
I would go with gulp using gulp-sftp, gulp-tar and gulp-gzip and an alias as command. Create a file called .bash_aliases
in your users home folder containing
我会使用 gulp-sftp、gulp-tar 和 gulp-gzip 以及一个别名作为命令来使用 gulp。创建一个.bash_aliases
在您的用户主文件夹中调用的文件,其中包含
alias installAndUpload='npm install && gulp runUploader'
After a reboot you can call both actions at once with this alias.
重新启动后,您可以使用此别名同时调用这两个操作。
A gulp file could look something like this
gulp 文件可能看起来像这样
var gulp = require('gulp');
var watch = require('gulp-watch');
var sftp = require('gulp-sftp');
var gzip = require('gulp-gzip');
gulp.task('runUploader', function () {
gulp.src('.path/to/folder/to/compress/**')
.pipe(tar('archive.tar'))
.pipe(gzip())
.pipe(gulp.dest('path/to/folder/to/store')) // if you want a local copy
.pipe(sftp({
host: 'website.com',
user: 'johndoe',
pass: '1234'
}))
});
Of course, you can also add gulp-watch to automatically create the tar/zip and upload it whenever there is a change in the directory.
当然,你也可以添加 gulp-watch 自动创建 tar/zip 并在目录发生变化时上传。
回答by eddies
I realize this answer comes years too late for the original poster. But I had virtually the same question about packaging up a Lambda function, so for posterity, here's a solution that doesn't require any additional devDependencies (like gulp or grunt) and just uses npm pack
along with the following package.json
(but does assume you have sed
and zip
available to you):
我意识到这个答案对于原始海报来说为时已晚。但是我对打包 Lambda 函数有几乎相同的问题,因此对于后代,这里有一个解决方案,它不需要任何额外的 devDependencies(如 gulp 或 grunt),只需npm pack
与以下内容一起使用package.json
(但假设您有sed
并zip
可用给你):
{
"name": "my-lambda",
"version": "1.0.0",
"scripts": {
"postpack": "tarball=$(npm list --depth 0 | sed 's/@/-/g; s/ .*/.tgz/g; 1q;'); tar -tf $tarball | sed 's/^package\///' | zip -@r package; rm $tarball"
},
"files": [
"/index.js",
"/lib"
],
"dependencies": {
"async": "*",
"collections": "*",
"underscore": "*",
"util": "*",
"xml2js": "*"
},
"bundledDependencies": [
"async",
"collections",
"underscore",
"util",
"xml2js"
],
"devDependencies": {
"chai": "*",
"mocha": "*"
}
}
Given the above package.json, calling npm pack
will produce a package.zip
file that contains:
鉴于上述 package.json,调用npm pack
将生成一个package.zip
文件,其中包含:
index.js
lib/
node_modules/
├── async/
├── collections/
├── underscore/
├── util/
└── xml2js/
The filesarray is a whitelist of what to include. Here, it's just index.js
and the lib
directory.
该文件数组是什么,包括白名单。在这里,它只是index.js
和lib
目录。
However, npm will also automatically include package.json
, README
(and variants like README.md
, CHANGELOG
(and its variants), and LICENSE
(and the alternative spelling LICENCE
) unless you explicitly exclude them (e.g. with .npmignore).
但是,除非您明确排除它们(例如使用.npmignore),否则npm 也会自动包含package.json
, README
(以及诸如README.md
, CHANGELOG
(及其变体)和LICENSE
(以及替代拼写LICENCE
)之类的变体。
The bundledDependenciesarray specifies what packages to bundle. In this case, it's all the dependencies but none of the devDependencies.
该bundledDependencies数组指定哪些软件包捆绑。在这种情况下,它是所有依赖项,但没有一个 devDependencies。
Finally, the postpackscript is run after npm pack
because npm pack
generates a tarball, but we need to generate a zip for AWS Lambda.
最后运行postpack脚本,npm pack
因为它会npm pack
生成一个 tarball,但我们需要为 AWS Lambda 生成一个 zip。
A more detailed explanation of what the postpack script is doing is available at https://hackernoon.com/package-lambda-functions-the-easy-way-with-npm-e38fc14613ba(and is also the source of the general approach).
在https://hackernoon.com/package-lambda-functions-the-easy-way-with-npm-e38fc14613ba上可以获得对 postpack 脚本正在做什么的更详细的解释(也是一般方法的来源) .
回答by Antoine Bolvy
You should take a look to npm scripts.
你应该看看npm scripts。
You'll still need a bash script laying around in your repository, but it will be automatically triggered by some npm
tasks when they are executed.
您仍然需要在您的存储库中放置一个 bash 脚本,但它会在npm
执行某些任务时自动触发。
回答by stdob--
If you need automate tasks take a look to Gruntor Gulp.
In the case of Grunt needed plugins:
在 Grunt 需要插件的情况下:
https://www.npmjs.com/package/grunt-zip
https://www.npmjs.com/package/grunt-zip
回答by C.M.
npm-pack-zip worked for me.
npm-pack-zip 对我有用。
npm install --save-dev npm-pack-zip
To publish the whole lambda using aws I used this node script in package.json:
为了使用 aws 发布整个 lambda,我在 package.json 中使用了这个节点脚本:
"publish": "npm-pack-zip && aws lambda update-function-code --function-name %npm_package_name% --zip-file fileb://%npm_package_name%.zip && rm %npm_package_name%.zip"
回答by Cagdas Tulek
Check out my gist at https://gist.github.com/ctulek/6f16352ebdfc166ce905
在https://gist.github.com/ctulek/6f16352ebdfc166ce905查看我的要点
This uses gulp for all the tasks you mentioned except creating the lambda function initially (it only updates the code)
这将 gulp 用于您提到的所有任务,除了最初创建 lambda 函数(它只更新代码)
It assumes every lambda function is implemented in its own folder, and you need to define your AWS credential profile.
它假设每个 lambda 函数都在其自己的文件夹中实现,并且您需要定义您的 AWS 凭证配置文件。