| 
 | 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 {ComponentSchema, NativeModuleSchema} from '../../CodegenSchema.js';  | 
 | 14 | + | 
 | 15 | +const assert = require('assert');  | 
 | 16 | +const fs = require('fs');  | 
 | 17 | +const util = require('util');  | 
 | 18 | + | 
 | 19 | +const {values: args} = util.parseArgs({  | 
 | 20 | +  options: {  | 
 | 21 | +    platform: {  | 
 | 22 | +      type: 'string',  | 
 | 23 | +    },  | 
 | 24 | +    output: {  | 
 | 25 | +      type: 'string',  | 
 | 26 | +    },  | 
 | 27 | +    ['schema-query']: {  | 
 | 28 | +      type: 'string',  | 
 | 29 | +    },  | 
 | 30 | +  },  | 
 | 31 | +});  | 
 | 32 | +if (!['iOS', 'android'].includes(args.platform)) {  | 
 | 33 | +  throw new Error(`Invalid platform ${args.platform}`);  | 
 | 34 | +}  | 
 | 35 | +const platform = args.platform;  | 
 | 36 | +const output = args.output;  | 
 | 37 | +const schemaQuery = args['schema-query'];  | 
 | 38 | + | 
 | 39 | +if (!schemaQuery.startsWith('@')) {  | 
 | 40 | +  throw new Error(  | 
 | 41 | +    "The argument provided to --schema-query must be a filename that starts with '@'.",  | 
 | 42 | +  );  | 
 | 43 | +}  | 
 | 44 | + | 
 | 45 | +const schemaQueryOutputFile = schemaQuery.replace(/^@/, '');  | 
 | 46 | +const schemaQueryOutput = fs.readFileSync(schemaQueryOutputFile, 'utf8');  | 
 | 47 | + | 
 | 48 | +const schemaFiles = schemaQueryOutput.split(' ');  | 
 | 49 | +const modules: {  | 
 | 50 | +  [hasteModuleName: string]: NativeModuleSchema | ComponentSchema,  | 
 | 51 | +} = {};  | 
 | 52 | +const specNameToFile: {[hasteModuleName: string]: string} = {};  | 
 | 53 | + | 
 | 54 | +for (const file of schemaFiles) {  | 
 | 55 | +  const schema = JSON.parse(fs.readFileSync(file, 'utf8'));  | 
 | 56 | + | 
 | 57 | +  if (schema.modules) {  | 
 | 58 | +    for (const specName in schema.modules) {  | 
 | 59 | +      const module = schema.modules[specName];  | 
 | 60 | +      if (modules[specName]) {  | 
 | 61 | +        assert.deepEqual(  | 
 | 62 | +          module,  | 
 | 63 | +          modules[specName],  | 
 | 64 | +          `App contained two specs with the same file name '${specName}'. Schemas: ${specNameToFile[specName]}, ${file}. Please rename one of the specs.`,  | 
 | 65 | +        );  | 
 | 66 | +      }  | 
 | 67 | + | 
 | 68 | +      if (  | 
 | 69 | +        module.excludedPlatforms &&  | 
 | 70 | +        module.excludedPlatforms.indexOf(platform) >= 0  | 
 | 71 | +      ) {  | 
 | 72 | +        continue;  | 
 | 73 | +      }  | 
 | 74 | + | 
 | 75 | +      modules[specName] = module;  | 
 | 76 | +      specNameToFile[specName] = file;  | 
 | 77 | +    }  | 
 | 78 | +  }  | 
 | 79 | +}  | 
 | 80 | + | 
 | 81 | +fs.writeFileSync(output, JSON.stringify({modules}, null, 2));  | 
0 commit comments