Update script to parse all specs in folder

Summary:
Updates the combine-js-to-schema to expose a cli and combine all passed files into a single schema output

Note: as far as I could tell, there isn't a way for buck to pass a glob of directories, so instead of accepting a dir and crawling it, this update accepts a list of files and combines them. Which makes sense, since buck is good at crawling already

Reviewed By: TheSavior

Differential Revision: D14007193

fbshipit-source-id: dbc209bb8d1cadd381269e9f70dc71a90f77878e
This commit is contained in:
Rick Hanlon 2019-02-11 14:50:44 -08:00 committed by Facebook Github Bot
parent 17e1694076
commit 34763bf7f9
5 changed files with 105 additions and 21 deletions

View File

@ -0,0 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`should combine files 1`] = `
Object {
"modules": Object {
"ComponentOne": Object {
"foo": "baz",
},
"ComponentTwo": Object {
"foo": "bar",
},
},
}
`;

View File

@ -0,0 +1,48 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+react_native
* @flow strict-local
* @format
*/
import combine from '../combine-js-to-schema';
jest.mock(
'/test/module/SchemaOne',
() => ({
modules: {
ComponentOne: {
foo: 'baz',
},
},
}),
{virtual: true},
);
jest.mock(
'/test/module/SchemaTwo',
() => ({
modules: {
ComponentTwo: {
foo: 'bar',
},
},
}),
{virtual: true},
);
test('should combine files', () => {
const files = ['/test/module/SchemaOne', '/test/module/SchemaTwo'];
expect(combine(files)).toMatchSnapshot();
});
test('should throw for failed require', () => {
const files = ['/test/module/does/not/exist'];
expect(() => combine(files)).toThrow(
"Can't require file at /test/module/does/not/exist",
);
});

View File

@ -0,0 +1,24 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+react_native
* @flow
* @format
*/
'use strict';
const combine = require('./combine-js-to-schema');
const fs = require('fs');
const [outfile, ...fileList] = process.argv.slice(2);
const formattedSchema = JSON.stringify(combine(fileList), null, 2);
if (outfile != null) {
fs.writeFileSync(outfile, formattedSchema);
} else {
console.log(formattedSchema);
}

View File

@ -9,28 +9,26 @@
*/
'use strict';
import type {SchemaType} from '../src/CodegenSchema.js';
const fs = require('fs');
function parse(filename: string): SchemaType {
try {
// $FlowFixMe Can't require dynamic variables
return require(filename);
} catch (err) {
throw new Error(`Can't require file at ${filename} ${err}`);
}
}
const args = process.argv.slice(2);
if (args.length !== 2) {
throw new Error(
'Expected to receive the input path and output path as arguments',
function combineSchemas(files: Array<string>): SchemaType {
return files.reduce(
(merged, filename) => {
const schema = parse(filename);
merged.modules = {...merged.modules, ...schema.modules};
return merged;
},
{modules: {}},
);
}
const src = args[0];
const outputPath = args[1];
let file;
try {
// Eventually this will be replaced with a script that reads and parses
// the file via ast
// $FlowFixMe Can't require dynamic variables
file = require(src);
} catch (err) {
throw new Error(`Can't require file at ${src}`);
}
fs.writeFileSync(outputPath, JSON.stringify(file, null, 2));
module.exports = combineSchemas;

View File

@ -8,4 +8,4 @@ THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOUR
# shellcheck source=xplat/js/env-utils/setup_env_vars.sh
source "$THIS_DIR/../../../../env-utils/setup_env_vars.sh"
exec "$FLOW_NODE_BINARY" "$THIS_DIR/combine-js-to-schema.js" "$@"
exec "$FLOW_NODE_BINARY" "$THIS_DIR/combine-js-to-schema-cli.js" "$@"