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 committed Sep 21, 2021
1 parent 94cb8bd commit 5eef42e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
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
52 changes: 52 additions & 0 deletions 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,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, {})
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -106,10 +147,17 @@ 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(path.relative(src, filename), options.ignoreDir, ignoreDirs)
) {
break
}

if (options.unpackDir) {
shouldUnpack = isUnpackedDir(path.relative(src, filename), options.unpackDir, unpackDirs)
} else {
Expand All @@ -118,6 +166,10 @@ module.exports.createPackageFromFiles = async function (src, dest, filenames, me
filesystem.insertDirectory(filename, shouldUnpack)
break
case 'file':
if (isIgnoredFile(filePath, options.ignore, options.ignoreDir)) {
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

0 comments on commit 5eef42e

Please sign in to comment.