UNPKG

1.34 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) => {
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// Sync
21// ---------------------------------------------------------
22
23const renameSync = (path, newName) => {
24 const newPath = pathUtil.join(pathUtil.dirname(path), newName);
25 move.sync(path, newPath);
26};
27
28// ---------------------------------------------------------
29// Async
30// ---------------------------------------------------------
31
32const renameAsync = (path, newName) => {
33 const newPath = pathUtil.join(pathUtil.dirname(path), newName);
34 return move.async(path, newPath);
35};
36
37// ---------------------------------------------------------
38// API
39// ---------------------------------------------------------
40
41exports.validateInput = validateInput;
42exports.sync = renameSync;
43exports.async = renameAsync;