UNPKG

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