-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3823 from imarakho/flag--ext
added --ext flag
- Loading branch information
Showing
4 changed files
with
98 additions
and
0 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,46 @@ | ||
var fs = require('fs'); | ||
var conf = require('../../../constants.js'); | ||
|
||
function find_extensions(folder, ext, ret) | ||
{ | ||
try { | ||
fs.accessSync(folder, fs.constants.R_OK); | ||
} catch (err) { | ||
return; | ||
} | ||
if(fs.statSync(folder).isDirectory() && folder.indexOf('node_modules') == -1 && (fs.statSync(folder)["mode"] & 4)) | ||
{ | ||
fs.readdirSync(folder).forEach(file => { | ||
var tmp; | ||
if(Number.parseInt(folder.lastIndexOf('/') + 1) === folder.length) | ||
tmp = folder + file; | ||
else | ||
tmp = folder + '/' + file; | ||
if(fs.statSync(tmp).isDirectory()) | ||
find_extensions(tmp, ext, ret); | ||
else | ||
{ | ||
var p = true; | ||
for(var i = 0; i < ext.length;i++) | ||
if(ext[i].test(file)) | ||
p = false; | ||
if(p) | ||
ret.push(folder + '/' + file); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
module.exports.make_available_extension = function make_available_extension(opts, ret) | ||
{ | ||
if(typeof opts == 'object' && typeof ret == 'object') | ||
{ | ||
var mas = opts.ext.split(','); | ||
for(var i = 0;i < mas.length;i++) | ||
mas[i] = '.' + mas[i]; | ||
var res = []; | ||
for(var i = 0;i < mas.length;i++) | ||
res[i] = new RegExp(mas[i] + '$'); | ||
find_extensions(process.cwd(), res, ret); | ||
} | ||
} |
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,43 @@ | ||
|
||
var should = require('should'); | ||
var f_e = require('../../lib/API/Modules/flagExt.js'); | ||
var fs = require('fs'); | ||
|
||
describe('Flag -ext', function() { | ||
|
||
var opts = {}; | ||
var res = []; | ||
opts.ext = 'js,json'; | ||
|
||
it('should return not empty result', function() { | ||
f_e.make_available_extension(opts, res); | ||
should(res).be.not.empty(); | ||
}); | ||
it('should not crash', function() { | ||
f_e.make_available_extension(); | ||
f_e.make_available_extension(res); | ||
f_e.make_available_extension(opts); | ||
}); | ||
it('should give different results', function() { | ||
var tmp_res = []; | ||
f_e.make_available_extension(opts, res); | ||
opts.ext = 'sh,py'; | ||
f_e.make_available_extension(opts, tmp_res); | ||
should(res).not.equal(tmp_res); | ||
}); | ||
it('should not crash in case, when no access for file or directory by permissions', function() { | ||
var dir = fs.mkdirSync("noAccessDir", 0777); | ||
opts.ext = 'txt' | ||
var fileStream = fs.createWriteStream("noAccessDir/checkPermissions.txt"); | ||
fileStream.write("It's a temporary file for testing flag --ext in PM2"); | ||
fileStream.end(); | ||
fs.chmodSync('noAccessDir/checkPermissions.txt', 0000); | ||
fs.chmodSync('noAccessDir', 0000); | ||
f_e.make_available_extension(opts, []); | ||
f_e.make_available_extension(opts, []); | ||
fs.chmodSync('noAccessDir', 0777); | ||
fs.chmodSync('noAccessDir/checkPermissions.txt', 0777); | ||
fs.unlinkSync('noAccessDir/checkPermissions.txt'); | ||
fs.rmdirSync('noAccessDir/'); | ||
}); | ||
}); |