Skip to content

Commit 47147d6

Browse files
committed
Add PreRun event hook option.
1 parent 00da097 commit 47147d6

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

README.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ const defaultOptions = {
9797
formatJson: false, // Format obfuscation data JSON file.
9898
showConfig: false, // Show config on terminal when runinng.
9999
keepData: true, // Keep or delete Data after obfuscation is finished?
100+
preRun: () => Promise.resolve(), // do something before the plugin runs.
100101
callBack: function () {}, // Callback function to call after obfuscation is done.
101102
};
102103
```
@@ -127,7 +128,8 @@ const defaultOptions = {
127128
- **`formatJson:`** Format obfuscation data JSON file, **default is false.**
128129
- **`showConfig:`** Show config on terminal when runinng, **default is false.**
129130
- **`keepData:`** Keep or delete data after obfuscation is finished? **default is true.**
130-
- **`callBack:`** Callback function to call after obfuscation is done. **default is an empty function**
131+
- **`preRun:`** Do something before the plugin runs. **default is a promise that immediately resolves.**
132+
- **`callBack:`** Callback function to call after obfuscation is done. **default is an empty function.**
131133

132134
## npm scripts example
133135

@@ -161,6 +163,13 @@ Then npm scripts can be something like this:
161163

162164
It's better to keep your source files as they are for easy development. Consider specifying another folder for the build, if you choose your build directory to be the same as the source directory you will be replaced and you will lose your original files.
163165

166+
### Support for CSS framworks?
167+
168+
It's designed to work with CSS, hence its supports any framework you can think of:
169+
- Tailwindcss.
170+
- Bootstrap.
171+
- Bulma.
172+
- ... .
164173
### Use indicators?
165174

166175
As mentioned this plugin uses Regex to replace all appearances of classes & ids on files with extensions you specify (be it html, cs, js, ...).
@@ -227,14 +236,20 @@ const isObfscMode = process.env.NODE_ENV === "obfuscation";
227236

228237
So basically you use `callBack` option to set the env mode back to `production` thus obfuscation will not run, and then config your app source folder to use `out` folder instead of `src` for production.
229238

230-
### Support for CSS framworks?
231-
232-
It's designed to work with CSS, hence its supports any framework you can think of:
233-
- Tailwindcss.
234-
- Bootstrap.
235-
- Bulma.
236-
- ... .
239+
### Run plugin after some tasks are finished?
237240

241+
You can use the `preRun` event hook option to delay the plugin until a certain task is finished or to perform some operation before the plugin runs. This option provides greater control over the timing of the plugin's execution and allows you to ensure that any necessary pre-processing or post-processing is completed before or after the plugin runs.
242+
```js
243+
preRun: () => new Promise(resolve => setTimeout(resolve, 10000)), // delay for 10s.
244+
```
245+
```js
246+
preRun: () => {
247+
return new Promise(resolve => {
248+
// some pre-tasks
249+
resolve();
250+
});
251+
}
252+
```
238253
### How To Use With?
239254
Are you struggling to use it with your tooling & stack environment? please create an Issue. We will be happy to help.
240255

@@ -328,7 +343,7 @@ group
328343
- Fix #9 update regex match exact wording.
329344
- Fix #10 obfuscation only works for indicators.
330345
- Set Indicators Start & End.
331-
- Add beforeRun event hook option.
346+
- Add PreRun event hook option.
332347
- Draft for orderJson option.
333348
- Discard draft: Force option (case: dev env or same Path).
334349
- Draft for framework option.

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const defaultOptions = {
4646
formatJson: false, // Format obfuscation data JSON file.
4747
showConfig: false, // Show config on terminal when runinng.
4848
keepData: true, // Keep or delete Data after obfuscation is finished?
49+
preRun: () => Promise.resolve(), // do something before the plugin runs.
4950
callBack: function () {}, // Callback function to call after obfuscation is done.
5051
};
5152
let data = {};
@@ -86,15 +87,17 @@ module.exports = (options = {}) => {
8687
formatJson,
8788
showConfig,
8889
keepData,
90+
preRun,
8991
callBack,
9092
} = { ...defaultOptions, ...options };
9193
return {
9294
postcssPlugin: pluginName,
93-
Once: (root, { result }) => {
95+
Once: async (root, { result }) => {
9496
// Add the file path to the set of processed files
9597
if (!enable) {
9698
return;
9799
} else {
100+
await preRun();
98101
if (processedFiles.size == 0) {
99102
console.log("\x1b[48;2;103;113;210m%s\x1b[0m", pluginHead);
100103
if (envMode === "dev" || envMode === "development") {
@@ -120,6 +123,7 @@ module.exports = (options = {}) => {
120123
...options,
121124
});
122125
}
126+
logger("info", pluginName, "PreRun:", "PreRun event hook finished.");
123127
}
124128
let cssFile = getRelativePath(result.opts.from);
125129
if (isFileOrInDirectory(cssExcludes, cssFile)) {

test/demo/postcss.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const options = {
3030
formatJson: true, // Format obfuscation data JSON file.
3131
showConfig: false, // Show config on terminal when runinng
3232
keepData: true, // Keep or delete Data after obfuscation is finished?
33+
// preRun: () => Promise.resolve(), // do something before the plugin runs.
3334
// callBack: function () {console.log("Call Me back! ;)");} // Callback function to call after obfuscation is done.
3435
};
3536

0 commit comments

Comments
 (0)