css-module-sync 0.1.33 → 0.1.34

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/css_sync.js +43 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "css-module-sync",
3
- "version": "0.1.33",
3
+ "version": "0.1.34",
4
4
  "description": "Auto sync css module classes with react components",
5
5
  "author": "Max Matinpalo",
6
6
  "type": "module",
package/src/css_sync.js CHANGED
@@ -214,14 +214,51 @@ async function main() {
214
214
  await run_scan();
215
215
  if (FLAGS.watch) {
216
216
  const debouncers = new Map();
217
+ let cache = new Set();
218
+ const refresh_cache = async () => {
219
+ const files = await fs.readdir(TARGET_DIR, { recursive: true });
220
+ cache = new Set(files.map(f => path.resolve(TARGET_DIR, f)));
221
+ };
222
+ await refresh_cache();
223
+
217
224
  console.log("Watching for changes...");
218
- watch(TARGET_DIR, { recursive: true }, (_, filename) => {
225
+ watch(TARGET_DIR, { recursive: true }, async (event, filename) => {
219
226
  if (!filename) return;
220
- const name = filename.toString();
221
- const is_tsx = /\.(jsx|tsx)$/.test(name);
222
- const is_css = /\.css$/.test(name);
223
- if (!is_tsx && !is_css) return;
224
- const full_path = path.resolve(TARGET_DIR, name);
227
+ const full_path = path.resolve(TARGET_DIR, filename.toString());
228
+ const exists = existsSync(full_path);
229
+ const is_tsx = /\.(jsx|tsx)$/.test(full_path);
230
+
231
+ if (event === "rename") {
232
+ const was_cached = cache.has(full_path);
233
+ if (!exists) setTimeout(() => { if (!existsSync(full_path)) cache.delete(full_path); }, 100);
234
+ else {
235
+ cache.add(full_path);
236
+ if (is_tsx && FLAGS.gen && !was_cached) setTimeout(async () => {
237
+ const dir = path.dirname(full_path);
238
+ const name = path.basename(full_path, path.extname(full_path));
239
+ const orphans = Array.from(cache).filter(p => {
240
+ if (!p.endsWith(".module.css") || path.dirname(p) !== dir || !existsSync(p)) return false;
241
+ const base = path.basename(p, ".module.css");
242
+ return !existsSync(path.join(dir, `${base}.tsx`)) && !existsSync(path.join(dir, `${base}.jsx`));
243
+ });
244
+ if (orphans.length === 1) {
245
+ const orphan = orphans[0];
246
+ const next_css = path.join(dir, `${name}.module.css`);
247
+ if (!existsSync(next_css)) {
248
+ await fs.rename(orphan, next_css);
249
+ cache.delete(orphan);
250
+ cache.add(next_css);
251
+ console.log(`Renamed: ${path.basename(orphan)} -> ${path.basename(next_css)}`);
252
+ }
253
+ } else if (orphans.length > 1) {
254
+ console.warn(`[RENAME] Multiple orphan .module.css in ${dir}; skipping:\n` +
255
+ orphans.map(p => `- ${path.basename(p)}`).join("\n"));
256
+ }
257
+ }, 150);
258
+ }
259
+ }
260
+
261
+ if (!is_tsx && !/\.css$/.test(full_path)) return;
225
262
  if (debouncers.has(full_path)) clearTimeout(debouncers.get(full_path));
226
263
  debouncers.set(full_path, setTimeout(() => {
227
264
  debouncers.delete(full_path);