Skip to content

Commit

Permalink
Update script to parse all specs in folder
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rickhanlonii authored and facebook-github-bot committed Feb 11, 2019
1 parent 17e1694 commit 34763bf
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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",
},
},
}
`;
Original file line number Diff line number Diff line change
@@ -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",
);
});
Original file line number Diff line number Diff line change
@@ -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);
}
38 changes: 18 additions & 20 deletions packages/react-native-codegen/buck_tests/combine-js-to-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,26 @@
*/

'use strict';
import type {SchemaType} from '../src/CodegenSchema.js';

const fs = require('fs');

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 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 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}`);
function combineSchemas(files: Array<string>): SchemaType {
return files.reduce(
(merged, filename) => {
const schema = parse(filename);
merged.modules = {...merged.modules, ...schema.modules};
return merged;
},
{modules: {}},
);
}

fs.writeFileSync(outputPath, JSON.stringify(file, null, 2));
module.exports = combineSchemas;
Original file line number Diff line number Diff line change
Expand Up @@ -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" "$@"

0 comments on commit 34763bf

Please sign in to comment.