UNPKG

8.09 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 write = require("./write");
21
22// The Jetpack Context object.
23// It provides the public API, and resolves all paths regarding to
24// passed cwdPath, or default process.cwd() if cwdPath was not specified.
25const jetpackContext = cwdPath => {
26 const getCwdPath = () => {
27 return cwdPath || process.cwd();
28 };
29
30 const cwd = function() {
31 // return current CWD if no arguments specified...
32 if (arguments.length === 0) {
33 return getCwdPath();
34 }
35
36 // ...create new CWD context otherwise
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 // resolves path to inner CWD path of this jetpack instance
43 const resolvePath = path => {
44 return pathUtil.resolve(getCwdPath(), path);
45 };
46
47 const getPath = function() {
48 // add CWD base path as first element of arguments array
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 // API
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 // startPath is optional parameter, if not specified move rest of params
131 // to proper places and default startPath to CWD.
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 // startPath is optional parameter, if not specified move rest of params
141 // to proper places and default startPath to CWD.
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 // If path not specified defaults to CWD
198 remove.sync(resolvePath(path || "."));
199 },
200 removeAsync: path => {
201 remove.validateInput("removeAsync", path);
202 // If path not specified defaults to CWD
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 // Without this console.log(jetpack) throws obscure error. Details:
236 // https://github.com/szwacz/fs-jetpack/issues/29
237 // https://nodejs.org/api/util.html#util_custom_inspection_functions_on_objects
238 api[util.inspect.custom] = () => {
239 return `[fs-jetpack CWD: ${getCwdPath()}]`;
240 };
241 }
242
243 return api;
244};
245
246module.exports = jetpackContext;