@rollup-extras/plugin-clean 1.0.3 → 1.2.0

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/dist/index.cjs CHANGED
@@ -4,6 +4,7 @@ var fs = require('fs/promises');
4
4
  var path = require('path');
5
5
  var logger = require('@niceties/logger');
6
6
  var options = require('@rollup-extras/utils/options');
7
+ var mutliConfigPluginBase = require('@rollup-extras/utils/mutli-config-plugin-base');
7
8
 
8
9
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
10
 
@@ -11,7 +12,9 @@ var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
11
12
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
12
13
 
13
14
  function index (options$1 = {}) {
14
- const deleted = new Map();
15
+ const inProgress = new Map();
16
+ const hasChildrenInProgress = new Map();
17
+ let deleted = false;
15
18
  const normalizedOptions = options.getOptions(options$1, {
16
19
  pluginName: '@rollup-extras/plugin-clean',
17
20
  deleteOnce: true,
@@ -20,17 +23,35 @@ function index (options$1 = {}) {
20
23
  }, 'targets');
21
24
  const { pluginName, deleteOnce, verbose, outputPlugin } = normalizedOptions;
22
25
  let { targets } = normalizedOptions;
23
- const pluginInstance = {
24
- name: pluginName
25
- };
26
+ const instance = mutliConfigPluginBase.multiConfigPluginBase(false, pluginName, cleanup);
27
+ const baseAddInstance = instance.api.addInstance;
28
+ const baseRenderStart = instance.renderStart;
26
29
  if (outputPlugin) {
27
- pluginInstance.renderStart = renderStart;
30
+ instance.renderStart = async function (options, inputOptions) {
31
+ baseRenderStart.call(this, options, inputOptions);
32
+ await renderStart(options);
33
+ };
28
34
  }
29
35
  else {
30
- pluginInstance.buildStart = buildStart;
31
- pluginInstance.options = optionsHook;
36
+ instance.buildStart = buildStart;
37
+ instance.options = optionsHook;
32
38
  }
33
- return pluginInstance;
39
+ instance.api.addInstance = () => {
40
+ const instance = baseAddInstance();
41
+ const baseRenderStart = instance.renderStart;
42
+ if (outputPlugin) {
43
+ instance.renderStart = async function (options, inputOptions) {
44
+ baseRenderStart.call(this, options, inputOptions);
45
+ await renderStart(options);
46
+ };
47
+ }
48
+ else {
49
+ instance.buildStart = buildStart;
50
+ instance.options = optionsHook;
51
+ }
52
+ return instance;
53
+ };
54
+ return instance;
34
55
  async function optionsHook(config) {
35
56
  var _a;
36
57
  if (!targets && config) {
@@ -40,11 +61,17 @@ function index (options$1 = {}) {
40
61
  return null;
41
62
  }
42
63
  async function buildStart() {
64
+ if (deleted) {
65
+ return;
66
+ }
43
67
  if (targets) {
44
68
  await Promise.all(targets.map(removeDir));
45
69
  }
46
70
  }
47
71
  async function renderStart(options) {
72
+ if (deleted) {
73
+ return;
74
+ }
48
75
  if (!targets) {
49
76
  if (options.dir) {
50
77
  await removeDir(options.dir);
@@ -56,11 +83,30 @@ function index (options$1 = {}) {
56
83
  }
57
84
  async function removeDir(dir) {
58
85
  const normalizedDir = normalizeSlash(path__default["default"].normalize(dir));
59
- if (deleteOnce && deleted.has(normalizedDir)) {
60
- return deleted.get(normalizedDir);
86
+ if (inProgress.has(normalizedDir)) {
87
+ return outputPlugin && inProgress.get(normalizedDir);
88
+ }
89
+ let removePromise;
90
+ let parentsInProgress;
91
+ if (hasChildrenInProgress.has(dir)) {
92
+ removePromise = Promise.resolve(hasChildrenInProgress.get(dir))
93
+ .then(() => doRemove(normalizedDir));
94
+ }
95
+ else if ((parentsInProgress = Array.from(parentDirs(dir)).filter(item => inProgress.has(item))).length > 0) {
96
+ return inProgress.get(parentsInProgress[0]);
97
+ }
98
+ else {
99
+ removePromise = doRemove(normalizedDir);
100
+ }
101
+ inProgress.set(normalizedDir, removePromise);
102
+ for (const parentDir of parentDirs(dir)) {
103
+ if (!hasChildrenInProgress.has(parentDir)) {
104
+ hasChildrenInProgress.set(parentDir, removePromise);
105
+ }
106
+ else {
107
+ hasChildrenInProgress.set(parentDir, Promise.all([removePromise, hasChildrenInProgress.get(parentDir)]));
108
+ }
61
109
  }
62
- const removePromise = doRemove(normalizedDir);
63
- deleted.set(normalizedDir, removePromise);
64
110
  return removePromise;
65
111
  }
66
112
  async function doRemove(normalizedDir) {
@@ -72,15 +118,25 @@ function index (options$1 = {}) {
72
118
  }
73
119
  catch (e) { // eslint-disable-line @typescript-eslint/no-explicit-any
74
120
  const loglevel = e['code'] === 'ENOENT' ? undefined : 2 /* warn */;
75
- logger$1.finish(`failed cleaning '${normalizedDir}'\n${e.stack}`, loglevel);
121
+ logger$1.finish(`failed cleaning '${normalizedDir}'`, loglevel, e);
76
122
  }
77
123
  }
124
+ function cleanup() {
125
+ inProgress.clear();
126
+ hasChildrenInProgress.clear();
127
+ deleted = deleteOnce;
128
+ }
78
129
  }
79
130
  function normalizeSlash(dir) {
80
131
  if (dir.endsWith('/')) {
81
132
  return `${dir.substring(0, dir.length - 1)}`;
82
133
  }
83
134
  return dir;
135
+ }
136
+ function* parentDirs(dir) {
137
+ while (((dir = path__default["default"].dirname(dir)) !== '.') && (dir !== '/')) {
138
+ yield dir;
139
+ }
84
140
  }
85
141
 
86
142
  module.exports = index;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,7 @@
1
1
  import { PluginHooks } from 'rollup';
2
2
  import { CleanPluginOptions } from './types';
3
- export default function (options?: CleanPluginOptions): Partial<PluginHooks>;
3
+ export default function (options?: CleanPluginOptions): Partial<PluginHooks> & {
4
+ api: {
5
+ addInstance(): void;
6
+ };
7
+ };
package/dist/index.mjs CHANGED
@@ -2,9 +2,12 @@ import fs from 'fs/promises';
2
2
  import path from 'path';
3
3
  import { createLogger } from '@niceties/logger';
4
4
  import { getOptions } from '@rollup-extras/utils/options';
5
+ import { multiConfigPluginBase } from '@rollup-extras/utils/mutli-config-plugin-base';
5
6
 
6
7
  function index (options = {}) {
7
- const deleted = new Map();
8
+ const inProgress = new Map();
9
+ const hasChildrenInProgress = new Map();
10
+ let deleted = false;
8
11
  const normalizedOptions = getOptions(options, {
9
12
  pluginName: '@rollup-extras/plugin-clean',
10
13
  deleteOnce: true,
@@ -13,17 +16,35 @@ function index (options = {}) {
13
16
  }, 'targets');
14
17
  const { pluginName, deleteOnce, verbose, outputPlugin } = normalizedOptions;
15
18
  let { targets } = normalizedOptions;
16
- const pluginInstance = {
17
- name: pluginName
18
- };
19
+ const instance = multiConfigPluginBase(false, pluginName, cleanup);
20
+ const baseAddInstance = instance.api.addInstance;
21
+ const baseRenderStart = instance.renderStart;
19
22
  if (outputPlugin) {
20
- pluginInstance.renderStart = renderStart;
23
+ instance.renderStart = async function (options, inputOptions) {
24
+ baseRenderStart.call(this, options, inputOptions);
25
+ await renderStart(options);
26
+ };
21
27
  }
22
28
  else {
23
- pluginInstance.buildStart = buildStart;
24
- pluginInstance.options = optionsHook;
29
+ instance.buildStart = buildStart;
30
+ instance.options = optionsHook;
25
31
  }
26
- return pluginInstance;
32
+ instance.api.addInstance = () => {
33
+ const instance = baseAddInstance();
34
+ const baseRenderStart = instance.renderStart;
35
+ if (outputPlugin) {
36
+ instance.renderStart = async function (options, inputOptions) {
37
+ baseRenderStart.call(this, options, inputOptions);
38
+ await renderStart(options);
39
+ };
40
+ }
41
+ else {
42
+ instance.buildStart = buildStart;
43
+ instance.options = optionsHook;
44
+ }
45
+ return instance;
46
+ };
47
+ return instance;
27
48
  async function optionsHook(config) {
28
49
  var _a;
29
50
  if (!targets && config) {
@@ -33,11 +54,17 @@ function index (options = {}) {
33
54
  return null;
34
55
  }
35
56
  async function buildStart() {
57
+ if (deleted) {
58
+ return;
59
+ }
36
60
  if (targets) {
37
61
  await Promise.all(targets.map(removeDir));
38
62
  }
39
63
  }
40
64
  async function renderStart(options) {
65
+ if (deleted) {
66
+ return;
67
+ }
41
68
  if (!targets) {
42
69
  if (options.dir) {
43
70
  await removeDir(options.dir);
@@ -49,11 +76,30 @@ function index (options = {}) {
49
76
  }
50
77
  async function removeDir(dir) {
51
78
  const normalizedDir = normalizeSlash(path.normalize(dir));
52
- if (deleteOnce && deleted.has(normalizedDir)) {
53
- return deleted.get(normalizedDir);
79
+ if (inProgress.has(normalizedDir)) {
80
+ return outputPlugin && inProgress.get(normalizedDir);
81
+ }
82
+ let removePromise;
83
+ let parentsInProgress;
84
+ if (hasChildrenInProgress.has(dir)) {
85
+ removePromise = Promise.resolve(hasChildrenInProgress.get(dir))
86
+ .then(() => doRemove(normalizedDir));
87
+ }
88
+ else if ((parentsInProgress = Array.from(parentDirs(dir)).filter(item => inProgress.has(item))).length > 0) {
89
+ return inProgress.get(parentsInProgress[0]);
90
+ }
91
+ else {
92
+ removePromise = doRemove(normalizedDir);
93
+ }
94
+ inProgress.set(normalizedDir, removePromise);
95
+ for (const parentDir of parentDirs(dir)) {
96
+ if (!hasChildrenInProgress.has(parentDir)) {
97
+ hasChildrenInProgress.set(parentDir, removePromise);
98
+ }
99
+ else {
100
+ hasChildrenInProgress.set(parentDir, Promise.all([removePromise, hasChildrenInProgress.get(parentDir)]));
101
+ }
54
102
  }
55
- const removePromise = doRemove(normalizedDir);
56
- deleted.set(normalizedDir, removePromise);
57
103
  return removePromise;
58
104
  }
59
105
  async function doRemove(normalizedDir) {
@@ -65,15 +111,25 @@ function index (options = {}) {
65
111
  }
66
112
  catch (e) { // eslint-disable-line @typescript-eslint/no-explicit-any
67
113
  const loglevel = e['code'] === 'ENOENT' ? undefined : 2 /* warn */;
68
- logger.finish(`failed cleaning '${normalizedDir}'\n${e.stack}`, loglevel);
114
+ logger.finish(`failed cleaning '${normalizedDir}'`, loglevel, e);
69
115
  }
70
116
  }
117
+ function cleanup() {
118
+ inProgress.clear();
119
+ hasChildrenInProgress.clear();
120
+ deleted = deleteOnce;
121
+ }
71
122
  }
72
123
  function normalizeSlash(dir) {
73
124
  if (dir.endsWith('/')) {
74
125
  return `${dir.substring(0, dir.length - 1)}`;
75
126
  }
76
127
  return dir;
128
+ }
129
+ function* parentDirs(dir) {
130
+ while (((dir = path.dirname(dir)) !== '.') && (dir !== '/')) {
131
+ yield dir;
132
+ }
77
133
  }
78
134
 
79
135
  export { index as default };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.3",
2
+ "version": "1.2.0",
3
3
  "license": "MIT",
4
4
  "name": "@rollup-extras/plugin-clean",
5
5
  "author": {
@@ -34,7 +34,8 @@
34
34
  "description": "Rollup plugin to clean a directory during build.",
35
35
  "keywords": [
36
36
  "rollup-plugin",
37
- "clean"
37
+ "clean",
38
+ "delete"
38
39
  ],
39
40
  "scripts": {
40
41
  "build": "pkgbld",
@@ -44,28 +45,28 @@
44
45
  },
45
46
  "devDependencies": {
46
47
  "@semantic-release/changelog": "6.0.1",
47
- "@semantic-release/commit-analyzer": "9.0.1",
48
+ "@semantic-release/commit-analyzer": "9.0.2",
48
49
  "@semantic-release/git": "10.0.1",
49
- "@semantic-release/npm": "8.0.2",
50
- "@semantic-release/release-notes-generator": "10.0.2",
51
- "@types/jest": "27.0.2",
52
- "@typescript-eslint/eslint-plugin": "5.3.0",
53
- "@typescript-eslint/parser": "5.3.0",
50
+ "@semantic-release/npm": "8.0.3",
51
+ "@semantic-release/release-notes-generator": "10.0.3",
52
+ "@types/jest": "27.0.3",
53
+ "@typescript-eslint/eslint-plugin": "5.8.1",
54
+ "@typescript-eslint/parser": "5.8.1",
54
55
  "conventional-changelog-angular": "5.0.13",
55
- "eslint": "8.1.0",
56
- "jest": "27.3.1",
57
- "semantic-release": "18.0.0",
56
+ "eslint": "8.5.0",
57
+ "jest": "27.4.5",
58
+ "semantic-release": "18.0.1",
58
59
  "semantic-release-monorepo": "7.0.5",
59
- "ts-jest": "27.0.7",
60
+ "ts-jest": "27.1.2",
60
61
  "typescript": "4.5.x",
61
62
  "update-monorepo-package-json": "0.2.0",
62
- "pkgbld": "1.1.1"
63
+ "pkgbld": "1.1.3"
63
64
  },
64
65
  "peerDependencies": {
65
- "rollup": "^2.60.2"
66
+ "rollup": "^2.60.1"
66
67
  },
67
68
  "dependencies": {
68
- "@niceties/logger": "^1.0.4",
69
- "@rollup-extras/utils": "^1.0.1"
69
+ "@niceties/logger": "^1.1.0",
70
+ "@rollup-extras/utils": "^1.2.0"
70
71
  }
71
72
  }