Skip to content

Commit

Permalink
feat: add flags to ignore files/directories
Browse files Browse the repository at this point in the history
Add '--ignore-dir' flag to not include directories and '--ignore' flag
to not include files while asar packaging

Signed-off-by: Rituka Patwal <[email protected]>
  • Loading branch information
1bitphoenix authored and Rituka Patwal committed Feb 7, 2022
1 parent 94cb8bd commit 48ec37d
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
<<: *steps-test
test-mac:
macos:
xcode: "10.2.0"
xcode: "12.5.0"
<<: *steps-test
test-windows:
executor:
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions bin/asar.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ program.command('pack <dir> <output>')
.option('--ordering <file path>', 'path to a text file for ordering contents')
.option('--unpack <expression>', 'do not pack files matching glob <expression>')
.option('--unpack-dir <expression>', 'do not pack dirs matching glob <expression> or starting with literal <expression>')
.option('--ignore <expression>', 'do not include files matching glob <expression>')
.option('--ignore-dir <expression>', 'do not include dirs matching glob <expression>')
.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,
Expand Down
47 changes: 46 additions & 1 deletion lib/asar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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, {})
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 })
Expand Down
2 changes: 2 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type CreateOptions = {
transform?: (filePath: string) => NodeJS.ReadWriteStream | void;
unpack?: string;
unpackDir?: string;
ignore?: string;
ignoreDir?: string;
};

export type ListOptions = {
Expand Down
24 changes: 24 additions & 0 deletions test/cli-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand Down Expand Up @@ -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')
})
})
Binary file added test/expected/packthis-ignore-cli.asar
Binary file not shown.
Binary file added test/expected/packthis-ignore-dir-cli.asar
Binary file not shown.
Binary file added test/expected/packthis-ignore-dir-file-cli.asar
Binary file not shown.
Binary file added test/expected/packthis-ignore-dir-glob-cli.asar
Binary file not shown.
Binary file not shown.
Binary file added test/expected/packthis-ignore-dir-globstar-cli.asar
Binary file not shown.

0 comments on commit 48ec37d

Please sign in to comment.