-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (53 loc) · 1.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const fs = require('fs');
const path = require('path');
const util = require('util');
const del = require('del');
const { promisify } = util;
const _access = promisify(fs.access);
const fileExists = (path) =>
_access(path).then(
() => true,
() => false
);
const getFullData = (data, cfg) => Object.assign({}, cfg.data, data);
const normalizePath = (path) => {
return !path.sep || path.sep === '\\' ? path.replace(/\\/g, '/') : path;
};
const getRelativeToBasePath = (filePath, plop) =>
filePath.replace(path.resolve(plop.getDestBasePath()), '');
const makePath = (data, cfg, plop) => {
return path.resolve(
plop.getDestBasePath(),
plop.renderString(normalizePath(cfg.path) || '', getFullData(data, cfg))
);
};
async function remove(data, cfg, plop) {
const { force, skipIfNonexistent = false } = cfg;
const filePath = makePath(data, cfg, plop);
const exists = await fileExists(filePath);
if (!exists) {
if (skipIfNonexistent) {
return `[SKIPPED] ${filePath} (does not exist)`;
}
throw `File already does not exists (set skipIfNonexistent to true to ignore this error)\n -> ${fileDestPath}`;
} else {
const deletedFilePaths = await del([filePath], { force });
return `Deleted files:\n -> ${deletedFilePaths
.map((filePath) => getRelativeToBasePath(filePath, plop))
.join('\n -> ')}`;
}
}
async function removeMany(data, cfg, plop) {
const { path, force } = cfg;
const deletedFilePaths = await del(Array.isArray(path) ? path : [path], {
force,
});
return `Deleted files:\n -> ${deletedFilePaths
.map((filePath) => getRelativeToBasePath(filePath, plop))
.join('\n -> ')}`;
}
module.exports = function (plop) {
plop.setDefaultInclude({ actionTypes: true });
plop.setActionType('remove', remove);
plop.setActionType('removeMany', removeMany);
};