Created
May 1, 2020 10:23
-
-
Save elleonard/e4453a423eaedbc44858a8bef5d07194 to your computer and use it in GitHub Desktop.
nw-builderと同じ方法でzip圧縮するためのjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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