1 | var common = require('./common');
|
2 | var fs = require('fs');
|
3 |
|
4 | common.register('test', _test, {
|
5 | cmdOptions: {
|
6 | 'b': 'block',
|
7 | 'c': 'character',
|
8 | 'd': 'directory',
|
9 | 'e': 'exists',
|
10 | 'f': 'file',
|
11 | 'L': 'link',
|
12 | 'p': 'pipe',
|
13 | 'S': 'socket',
|
14 | },
|
15 | wrapOutput: false,
|
16 | allowGlobbing: false,
|
17 | });
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | function _test(options, path) {
|
43 | if (!path) common.error('no path given');
|
44 |
|
45 | var canInterpret = false;
|
46 | Object.keys(options).forEach(function (key) {
|
47 | if (options[key] === true) {
|
48 | canInterpret = true;
|
49 | }
|
50 | });
|
51 |
|
52 | if (!canInterpret) common.error('could not interpret expression');
|
53 |
|
54 | if (options.link) {
|
55 | try {
|
56 | return common.statNoFollowLinks(path).isSymbolicLink();
|
57 | } catch (e) {
|
58 | return false;
|
59 | }
|
60 | }
|
61 |
|
62 | if (!fs.existsSync(path)) return false;
|
63 |
|
64 | if (options.exists) return true;
|
65 |
|
66 | var stats = common.statFollowLinks(path);
|
67 |
|
68 | if (options.block) return stats.isBlockDevice();
|
69 |
|
70 | if (options.character) return stats.isCharacterDevice();
|
71 |
|
72 | if (options.directory) return stats.isDirectory();
|
73 |
|
74 | if (options.file) return stats.isFile();
|
75 |
|
76 |
|
77 | if (options.pipe) return stats.isFIFO();
|
78 |
|
79 |
|
80 | if (options.socket) return stats.isSocket();
|
81 |
|
82 |
|
83 | return false;
|
84 | }
|
85 | module.exports = _test;
|