copy-static.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* eslint-disable no-console */
  2. import chokidar from 'chokidar';
  3. import fs from 'fs-extra';
  4. import { globSync } from 'glob';
  5. import path from 'path';
  6. const SCHEMAS_GLOB = '**/*.graphql';
  7. const MESSAGES_GLOB = 'i18n/messages/**/*';
  8. const DEST_DIR = path.join(__dirname, '../dist');
  9. function copyFiles(sourceGlob: string, destinationDir: string) {
  10. const srcDir = path.join(__dirname, '../src');
  11. const files = globSync(sourceGlob, {
  12. cwd: srcDir,
  13. });
  14. for (const file of files) {
  15. const destFile = path.join(destinationDir, file);
  16. try {
  17. fs.ensureDirSync(path.dirname(destFile));
  18. fs.copySync(path.join(srcDir, file), destFile);
  19. } catch (error: any) {
  20. console.error(`Error copying file ${file}:`, error);
  21. }
  22. }
  23. }
  24. function copySchemas() {
  25. try {
  26. copyFiles(SCHEMAS_GLOB, DEST_DIR);
  27. console.log('Schemas copied successfully!');
  28. } catch (error) {
  29. console.error('Error copying schemas:', error);
  30. }
  31. }
  32. function copyI18nMessages() {
  33. try {
  34. copyFiles(MESSAGES_GLOB, DEST_DIR);
  35. console.log('I18n messages copied successfully!');
  36. } catch (error) {
  37. console.error('Error copying i18n messages:', error);
  38. }
  39. }
  40. function build() {
  41. copySchemas();
  42. copyI18nMessages();
  43. }
  44. function watch() {
  45. const watcher1 = chokidar.watch(SCHEMAS_GLOB, { cwd: path.join(__dirname, '../src') });
  46. const watcher2 = chokidar.watch(MESSAGES_GLOB, { cwd: path.join(__dirname, '../src') });
  47. watcher1.on('change', copySchemas);
  48. watcher2.on('change', copyI18nMessages);
  49. console.log('Watching for changes...');
  50. }
  51. function runCommand() {
  52. const command = process.argv[2];
  53. if (command === 'build') {
  54. build();
  55. } else if (command === 'watch') {
  56. watch();
  57. } else {
  58. console.error('Invalid command. Please use "build" or "watch".');
  59. }
  60. }
  61. // Run command specified in the command line argument
  62. runCommand();