gen.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Phoenix Shell.
  5. *
  6. * Phoenix Shell is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. // Script that generates some of the javascript files
  20. import fs from 'fs';
  21. import path from 'path';
  22. const [directory] = process.argv.slice(2);
  23. const target = path.resolve(process.cwd(), directory);
  24. const outputFile = path.resolve(target, '__exports__.js');
  25. const files = fs.readdirSync(target);
  26. let output = '';
  27. const line = str => {
  28. output += str + '\n';
  29. }
  30. const toVar = name => {
  31. name = name.replace(/-/g, '_');
  32. return 'module_' + name;
  33. }
  34. const licenseLines = fs.readFileSync('../doc/license_header.txt', {encoding: 'utf8'}).split('\n');
  35. licenseLines.pop(); // Remove trailing empty line
  36. line('/*');
  37. for (const licenseLine of licenseLines) {
  38. if (licenseLine.length === 0) {
  39. line(' *');
  40. } else {
  41. line(` * ${licenseLine}`);
  42. }
  43. }
  44. line(' */');
  45. line('// Generated by /tools/gen.js');
  46. for ( const file of files ) {
  47. if ( ! file.endsWith('.js') ) continue;
  48. const name = path.parse(file).name;
  49. if ( name === '__exports__' ) continue;
  50. line(`import ${toVar(name)} from './${file}'`);
  51. }
  52. line('');
  53. line('export default {');
  54. for ( const file of files ) {
  55. if ( ! file.endsWith('.js') ) continue;
  56. const name = path.parse(file).name;
  57. if ( name === '__exports__' ) continue;
  58. line(` ${JSON.stringify(name)}: ${toVar(name)},`);
  59. }
  60. line('};');
  61. fs.writeFileSync(outputFile, output);