1 | "use strict";
|
2 |
|
3 | const pathUtil = require("path");
|
4 | const move = require("./move");
|
5 | const validate = require("./utils/validate");
|
6 |
|
7 | const validateInput = (methodName, path, newName) => {
|
8 | const methodSignature = `${methodName}(path, newName)`;
|
9 | validate.argument(methodSignature, "path", path, ["string"]);
|
10 | validate.argument(methodSignature, "newName", newName, ["string"]);
|
11 |
|
12 | if (pathUtil.basename(newName) !== newName) {
|
13 | throw new Error(
|
14 | `Argument "newName" passed to ${methodSignature} should be a filename, not a path. Received "${newName}"`
|
15 | );
|
16 | }
|
17 | };
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | const renameSync = (path, newName) => {
|
24 | const newPath = pathUtil.join(pathUtil.dirname(path), newName);
|
25 | move.sync(path, newPath);
|
26 | };
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | const renameAsync = (path, newName) => {
|
33 | const newPath = pathUtil.join(pathUtil.dirname(path), newName);
|
34 | return move.async(path, newPath);
|
35 | };
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | exports.validateInput = validateInput;
|
42 | exports.sync = renameSync;
|
43 | exports.async = renameAsync;
|