Skip to content

Instantly share code, notes, and snippets.

@elleonard
Created May 1, 2020 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elleonard/e4453a423eaedbc44858a8bef5d07194 to your computer and use it in GitHub Desktop.
Save elleonard/e4453a423eaedbc44858a8bef5d07194 to your computer and use it in GitHub Desktop.
nw-builderと同じ方法でzip圧縮するためのjs
(function() {
'use strict';
const fs = require('fs');
const path = require('path');
const archiver = require('archiver');
const Glob = require('simple-glob');
let output = fs.createWriteStream(__dirname + '/package.nw');
let archive = archiver('zip', {});
const targetDir = "デプロイしたディレクトリのパス";
const fileglob = ["package.json", "www/*", "www/**"];
const matches = Glob(fileglob.map(file => `${targetDir}\\${file}`))
let jsonfile;
let package_path;
let srcFiles = [];
let destFiles = [];
function closerPathDepth(path1, path2) {
if (!path2) { return path1; }
const depth1 = path1.split(path.sep).length;
const depth2 = path2.split(path.sep).length;
return depth1 < depth2 ? path1 : path2;
}
function getFileList() {
matches.forEach(file => {
const internalFileName = path.normalize(file);
if (internalFileName.match('package.json')) {
jsonfile = closerPathDepth(internalFileName, jsonfile);
package_path = path.normalize(jsonfile.split('package.json')[0] || './');
}
if (!fs.lstatSync(internalFileName).isDirectory()) {
srcFiles.push(internalFileName);
}
});
// mapにできそう
srcFiles.forEach(file => {
destFiles.push({
src: file,
dest: file.replace(package_path, '')
});
});
return {
files: destFiles,
json: jsonfile
};
}
const fileList = getFileList();
fileList.files.forEach(file => {
archive.file(file.src, {name: file.dest});
});
archive.pipe(output);
archive.finalize();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment