-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_fixer.js
65 lines (57 loc) · 2.2 KB
/
script_fixer.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
64
65
/**
* @author Samuel Hinchliffe <[email protected]>
* @see [Linkedin] {@link https://www.linkedin.com/in/samuel-hinchliffe-2bb5801a5/}
*
* @summary Removes the "use strict" directive from all JavaScript files in a specified directory.
* The motive here is that the chess script breaks when webpack automatically compiles it in.
*
* @requires fs
* @requires glob
*
* @constant {string} distDir - The directory containing the bundle files.
* @constant {RegExp} useStrictRegex - The regular expression to match the "use strict" directive.
*
* @function removeUseStrict
* @returns {void}
* Created at: 27/02/2023
*/
// Require the built-in file system module and the glob package
const fs = require('fs');
const glob = require('glob');
// Define the directory containing the bundle files
const distDir = 'dist';
// Define the regular expression to match the "use strict" directive
const useStrictRegex = /^"use strict";\n?/;
// Find all .js files in the dist directory
glob(`${distDir}/*.js`, (err, files) => {
// If there is an error finding the files, log an error message and return
if (err) {
console.error(`Failed to find .js files in ${distDir}:`, err);
return;
}
// For each .js file, remove the "use strict" directive
files.forEach((file) => {
// Read the contents of the file
fs.readFile(file, 'utf8', (error, data) => {
// If there is an error reading the file, log an error message and return
if (error) {
console.error(`Failed to read file ${file}:`, error);
return;
}
// Replace the "use strict" directive with an empty string
const newData = data.replace(useStrictRegex, '');
// If the file contents were changed, write the new contents to the file
if (newData !== data) {
fs.writeFile(file, newData, 'utf8', (erra) => {
// If there is an error writing the file, log an error message and return
if (erra) {
console.error(`Failed to write file ${file}:`, erra);
return;
}
// Log a message indicating that the "use strict" directive was removed
console.log(`Removed "use strict" directive from ${file}`);
});
}
});
});
});