@rollup-extras/plugin-clean 1.3.9 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/index.d.ts +20 -0
- package/index.d.ts.map +16 -0
- package/{dist/index.mjs → index.js} +89 -51
- package/package.json +13 -19
- package/dist/index.cjs +0 -136
- package/dist/index.d.ts +0 -7
- package/dist/types.d.ts +0 -7
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ Rollup plugin to clean a directory during the build.
|
|
|
4
4
|
|
|
5
5
|
Points:
|
|
6
6
|
|
|
7
|
-
- Uses fs.rm to remove directories
|
|
7
|
+
- Uses `fs.rm` (shipped with Node.js) to remove directories, with built-in retries
|
|
8
8
|
- Can be used with no configuration
|
|
9
9
|
- Runs once per directory by default (good for watch mode)
|
|
10
10
|
- A minimal amount of logs by default
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
declare module '@rollup-extras/plugin-clean' {
|
|
2
|
+
import type { Plugin } from 'rollup';
|
|
3
|
+
export default function _default(options?: CleanPluginOptions): Plugin & {
|
|
4
|
+
api: {
|
|
5
|
+
addInstance(): Plugin;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
export type CleanPluginOptionsObject = {
|
|
9
|
+
targets?: string | string[];
|
|
10
|
+
pluginName?: string;
|
|
11
|
+
deleteOnce?: boolean;
|
|
12
|
+
outputPlugin?: boolean;
|
|
13
|
+
verbose?: boolean;
|
|
14
|
+
};
|
|
15
|
+
export type CleanPluginOptions = CleanPluginOptionsObject | string | string[];
|
|
16
|
+
|
|
17
|
+
export {};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
ADDED
|
@@ -1,65 +1,93 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
import { rm } from 'node:fs/promises';
|
|
2
|
+
import { dirname, normalize } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { createLogger, LogLevel } from '@niceties/logger';
|
|
5
|
+
import { multiConfigPluginBase } from '@rollup-extras/utils/multi-config-plugin-base';
|
|
4
6
|
import { getOptions } from '@rollup-extras/utils/options';
|
|
5
|
-
import { multiConfigPluginBase } from '@rollup-extras/utils/mutli-config-plugin-base';
|
|
6
7
|
|
|
7
|
-
function
|
|
8
|
+
export default function (options = {}) {
|
|
9
|
+
|
|
8
10
|
const inProgress = new Map();
|
|
11
|
+
|
|
9
12
|
const hasChildrenInProgress = new Map();
|
|
10
13
|
let deleted = false;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
|
|
15
|
+
const normalizedOptions = getOptions(
|
|
16
|
+
options,
|
|
17
|
+
{
|
|
18
|
+
pluginName: '@rollup-extras/plugin-clean',
|
|
19
|
+
deleteOnce: true,
|
|
20
|
+
verbose: false,
|
|
21
|
+
outputPlugin: true,
|
|
22
|
+
},
|
|
23
|
+
'targets'
|
|
24
|
+
);
|
|
17
25
|
const { pluginName, deleteOnce, verbose, outputPlugin } = normalizedOptions;
|
|
18
26
|
let { targets } = normalizedOptions;
|
|
27
|
+
|
|
19
28
|
const instance = multiConfigPluginBase(false, pluginName, cleanup);
|
|
20
|
-
const baseAddInstance =
|
|
21
|
-
const baseRenderStart =
|
|
29
|
+
const baseAddInstance = (instance.api.addInstance);
|
|
30
|
+
const baseRenderStart =
|
|
31
|
+
(
|
|
32
|
+
(instance).renderStart
|
|
33
|
+
);
|
|
34
|
+
|
|
22
35
|
if (outputPlugin) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
36
|
+
|
|
37
|
+
instance.renderStart = async function (
|
|
38
|
+
outputOptions,
|
|
39
|
+
inputOptions
|
|
40
|
+
) {
|
|
41
|
+
baseRenderStart.call(this, outputOptions, inputOptions);
|
|
42
|
+
await renderStart(outputOptions);
|
|
26
43
|
};
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
44
|
+
} else {
|
|
29
45
|
instance.buildStart = buildStart;
|
|
30
46
|
instance.options = optionsHook;
|
|
31
47
|
}
|
|
48
|
+
|
|
32
49
|
instance.api.addInstance = () => {
|
|
33
50
|
const instance = baseAddInstance();
|
|
34
|
-
const baseRenderStart =
|
|
51
|
+
const baseRenderStart =
|
|
52
|
+
(
|
|
53
|
+
(instance).renderStart
|
|
54
|
+
);
|
|
55
|
+
|
|
35
56
|
if (outputPlugin) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
57
|
+
|
|
58
|
+
instance.renderStart = async function (
|
|
59
|
+
outputOptions,
|
|
60
|
+
inputOptions
|
|
61
|
+
) {
|
|
62
|
+
baseRenderStart.call(this, outputOptions, inputOptions);
|
|
63
|
+
await renderStart(outputOptions);
|
|
39
64
|
};
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
65
|
+
} else {
|
|
42
66
|
instance.buildStart = buildStart;
|
|
43
67
|
instance.options = optionsHook;
|
|
44
68
|
}
|
|
69
|
+
|
|
45
70
|
return instance;
|
|
46
71
|
};
|
|
72
|
+
|
|
47
73
|
return instance;
|
|
74
|
+
|
|
48
75
|
async function optionsHook(config) {
|
|
49
76
|
if (!targets && config) {
|
|
50
|
-
targets =
|
|
51
|
-
.filter(Boolean)
|
|
77
|
+
targets = (
|
|
78
|
+
(Array.isArray(config.output) ? config.output.map(item => item.dir) : [config.output?.dir]).filter(Boolean)
|
|
79
|
+
);
|
|
52
80
|
}
|
|
53
81
|
return null;
|
|
54
82
|
}
|
|
83
|
+
|
|
55
84
|
async function buildStart() {
|
|
56
85
|
if (deleted) {
|
|
57
86
|
return;
|
|
58
87
|
}
|
|
59
|
-
|
|
60
|
-
await Promise.all(targets.map(removeDir));
|
|
61
|
-
}
|
|
88
|
+
await Promise.all( (targets).map(removeDir));
|
|
62
89
|
}
|
|
90
|
+
|
|
63
91
|
async function renderStart(options) {
|
|
64
92
|
if (deleted) {
|
|
65
93
|
return;
|
|
@@ -68,67 +96,77 @@ function index (options = {}) {
|
|
|
68
96
|
if (options.dir) {
|
|
69
97
|
await removeDir(options.dir);
|
|
70
98
|
}
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
99
|
+
} else {
|
|
73
100
|
await Promise.all(targets.map(removeDir));
|
|
74
101
|
}
|
|
75
102
|
}
|
|
103
|
+
|
|
76
104
|
async function removeDir(dir) {
|
|
77
|
-
const normalizedDir = normalizeSlash(
|
|
105
|
+
const normalizedDir = normalizeSlash(normalize(dir));
|
|
78
106
|
if (inProgress.has(normalizedDir)) {
|
|
79
107
|
return outputPlugin && inProgress.get(normalizedDir);
|
|
80
108
|
}
|
|
109
|
+
|
|
81
110
|
let removePromise;
|
|
111
|
+
|
|
82
112
|
let parentsInProgress;
|
|
83
113
|
if (hasChildrenInProgress.has(dir)) {
|
|
84
|
-
removePromise = Promise.resolve(hasChildrenInProgress.get(dir))
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
else {
|
|
114
|
+
removePromise = Promise.resolve(hasChildrenInProgress.get(dir)).then(() => doRemove(normalizedDir));
|
|
115
|
+
} else {
|
|
116
|
+
parentsInProgress = Array.from(parentDirs(dir)).filter(item => inProgress.has(item));
|
|
117
|
+
if (parentsInProgress.length > 0) {
|
|
118
|
+
return inProgress.get( (parentsInProgress[0]));
|
|
119
|
+
}
|
|
91
120
|
removePromise = doRemove(normalizedDir);
|
|
92
121
|
}
|
|
93
122
|
inProgress.set(normalizedDir, removePromise);
|
|
94
123
|
for (const parentDir of parentDirs(dir)) {
|
|
95
124
|
if (!hasChildrenInProgress.has(parentDir)) {
|
|
96
125
|
hasChildrenInProgress.set(parentDir, removePromise);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
126
|
+
} else {
|
|
127
|
+
hasChildrenInProgress.set(
|
|
128
|
+
parentDir,
|
|
129
|
+
(
|
|
130
|
+
(Promise.all([removePromise, hasChildrenInProgress.get(parentDir)]))
|
|
131
|
+
)
|
|
132
|
+
);
|
|
100
133
|
}
|
|
101
134
|
}
|
|
102
135
|
return removePromise;
|
|
103
136
|
}
|
|
137
|
+
|
|
104
138
|
async function doRemove(normalizedDir) {
|
|
105
139
|
const logger = createLogger(pluginName);
|
|
106
140
|
try {
|
|
107
|
-
logger.start(`cleaning '${normalizedDir}'`, verbose ?
|
|
108
|
-
await
|
|
141
|
+
logger.start(`cleaning '${normalizedDir}'`, verbose ? LogLevel.info : LogLevel.verbose);
|
|
142
|
+
await rm(normalizedDir, { recursive: true });
|
|
109
143
|
logger.finish(`cleaned '${normalizedDir}'`);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
logger.finish(`failed cleaning '${normalizedDir}'`, loglevel, e);
|
|
144
|
+
} catch ( e) {
|
|
145
|
+
const loglevel = (e).code === 'ENOENT' ? undefined : LogLevel.warn;
|
|
146
|
+
logger.finish(`failed cleaning '${normalizedDir}'`, loglevel, (e));
|
|
114
147
|
}
|
|
115
148
|
}
|
|
149
|
+
|
|
116
150
|
function cleanup() {
|
|
117
151
|
inProgress.clear();
|
|
118
152
|
hasChildrenInProgress.clear();
|
|
119
153
|
deleted = deleteOnce;
|
|
120
154
|
}
|
|
121
155
|
}
|
|
156
|
+
|
|
122
157
|
function normalizeSlash(dir) {
|
|
123
158
|
if (dir.endsWith('/')) {
|
|
124
159
|
return `${dir.substring(0, dir.length - 1)}`;
|
|
125
160
|
}
|
|
126
161
|
return dir;
|
|
127
162
|
}
|
|
163
|
+
|
|
128
164
|
function* parentDirs(dir) {
|
|
129
|
-
|
|
165
|
+
for (;;) {
|
|
166
|
+
dir = dirname(dir);
|
|
167
|
+
if (dir === '.' || dir === '/') {
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
130
170
|
yield dir;
|
|
131
171
|
}
|
|
132
172
|
}
|
|
133
|
-
|
|
134
|
-
export { index as default };
|
package/package.json
CHANGED
|
@@ -1,25 +1,19 @@
|
|
|
1
1
|
{
|
|
2
|
-
"
|
|
3
|
-
"version": "1.3.9",
|
|
4
|
-
"name": "@rollup-extras/plugin-clean",
|
|
5
|
-
"description": "Rollup plugin to clean a directory during build.",
|
|
2
|
+
"version": "2.0.1",
|
|
6
3
|
"license": "MIT",
|
|
4
|
+
"name": "@rollup-extras/plugin-clean",
|
|
7
5
|
"author": "Konstantin Shutkin",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
6
|
+
"type": "module",
|
|
7
|
+
"types": "./index.d.ts",
|
|
10
8
|
"exports": {
|
|
11
9
|
".": {
|
|
12
|
-
"types": "./
|
|
13
|
-
"
|
|
14
|
-
"require": "./dist/index.cjs",
|
|
15
|
-
"default": "./dist/index.mjs"
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"default": "./index.js"
|
|
16
12
|
},
|
|
17
13
|
"./package.json": "./package.json"
|
|
18
14
|
},
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
"LICENSE"
|
|
22
|
-
],
|
|
15
|
+
"main": "./index.js",
|
|
16
|
+
"module": "./index.js",
|
|
23
17
|
"engines": {
|
|
24
18
|
"node": ">=15"
|
|
25
19
|
},
|
|
@@ -31,22 +25,22 @@
|
|
|
31
25
|
"bugs": "https://github.com/kshutkin/rollup-extras/issues",
|
|
32
26
|
"homepage": "https://github.com/kshutkin/rollup-extras/blob/main/plugin-clean/README.md",
|
|
33
27
|
"readme": "README.md",
|
|
28
|
+
"description": "Rollup plugin to clean a directory during build.",
|
|
34
29
|
"keywords": [
|
|
35
30
|
"rollup-plugin",
|
|
36
31
|
"clean",
|
|
37
32
|
"delete"
|
|
38
33
|
],
|
|
39
34
|
"dependencies": {
|
|
40
|
-
"@niceties/logger": "^
|
|
41
|
-
"@rollup-extras/utils": "^
|
|
35
|
+
"@niceties/logger": "^2.1.0",
|
|
36
|
+
"@rollup-extras/utils": "^2.0.0"
|
|
42
37
|
},
|
|
43
38
|
"peerDependencies": {
|
|
44
|
-
"rollup": "^
|
|
39
|
+
"rollup": "^4.0.0"
|
|
45
40
|
},
|
|
46
41
|
"peerDependenciesMeta": {
|
|
47
42
|
"rollup": {
|
|
48
43
|
"optional": true
|
|
49
44
|
}
|
|
50
|
-
}
|
|
51
|
-
"types": "./dist/index.d.ts"
|
|
45
|
+
}
|
|
52
46
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var fs = require('fs/promises');
|
|
4
|
-
var path = require('path');
|
|
5
|
-
var logger = require('@niceties/logger');
|
|
6
|
-
var options = require('@rollup-extras/utils/options');
|
|
7
|
-
var mutliConfigPluginBase = require('@rollup-extras/utils/mutli-config-plugin-base');
|
|
8
|
-
|
|
9
|
-
function index (options$1 = {}) {
|
|
10
|
-
const inProgress = new Map();
|
|
11
|
-
const hasChildrenInProgress = new Map();
|
|
12
|
-
let deleted = false;
|
|
13
|
-
const normalizedOptions = options.getOptions(options$1, {
|
|
14
|
-
pluginName: '@rollup-extras/plugin-clean',
|
|
15
|
-
deleteOnce: true,
|
|
16
|
-
verbose: false,
|
|
17
|
-
outputPlugin: true
|
|
18
|
-
}, 'targets');
|
|
19
|
-
const { pluginName, deleteOnce, verbose, outputPlugin } = normalizedOptions;
|
|
20
|
-
let { targets } = normalizedOptions;
|
|
21
|
-
const instance = mutliConfigPluginBase.multiConfigPluginBase(false, pluginName, cleanup);
|
|
22
|
-
const baseAddInstance = instance.api.addInstance;
|
|
23
|
-
const baseRenderStart = instance.renderStart;
|
|
24
|
-
if (outputPlugin) {
|
|
25
|
-
instance.renderStart = async function (options, inputOptions) {
|
|
26
|
-
baseRenderStart.call(this, options, inputOptions);
|
|
27
|
-
await renderStart(options);
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
instance.buildStart = buildStart;
|
|
32
|
-
instance.options = optionsHook;
|
|
33
|
-
}
|
|
34
|
-
instance.api.addInstance = () => {
|
|
35
|
-
const instance = baseAddInstance();
|
|
36
|
-
const baseRenderStart = instance.renderStart;
|
|
37
|
-
if (outputPlugin) {
|
|
38
|
-
instance.renderStart = async function (options, inputOptions) {
|
|
39
|
-
baseRenderStart.call(this, options, inputOptions);
|
|
40
|
-
await renderStart(options);
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
instance.buildStart = buildStart;
|
|
45
|
-
instance.options = optionsHook;
|
|
46
|
-
}
|
|
47
|
-
return instance;
|
|
48
|
-
};
|
|
49
|
-
return instance;
|
|
50
|
-
async function optionsHook(config) {
|
|
51
|
-
if (!targets && config) {
|
|
52
|
-
targets = (Array.isArray(config.output) ? config.output.map(item => item.dir) : [config.output?.dir])
|
|
53
|
-
.filter(Boolean);
|
|
54
|
-
}
|
|
55
|
-
return null;
|
|
56
|
-
}
|
|
57
|
-
async function buildStart() {
|
|
58
|
-
if (deleted) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
if (targets) {
|
|
62
|
-
await Promise.all(targets.map(removeDir));
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
async function renderStart(options) {
|
|
66
|
-
if (deleted) {
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
if (!targets) {
|
|
70
|
-
if (options.dir) {
|
|
71
|
-
await removeDir(options.dir);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
await Promise.all(targets.map(removeDir));
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
async function removeDir(dir) {
|
|
79
|
-
const normalizedDir = normalizeSlash(path.normalize(dir));
|
|
80
|
-
if (inProgress.has(normalizedDir)) {
|
|
81
|
-
return outputPlugin && inProgress.get(normalizedDir);
|
|
82
|
-
}
|
|
83
|
-
let removePromise;
|
|
84
|
-
let parentsInProgress;
|
|
85
|
-
if (hasChildrenInProgress.has(dir)) {
|
|
86
|
-
removePromise = Promise.resolve(hasChildrenInProgress.get(dir))
|
|
87
|
-
.then(() => doRemove(normalizedDir));
|
|
88
|
-
}
|
|
89
|
-
else if ((parentsInProgress = Array.from(parentDirs(dir)).filter(item => inProgress.has(item))).length > 0) {
|
|
90
|
-
return inProgress.get(parentsInProgress[0]);
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
removePromise = doRemove(normalizedDir);
|
|
94
|
-
}
|
|
95
|
-
inProgress.set(normalizedDir, removePromise);
|
|
96
|
-
for (const parentDir of parentDirs(dir)) {
|
|
97
|
-
if (!hasChildrenInProgress.has(parentDir)) {
|
|
98
|
-
hasChildrenInProgress.set(parentDir, removePromise);
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
hasChildrenInProgress.set(parentDir, Promise.all([removePromise, hasChildrenInProgress.get(parentDir)]));
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return removePromise;
|
|
105
|
-
}
|
|
106
|
-
async function doRemove(normalizedDir) {
|
|
107
|
-
const logger$1 = logger.createLogger(pluginName);
|
|
108
|
-
try {
|
|
109
|
-
logger$1.start(`cleaning '${normalizedDir}'`, verbose ? 1 /* LogLevel.info */ : 0 /* LogLevel.verbose */);
|
|
110
|
-
await fs.rm(normalizedDir, { recursive: true });
|
|
111
|
-
logger$1.finish(`cleaned '${normalizedDir}'`);
|
|
112
|
-
}
|
|
113
|
-
catch (e) { // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
114
|
-
const loglevel = e['code'] === 'ENOENT' ? undefined : 2 /* LogLevel.warn */;
|
|
115
|
-
logger$1.finish(`failed cleaning '${normalizedDir}'`, loglevel, e);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
function cleanup() {
|
|
119
|
-
inProgress.clear();
|
|
120
|
-
hasChildrenInProgress.clear();
|
|
121
|
-
deleted = deleteOnce;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
function normalizeSlash(dir) {
|
|
125
|
-
if (dir.endsWith('/')) {
|
|
126
|
-
return `${dir.substring(0, dir.length - 1)}`;
|
|
127
|
-
}
|
|
128
|
-
return dir;
|
|
129
|
-
}
|
|
130
|
-
function* parentDirs(dir) {
|
|
131
|
-
while (((dir = path.dirname(dir)) !== '.') && (dir !== '/')) {
|
|
132
|
-
yield dir;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
module.exports = index;
|
package/dist/index.d.ts
DELETED