mirror of
https://gitee.com/github-actions/setup-node
synced 2024-04-27 22:27:08 +08:00
86 lines
1.9 KiB
JavaScript
86 lines
1.9 KiB
JavaScript
|
var common = require('./common');
|
||
|
var fs = require('fs');
|
||
|
|
||
|
//@
|
||
|
//@ ### test(expression)
|
||
|
//@ Available expression primaries:
|
||
|
//@
|
||
|
//@ + `'-b', 'path'`: true if path is a block device
|
||
|
//@ + `'-c', 'path'`: true if path is a character device
|
||
|
//@ + `'-d', 'path'`: true if path is a directory
|
||
|
//@ + `'-e', 'path'`: true if path exists
|
||
|
//@ + `'-f', 'path'`: true if path is a regular file
|
||
|
//@ + `'-L', 'path'`: true if path is a symboilc link
|
||
|
//@ + `'-p', 'path'`: true if path is a pipe (FIFO)
|
||
|
//@ + `'-S', 'path'`: true if path is a socket
|
||
|
//@
|
||
|
//@ Examples:
|
||
|
//@
|
||
|
//@ ```javascript
|
||
|
//@ if (test('-d', path)) { /* do something with dir */ };
|
||
|
//@ if (!test('-f', path)) continue; // skip if it's a regular file
|
||
|
//@ ```
|
||
|
//@
|
||
|
//@ Evaluates expression using the available primaries and returns corresponding value.
|
||
|
function _test(options, path) {
|
||
|
if (!path)
|
||
|
common.error('no path given');
|
||
|
|
||
|
// hack - only works with unary primaries
|
||
|
options = common.parseOptions(options, {
|
||
|
'b': 'block',
|
||
|
'c': 'character',
|
||
|
'd': 'directory',
|
||
|
'e': 'exists',
|
||
|
'f': 'file',
|
||
|
'L': 'link',
|
||
|
'p': 'pipe',
|
||
|
'S': 'socket'
|
||
|
});
|
||
|
|
||
|
var canInterpret = false;
|
||
|
for (var key in options)
|
||
|
if (options[key] === true) {
|
||
|
canInterpret = true;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if (!canInterpret)
|
||
|
common.error('could not interpret expression');
|
||
|
|
||
|
if (options.link) {
|
||
|
try {
|
||
|
return fs.lstatSync(path).isSymbolicLink();
|
||
|
} catch(e) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!fs.existsSync(path))
|
||
|
return false;
|
||
|
|
||
|
if (options.exists)
|
||
|
return true;
|
||
|
|
||
|
var stats = fs.statSync(path);
|
||
|
|
||
|
if (options.block)
|
||
|
return stats.isBlockDevice();
|
||
|
|
||
|
if (options.character)
|
||
|
return stats.isCharacterDevice();
|
||
|
|
||
|
if (options.directory)
|
||
|
return stats.isDirectory();
|
||
|
|
||
|
if (options.file)
|
||
|
return stats.isFile();
|
||
|
|
||
|
if (options.pipe)
|
||
|
return stats.isFIFO();
|
||
|
|
||
|
if (options.socket)
|
||
|
return stats.isSocket();
|
||
|
} // test
|
||
|
module.exports = _test;
|