Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add relay-config package for centralised configuration #2746

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const babelOptions = require('./scripts/getBabelOptions')({
'babel-plugin-macros': 'babel-plugin-macros',
chalk: 'chalk',
child_process: 'child_process',
cosmiconfig: 'cosmiconfig',
crypto: 'crypto',
'fast-glob': 'fast-glob',
'fb-watchman': 'fb-watchman',
Expand Down Expand Up @@ -243,6 +244,21 @@ const builds = [
},
],
},
{
package: 'relay-config',
exports: {
index: 'index.js',
},
bundles: [
{
entry: 'index.js',
output: 'relay-config',
libraryName: 'RelayConfig',
target: 'node',
noMinify: true, // Note: uglify can't yet handle modern JS
},
],
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Cargo-culted this from the test utils package.)

];

function clean() {
Expand All @@ -260,6 +276,7 @@ const modules = gulp.parallel(
'!**/__tests__/**',
'!**/__flowtests__/**',
'!**/__mocks__/**',
'!**/node_modules/**',
],
{
cwd: path.join(PACKAGES, build.package),
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"babel-plugin-tester": "^6.0.1",
"babel-preset-fbjs": "^3.1.2",
"chalk": "^2.4.1",
"cosmiconfig": "^5.2.1",
"del": "3.0.0",
"eslint": "5.4.0",
"eslint-config-fbjs": "2.0.1",
Expand Down
11 changes: 9 additions & 2 deletions packages/babel-plugin-relay/BabelPluginRelay.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ const getValidGraphQLTag = require('./getValidGraphQLTag');
const getValidRelayQLTag = require('./getValidRelayQLTag');
const invariant = require('./invariant');

let RelayConfig;
try {
// eslint-disable-next-line no-eval
RelayConfig = eval('require')('relay-config');
alloy marked this conversation as resolved.
Show resolved Hide resolved
} catch (_) {}
alloy marked this conversation as resolved.
Show resolved Hide resolved

import type {Validator} from './RelayQLTransformer';

export type RelayPluginOptions = {
Expand All @@ -38,7 +44,7 @@ export type RelayPluginOptions = {
snakeCase?: boolean,
substituteVariables?: boolean,
validator?: Validator<any>,
// Directory as specified by outputDir when running relay-compiler
// Directory as specified by artifactDirectory when running relay-compiler
artifactDirectory?: string,
};

Expand Down Expand Up @@ -115,7 +121,8 @@ module.exports = function BabelPluginRelay(context: {types: $FlowFixMe}): any {
return {
visitor: {
Program(path, state) {
path.traverse(visitor, state);
const config = RelayConfig && RelayConfig.loadConfig();
path.traverse(visitor, {...state, opts: {...config, ...state.opts}});
},
},
};
Expand Down
248 changes: 165 additions & 83 deletions packages/relay-compiler/bin/RelayCompilerBin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,96 +15,178 @@ require('@babel/polyfill');
const {main} = require('./RelayCompilerMain');
const yargs = require('yargs');

// Collect args
let RelayConfig;
try {
// eslint-disable-next-line no-eval
RelayConfig = eval('require')('relay-config');
} catch (_) {}

import type {Config} from './RelayCompilerMain';

type OptionBase<T> = {|
describe: string,
default?: T,
|};

/**
* Maps from a value type to a corresponding yargs option object type.
*
* For instance, a required array string type maps to:
*
* { describe: string, default?: Array<string>, type: 'string', array: true, demandOption: true }
*
* Whereas an optional single string type maps to:
*
* { describe: string, default?: string, type: 'string', array: false, demandOption: false }
*/
type OptionTypeMap<T> = $Call<
| ((
Array<string>,
) => {|...OptionBase<Array<string>>, array: true, type: 'string'|})
| ((
?string,
) => {|
...OptionBase<string>,
array: false,
demandOption: false,
type: 'string',
|})
| (string => {|
...OptionBase<string>,
array: false,
demandOption: true,
type: 'string',
|})
| ((?boolean) => {|...OptionBase<boolean>, type: 'boolean'|})
| (({}) => {|...OptionBase<{}>, type: 'object'|}),
T,
>;

/**
* Generates an object type with all keys from Config and values mapped to yargs option object types.
*/
type Options = $ObjMap<
// TODO: Unsure how to remove function types from T with Flow, so instead just narrowing it here by hand.
Config & {language: string},
<T>(T) => OptionTypeMap<T>,
>;

const options: Options = {
schema: {
describe: 'Path to schema.graphql or schema.json',
demandOption: true,
type: 'string',
array: false,
},
src: {
describe: 'Root directory of application code',
demandOption: true,
type: 'string',
array: false,
},
include: {
describe: 'Directories to include under src',
type: 'string',
array: true,
default: ['**'],
},
exclude: {
describe: 'Directories to ignore under src',
type: 'string',
array: true,
default: ['**/node_modules/**', '**/__mocks__/**', '**/__generated__/**'],
},
extensions: {
array: true,
describe:
'File extensions to compile (defaults to extensions provided by the ' +
'language plugin)',
type: 'string',
},
verbose: {
describe: 'More verbose logging',
type: 'boolean',
default: false,
},
quiet: {
describe: 'No output to stdout',
type: 'boolean',
default: false,
},
watchman: {
describe: 'Use watchman when not in watch mode',
type: 'boolean',
default: true,
},
watch: {
describe: 'If specified, watches files and regenerates on changes',
type: 'boolean',
default: false,
},
validate: {
describe:
'Looks for pending changes and exits with non-zero code instead of ' +
'writing to disk',
type: 'boolean',
default: false,
},
persistOutput: {
describe:
'A path to a .json file where persisted query metadata should be saved',
demandOption: false,
type: 'string',
array: false,
},
noFutureProofEnums: {
describe:
'This option controls whether or not a catch-all entry is added to enum type definitions ' +
'for values that may be added in the future. Enabling this means you will have to update ' +
'your application whenever the GraphQL server schema adds new enum values to prevent it ' +
'from breaking.',
type: 'boolean',
default: false,
},
language: {
describe:
'The name of the language plugin used for input files and artifacts',
demandOption: false,
type: 'string',
array: false,
default: 'javascript',
},
artifactDirectory: {
describe:
'A specific directory to output all artifacts to. When enabling this ' +
'the babel plugin needs `artifactDirectory` set as well.',
demandOption: false,
type: 'string',
array: false,
},
customScalars: {
describe:
'Mappings from custom scalars in your schema to built-in GraphQL ' +
'types, for type emission purposes. (Uses yargs dot-notation, e.g. ' +
'--customScalars.URL=String)',
type: 'object',
},
};

// Load external config
const config = RelayConfig && RelayConfig.loadConfig();

// Parse CLI args
const argv = yargs
.usage(
'Create Relay generated files\n\n' +
'$0 --schema <path> --src <path> [--watch]',
)
.options({
schema: {
describe: 'Path to schema.graphql or schema.json',
demandOption: true,
type: 'string',
},
src: {
describe: 'Root directory of application code',
demandOption: true,
type: 'string',
},
include: {
array: true,
default: ['**'],
describe: 'Directories to include under src',
type: 'string',
},
exclude: {
array: true,
default: ['**/node_modules/**', '**/__mocks__/**', '**/__generated__/**'],
describe: 'Directories to ignore under src',
type: 'string',
},
extensions: {
array: true,
describe:
'File extensions to compile (defaults to extensions provided by the ' +
'language plugin)',
type: 'string',
},
verbose: {
describe: 'More verbose logging',
type: 'boolean',
},
quiet: {
describe: 'No output to stdout',
type: 'boolean',
},
watchman: {
describe: 'Use watchman when not in watch mode',
type: 'boolean',
default: true,
},
watch: {
describe: 'If specified, watches files and regenerates on changes',
type: 'boolean',
},
validate: {
describe:
'Looks for pending changes and exits with non-zero code instead of ' +
'writing to disk',
type: 'boolean',
default: false,
},
'persist-output': {
describe:
'A path to a .json file where persisted query metadata should be saved',
},
noFutureProofEnums: {
describe:
'This option controls whether or not a catch-all entry is added to enum type definitions ' +
'for values that may be added in the future. Enabling this means you will have to update ' +
'your application whenever the GraphQL server schema adds new enum values to prevent it ' +
'from breaking.',
default: false,
},
language: {
describe:
'The name of the language plugin used for input files and artifacts',
type: 'string',
default: 'javascript',
},
artifactDirectory: {
describe:
'A specific directory to output all artifacts to. When enabling this ' +
'the babel plugin needs `artifactDirectory` set as well.',
type: 'string',
default: null,
},
})
.options(options)
// Apply externally loaded config through the yargs API so that we can leverage yargs' defaults and have them show up
// in the help banner.
.config(config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is just populating defaults, and command line args will override them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order of overrides is:

  1. Defaults defined in options above
  2. config from relay-config
  3. CLI args

.help().argv;

// Run script with args
// $FlowFixMe: Invalid types for yargs. Please fix this when touching this code.
// Start the application
main(argv).catch(error => {
console.error(String(error.stack || error));
process.exit(1);
Expand Down
Loading