|
1 | 1 | /*
|
2 |
| - * Copyright 2018-19 IBM Corporation |
| 2 | + * Copyright 2018-21 IBM Corporation |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -39,57 +39,62 @@ export const fstat = ({
|
39 | 39 | argvNoOptions,
|
40 | 40 | parsedOptions
|
41 | 41 | }: Pick<Arguments<FStatOptions>, 'argvNoOptions' | 'parsedOptions'>) => {
|
42 |
| - return new Promise<FStat>((resolve, reject) => { |
43 |
| - const filepath = argvNoOptions[1] |
| 42 | + const filepath = argvNoOptions[1] |
| 43 | + const { resolved: fullpath, viewer = 'open' } = findFileWithViewer(expandHomeDir(filepath)) |
44 | 44 |
|
45 |
| - const { resolved: fullpath, viewer = 'open' } = findFileWithViewer(expandHomeDir(filepath)) |
| 45 | + const prettyFullPath = fullpath.replace(new RegExp(`^${process.env.HOME}`), '~') |
46 | 46 |
|
47 |
| - const prettyFullPath = fullpath.replace(new RegExp(`^${process.env.HOME}`), '~') |
| 47 | + return new Promise<FStat>((resolve, reject) => { |
| 48 | + const handleError = (err: CodedError<string>) => { |
| 49 | + if (err.code === 'ENOENT') { |
| 50 | + if (parsedOptions['enoent-ok']) { |
| 51 | + // file does not exist; caller told us that's ok |
| 52 | + resolve({ |
| 53 | + viewer, |
| 54 | + filepath, |
| 55 | + fullpath: prettyFullPath, |
| 56 | + isDirectory: false, |
| 57 | + data: '' |
| 58 | + }) |
| 59 | + } |
48 | 60 |
|
49 |
| - // note: stat not lstat, because we want to follow the link |
50 |
| - stat(fullpath, (err, stats) => { |
51 |
| - if (err) { |
52 |
| - if (err.code === 'ENOENT') { |
53 |
| - if (parsedOptions['enoent-ok']) { |
54 |
| - // file does not exist; caller told us that's ok |
55 |
| - resolve({ |
56 |
| - viewer, |
57 |
| - filepath, |
58 |
| - fullpath: prettyFullPath, |
59 |
| - isDirectory: false, |
60 |
| - data: '' |
61 |
| - }) |
62 |
| - } |
| 61 | + const error: CodedError = new Error(err.message) |
| 62 | + error.stack = err.stack |
| 63 | + error.code = 404 |
| 64 | + reject(error) |
| 65 | + } else { |
| 66 | + reject(err) |
| 67 | + } |
| 68 | + } |
63 | 69 |
|
64 |
| - const error: CodedError = new Error(err.message) |
65 |
| - error.stack = err.stack |
66 |
| - error.code = 404 |
67 |
| - reject(error) |
| 70 | + if (parsedOptions['with-data']) { |
| 71 | + readFile(fullpath, (err, data) => { |
| 72 | + if (err) { |
| 73 | + return handleError(err) |
68 | 74 | } else {
|
69 |
| - reject(err) |
| 75 | + resolve({ |
| 76 | + viewer, |
| 77 | + filepath, |
| 78 | + fullpath: prettyFullPath, |
| 79 | + data: data.toString(), |
| 80 | + isDirectory: false |
| 81 | + }) |
70 | 82 | }
|
71 |
| - } else if (stats.isDirectory() || !parsedOptions['with-data']) { |
72 |
| - resolve({ |
73 |
| - viewer, |
74 |
| - filepath, |
75 |
| - fullpath: prettyFullPath, |
76 |
| - isDirectory: stats.isDirectory() |
77 |
| - }) |
78 |
| - } else { |
79 |
| - readFile(fullpath, (err, data) => { |
80 |
| - if (err) { |
81 |
| - reject(err) |
82 |
| - } else { |
83 |
| - resolve({ |
84 |
| - viewer, |
85 |
| - filepath, |
86 |
| - fullpath: prettyFullPath, |
87 |
| - data: data.toString(), |
88 |
| - isDirectory: false |
89 |
| - }) |
90 |
| - } |
91 |
| - }) |
92 |
| - } |
93 |
| - }) |
| 83 | + }) |
| 84 | + } else { |
| 85 | + // note: stat not lstat, because we want to follow the link |
| 86 | + stat(fullpath, (err, stats) => { |
| 87 | + if (err) { |
| 88 | + return handleError(err) |
| 89 | + } else { |
| 90 | + resolve({ |
| 91 | + viewer, |
| 92 | + filepath, |
| 93 | + fullpath: prettyFullPath, |
| 94 | + isDirectory: stats.isDirectory() |
| 95 | + }) |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
94 | 99 | })
|
95 | 100 | }
|
0 commit comments