From 4c0838c10240a1f7b1b03f489aab02e883e79454 Mon Sep 17 00:00:00 2001 From: Rituka Patwal Date: Sun, 19 Sep 2021 16:23:48 +0530 Subject: [PATCH] feat: add flags to ignore files/directories Add '--ignore-dir' flag to not include directories and '--ignore' flag to not include files while asar packaging Signed-off-by: Rituka Patwal --- bin/asar.js | 4 ++++ lib/asar.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/index.d.ts | 2 ++ 3 files changed, 58 insertions(+) diff --git a/bin/asar.js b/bin/asar.js index 8efcc1a..9482836 100755 --- a/bin/asar.js +++ b/bin/asar.js @@ -24,11 +24,15 @@ program.command('pack ') .option('--ordering ', 'path to a text file for ordering contents') .option('--unpack ', 'do not pack files matching glob ') .option('--unpack-dir ', 'do not pack dirs matching glob or starting with literal ') + .option('--ignore ', 'do not include files matching glob ') + .option('--ignore-dir ', 'do not include dirs matching glob ') .option('--exclude-hidden', 'exclude hidden files') .action(function (dir, output, options) { options = { unpack: options.unpack, unpackDir: options.unpackDir, + ignore: options.ignore, + ignoreDir: options.ignoreDir, ordering: options.ordering, version: options.sv, arch: options.sa, diff --git a/lib/asar.js b/lib/asar.js index 050e1a7..dad6b0e 100644 --- a/lib/asar.js +++ b/lib/asar.js @@ -8,6 +8,8 @@ const Filesystem = require('./filesystem') const disk = require('./disk') const crawlFilesystem = require('./crawlfs') +const END_CHAR = '/' + /** * Whether a directory should be excluded from packing due to the `--unpack-dir" option. * @@ -26,6 +28,44 @@ function isUnpackedDir (dirPath, pattern, unpackDirs) { } } +/** + * Whether a directory should be ignored while packing due to the `--ignore-dir` option. + * + * @param {string} dirPath - directory path to check + * @param {string} pattern - directory name or glob pattern + */ +function isIgnoredDir (dirPath, pattern, ignoreDirs) { + const dirPathStr = dirPath + END_CHAR + if (dirPathStr.startsWith(pattern + END_CHAR)|| minimatch(dirPath, pattern)) { + if (!ignoreDirs.includes(dirPath)) { + ignoreDirs.push(dirPath) + } + return true + } + return ignoreDirs.some(ignoreDir => dirPath.startsWith(ignoreDir)) +} + +/** + * Whether a file should be ignored while packing due to the `--ignore` or `--ignore-dir` option. + * + * @param {string} filePath - file path to check + * @param {string} ignoreFile - file name or glob pattern + * @param {string} ignoreDir - directory name or glob pattern + */ +function isIgnoredFile (filePath, ignoreFile, ignoreDir) { + if (ignoreFile) { + const filePathStr = filePath + END_CHAR + return filePathStr.startsWith(ignoreFile + END_CHAR)|| minimatch(filePath, ignoreFile) + } + + if (ignoreDir) { + const dirName = path.dirname(filePath) + return isIgnoredDir(dirName, ignoreDir, []) + } + + return false +} + module.exports.createPackage = async function (src, dest) { return module.exports.createPackageWithOptions(src, dest, {}) } @@ -60,6 +100,7 @@ module.exports.createPackageFromFiles = async function (src, dest, filenames, me const filesystem = new Filesystem(src) const files = [] const unpackDirs = [] + const ignoreDirs = [] let filenamesSorted = [] if (options.ordering) { @@ -110,6 +151,12 @@ module.exports.createPackageFromFiles = async function (src, dest, filenames, me let shouldUnpack switch (file.type) { case 'directory': + if (options.ignoreDir && + isIgnoredDir(path.relative(src, filename), options.ignoreDir, ignoreDirs) + ) { + break + } + if (options.unpackDir) { shouldUnpack = isUnpackedDir(path.relative(src, filename), options.unpackDir, unpackDirs) } else { @@ -118,6 +165,11 @@ module.exports.createPackageFromFiles = async function (src, dest, filenames, me filesystem.insertDirectory(filename, shouldUnpack) break case 'file': + const filePath = path.relative(src, filename) + if (isIgnoredFile(filePath, options.ignore, options.ignoreDir)) { + break + } + shouldUnpack = false if (options.unpack) { shouldUnpack = minimatch(filename, options.unpack, { matchBase: true }) diff --git a/lib/index.d.ts b/lib/index.d.ts index b3790ec..556784d 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -9,6 +9,8 @@ export type CreateOptions = { transform?: (filePath: string) => NodeJS.ReadWriteStream | void; unpack?: string; unpackDir?: string; + ignore?: string; + ignoreDir?: string; }; export type ListOptions = {