From 7c08de2942cfd316fc5d7dee2faf10885c324afc 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 --- .circleci/config.yml | 2 +- .gitattributes | 2 +- README.md | 7 +-- bin/asar.js | 4 ++ lib/asar.js | 47 +++++++++++++++++- lib/index.d.ts | 2 + test/cli-spec.js | 24 +++++++++ test/expected/packthis-ignore-cli.asar | Bin 0 -> 1340 bytes test/expected/packthis-ignore-dir-cli.asar | Bin 0 -> 1077 bytes .../packthis-ignore-dir-file-cli.asar | Bin 0 -> 1077 bytes .../packthis-ignore-dir-glob-cli.asar | Bin 0 -> 1144 bytes ...kthis-ignore-dir-glob-foo-bar-baz-cli.asar | Bin 0 -> 1122 bytes .../packthis-ignore-dir-globstar-cli.asar | Bin 0 -> 602 bytes 13 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 test/expected/packthis-ignore-cli.asar create mode 100644 test/expected/packthis-ignore-dir-cli.asar create mode 100644 test/expected/packthis-ignore-dir-file-cli.asar create mode 100644 test/expected/packthis-ignore-dir-glob-cli.asar create mode 100644 test/expected/packthis-ignore-dir-glob-foo-bar-baz-cli.asar create mode 100644 test/expected/packthis-ignore-dir-globstar-cli.asar diff --git a/.circleci/config.yml b/.circleci/config.yml index a21dc83..10196b2 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -36,7 +36,7 @@ jobs: <<: *steps-test test-mac: macos: - xcode: "10.2.0" + xcode: "12.5.0" <<: *steps-test test-windows: executor: diff --git a/.gitattributes b/.gitattributes index b6fcc92..3cd79a9 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -test/input/**/*.txt text eol=lf +test/input/**/*.txt text eol=lf \ No newline at end of file diff --git a/README.md b/README.md index e05497f..be98c2c 100644 --- a/README.md +++ b/README.md @@ -67,21 +67,22 @@ Given: (h) └── w1 ``` -Exclude: a, b +Unpack: a, b ```bash $ asar pack app app.asar --unpack-dir "{x1,x2}" ``` -Exclude: a, b, d, f +Unpack: a, b, d, f ```bash $ asar pack app app.asar --unpack-dir "**/{x1,x2}" ``` -Exclude: a, b, d, f, h +Unpack: a, b, d, f, h ```bash $ asar pack app app.asar --unpack-dir "{**/x1,**/x2,z4/w1}" ``` +Similarly, `--ignore-dir` flag can be used to ignore certain directories and `--ignore` flag can be used to ignore certain files while packaging. ## Using programatically ### Example 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..50f97c2 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,34 @@ 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 + */ +function isIgnoredFile (filePath, ignoreFile) { + const filePathStr = filePath + END_CHAR + return filePathStr.startsWith(ignoreFile + END_CHAR) || minimatch(filePath, ignoreFile, { matchBase: true }) +} + module.exports.createPackage = async function (src, dest) { return module.exports.createPackageWithOptions(src, dest, {}) } @@ -60,6 +90,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) { @@ -106,18 +137,32 @@ module.exports.createPackageFromFiles = async function (src, dest, filenames, me metadata[filename] = await crawlFilesystem.determineFileType(filename) } const file = metadata[filename] + const filePath = path.relative(src, filename) let shouldUnpack switch (file.type) { case 'directory': + if (options.ignoreDir && + isIgnoredDir(filePath, options.ignoreDir, ignoreDirs) + ) { + break + } + if (options.unpackDir) { - shouldUnpack = isUnpackedDir(path.relative(src, filename), options.unpackDir, unpackDirs) + shouldUnpack = isUnpackedDir(filePath, options.unpackDir, unpackDirs) } else { shouldUnpack = false } filesystem.insertDirectory(filename, shouldUnpack) break case 'file': + if ( + (options.ignore && isIgnoredFile(filePath, options.ignore, options.ignoreDir, ignoreDirs)) || + (options.ignoreDir && isIgnoredDir(filePath, options.ignoreDir, ignoreDirs)) + ) { + 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 = { diff --git a/test/cli-spec.js b/test/cli-spec.js index 2017d32..8f9be16 100644 --- a/test/cli-spec.js +++ b/test/cli-spec.js @@ -45,6 +45,10 @@ describe('command line interface', function () { assert.ok(fs.existsSync('tmp/packthis-unpack-cli.asar.unpacked/dir2/file2.png')) await compFiles('tmp/packthis-unpack-cli.asar', 'test/expected/packthis-unpack.asar') }) + it('should create archive from directory without ignored files', async () => { + await execAsar('p test/input/packthis/ tmp/packthis-ignore-cli.asar --ignore *.png') + await compFiles('tmp/packthis-ignore-cli.asar', 'test/expected/packthis-ignore-cli.asar') + }) it('should list files/dirs in archive', async () => { return assertAsarOutputMatches('l test/input/extractthis.asar', 'test/expected/extractthis-filelist.txt') }) @@ -137,4 +141,24 @@ describe('command line interface', function () { assert.ok(fs.existsSync('tmp/packthis-unpack-subdir-cli.asar.unpacked/dir2/subdir/file2.png')) assert.ok(fs.existsSync('tmp/packthis-unpack-subdir-cli.asar.unpacked/dir2/subdir/file3.txt')) }) + it('should create archive from directory without ignored dirs', async () => { + await execAsar('p test/input/packthis/ tmp/packthis-ignore-dir-cli.asar --ignore-dir dir2') + return compFiles('tmp/packthis-ignore-dir-cli.asar', 'test/expected/packthis-ignore-dir-cli.asar') + }) + it('should create archive from directory without ignored dirs specified by glob pattern', async () => { + await execAsar('p test/input/packthis-glob/ tmp/packthis-ignore-dir-glob-cli.asar --ignore-dir "{x1,x2}"') + return compFiles('tmp/packthis-ignore-dir-glob-cli.asar', 'test/expected/packthis-ignore-dir-glob-cli.asar') + }) + it('should create archive from directory without ignored dirs specified by globstar pattern', async () => { + await execAsar('p test/input/packthis-glob/ tmp/packthis-ignore-dir-globstar-cli.asar --ignore-dir "**/{x1,x2}"') + return compFiles('tmp/packthis-ignore-dir-globstar-cli.asar', 'test/expected/packthis-ignore-dir-globstar-cli.asar') + }) + it('should create archive from directory without ignored dirs specified by foo/{bar,baz} style pattern', async () => { + await execAsar('p test/input/packthis-glob/ tmp/packthis-ignore-dir-glob-foo-bar-baz-cli.asar --ignore-dir "y3/{x1,z1}"') + return compFiles('tmp/packthis-ignore-dir-glob-foo-bar-baz-cli.asar', 'test/expected/packthis-ignore-dir-glob-foo-bar-baz-cli.asar') + }) + it('should create archive from directory without ignored dirs and files', async () => { + await execAsar('p test/input/packthis/ tmp/packthis-ignore-dir-file-cli.asar --ignore *.png --ignore-dir dir2') + return compFiles('tmp/packthis-ignore-dir-file-cli.asar', 'test/expected/packthis-ignore-dir-file-cli.asar') + }) }) diff --git a/test/expected/packthis-ignore-cli.asar b/test/expected/packthis-ignore-cli.asar new file mode 100644 index 0000000000000000000000000000000000000000..a6b3f147781d557123f05fff9881522d20bf96ec GIT binary patch literal 1340 zcmbu9J#QN^42Ew=f&L8RX2TFg>Vw(3bx*gVgLER}Lmaz^Z)h6izn5nit&I$>;Xyc& z5=6ZZ-_u>!b)P?V-JOlj##^uBM)v*vTfbV5t;u$dSJSU)PVL9v(%%Cw`v9$&`kb>34pk2{;MpKbBs>lYuQ?O2!n+TupeH7^hr>Mxn9$KNXLtU7dx0PC@5{c8FEC5_i(;O@IfPRN zz}G+^?|`_W=HN3^ElJUfC1gk~Gq8xK0EVZk57|`*ISviejHhLP5yH&{48jqE|9}C{ zG2n?6{M?`a_Ckr#dK%a}@?NA1@6E) zdsk6Jf=xb!1QN71>P`7D4|g}GcOWo&i$8Pv=C7AG`1bO$Gi7>&Cgm53wt{~Lc<{U59`yP z_S<*vyJpE>+b=%b0nu@+0}Kiw)m(9i%ze%5&`}Zu!5%rY#~~pHX2_(Zatz_D@{1ds z>~E6oN4LTMw`hdK6tl!wYorv&+o;BR1lwyQxpGRD3B-W~p#b0mQi>3=&PYBh`r-yB fyY(0FWt^666Tj8<_waA$qwG&iw!^HGeq7%G+#WMd literal 0 HcmV?d00001 diff --git a/test/expected/packthis-ignore-dir-file-cli.asar b/test/expected/packthis-ignore-dir-file-cli.asar new file mode 100644 index 0000000000000000000000000000000000000000..a6e80a6dde9af671ba6eab335ca0ad4ded47ce2d GIT binary patch literal 1077 zcmbu9v2NTj42BK#9^|0;a1bTR6tiXPo^C}4$)aK-mvaz%Xd2|fvxPboZGsNh_7+Q| z2vPs{gWhyqx7oB$*S&t~x@S|TgDmFi+3d%u6gjqdzkXj^YMFkBxdPsqxzA1@6E) zdsk6Jf=xb!1QN71>P`7D4|g}GcOWo&i$8Pv=C7AG`1bO$Gi7>&Cgm53wt{~Lc<{U59`yP z_S<*vyJpE>+b=%b0nu@+0}Kiw)m(9i%ze%5&`}Zu!5%rY#~~pHX2_(Zatz_D@{1ds z>~E6oN4LTMw`hdK6tl!wYorv&+o;BR1lwyQxpGRD3B-W~p#b0mQi>3=&PYBh`r-yB fyY(0FWt^666Tj8<_waA$qwG&iw!^HGeq7%G+#WMd literal 0 HcmV?d00001 diff --git a/test/expected/packthis-ignore-dir-glob-cli.asar b/test/expected/packthis-ignore-dir-glob-cli.asar new file mode 100644 index 0000000000000000000000000000000000000000..4ff7f8d9e8249247923047427794c5725cdb726a GIT binary patch literal 1144 zcmbu9u}&m042Gwm;xSUMPDvam4jml@9p&kYBu>~73oGpmuz+~?a{wVmNVkVpql|3F zitSH-axx6V<;gHy$U2wxqT0BrlRA4*PtgCLZa3i9yq|k?8lS5=GkT@AtxZ*8N~@I` z@8){F8|SB6N1sz)jjAQ6XDY|uVqP0q0<3MD4*O<;gR zu{6m@Bp+%A1u=vwC*{qy++4l&;Y@U(-A7EC|9yOcKd-MVwKv}oxFv%4g@E}H0?xJO z+%kJ#tpjE(w%W$gGXWT~KFaVMqh#rnOu}TE7EsZ$pfU#$99=*VbO_H&2z&dD3I2mg z{D~}rOS!lpQ)6)y8))4OY4W+G>_KeG67~!_1}l2;21@gyAx9w*9b|HJfj1@}T-*Z0 z;}TQ9E`G^{zG%hvUjO^L7@ZiwplBjn<1pzMwa5oFwh(PWYa24v#z`Ei!mQ<18FRH@ j3LoThbb(JU@}Bh3=k>1E({8ox{qEfuAKoL=xIg_1UieTH literal 0 HcmV?d00001 diff --git a/test/expected/packthis-ignore-dir-glob-foo-bar-baz-cli.asar b/test/expected/packthis-ignore-dir-glob-foo-bar-baz-cli.asar new file mode 100644 index 0000000000000000000000000000000000000000..905e0989c2860aebb2b4832942ef5bea76833ecd GIT binary patch literal 1122 zcmbu9!EPKO3_$0QQ$B<9ss&?%$*s5EbF6v*Y_g+nGg4-xY!c<)%OtBRt5vm)7Rg|Y zh0T-A)i4ZvH4ND0YnK<(#+}aQ=EdyYI{)3Siu4t23=Lza{tTbXur13UWryvydyiuW$CjJWY=I`Qevtn@r5mF?jZqmHz2}D| z9=flp>glek9yvYdNtCvuL99;V0Q3C|=K>za{W!BjdnB<^aw+;!24YS~u@vpcNSj?d z9%fDKHa*wPJ3p>po;6xlG;wGc1CT|Z0zmZH#iX6IHBvMtOYdC@(OTs)K=L6KRz%MS zrGbR#9Pv+e*Qf3JJcm)ifHXfG5C3|5gSTl~ile%;VB%oTTyo;`N^-W#Uc2NkOj7@H zNy(TI6lxo4tqevPf+VUzVsSwy)Vg5yRB~kCNqJf7LK$n)pi=(RrJEbDOW!eX$Jp$M K7UzX&_lqy~qpz9( literal 0 HcmV?d00001