UNPKG

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