This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ignore globs to zip functionality
- Loading branch information
Showing
10 changed files
with
225 additions
and
99 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
const glob = (root, ignoreGlobs) => { | ||
let list = [] | ||
let skipped = [] | ||
let globRegexes = (ignoreGlobs || []).map(toGlobRegex) | ||
|
||
let walkSync = dir => { | ||
let files = fs.readdirSync(dir) | ||
|
||
files.forEach(file => { | ||
let fullPath = path.join(dir, file) | ||
let globPath = path.resolve(fullPath).replace(path.resolve(root) + '/', '') | ||
|
||
if (file.startsWith('.') && !file.match(/^\.pgb/)) { | ||
skipped.push(`${fullPath} [HIDDEN]`) | ||
return | ||
} | ||
|
||
try { | ||
let stat = fs.statSync(fullPath) | ||
fs.closeSync(fs.openSync(fullPath, 'r')) | ||
|
||
let ignored = filter(globPath, stat.isDirectory(), globRegexes) | ||
|
||
if (stat.isDirectory()) { | ||
if (ignored) { | ||
skipped.push(`${fullPath}/ [IGNORED]`) | ||
} else { | ||
list.push({ path: fullPath, size: 0 }) | ||
walkSync(fullPath) | ||
} | ||
} else { | ||
if (ignored) { | ||
skipped.push(`${fullPath} [IGNORED]`) | ||
} else { | ||
list.push({ path: fullPath, size: stat.size }) | ||
} | ||
list.push() | ||
} | ||
} catch (e) { | ||
skipped.push(`${fullPath} [${e.code}]`) | ||
} | ||
}) | ||
} | ||
|
||
walkSync(root) | ||
return { list, skipped } | ||
} | ||
|
||
const toGlobRegex = (glob) => { | ||
if (glob == null || glob[0] === '#' || glob.trim() === '') return null | ||
|
||
let negation = glob.indexOf('!') === 0 | ||
let dir = false | ||
|
||
if (glob.endsWith('/')) { | ||
glob = glob.slice(0, -1) | ||
dir = true | ||
} | ||
|
||
if (negation || glob[0] === '/') { | ||
glob = glob.slice(1) | ||
} | ||
|
||
glob = glob | ||
.replace(/[.+^${}()|[\]\\]/g, '\\$&') | ||
.replace(/\?/g, '.') | ||
.replace(/\*\*\//g, '^e^') | ||
.replace(/\*\*/g, '^e^') | ||
.replace(/\*/g, `[^/]+`) | ||
.replace(/\^e\^/g, '.*') | ||
|
||
if (glob.indexOf('/') === -1) { | ||
glob = glob + '/?' | ||
} | ||
|
||
return { dir, negation, regex: new RegExp(`^${glob}$`) } | ||
} | ||
|
||
const filter = (filePath, isDir, globRegexes) => { | ||
let result = false | ||
|
||
for (let globRegex of globRegexes) { | ||
if (globRegex == null) continue | ||
|
||
if (globRegex.dir && !isDir) continue | ||
|
||
if (globRegex.regex.test(filePath)) { | ||
if (globRegex.dir && isDir) { | ||
return !globRegex.negation | ||
} | ||
result = !globRegex.negation | ||
} | ||
} | ||
return result | ||
} | ||
|
||
module.exports = { glob, toGlobRegex, filter } |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
const glob = require('../src/glob') | ||
const os = require('os') | ||
const fs = require('fs') | ||
|
||
beforeEach(() => { | ||
os.tmpdir = jest.fn().mockImplementation(() => '/tmp') | ||
fs.mkdirSync('/tmp') | ||
fs.mkdirSync('/app_to_zip') | ||
fs.writeFileSync('/app_to_zip/.delete_me', '1') | ||
fs.writeFileSync('/app_to_zip/.pgb_dont_delete_me', '22') | ||
fs.writeFileSync('/app_to_zip/index.html', '333') | ||
fs.writeFileSync('/app_to_zip/cordova.js', '4444') | ||
fs.mkdirSync('/app_to_zip/res') | ||
}) | ||
|
||
afterEach(() => { | ||
fs.rmdirSync('/tmp') | ||
fs.unlinkSync('/app_to_zip/.delete_me') | ||
fs.unlinkSync('/app_to_zip/.pgb_dont_delete_me') | ||
fs.unlinkSync('/app_to_zip/index.html') | ||
fs.unlinkSync('/app_to_zip/cordova.js') | ||
fs.rmdirSync('/app_to_zip/res') | ||
fs.rmdirSync('/app_to_zip') | ||
}) | ||
|
||
describe('glob', () => { | ||
test('should return files', () => { | ||
let result = { | ||
list: [ | ||
{ 'path': '/app_to_zip/.pgb_dont_delete_me', 'size': 2 }, | ||
{ 'path': '/app_to_zip/cordova.js', 'size': 4 }, | ||
{ 'path': '/app_to_zip/index.html', 'size': 3 }, | ||
{ 'path': '/app_to_zip/res', 'size': 0 } | ||
], | ||
skipped: [ '/app_to_zip/.delete_me [HIDDEN]' ] | ||
} | ||
expect(glob.glob('/app_to_zip')).toEqual(result) | ||
}) | ||
|
||
test('should skip if file cant be read', () => { | ||
let error = new Error('bad file') | ||
error.code = 'ENOENT' | ||
let oldOpenSync = fs.openSync | ||
fs.openSync = jest.fn().mockImplementation(() => { throw error }) | ||
let result = { | ||
skipped: [ | ||
'/app_to_zip/.delete_me [HIDDEN]', | ||
'/app_to_zip/.pgb_dont_delete_me [ENOENT]', | ||
'/app_to_zip/cordova.js [ENOENT]', | ||
'/app_to_zip/index.html [ENOENT]', | ||
'/app_to_zip/res [ENOENT]' | ||
], | ||
list: [ ] | ||
} | ||
expect(glob.glob('/app_to_zip')).toEqual(result) | ||
fs.openSync = oldOpenSync | ||
}) | ||
|
||
test('should skip ignored files', () => { | ||
let result = { | ||
list: [ | ||
{ 'path': '/app_to_zip/.pgb_dont_delete_me', 'size': 2 }, | ||
{ 'path': '/app_to_zip/cordova.js', 'size': 4 } | ||
], | ||
skipped: [ '/app_to_zip/.delete_me [HIDDEN]', '/app_to_zip/index.html [IGNORED]', '/app_to_zip/res/ [IGNORED]' ] | ||
} | ||
expect(glob.glob('/app_to_zip', [ '**/*.html', 'res/' ])).toEqual(result) | ||
}) | ||
}) | ||
|
||
describe('toGlobRegex', () => { | ||
test('should skip ignored files', () => { | ||
expect(glob.toGlobRegex('')).toEqual(null) | ||
expect(glob.toGlobRegex(null)).toEqual(null) | ||
expect(glob.toGlobRegex('# comment')).toEqual(null) | ||
expect(glob.toGlobRegex('shell')).toEqual({ 'dir': false, 'negation': false, 'regex': /^shell\/?$/ }) | ||
expect(glob.toGlobRegex('dir/')).toEqual({ 'dir': true, 'negation': false, 'regex': /^dir\/?$/ }) | ||
expect(glob.toGlobRegex('dir/**')).toEqual({ 'dir': false, 'negation': false, 'regex': /^dir\/.*$/ }) | ||
expect(glob.toGlobRegex('/dir')).toEqual({ 'dir': false, 'negation': false, 'regex': /^dir\/?$/ }) | ||
expect(glob.toGlobRegex('!not_me')).toEqual({ 'dir': false, 'negation': true, 'regex': /^not_me\/?$/ }) | ||
expect(glob.toGlobRegex('(.moot?[]+)')).toEqual({ 'dir': false, 'negation': false, 'regex': /^\(\.moot.\[\]\+\)\/?$/ }) | ||
}) | ||
}) | ||
|
||
describe('filter', () => { | ||
test('should skip ignored files', () => { | ||
expect(glob.filter('', true, [])).toEqual(false) | ||
expect(glob.filter('', true, [null])).toEqual(false) | ||
expect(glob.filter('rabbit', true, [{ dir: true, negation: false, regex: /^rabbit\/?$/ }])).toEqual(true) | ||
expect(glob.filter('rabbit', true, [{ dir: false, negation: false, regex: /^rabbit\/?$/ }])).toEqual(true) | ||
expect(glob.filter('rabbit', true, [{ dir: true, negation: true, regex: /^rabbit$/ }])).toEqual(false) | ||
expect(glob.filter('rabbit', false, [{ dir: true, negation: false, regex: /^rabbit\/?$/ }])).toEqual(false) | ||
}) | ||
}) |
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
Oops, something went wrong.