1 | "use strict";
|
2 |
|
3 | const util = require("util");
|
4 | const pathUtil = require("path");
|
5 | const append = require("./append");
|
6 | const dir = require("./dir");
|
7 | const file = require("./file");
|
8 | const find = require("./find");
|
9 | const inspect = require("./inspect");
|
10 | const inspectTree = require("./inspect_tree");
|
11 | const copy = require("./copy");
|
12 | const exists = require("./exists");
|
13 | const list = require("./list");
|
14 | const move = require("./move");
|
15 | const read = require("./read");
|
16 | const remove = require("./remove");
|
17 | const rename = require("./rename");
|
18 | const symlink = require("./symlink");
|
19 | const streams = require("./streams");
|
20 | const write = require("./write");
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | const jetpackContext = cwdPath => {
|
26 | const getCwdPath = () => {
|
27 | return cwdPath || process.cwd();
|
28 | };
|
29 |
|
30 | const cwd = function() {
|
31 |
|
32 | if (arguments.length === 0) {
|
33 | return getCwdPath();
|
34 | }
|
35 |
|
36 |
|
37 | const args = Array.prototype.slice.call(arguments);
|
38 | const pathParts = [getCwdPath()].concat(args);
|
39 | return jetpackContext(pathUtil.resolve.apply(null, pathParts));
|
40 | };
|
41 |
|
42 |
|
43 | const resolvePath = path => {
|
44 | return pathUtil.resolve(getCwdPath(), path);
|
45 | };
|
46 |
|
47 | const getPath = function() {
|
48 |
|
49 | Array.prototype.unshift.call(arguments, getCwdPath());
|
50 | return pathUtil.resolve.apply(null, arguments);
|
51 | };
|
52 |
|
53 | const normalizeOptions = options => {
|
54 | const opts = options || {};
|
55 | opts.cwd = getCwdPath();
|
56 | return opts;
|
57 | };
|
58 |
|
59 |
|
60 |
|
61 | const api = {
|
62 | cwd,
|
63 | path: getPath,
|
64 |
|
65 | append: (path, data, options) => {
|
66 | append.validateInput("append", path, data, options);
|
67 | append.sync(resolvePath(path), data, options);
|
68 | },
|
69 | appendAsync: (path, data, options) => {
|
70 | append.validateInput("appendAsync", path, data, options);
|
71 | return append.async(resolvePath(path), data, options);
|
72 | },
|
73 |
|
74 | copy: (from, to, options) => {
|
75 | copy.validateInput("copy", from, to, options);
|
76 | copy.sync(resolvePath(from), resolvePath(to), options);
|
77 | },
|
78 | copyAsync: (from, to, options) => {
|
79 | copy.validateInput("copyAsync", from, to, options);
|
80 | return copy.async(resolvePath(from), resolvePath(to), options);
|
81 | },
|
82 |
|
83 | createWriteStream: (path, options) => {
|
84 | return streams.createWriteStream(resolvePath(path), options);
|
85 | },
|
86 | createReadStream: (path, options) => {
|
87 | return streams.createReadStream(resolvePath(path), options);
|
88 | },
|
89 |
|
90 | dir: (path, criteria) => {
|
91 | dir.validateInput("dir", path, criteria);
|
92 | const normalizedPath = resolvePath(path);
|
93 | dir.sync(normalizedPath, criteria);
|
94 | return cwd(normalizedPath);
|
95 | },
|
96 | dirAsync: (path, criteria) => {
|
97 | dir.validateInput("dirAsync", path, criteria);
|
98 | return new Promise((resolve, reject) => {
|
99 | const normalizedPath = resolvePath(path);
|
100 | dir.async(normalizedPath, criteria).then(() => {
|
101 | resolve(cwd(normalizedPath));
|
102 | }, reject);
|
103 | });
|
104 | },
|
105 |
|
106 | exists: path => {
|
107 | exists.validateInput("exists", path);
|
108 | return exists.sync(resolvePath(path));
|
109 | },
|
110 | existsAsync: path => {
|
111 | exists.validateInput("existsAsync", path);
|
112 | return exists.async(resolvePath(path));
|
113 | },
|
114 |
|
115 | file: (path, criteria) => {
|
116 | file.validateInput("file", path, criteria);
|
117 | file.sync(resolvePath(path), criteria);
|
118 | return api;
|
119 | },
|
120 | fileAsync: (path, criteria) => {
|
121 | file.validateInput("fileAsync", path, criteria);
|
122 | return new Promise((resolve, reject) => {
|
123 | file.async(resolvePath(path), criteria).then(() => {
|
124 | resolve(api);
|
125 | }, reject);
|
126 | });
|
127 | },
|
128 |
|
129 | find: (startPath, options) => {
|
130 |
|
131 |
|
132 | if (typeof options === "undefined" && typeof startPath === "object") {
|
133 | options = startPath;
|
134 | startPath = ".";
|
135 | }
|
136 | find.validateInput("find", startPath, options);
|
137 | return find.sync(resolvePath(startPath), normalizeOptions(options));
|
138 | },
|
139 | findAsync: (startPath, options) => {
|
140 |
|
141 |
|
142 | if (typeof options === "undefined" && typeof startPath === "object") {
|
143 | options = startPath;
|
144 | startPath = ".";
|
145 | }
|
146 | find.validateInput("findAsync", startPath, options);
|
147 | return find.async(resolvePath(startPath), normalizeOptions(options));
|
148 | },
|
149 |
|
150 | inspect: (path, fieldsToInclude) => {
|
151 | inspect.validateInput("inspect", path, fieldsToInclude);
|
152 | return inspect.sync(resolvePath(path), fieldsToInclude);
|
153 | },
|
154 | inspectAsync: (path, fieldsToInclude) => {
|
155 | inspect.validateInput("inspectAsync", path, fieldsToInclude);
|
156 | return inspect.async(resolvePath(path), fieldsToInclude);
|
157 | },
|
158 |
|
159 | inspectTree: (path, options) => {
|
160 | inspectTree.validateInput("inspectTree", path, options);
|
161 | return inspectTree.sync(resolvePath(path), options);
|
162 | },
|
163 | inspectTreeAsync: (path, options) => {
|
164 | inspectTree.validateInput("inspectTreeAsync", path, options);
|
165 | return inspectTree.async(resolvePath(path), options);
|
166 | },
|
167 |
|
168 | list: path => {
|
169 | list.validateInput("list", path);
|
170 | return list.sync(resolvePath(path || "."));
|
171 | },
|
172 | listAsync: path => {
|
173 | list.validateInput("listAsync", path);
|
174 | return list.async(resolvePath(path || "."));
|
175 | },
|
176 |
|
177 | move: (from, to) => {
|
178 | move.validateInput("move", from, to);
|
179 | move.sync(resolvePath(from), resolvePath(to));
|
180 | },
|
181 | moveAsync: (from, to) => {
|
182 | move.validateInput("moveAsync", from, to);
|
183 | return move.async(resolvePath(from), resolvePath(to));
|
184 | },
|
185 |
|
186 | read: (path, returnAs) => {
|
187 | read.validateInput("read", path, returnAs);
|
188 | return read.sync(resolvePath(path), returnAs);
|
189 | },
|
190 | readAsync: (path, returnAs) => {
|
191 | read.validateInput("readAsync", path, returnAs);
|
192 | return read.async(resolvePath(path), returnAs);
|
193 | },
|
194 |
|
195 | remove: path => {
|
196 | remove.validateInput("remove", path);
|
197 |
|
198 | remove.sync(resolvePath(path || "."));
|
199 | },
|
200 | removeAsync: path => {
|
201 | remove.validateInput("removeAsync", path);
|
202 |
|
203 | return remove.async(resolvePath(path || "."));
|
204 | },
|
205 |
|
206 | rename: (path, newName) => {
|
207 | rename.validateInput("rename", path, newName);
|
208 | rename.sync(resolvePath(path), newName);
|
209 | },
|
210 | renameAsync: (path, newName) => {
|
211 | rename.validateInput("renameAsync", path, newName);
|
212 | return rename.async(resolvePath(path), newName);
|
213 | },
|
214 |
|
215 | symlink: (symlinkValue, path) => {
|
216 | symlink.validateInput("symlink", symlinkValue, path);
|
217 | symlink.sync(symlinkValue, resolvePath(path));
|
218 | },
|
219 | symlinkAsync: (symlinkValue, path) => {
|
220 | symlink.validateInput("symlinkAsync", symlinkValue, path);
|
221 | return symlink.async(symlinkValue, resolvePath(path));
|
222 | },
|
223 |
|
224 | write: (path, data, options) => {
|
225 | write.validateInput("write", path, data, options);
|
226 | write.sync(resolvePath(path), data, options);
|
227 | },
|
228 | writeAsync: (path, data, options) => {
|
229 | write.validateInput("writeAsync", path, data, options);
|
230 | return write.async(resolvePath(path), data, options);
|
231 | }
|
232 | };
|
233 |
|
234 | if (util.inspect.custom !== undefined) {
|
235 |
|
236 |
|
237 |
|
238 | api[util.inspect.custom] = () => {
|
239 | return `[fs-jetpack CWD: ${getCwdPath()}]`;
|
240 | };
|
241 | }
|
242 |
|
243 | return api;
|
244 | };
|
245 |
|
246 | module.exports = jetpackContext;
|