forked from fuddl/wikibase-for-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare-importmap.js
174 lines (152 loc) · 4.78 KB
/
prepare-importmap.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const fs = require('fs-extra');
const path = require('path');
const { glob } = require('glob');
// Define the source and destination directories
const srcDir = path.join(__dirname, 'node_modules');
const destDir = path.join(__dirname, 'importmap');
// List of modules to copy
const modulesToCopy = [
'binary-variations',
'htm',
'normalize.css',
'isbn3',
'leaflet',
'preact',
'wikibase-sdk',
];
async function copySpecifiedModules() {
try {
// Ensure the destination directory exists
await fs.ensureDir(destDir);
// Copy each specified module if it exists in node_modules
for (let moduleName of modulesToCopy) {
const srcPath = path.join(srcDir, moduleName);
const destPath = path.join(destDir, moduleName);
// Check if the source path exists and is a directory
if (
(await fs.pathExists(srcPath)) &&
(await fs.stat(srcPath)).isDirectory()
) {
// Copy the module directory to the destination
await fs.copy(srcPath, destPath);
console.log(`Copied '${moduleName}' to importmap directory.`);
} else {
console.log(
`Module '${moduleName}' does not exist or is not a directory.`,
);
}
}
console.log('Specified modules have been copied successfully.');
} catch (err) {
console.error('Error copying modules:', err);
}
}
// Allowed file extensions
const allowedExtensions = new Set([
'.css',
'.js',
'.mjs',
'.png',
'.svg',
'.json',
'.md',
]);
async function deleteDisallowedFiles(directory) {
try {
// Read all files and directories in the current directory
const items = await fs.readdir(directory);
for (let item of items) {
const itemPath = path.join(directory, item);
const itemStat = await fs.stat(itemPath);
if (itemStat.isDirectory()) {
// If the item is a directory, recurse into it
await deleteDisallowedFiles(itemPath);
} else {
// If the item is a file, check the extension
const ext = path.extname(item);
if (!allowedExtensions.has(ext)) {
await fs.remove(itemPath);
console.log(`Deleted '${itemPath}' - disallowed extension.`);
}
}
}
} catch (err) {
console.error('Error during cleanup:', err);
}
}
/**
* Opens a file, replaces specified content, and saves it back.
*
* @param {string} filePath - The path to the file.
* @param {RegExp} searchValue - The regular expression to match content to replace.
* @param {string} replaceValue - The string to replace the matched content with.
*/
function modifyFile(filePath, searchValue, replaceValue) {
// Read the file asynchronously
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
// Replace the content
const modifiedData = data.replace(searchValue, replaceValue);
// Save the modified file back
fs.writeFile(filePath, modifiedData, 'utf8', err => {
if (err) {
console.error('Error writing file:', err);
} else {
console.log('File has been modified and saved successfully.');
}
});
});
}
async function fixImportExport(directory) {
try {
const pattern = '**/*.js'; // Pattern to match all JS files
const options = {
cwd: directory,
ignore: 'node_modules/**', // Ignoring node_modules directory
};
const files = await glob(pattern, options);
for (let file of files) {
const fullPath = path.join(directory, file);
const data = await fs.readFile(fullPath, 'utf8');
// Regex to modify import and export paths that lack a '.js' extension, supporting `* as Alias` and multiline
const regex =
/(import\s+(?:\* as \S+,\s*)?(?:[\s\S]*?)from\s+|export\s+(?:\*|{[\s\S]*?})\s+from\s+)['"]([^'"\s]+?)['"];?/gm;
const fixedData = data.replace(regex, (match, prefix, filepath) => {
if (
!filepath.endsWith('.js') &&
!filepath.startsWith('http') &&
!filepath.includes('.json')
) {
return `${prefix}'${filepath}.js';`;
}
return match;
});
if (data !== fixedData) {
await fs.writeFile(fullPath, fixedData, 'utf8');
console.log(`Updated imports/exports in file ${fullPath}`);
}
}
} catch (err) {
console.error('Error processing files:', err);
}
}
(async () => {
// Run the copy operation
await copySpecifiedModules();
// Run the cleanup function
await deleteDisallowedFiles(destDir);
modifyFile(
'./importmap/preact/hooks/src/index.js',
"'preact'",
'"../../src/index.js"',
);
modifyFile(
'./importmap/leaflet/src/Leaflet.js',
"import {version} from '../package.json';",
"const version = '1.9.4';",
);
await fixImportExport(path.join(__dirname, 'importmap'));
})();