@irrelon/emitter 5.0.6 → 6.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/.eslintignore +2 -1
- package/.prettierignore +2 -1
- package/.prettierrc.yml +18 -1
- package/dist/cjs/src/Emitter.d.ts +71 -20
- package/dist/cjs/src/Emitter.js +128 -78
- package/dist/cjs/tests/Emitter.test.js +125 -52
- package/dist/cjs/tsconfig-cjs.tsbuildinfo +1 -1
- package/dist/{mjs → esm}/src/Emitter.d.ts +71 -20
- package/dist/{mjs → esm}/src/Emitter.js +128 -67
- package/dist/{mjs → esm}/tests/Emitter.test.js +123 -50
- package/dist/esm/tsconfig-esm.tsbuildinfo +1 -0
- package/fixup.sh +1 -1
- package/package.json +8 -7
- package/readme.md +101 -20
- package/src/Emitter.ts +287 -114
- package/tests/Emitter.test.ts +249 -82
- package/tsconfig-cjs.json +1 -1
- package/tsconfig-esm.json +8 -0
- package/tsconfig.json +33 -5
- package/fixPaths.cjs +0 -183
- package/tsconfig-base.json +0 -36
- /package/dist/{mjs → esm}/package.json +0 -0
- /package/dist/{mjs → esm}/tests/Emitter.test.d.ts +0 -0
- /package/dist/{mjs → esm}/tsconfig.tsbuildinfo +0 -0
package/fixPaths.cjs
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A very basic search and replace function to find import and export statements
|
|
3
|
-
* in .js files where the path of the file being specified does not have a .js
|
|
4
|
-
* extension and replace those paths with one that has a .js extension.
|
|
5
|
-
*
|
|
6
|
-
* This would be much better written using abstract source trees to do the
|
|
7
|
-
* re-writing which would be "safer", however this is a fully working function
|
|
8
|
-
* and doesn't seem to mess anything up at present.
|
|
9
|
-
*/
|
|
10
|
-
const chokidar = require("chokidar");
|
|
11
|
-
const fs = require("fs");
|
|
12
|
-
const { readFile, writeFile, readdir } = require("fs").promises;
|
|
13
|
-
const { resolve } = require("path");
|
|
14
|
-
const { minimatch } = require("minimatch");
|
|
15
|
-
|
|
16
|
-
const matchPattern = ["**/*.js", "**/*.jsx"];
|
|
17
|
-
const excludePattern = ["node_modules", "react", ".git"];
|
|
18
|
-
|
|
19
|
-
const expressions = [{
|
|
20
|
-
exp: /import(.*)?["']([.]+)(((?!\.js).)*?)["'];/g,
|
|
21
|
-
update: (match, isIndex) => {
|
|
22
|
-
if (isIndex) {
|
|
23
|
-
return `import${match[1]}"${match[2]}${match[3]}/index.js";`;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return `import${match[1]}"${match[2]}${match[3]}.js";`;
|
|
27
|
-
}
|
|
28
|
-
}, {
|
|
29
|
-
exp: /export\s(.*)?\sfrom\s["']([.]+)(((?!\.js).)*?)["'];/g,
|
|
30
|
-
update: (match, isIndex) => {
|
|
31
|
-
if (isIndex) {
|
|
32
|
-
return `export ${match[1]} from "${match[2]}${match[3]}/index.js";`;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return `export ${match[1]} from "${match[2]}${match[3]}.js";`;
|
|
36
|
-
}
|
|
37
|
-
}];
|
|
38
|
-
|
|
39
|
-
let tsPaths = {};
|
|
40
|
-
let tsResolvedPaths = {};
|
|
41
|
-
|
|
42
|
-
const readTsConfig = () => {
|
|
43
|
-
const config = require("./tsconfig.json");
|
|
44
|
-
tsPaths = config.compilerOptions.paths;
|
|
45
|
-
|
|
46
|
-
Object.entries(tsPaths).forEach(([key, replacementArr]) => {
|
|
47
|
-
tsResolvedPaths[key] = replacementArr.map((dir) => {
|
|
48
|
-
return resolve(__dirname, dir);
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
async function getFiles (dir, gitIgnoreArr) {
|
|
54
|
-
const dirArr = await readdir(dir, { withFileTypes: true });
|
|
55
|
-
|
|
56
|
-
const finalList = dirArr.filter((filePath) => {
|
|
57
|
-
return excludePattern.every((glob) => !minimatch(filePath.name, glob));
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
const files = await Promise.all(finalList.map((tmpDir) => {
|
|
61
|
-
const res = resolve(dir, tmpDir.name);
|
|
62
|
-
return tmpDir.isDirectory() ? getFiles(res) : [res];
|
|
63
|
-
})).then((results) => {
|
|
64
|
-
return results.reduce((arr, item) => {
|
|
65
|
-
arr.push(...item);
|
|
66
|
-
return arr;
|
|
67
|
-
}, []);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
const finalFiles = files.filter((file) => {
|
|
71
|
-
if (!file || !file.trim()) return false;
|
|
72
|
-
return matchPattern.some((glob) => minimatch(file, glob));
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
return Array.prototype.concat(...finalFiles);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const processMatches = (regExp, fileFolder, content, updateFunc) => {
|
|
79
|
-
let match;
|
|
80
|
-
while (match = regExp.exec(content)) {
|
|
81
|
-
const pathToItemFile = resolve(fileFolder, match[2] + match[3] + ".js");
|
|
82
|
-
const pathToItemFolderIndexFile = resolve(fileFolder, match[2] + match[3] + "/index.js");
|
|
83
|
-
let pathStatFile;
|
|
84
|
-
let pathStatFolderIndexFile;
|
|
85
|
-
|
|
86
|
-
try {
|
|
87
|
-
pathStatFile = fs.lstatSync(pathToItemFile);
|
|
88
|
-
} catch (err) {
|
|
89
|
-
pathStatFolderIndexFile = undefined;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
try {
|
|
93
|
-
pathStatFolderIndexFile = fs.lstatSync(pathToItemFolderIndexFile);
|
|
94
|
-
} catch (err) {
|
|
95
|
-
pathStatFolderIndexFile = undefined;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (pathStatFile) {
|
|
99
|
-
// Just add .js to the end of the path
|
|
100
|
-
content = content.replace(match[0], updateFunc(match, false));
|
|
101
|
-
} else if (pathStatFolderIndexFile) {
|
|
102
|
-
content = content.replace(match[0], updateFunc(match, true));
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return content;
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
const processFile = (file) => {
|
|
110
|
-
if (!file) return;
|
|
111
|
-
|
|
112
|
-
//console.log(`Processing ${file}...`);
|
|
113
|
-
|
|
114
|
-
readFile(file).then((fileContentsBuffer) => {
|
|
115
|
-
const fileFolderArr = file.split("/");
|
|
116
|
-
fileFolderArr.pop();
|
|
117
|
-
|
|
118
|
-
const fileFolder = fileFolderArr.join("/");
|
|
119
|
-
|
|
120
|
-
const fileContent = fileContentsBuffer.toString();
|
|
121
|
-
let updatedContent = fileContent;
|
|
122
|
-
|
|
123
|
-
Object.entries(tsPaths).forEach(([key, replacementArr]) => {
|
|
124
|
-
const find = key.replace("*", "");
|
|
125
|
-
const replace = replacementArr[0].replace("*", "");
|
|
126
|
-
|
|
127
|
-
const relativeSteps = new Array(file.replace(__dirname, "").split("/").length - 2).fill("../").join("");
|
|
128
|
-
const replacementPath = relativeSteps + replace;
|
|
129
|
-
|
|
130
|
-
updatedContent = updatedContent.replaceAll(find, replacementPath);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
expressions.forEach((expression) => {
|
|
134
|
-
updatedContent = processMatches(expression.exp, fileFolder, updatedContent, expression.update);
|
|
135
|
-
expression.exp.lastIndex = 0;
|
|
136
|
-
})
|
|
137
|
-
|
|
138
|
-
if (fileContent === updatedContent) {
|
|
139
|
-
//console.log(`No change ${file}`);
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
writeFile(file, updatedContent).then(() => {
|
|
144
|
-
console.log(`Updated ${file}`);
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
const runSearchReplace = () => {
|
|
150
|
-
// Scan all folders
|
|
151
|
-
readFile(resolve(__dirname, ".gitignore")).then((result) => {
|
|
152
|
-
return result.toString().split("\n").filter((line) => {
|
|
153
|
-
return !line.startsWith("#");
|
|
154
|
-
}).filter((line) => {
|
|
155
|
-
return Boolean(line.trim());
|
|
156
|
-
});
|
|
157
|
-
}).then((gitIgnoreArr) => {
|
|
158
|
-
return getFiles(__dirname, gitIgnoreArr);
|
|
159
|
-
}).then((files) => {
|
|
160
|
-
// Now scan the files for matching regular expressions
|
|
161
|
-
// of imports without extensions
|
|
162
|
-
files.forEach((file) => {
|
|
163
|
-
processFile(file);
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
console.log(`Processed ${files.length} files`);
|
|
167
|
-
});
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
readTsConfig();
|
|
171
|
-
runSearchReplace();
|
|
172
|
-
|
|
173
|
-
const watcher = chokidar.watch(__dirname, { persistent: true });
|
|
174
|
-
|
|
175
|
-
watcher.on("change", (filename) => {
|
|
176
|
-
if (filename.indexOf("tsconfig.json") > -1) {
|
|
177
|
-
console.log("Updating tsconfig.json data");
|
|
178
|
-
readTsConfig();
|
|
179
|
-
}
|
|
180
|
-
if (!matchPattern.some((glob) => minimatch(filename, glob))) return;
|
|
181
|
-
//console.log("Changed", filename);
|
|
182
|
-
processFile(filename);
|
|
183
|
-
});
|
package/tsconfig-base.json
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"downlevelIteration": true,
|
|
4
|
-
"baseUrl": ".",
|
|
5
|
-
"target": "es6",
|
|
6
|
-
"lib": [
|
|
7
|
-
"esnext"
|
|
8
|
-
],
|
|
9
|
-
"allowJs": false,
|
|
10
|
-
"skipLibCheck": false,
|
|
11
|
-
"strict": true,
|
|
12
|
-
"pretty": true,
|
|
13
|
-
"allowSyntheticDefaultImports": true,
|
|
14
|
-
"forceConsistentCasingInFileNames": true,
|
|
15
|
-
"noEmit": false,
|
|
16
|
-
"incremental": true,
|
|
17
|
-
"esModuleInterop": true,
|
|
18
|
-
"module": "es2022",
|
|
19
|
-
"moduleResolution": "node",
|
|
20
|
-
"resolveJsonModule": true,
|
|
21
|
-
"isolatedModules": true,
|
|
22
|
-
"declaration": true,
|
|
23
|
-
"inlineSourceMap": false,
|
|
24
|
-
"types": ["node", "jest"]
|
|
25
|
-
},
|
|
26
|
-
"include": [
|
|
27
|
-
"src/**/*.ts",
|
|
28
|
-
"tests/*.ts",
|
|
29
|
-
"tests/**/*.ts",
|
|
30
|
-
"src/**/*.tsx"
|
|
31
|
-
],
|
|
32
|
-
"exclude": [
|
|
33
|
-
"node_modules",
|
|
34
|
-
"dist"
|
|
35
|
-
]
|
|
36
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|