-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwatch-typegen.ts
More file actions
82 lines (72 loc) · 2.74 KB
/
Copy pathwatch-typegen.ts
File metadata and controls
82 lines (72 loc) · 2.74 KB
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
import { exec } from 'node:child_process';
import { watch } from 'chokidar';
import data from './sanity-typegen.json' with { type: 'json' };
const queryPaths = data.path.map((p) => {
// Handle both glob patterns: **/*.ts and *.ts
return p
.replace(/\*\*\/\*\..*$/, '') // Remove **/*.ts pattern
.replace(/\*\..*$/, ''); // Remove *.ts pattern
});
const schemaPath = 'src/sanity/schema'; // Hardcoded schema path
console.log('Watching for changes in:');
console.log('🔍 Query Paths:', queryPaths);
console.log('📜 Schema Path:', schemaPath);
// Enum for file types
enum FileType {
QUERY = 'query',
SCHEMA = 'schema',
}
// Function to determine if a file belongs to queryPaths or schemaPath
const getFileType = (filePath: string): FileType => {
console.log('getFileType', filePath, schemaPath);
if (filePath.includes(schemaPath)) {
return FileType.SCHEMA;
} else {
return FileType.QUERY;
}
};
// Shared event handler function
const handleFileEvent = (event: string, filePath: string): void => {
const fileType = getFileType(filePath);
if (fileType === FileType.SCHEMA) {
console.log('📜 Schema updated - running Sanity Typegen...');
exec('npm run sanity:typegen', (err, stdout, stderr) => {
if (err) {
console.error(`Error running Sanity Typegen: ${err.message}`);
return;
}
console.log('Sanity Typegen generated successfully');
if (stderr) console.error(stderr);
});
}
if (fileType === FileType.QUERY) {
console.log('🟢 Query updated - running Sanity Typegen...');
exec('npx sanity typegen generate', (err, stdout, stderr) => {
if (err) {
console.error(`Error running npx sanity typegen generate: ${err.message}`);
return;
}
console.log('Sanity Typegen generated successfully');
if (stderr) console.error(stderr);
});
}
};
// Initialize watcher
const watcher = watch([...queryPaths, schemaPath], {
ignored: /node_modules|\.git/, // Ignore unnecessary files
persistent: true,
ignoreInitial: true, // Detect existing files
depth: Infinity, // Watch subdirectories
});
// Attach event handlers with shared callback
watcher
.on('add', (filePath: string) => handleFileEvent('File added', filePath))
.on('change', (filePath: string) => handleFileEvent('File changed', filePath))
.on('unlink', (filePath: string) => handleFileEvent('File deleted', filePath))
.on('addDir', (dirPath: string) => handleFileEvent('Directory added', dirPath))
.on('unlinkDir', (dirPath: string) => handleFileEvent('Directory removed', dirPath))
.on('error', (error: unknown) => console.error(`⚠️ Watcher error: ${error}`));
process.on('SIGINT', () => {
console.log('\nStopping watcher...');
watcher.close().then(() => process.exit(0));
});