-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b85c928
[RelayCompilerMain] Refactor tests
alloy 952a19b
[RelayCompilerMain] Pass custom scalars to file writer
alloy 5c905f7
[gulp] Ignore packages/*/node_modules
alloy c648d5b
[RelayCompilerMain] Rename options to config
alloy 41e345a
[RelayCompilerBin] Type options as per Config
alloy 6ef6724
[RelayConfig] Initial import
alloy 4f4b4b6
[RelayCompilerBin] Use relay-config, if installed
alloy cc1c44c
[BabelPluginRelay] Use relay-config, if installed
alloy a1d530c
[RelayCompilerMain] Add language plugin loading tests
alloy 527fd77
[RelayCompilerMain] Allow external config to specify language plugin …
alloy 2d8a70a
[RelayCompilerMain] Follow existing test decsription pattern
alloy 074efda
[RelayCompilerMain] Cover all watch related config behaviour
alloy e996a7d
[RelayCompilerMain] Throw when enabling watch without watchman being …
alloy 9ad6486
[RelayCompilerMain] Follow bin defaults in tests
alloy 8dd5f9c
[RelayCompilerMain] Small cleanup
alloy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The order of overrides is:
|
||
.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); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.)