|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +import type { |
| 14 | + ComponentSchema, |
| 15 | + NativeModuleSchema, |
| 16 | + SchemaType, |
| 17 | +} from '../../CodegenSchema.js'; |
| 18 | + |
| 19 | +const assert = require('assert'); |
| 20 | +const fs = require('fs'); |
| 21 | +const util = require('util'); |
| 22 | + |
| 23 | +const {values: args} = util.parseArgs({ |
| 24 | + options: { |
| 25 | + platform: { |
| 26 | + type: 'string', |
| 27 | + }, |
| 28 | + output: { |
| 29 | + type: 'string', |
| 30 | + }, |
| 31 | + ['schema-query']: { |
| 32 | + type: 'string', |
| 33 | + }, |
| 34 | + }, |
| 35 | +}); |
| 36 | +if (!['iOS', 'android'].includes(args.platform)) { |
| 37 | + throw new Error(`Invalid platform ${args.platform}`); |
| 38 | +} |
| 39 | +const platform = args.platform; |
| 40 | +const output = args.output; |
| 41 | +const schemaQuery: string = args['schema-query']; |
| 42 | + |
| 43 | +if (!schemaQuery.startsWith('@')) { |
| 44 | + throw new Error( |
| 45 | + "The argument provided to --schema-query must be a filename that starts with '@'.", |
| 46 | + ); |
| 47 | +} |
| 48 | + |
| 49 | +const schemaQueryOutputFile = schemaQuery.replace(/^@/, ''); |
| 50 | +const schemaQueryOutput = fs.readFileSync(schemaQueryOutputFile, 'utf8'); |
| 51 | + |
| 52 | +const schemaFiles = schemaQueryOutput.split(' '); |
| 53 | +const modules: { |
| 54 | + [hasteModuleName: string]: NativeModuleSchema | ComponentSchema, |
| 55 | +} = {}; |
| 56 | +const specNameToFile: {[hasteModuleName: string]: string} = {}; |
| 57 | + |
| 58 | +for (const file of schemaFiles) { |
| 59 | + const schema: SchemaType = JSON.parse(fs.readFileSync(file, 'utf8')); |
| 60 | + |
| 61 | + if (schema.modules) { |
| 62 | + for (const specName in schema.modules) { |
| 63 | + const module = schema.modules[specName]; |
| 64 | + if (modules[specName]) { |
| 65 | + assert.deepEqual( |
| 66 | + module, |
| 67 | + modules[specName], |
| 68 | + `App contained two specs with the same file name '${specName}'. Schemas: ${specNameToFile[specName]}, ${file}. Please rename one of the specs.`, |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + if ( |
| 73 | + module.excludedPlatforms && |
| 74 | + module.excludedPlatforms.indexOf(platform) >= 0 |
| 75 | + ) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + modules[specName] = module; |
| 80 | + specNameToFile[specName] = file; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +fs.writeFileSync(output, JSON.stringify({modules}, null, 2)); |
0 commit comments