extract-ios-device-names.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* eslint-env node */
  2. import path from 'path';
  3. import fs from 'fs';
  4. import prettier from 'prettier';
  5. //joining path of directory
  6. const outputPath = path.join(__dirname, '../static/app/constants/ios-device-list.tsx');
  7. const directoryPath = path.join(__dirname, '../node_modules/ios-device-list/');
  8. async function getDefinitionFiles(): Promise<string[]> {
  9. const files: string[] = [];
  10. const maybeJSONFiles = await fs.readdirSync(directoryPath);
  11. //listing all files using forEach
  12. maybeJSONFiles.forEach(file => {
  13. if (!file.endsWith('.json') || file === 'package.json') return;
  14. files.push(path.join(path.resolve(directoryPath), file));
  15. });
  16. return files;
  17. }
  18. type Generation = string;
  19. type Identifier = string;
  20. type Mapping = Record<Identifier, Generation>;
  21. async function collectDefinitions(files: string[]): Promise<Mapping> {
  22. const definitions: Mapping = {};
  23. const queue = [...files];
  24. while (queue.length > 0) {
  25. const file = queue.pop();
  26. if (!file) {
  27. throw new Error('Empty queue');
  28. }
  29. const contents = fs.readFileSync(file, 'utf-8');
  30. const content = JSON.parse(contents);
  31. if (typeof content?.[0]?.Identifier === 'undefined') {
  32. continue;
  33. }
  34. for (let i = 0; i < content.length; i++) {
  35. definitions[content[i].Identifier] = content[i].Generation;
  36. }
  37. }
  38. return definitions;
  39. }
  40. const HEADER = `
  41. // THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
  42. // generated using scripts/extract-ios-device-names.ts as part of build step.
  43. // the purpose of the script is to extract only the iOS information that Sentry cares about
  44. // and discard the rest of the JSON so we do not end up bloating bundle size.
  45. `;
  46. const template = (contents: string) => {
  47. return `
  48. ${HEADER}
  49. const iOSDeviceMapping: Record<string, string> = ${contents}
  50. export {iOSDeviceMapping}
  51. `;
  52. };
  53. const formatOutput = async (unformatted: string) => {
  54. const config = await prettier.resolveConfig(outputPath);
  55. if (config) {
  56. return prettier.format(unformatted, config);
  57. }
  58. return unformatted;
  59. };
  60. export async function extractIOSDeviceNames() {
  61. try {
  62. if (fs.statSync(outputPath)) {
  63. // Out with the old, in with the new
  64. fs.unlinkSync(outputPath);
  65. }
  66. } catch (e) {
  67. // File does not exists, carry along
  68. }
  69. const files = await getDefinitionFiles();
  70. const definitions = await collectDefinitions(files);
  71. const formatted = await formatOutput(
  72. template(JSON.stringify(definitions, undefined, 2))
  73. );
  74. fs.writeFileSync(outputPath, formatted);
  75. console.log('✅ Regenerated ios-device-list.tsx');
  76. }