@khanacademy/graphql-flow 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/changeset-release.yml +2 -16
- package/CHANGELOG.md +6 -0
- package/Readme.md +11 -1
- package/dist/cli/config.js +1 -1
- package/dist/cli/config.js.flow +3 -4
- package/dist/cli/config.js.map +1 -1
- package/dist/generateTypeFiles.js +15 -2
- package/dist/generateTypeFiles.js.flow +18 -4
- package/dist/generateTypeFiles.js.map +1 -1
- package/package.json +1 -1
- package/src/cli/config.js +3 -4
- package/src/generateTypeFiles.js +18 -4
|
@@ -55,8 +55,8 @@ jobs:
|
|
|
55
55
|
if: steps.changesets.outputs.published == 'true'
|
|
56
56
|
uses: rtCamp/action-slack-notify@v2
|
|
57
57
|
env:
|
|
58
|
-
SLACK_WEBHOOK: ${{ secrets.
|
|
59
|
-
SLACK_CHANNEL:
|
|
58
|
+
SLACK_WEBHOOK: ${{ secrets.SLACK_FEI_FIREHOSE }}
|
|
59
|
+
SLACK_CHANNEL: fei-firehose
|
|
60
60
|
SLACK_MSG_AUTHOR: ${{ github.event.pull_request.user.login }}
|
|
61
61
|
SLACK_USERNAME: GithubGoose
|
|
62
62
|
SLACK_ICON_EMOJI: ":goose:"
|
|
@@ -64,17 +64,3 @@ jobs:
|
|
|
64
64
|
SLACK_TITLE: "New Graphql-Flow release!"
|
|
65
65
|
SLACK_FOOTER: Graphql-Flow Slack Notification
|
|
66
66
|
MSG_MINIMAL: true
|
|
67
|
-
|
|
68
|
-
- name: Send a Slack notification for mobile if a publish happens
|
|
69
|
-
if: steps.changesets.outputs.published == 'true'
|
|
70
|
-
uses: rtCamp/action-slack-notify@v2
|
|
71
|
-
env:
|
|
72
|
-
SLACK_WEBHOOK: ${{ secrets.SLACK_FEIMOBILE_WEBHOOK }}
|
|
73
|
-
SLACK_CHANNEL: frontend-infra-mobile
|
|
74
|
-
SLACK_MSG_AUTHOR: ${{ github.event.pull_request.user.login }}
|
|
75
|
-
SLACK_USERNAME: GithubGoose
|
|
76
|
-
SLACK_ICON_EMOJI: ":goose:"
|
|
77
|
-
SLACK_MESSAGE: "A new version of ${{ github.event.repository.name }} was published! 🎉 \nRelease notes → https://github.com/Khan/${{ github.event.repository.name }}/releases/"
|
|
78
|
-
SLACK_TITLE: "New Graphql-Flow release!"
|
|
79
|
-
SLACK_FOOTER: Graphql-Flow Slack Notification
|
|
80
|
-
MSG_MINIMAL: true
|
package/CHANGELOG.md
CHANGED
package/Readme.md
CHANGED
|
@@ -42,7 +42,17 @@ type Options = {
|
|
|
42
42
|
readOnlyArray: boolean = true,
|
|
43
43
|
scalars: {[key: string]: 'string' | 'boolean' | 'number'}
|
|
44
44
|
|
|
45
|
-
// Specify the name of the generated types directory
|
|
45
|
+
// Specify the name of the generated types directory. If this
|
|
46
|
+
// is a relative path, then this is used to suffix the output
|
|
47
|
+
// directory; if it's an absolute path it's used to prefix the
|
|
48
|
+
// output directory. For instance, if a gql directive is
|
|
49
|
+
// found in /foo/bar/baz/query.js and you run the cli (or
|
|
50
|
+
// jest) from directory /foo, then:
|
|
51
|
+
// * if `generatedDirectory` is "__generated__", the output will
|
|
52
|
+
// be in /foo/bar/baz/__generated__/index.js and sibling files
|
|
53
|
+
// * if `generatedDirectory` is "/tmp/__generated__", the output
|
|
54
|
+
// will be in /tmp/__generated__/bar/baz/index.js and sibling
|
|
55
|
+
// files.
|
|
46
56
|
generatedDirectory: string = '__generated__',
|
|
47
57
|
|
|
48
58
|
// The default generated type contains both the types of the response
|
package/dist/cli/config.js
CHANGED
|
@@ -39,7 +39,7 @@ const loadConfigFile = configFile => {
|
|
|
39
39
|
return {
|
|
40
40
|
options: (_data$options = data.options) !== null && _data$options !== void 0 ? _data$options : {},
|
|
41
41
|
excludes: (_data$excludes$map = (_data$excludes = data.excludes) === null || _data$excludes === void 0 ? void 0 : _data$excludes.map(string => new RegExp(string))) !== null && _data$excludes$map !== void 0 ? _data$excludes$map : [],
|
|
42
|
-
schemaFilePath: _path.default.join(_path.default.dirname(configFile), data.schemaFilePath),
|
|
42
|
+
schemaFilePath: _path.default.isAbsolute(data.schemaFilePath) ? data.schemaFilePath : _path.default.join(_path.default.dirname(configFile), data.schemaFilePath),
|
|
43
43
|
dumpOperations: data.dumpOperations
|
|
44
44
|
};
|
|
45
45
|
};
|
package/dist/cli/config.js.flow
CHANGED
|
@@ -78,10 +78,9 @@ export const loadConfigFile = (configFile: string): CliConfig => {
|
|
|
78
78
|
return {
|
|
79
79
|
options: data.options ?? {},
|
|
80
80
|
excludes: data.excludes?.map((string) => new RegExp(string)) ?? [],
|
|
81
|
-
schemaFilePath: path.
|
|
82
|
-
|
|
83
|
-
data.schemaFilePath,
|
|
84
|
-
),
|
|
81
|
+
schemaFilePath: path.isAbsolute(data.schemaFilePath)
|
|
82
|
+
? data.schemaFilePath
|
|
83
|
+
: path.join(path.dirname(configFile), data.schemaFilePath),
|
|
85
84
|
dumpOperations: data.dumpOperations,
|
|
86
85
|
};
|
|
87
86
|
};
|
package/dist/cli/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/cli/config.js"],"names":["loadConfigFile","configFile","data","JSON","parse","fs","readFileSync","toplevelKeys","Object","keys","forEach","k","includes","Error","join","options","externalOptionsKeys","excludes","map","string","RegExp","schemaFilePath","path","dirname","dumpOperations","getSchemas","raw","endsWith","schemaForValidation","queryResponse","descriptions","schemaForTypeGeneration","introspectionData"],"mappings":";;;;;;;AAKA;;AAEA;;AACA;;AAOA;;;;AAoBO,MAAMA,cAAc,GAAIC,UAAD,IAAmC;AAAA;;AAC7D;AACA,QAAMC,IAAgB,GAAGC,IAAI,CAACC,KAAL,CAAWC,YAAGC,YAAH,CAAgBL,UAAhB,EAA4B,MAA5B,CAAX,CAAzB;AACA,QAAMM,YAAY,GAAG,CACjB,UADiB,EAEjB,gBAFiB,EAGjB,SAHiB,EAIjB,gBAJiB,CAArB;AAMAC,EAAAA,MAAM,CAACC,IAAP,CAAYP,IAAZ,EAAkBQ,OAAlB,CAA2BC,CAAD,IAAO;AAC7B,QAAI,CAACJ,YAAY,CAACK,QAAb,CAAsBD,CAAtB,CAAL,EAA+B;AAC3B,YAAM,IAAIE,KAAJ,CACD,oCAAmCZ,UAAW,KAAIU,CAAE,yBAAwBJ,YAAY,CAACO,IAAb,CACzE,IADyE,CAE3E,EAHA,CAAN;AAKH;AACJ,GARD;;AASA,MAAIZ,IAAI,CAACa,OAAT,EAAkB;AACd,UAAMC,mBAAmB,GAAG,CACxB,QADwB,EAExB,aAFwB,EAGxB,cAHwB,EAIxB,SAJwB,EAKxB,mBALwB,EAMxB,mBANwB,EAOxB,eAPwB,EAQxB,YARwB,EASxB,oBATwB,EAUxB,sBAVwB,EAWxB,cAXwB,CAA5B;AAaAR,IAAAA,MAAM,CAACC,IAAP,CAAYP,IAAI,CAACa,OAAjB,EAA0BL,OAA1B,CAAmCC,CAAD,IAAO;AACrC,UAAI,CAACK,mBAAmB,CAACJ,QAApB,CAA6BD,CAA7B,CAAL,EAAsC;AAClC,cAAM,IAAIE,KAAJ,CACD,iCAAgCZ,UAAW,KAAIU,CAAE,sBAAqBK,mBAAmB,CAACF,IAApB,CACnE,IADmE,CAErE,EAHA,CAAN;AAKH;AACJ,KARD;AASH;;AACD,SAAO;AACHC,IAAAA,OAAO,mBAAEb,IAAI,CAACa,OAAP,yDAAkB,EADtB;AAEHE,IAAAA,QAAQ,0CAAEf,IAAI,CAACe,QAAP,mDAAE,eAAeC,GAAf,CAAoBC,MAAD,IAAY,IAAIC,MAAJ,CAAWD,MAAX,CAA/B,CAAF,mEAAwD,EAF7D;AAGHE,IAAAA,cAAc,EAAEC,cAAKR,IAAL,
|
|
1
|
+
{"version":3,"sources":["../../src/cli/config.js"],"names":["loadConfigFile","configFile","data","JSON","parse","fs","readFileSync","toplevelKeys","Object","keys","forEach","k","includes","Error","join","options","externalOptionsKeys","excludes","map","string","RegExp","schemaFilePath","path","isAbsolute","dirname","dumpOperations","getSchemas","raw","endsWith","schemaForValidation","queryResponse","descriptions","schemaForTypeGeneration","introspectionData"],"mappings":";;;;;;;AAKA;;AAEA;;AACA;;AAOA;;;;AAoBO,MAAMA,cAAc,GAAIC,UAAD,IAAmC;AAAA;;AAC7D;AACA,QAAMC,IAAgB,GAAGC,IAAI,CAACC,KAAL,CAAWC,YAAGC,YAAH,CAAgBL,UAAhB,EAA4B,MAA5B,CAAX,CAAzB;AACA,QAAMM,YAAY,GAAG,CACjB,UADiB,EAEjB,gBAFiB,EAGjB,SAHiB,EAIjB,gBAJiB,CAArB;AAMAC,EAAAA,MAAM,CAACC,IAAP,CAAYP,IAAZ,EAAkBQ,OAAlB,CAA2BC,CAAD,IAAO;AAC7B,QAAI,CAACJ,YAAY,CAACK,QAAb,CAAsBD,CAAtB,CAAL,EAA+B;AAC3B,YAAM,IAAIE,KAAJ,CACD,oCAAmCZ,UAAW,KAAIU,CAAE,yBAAwBJ,YAAY,CAACO,IAAb,CACzE,IADyE,CAE3E,EAHA,CAAN;AAKH;AACJ,GARD;;AASA,MAAIZ,IAAI,CAACa,OAAT,EAAkB;AACd,UAAMC,mBAAmB,GAAG,CACxB,QADwB,EAExB,aAFwB,EAGxB,cAHwB,EAIxB,SAJwB,EAKxB,mBALwB,EAMxB,mBANwB,EAOxB,eAPwB,EAQxB,YARwB,EASxB,oBATwB,EAUxB,sBAVwB,EAWxB,cAXwB,CAA5B;AAaAR,IAAAA,MAAM,CAACC,IAAP,CAAYP,IAAI,CAACa,OAAjB,EAA0BL,OAA1B,CAAmCC,CAAD,IAAO;AACrC,UAAI,CAACK,mBAAmB,CAACJ,QAApB,CAA6BD,CAA7B,CAAL,EAAsC;AAClC,cAAM,IAAIE,KAAJ,CACD,iCAAgCZ,UAAW,KAAIU,CAAE,sBAAqBK,mBAAmB,CAACF,IAApB,CACnE,IADmE,CAErE,EAHA,CAAN;AAKH;AACJ,KARD;AASH;;AACD,SAAO;AACHC,IAAAA,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eFiles';\nimport type {Schema} from '../types';\nimport type {GraphQLSchema} from 'graphql/type/schema';\n\nimport {schemaFromIntrospectionData} from '../schemaFromIntrospectionData';\n\nimport fs from 'fs';\nimport {\n buildClientSchema,\n buildSchema,\n getIntrospectionQuery,\n graphqlSync,\n type IntrospectionQuery,\n} from 'graphql';\nimport path from 'path';\n\nexport type CliConfig = {\n excludes: Array<RegExp>,\n schemaFilePath: string,\n dumpOperations?: string,\n options: ExternalOptions,\n};\n\n/**\n * This is the json-compatible form of the config\n * object.\n */\ntype JSONConfig = {\n excludes?: Array<string>,\n schemaFilePath: string,\n options?: ExternalOptions,\n dumpOperations?: string,\n};\n\nexport const loadConfigFile = (configFile: string): CliConfig => {\n // eslint-disable-next-line flowtype-errors/uncovered\n const data: JSONConfig = JSON.parse(fs.readFileSync(configFile, 'utf8'));\n const toplevelKeys = [\n 'excludes',\n 'schemaFilePath',\n 'options',\n 'dumpOperations',\n ];\n Object.keys(data).forEach((k) => {\n if (!toplevelKeys.includes(k)) {\n throw new Error(\n `Invalid attribute in config file ${configFile}: ${k}. Allowed attributes: ${toplevelKeys.join(\n ', ',\n )}`,\n );\n }\n });\n if (data.options) {\n const externalOptionsKeys = [\n 'pragma',\n 'loosePragma',\n 'ignorePragma',\n 'scalars',\n 'strictNullability',\n 'regenerateCommand',\n 'readOnlyArray',\n 'splitTypes',\n 'generatedDirectory',\n 'exportAllObjectTypes',\n 'typeFileName',\n ];\n Object.keys(data.options).forEach((k) => {\n if (!externalOptionsKeys.includes(k)) {\n throw new Error(\n `Invalid option in config file ${configFile}: ${k}. Allowed options: ${externalOptionsKeys.join(\n ', ',\n )}`,\n );\n }\n });\n }\n return {\n options: data.options ?? {},\n excludes: data.excludes?.map((string) => new RegExp(string)) ?? [],\n schemaFilePath: path.isAbsolute(data.schemaFilePath)\n ? data.schemaFilePath\n : path.join(path.dirname(configFile), data.schemaFilePath),\n dumpOperations: data.dumpOperations,\n };\n};\n\n/**\n * Loads a .json 'introspection query response', or a .graphql schema definition.\n */\nexport const getSchemas = (schemaFilePath: string): [GraphQLSchema, Schema] => {\n const raw = fs.readFileSync(schemaFilePath, 'utf8');\n if (schemaFilePath.endsWith('.graphql')) {\n const schemaForValidation = buildSchema(raw);\n const queryResponse = graphqlSync(\n schemaForValidation,\n getIntrospectionQuery({descriptions: true}),\n );\n const schemaForTypeGeneration = schemaFromIntrospectionData(\n // eslint-disable-next-line flowtype-errors/uncovered\n ((queryResponse.data: any): IntrospectionQuery),\n );\n return [schemaForValidation, schemaForTypeGeneration];\n } else {\n // eslint-disable-next-line flowtype-errors/uncovered\n const introspectionData: IntrospectionQuery = JSON.parse(raw);\n const schemaForValidation = buildClientSchema(introspectionData);\n const schemaForTypeGeneration =\n schemaFromIntrospectionData(introspectionData);\n return [schemaForValidation, schemaForTypeGeneration];\n }\n};\n"],"file":"config.js"}
|
|
@@ -82,10 +82,23 @@ const generateTypeFileContents = (fileName, schema, document, options, generated
|
|
|
82
82
|
|
|
83
83
|
exports.generateTypeFileContents = generateTypeFileContents;
|
|
84
84
|
|
|
85
|
-
const
|
|
85
|
+
const getGeneratedDir = (fileName, options) => {
|
|
86
86
|
var _options$generatedDir;
|
|
87
87
|
|
|
88
|
-
const
|
|
88
|
+
const generatedDirectory = (_options$generatedDir = options.generatedDirectory) !== null && _options$generatedDir !== void 0 ? _options$generatedDir : '__generated__';
|
|
89
|
+
|
|
90
|
+
if (_path.default.isAbsolute(generatedDirectory)) {
|
|
91
|
+
// fileName is absolute here, so we make it relative to cwd
|
|
92
|
+
// for more reasonable filenames. We convert leading ..'s
|
|
93
|
+
// to `__` so this doesn't escape the output directory.
|
|
94
|
+
return _path.default.join(generatedDirectory, _path.default.relative(process.cwd(), _path.default.dirname(fileName)).replace(/\.\.\//g, '__/'));
|
|
95
|
+
} else {
|
|
96
|
+
return _path.default.join(_path.default.dirname(fileName), generatedDirectory);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const generateTypeFiles = (fileName, schema, document, options) => {
|
|
101
|
+
const generatedDir = getGeneratedDir(fileName, options);
|
|
89
102
|
|
|
90
103
|
const indexFile = _path.default.join(generatedDir, 'index.js');
|
|
91
104
|
|
|
@@ -100,16 +100,30 @@ export const generateTypeFileContents = (
|
|
|
100
100
|
return {files, indexContents};
|
|
101
101
|
};
|
|
102
102
|
|
|
103
|
+
const getGeneratedDir = (fileName: string, options: Options) => {
|
|
104
|
+
const generatedDirectory = options.generatedDirectory ?? '__generated__';
|
|
105
|
+
if (path.isAbsolute(generatedDirectory)) {
|
|
106
|
+
// fileName is absolute here, so we make it relative to cwd
|
|
107
|
+
// for more reasonable filenames. We convert leading ..'s
|
|
108
|
+
// to `__` so this doesn't escape the output directory.
|
|
109
|
+
return path.join(
|
|
110
|
+
generatedDirectory,
|
|
111
|
+
path
|
|
112
|
+
.relative(process.cwd(), path.dirname(fileName))
|
|
113
|
+
.replace(/\.\.\//g, '__/'),
|
|
114
|
+
);
|
|
115
|
+
} else {
|
|
116
|
+
return path.join(path.dirname(fileName), generatedDirectory);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
103
120
|
export const generateTypeFiles = (
|
|
104
121
|
fileName: string,
|
|
105
122
|
schema: Schema,
|
|
106
123
|
document: DocumentNode,
|
|
107
124
|
options: Options,
|
|
108
125
|
) => {
|
|
109
|
-
const generatedDir =
|
|
110
|
-
path.dirname(fileName),
|
|
111
|
-
options.generatedDirectory ?? '__generated__',
|
|
112
|
-
);
|
|
126
|
+
const generatedDir = getGeneratedDir(fileName, options);
|
|
113
127
|
const indexFile = path.join(generatedDir, 'index.js');
|
|
114
128
|
|
|
115
129
|
if (!fs.existsSync(generatedDir)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/generateTypeFiles.js"],"names":["indexPrelude","regenerateCommand","generateTypeFileContents","fileName","schema","document","options","generatedDir","indexContents","files","addToIndex","filePath","typeName","newLine","path","basename","indexOf","lines","split","map","line","includes","join","generated","forEach","name","code","isFragment","extraTypes","targetFileName","typeFileName","replace","targetPath","fileContents","splitTypes","Object","keys","generateTypeFiles","dirname","generatedDirectory","indexFile","fs","existsSync","mkdirSync","recursive","writeFileSync","readFileSync","key","processPragmas","rawSource","ignorePragma","autogen","loosePragma","autogenStrict","pragma","noPragmas","strictNullability","readOnlyArray","scalars","exportAllObjectTypes"],"mappings":";;;;;;;AAIA;;AACA;;AACA;;;;AALA;AAwBO,MAAMA,YAAY,GAAIC,iBAAD,IAAyC;AACrE;AACA;AACA;AACA;AACA,IAAIA,iBAAiB,GAAG,8BAA8BA,iBAAjC,GAAqD,EAAG;AAC7E;AACA;AACA,CARO;;;;AAUA,MAAMC,wBAAwB,GAAG,CACpCC,QADoC,EAEpCC,MAFoC,EAGpCC,QAHoC,EAIpCC,OAJoC,EAKpCC,YALoC,EAMpCC,aANoC,KAOsB;AAC1D,QAAMC,KAAK,GAAG,EAAd,CAD0D,CAG1D;;AACA,QAAMC,UAAU,GAAG,CAACC,QAAD,EAAWC,QAAX,KAAwB;AACvC,UAAMC,OAAO,GAAI,gBAAeD,QAAS,aAAYE,cAAKC,QAAL,CACjDJ,QADiD,CAEnD,IAFF;;AAGA,QAAIH,aAAa,CAACQ,OAAd,CAAsB,OAAOF,cAAKC,QAAL,CAAcJ,QAAd,CAA7B,MAA0D,CAAC,CAA/D,EAAkE;AAC9DH,MAAAA,aAAa,IAAIK,OAAO,GAAG,IAA3B;AACH,KAFD,MAEO;AACH,YAAMI,KAAK,GAAGT,aAAa,CAACU,KAAd,CAAoB,IAApB,EAA0BC,GAA1B,CAA+BC,IAAD,IAAU;AAClD,YAAIA,IAAI,CAACC,QAAL,CAAc,OAAOP,cAAKC,QAAL,CAAcJ,QAAd,CAArB,CAAJ,EAAmD;AAC/C,iBAAOE,OAAP;AACH;;AACD,eAAOO,IAAP;AACH,OALa,CAAd;AAMAZ,MAAAA,aAAa,GAAGS,KAAK,CAACK,IAAN,CAAW,IAAX,CAAhB;AACH;AACJ,GAf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1C,OAD0B,EAE1B2C,SAF0B,KAGT;AACjB,MAAI3C,OAAO,CAAC4C,YAAR,IAAwBD,SAAS,CAAC5B,QAAV,CAAmBf,OAAO,CAAC4C,YAA3B,CAA5B,EAAsE;AAClE,WAAO,IAAP;AACH;;AAED,QAAMC,OAAO,GAAG7C,OAAO,CAAC8C,WAAR,GACVH,SAAS,CAAC5B,QAAV,CAAmBf,OAAO,CAAC8C,WAA3B,CADU,GAEV,KAFN;AAGA,QAAMC,aAAa,GAAG/C,OAAO,CAACgD,MAAR,GAChBL,SAAS,CAAC5B,QAAV,CAAmBf,OAAO,CAACgD,MAA3B,CADgB,GAEhB,KAFN;AAGA,QAAMC,SAAS,GAAG,CAACjD,OAAO,CAAC8C,WAAT,IAAwB,CAAC9C,OAAO,CAACgD,MAAnD;;AAEA,MAAIH,OAAO,IAAIE,aAAX,IAA4BE,SAAhC,EAA2C;AACvC,WAAO;AACHtD,MAAAA,iBAAiB,EAAEK,OAAO,CAACL,iBADxB;AAEHuD,MAAAA,iBAAiB,EAAED,SAAS,GACtBjD,OAAO,CAACkD,iBADc,GAEtBH,aAAa,IAAI,CAACF,OAJrB;AAKHM,MAAAA,aAAa,EAAEnD,OAAO,CAACmD,aALpB;AAMHC,MAAAA,OAAO,EAAEpD,OAAO,CAACoD,OANd;AAOHxB,MAAAA,UAAU,EAAE5B,OAAO,CAAC4B,UAPjB;AAQHK,MAAAA,kBAAkB,EAAEjC,OAAO,CAACiC,kBARzB;AASHoB,MAAAA,oBAAoB,EAAErD,OAAO,CAACqD,oBAT3B;AAUH7B,MAAAA,YAAY,EAAExB,OAAO,CAACwB;AAVnB,KAAP;AAYH,GAbD,MAaO;AACH,WAAO,IAAP;AACH;AACJ,CAhCM","sourcesContent":["// @flow\n// Import this in your jest setup, to mock out graphql-tag!\nimport type {DocumentNode} from 'graphql';\nimport type {Options, Schema, Scalars} from './types';\nimport fs from 'fs';\nimport path from 'path';\nimport {documentToFlowTypes} from '.';\n\nexport type ExternalOptions = {\n pragma?: string,\n loosePragma?: string,\n ignorePragma?: string,\n scalars?: Scalars,\n strictNullability?: boolean,\n /**\n * The command that users should run to regenerate the types files.\n */\n regenerateCommand?: string,\n readOnlyArray?: boolean,\n splitTypes?: boolean,\n generatedDirectory?: string,\n exportAllObjectTypes?: boolean,\n typeFileName?: string,\n};\n\nexport const indexPrelude = (regenerateCommand?: string): string => `// @flow\n//\n// AUTOGENERATED\n// NOTE: New response types are added to this file automatically.\n// Outdated response types can be removed manually as they are deprecated.\n//${regenerateCommand ? ' To regenerate, run ' + regenerateCommand : ''}\n//\n\n`;\n\nexport const generateTypeFileContents = (\n fileName: string,\n schema: Schema,\n document: DocumentNode,\n options: Options,\n generatedDir: string,\n indexContents: string,\n): {indexContents: string, files: {[key: string]: string}} => {\n const files = {};\n\n /// Write export for __generated__/index.js if it doesn't exist\n const addToIndex = (filePath, typeName) => {\n const newLine = `export type {${typeName}} from './${path.basename(\n filePath,\n )}';`;\n if (indexContents.indexOf('./' + path.basename(filePath)) === -1) {\n indexContents += newLine + '\\n';\n } else {\n const lines = indexContents.split('\\n').map((line) => {\n if (line.includes('./' + path.basename(filePath))) {\n return newLine;\n }\n return line;\n });\n indexContents = lines.join('\\n');\n }\n };\n\n const generated = documentToFlowTypes(document, schema, options);\n generated.forEach(({name, typeName, code, isFragment, extraTypes}) => {\n // We write all generated files to a `__generated__` subdir to keep\n // things tidy.\n const targetFileName = options.typeFileName\n ? options.typeFileName.replace('[operationName]', name)\n : `${name}.js`;\n const targetPath = path.join(generatedDir, targetFileName);\n\n let fileContents =\n '// @' +\n `flow\\n// AUTOGENERATED -- DO NOT EDIT\\n` +\n `// Generated for operation '${name}' in file '../${path.basename(\n fileName,\n )}'\\n` +\n (options.regenerateCommand\n ? `// To regenerate, run '${options.regenerateCommand}'.\\n`\n : '') +\n code;\n if (options.splitTypes && !isFragment) {\n fileContents +=\n `\\nexport type ${name} = ${typeName}['response'];\\n` +\n `export type ${name}Variables = ${typeName}['variables'];\\n`;\n }\n Object.keys(extraTypes).forEach((name) => {\n fileContents += `\\n\\nexport type ${name} = ${extraTypes[name]};`;\n });\n\n addToIndex(targetPath, typeName);\n files[targetPath] =\n fileContents\n // Remove whitespace from the ends of lines; babel's generate sometimes\n // leaves them hanging around.\n .replace(/\\s+$/gm, '') + '\\n';\n });\n\n return {files, indexContents};\n};\n\nexport const generateTypeFiles = (\n fileName: string,\n schema: Schema,\n document: DocumentNode,\n options: Options,\n) => {\n const generatedDir = path.join(\n path.dirname(fileName),\n options.generatedDirectory ?? '__generated__',\n );\n const indexFile = path.join(generatedDir, 'index.js');\n\n if (!fs.existsSync(generatedDir)) {\n fs.mkdirSync(generatedDir, {recursive: true});\n }\n if (!fs.existsSync(indexFile)) {\n fs.writeFileSync(indexFile, indexPrelude(options.regenerateCommand));\n }\n\n const {indexContents, files} = generateTypeFileContents(\n fileName,\n schema,\n document,\n options,\n generatedDir,\n fs.readFileSync(indexFile, 'utf8'),\n );\n\n fs.writeFileSync(indexFile, indexContents);\n Object.keys(files).forEach((key) => {\n fs.writeFileSync(key, files[key]);\n });\n\n fs.writeFileSync(indexFile, indexContents);\n};\n\nexport const processPragmas = (\n options: ExternalOptions,\n rawSource: string,\n): null | Options => {\n if (options.ignorePragma && rawSource.includes(options.ignorePragma)) {\n return null;\n }\n\n const autogen = options.loosePragma\n ? rawSource.includes(options.loosePragma)\n : false;\n const autogenStrict = options.pragma\n ? rawSource.includes(options.pragma)\n : false;\n const noPragmas = !options.loosePragma && !options.pragma;\n\n if (autogen || autogenStrict || noPragmas) {\n return {\n regenerateCommand: options.regenerateCommand,\n strictNullability: noPragmas\n ? options.strictNullability\n : autogenStrict || !autogen,\n readOnlyArray: options.readOnlyArray,\n scalars: options.scalars,\n splitTypes: options.splitTypes,\n generatedDirectory: options.generatedDirectory,\n exportAllObjectTypes: options.exportAllObjectTypes,\n typeFileName: options.typeFileName,\n };\n } else {\n return null;\n }\n};\n"],"file":"generateTypeFiles.js"}
|
|
1
|
+
{"version":3,"sources":["../src/generateTypeFiles.js"],"names":["indexPrelude","regenerateCommand","generateTypeFileContents","fileName","schema","document","options","generatedDir","indexContents","files","addToIndex","filePath","typeName","newLine","path","basename","indexOf","lines","split","map","line","includes","join","generated","forEach","name","code","isFragment","extraTypes","targetFileName","typeFileName","replace","targetPath","fileContents","splitTypes","Object","keys","getGeneratedDir","generatedDirectory","isAbsolute","relative","process","cwd","dirname","generateTypeFiles","indexFile","fs","existsSync","mkdirSync","recursive","writeFileSync","readFileSync","key","processPragmas","rawSource","ignorePragma","autogen","loosePragma","autogenStrict","pragma","noPragmas","strictNullability","readOnlyArray","scalars","exportAllObjectTypes"],"mappings":";;;;;;;AAIA;;AACA;;AACA;;;;AALA;AAwBO,MAAMA,YAAY,GAAIC,iBAAD,IAAyC;AACrE;AACA;AACA;AACA;AACA,IAAIA,iBAAiB,GAAG,8BAA8BA,iBAAjC,GAAqD,EAAG;AAC7E;AACA;AACA,CARO;;;;AAUA,MAAMC,wBAAwB,GAAG,CACpCC,QADoC,EAEpCC,MAFoC,EAGpCC,QAHoC,EAIpCC,OAJoC,EAKpCC,YALoC,EAMpCC,aANoC,KAOsB;AAC1D,QAAMC,KAAK,GAAG,EAAd,CAD0D,CAG1D;;AACA,QAAMC,UAAU,GAAG,CAACC,QAAD,EAAWC,QAAX,KAAwB;AACvC,UAAMC,OAAO,GAAI,gBAAeD,QAAS,aAAYE,cAAKC,QAAL,CACjDJ,QADiD,CAEnD,IAFF;;AAGA,QAAIH,aAAa,CAACQ,OAAd,CAAsB,OAAOF,cAAKC,QAAL,CAAcJ,QAAd,CAA7B,MAA0D,CAAC,CAA/D,EAAkE;AAC9DH,MAAAA,aAAa,IAAIK,OAAO,GAAG,IAA3B;AACH,KAFD,MAEO;AACH,YAAMI,KAAK,GAAGT,aAAa,CAACU,KAAd,CAAoB,IAApB,EAA0BC,GAA1B,CAA+BC,IAAD,IAAU;AAClD,YAAIA,IAAI,CAACC,QAAL,CAAc,OAAOP,cAAKC,QAA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AO,CAACL,iBADxB;AAEH4D,MAAAA,iBAAiB,EAAED,SAAS,GACtBtD,OAAO,CAACuD,iBADc,GAEtBH,aAAa,IAAI,CAACF,OAJrB;AAKHM,MAAAA,aAAa,EAAExD,OAAO,CAACwD,aALpB;AAMHC,MAAAA,OAAO,EAAEzD,OAAO,CAACyD,OANd;AAOH7B,MAAAA,UAAU,EAAE5B,OAAO,CAAC4B,UAPjB;AAQHI,MAAAA,kBAAkB,EAAEhC,OAAO,CAACgC,kBARzB;AASH0B,MAAAA,oBAAoB,EAAE1D,OAAO,CAAC0D,oBAT3B;AAUHlC,MAAAA,YAAY,EAAExB,OAAO,CAACwB;AAVnB,KAAP;AAYH,GAbD,MAaO;AACH,WAAO,IAAP;AACH;AACJ,CAhCM","sourcesContent":["// @flow\n// Import this in your jest setup, to mock out graphql-tag!\nimport type {DocumentNode} from 'graphql';\nimport type {Options, Schema, Scalars} from './types';\nimport fs from 'fs';\nimport path from 'path';\nimport {documentToFlowTypes} from '.';\n\nexport type ExternalOptions = {\n pragma?: string,\n loosePragma?: string,\n ignorePragma?: string,\n scalars?: Scalars,\n strictNullability?: boolean,\n /**\n * The command that users should run to regenerate the types files.\n */\n regenerateCommand?: string,\n readOnlyArray?: boolean,\n splitTypes?: boolean,\n generatedDirectory?: string,\n exportAllObjectTypes?: boolean,\n typeFileName?: string,\n};\n\nexport const indexPrelude = (regenerateCommand?: string): string => `// @flow\n//\n// AUTOGENERATED\n// NOTE: New response types are added to this file automatically.\n// Outdated response types can be removed manually as they are deprecated.\n//${regenerateCommand ? ' To regenerate, run ' + regenerateCommand : ''}\n//\n\n`;\n\nexport const generateTypeFileContents = (\n fileName: string,\n schema: Schema,\n document: DocumentNode,\n options: Options,\n generatedDir: string,\n indexContents: string,\n): {indexContents: string, files: {[key: string]: string}} => {\n const files = {};\n\n /// Write export for __generated__/index.js if it doesn't exist\n const addToIndex = (filePath, typeName) => {\n const newLine = `export type {${typeName}} from './${path.basename(\n filePath,\n )}';`;\n if (indexContents.indexOf('./' + path.basename(filePath)) === -1) {\n indexContents += newLine + '\\n';\n } else {\n const lines = indexContents.split('\\n').map((line) => {\n if (line.includes('./' + path.basename(filePath))) {\n return newLine;\n }\n return line;\n });\n indexContents = lines.join('\\n');\n }\n };\n\n const generated = documentToFlowTypes(document, schema, options);\n generated.forEach(({name, typeName, code, isFragment, extraTypes}) => {\n // We write all generated files to a `__generated__` subdir to keep\n // things tidy.\n const targetFileName = options.typeFileName\n ? options.typeFileName.replace('[operationName]', name)\n : `${name}.js`;\n const targetPath = path.join(generatedDir, targetFileName);\n\n let fileContents =\n '// @' +\n `flow\\n// AUTOGENERATED -- DO NOT EDIT\\n` +\n `// Generated for operation '${name}' in file '../${path.basename(\n fileName,\n )}'\\n` +\n (options.regenerateCommand\n ? `// To regenerate, run '${options.regenerateCommand}'.\\n`\n : '') +\n code;\n if (options.splitTypes && !isFragment) {\n fileContents +=\n `\\nexport type ${name} = ${typeName}['response'];\\n` +\n `export type ${name}Variables = ${typeName}['variables'];\\n`;\n }\n Object.keys(extraTypes).forEach((name) => {\n fileContents += `\\n\\nexport type ${name} = ${extraTypes[name]};`;\n });\n\n addToIndex(targetPath, typeName);\n files[targetPath] =\n fileContents\n // Remove whitespace from the ends of lines; babel's generate sometimes\n // leaves them hanging around.\n .replace(/\\s+$/gm, '') + '\\n';\n });\n\n return {files, indexContents};\n};\n\nconst getGeneratedDir = (fileName: string, options: Options) => {\n const generatedDirectory = options.generatedDirectory ?? '__generated__';\n if (path.isAbsolute(generatedDirectory)) {\n // fileName is absolute here, so we make it relative to cwd\n // for more reasonable filenames. We convert leading ..'s\n // to `__` so this doesn't escape the output directory.\n return path.join(\n generatedDirectory,\n path\n .relative(process.cwd(), path.dirname(fileName))\n .replace(/\\.\\.\\//g, '__/'),\n );\n } else {\n return path.join(path.dirname(fileName), generatedDirectory);\n }\n};\n\nexport const generateTypeFiles = (\n fileName: string,\n schema: Schema,\n document: DocumentNode,\n options: Options,\n) => {\n const generatedDir = getGeneratedDir(fileName, options);\n const indexFile = path.join(generatedDir, 'index.js');\n\n if (!fs.existsSync(generatedDir)) {\n fs.mkdirSync(generatedDir, {recursive: true});\n }\n if (!fs.existsSync(indexFile)) {\n fs.writeFileSync(indexFile, indexPrelude(options.regenerateCommand));\n }\n\n const {indexContents, files} = generateTypeFileContents(\n fileName,\n schema,\n document,\n options,\n generatedDir,\n fs.readFileSync(indexFile, 'utf8'),\n );\n\n fs.writeFileSync(indexFile, indexContents);\n Object.keys(files).forEach((key) => {\n fs.writeFileSync(key, files[key]);\n });\n\n fs.writeFileSync(indexFile, indexContents);\n};\n\nexport const processPragmas = (\n options: ExternalOptions,\n rawSource: string,\n): null | Options => {\n if (options.ignorePragma && rawSource.includes(options.ignorePragma)) {\n return null;\n }\n\n const autogen = options.loosePragma\n ? rawSource.includes(options.loosePragma)\n : false;\n const autogenStrict = options.pragma\n ? rawSource.includes(options.pragma)\n : false;\n const noPragmas = !options.loosePragma && !options.pragma;\n\n if (autogen || autogenStrict || noPragmas) {\n return {\n regenerateCommand: options.regenerateCommand,\n strictNullability: noPragmas\n ? options.strictNullability\n : autogenStrict || !autogen,\n readOnlyArray: options.readOnlyArray,\n scalars: options.scalars,\n splitTypes: options.splitTypes,\n generatedDirectory: options.generatedDirectory,\n exportAllObjectTypes: options.exportAllObjectTypes,\n typeFileName: options.typeFileName,\n };\n } else {\n return null;\n }\n};\n"],"file":"generateTypeFiles.js"}
|
package/package.json
CHANGED
package/src/cli/config.js
CHANGED
|
@@ -78,10 +78,9 @@ export const loadConfigFile = (configFile: string): CliConfig => {
|
|
|
78
78
|
return {
|
|
79
79
|
options: data.options ?? {},
|
|
80
80
|
excludes: data.excludes?.map((string) => new RegExp(string)) ?? [],
|
|
81
|
-
schemaFilePath: path.
|
|
82
|
-
|
|
83
|
-
data.schemaFilePath,
|
|
84
|
-
),
|
|
81
|
+
schemaFilePath: path.isAbsolute(data.schemaFilePath)
|
|
82
|
+
? data.schemaFilePath
|
|
83
|
+
: path.join(path.dirname(configFile), data.schemaFilePath),
|
|
85
84
|
dumpOperations: data.dumpOperations,
|
|
86
85
|
};
|
|
87
86
|
};
|
package/src/generateTypeFiles.js
CHANGED
|
@@ -100,16 +100,30 @@ export const generateTypeFileContents = (
|
|
|
100
100
|
return {files, indexContents};
|
|
101
101
|
};
|
|
102
102
|
|
|
103
|
+
const getGeneratedDir = (fileName: string, options: Options) => {
|
|
104
|
+
const generatedDirectory = options.generatedDirectory ?? '__generated__';
|
|
105
|
+
if (path.isAbsolute(generatedDirectory)) {
|
|
106
|
+
// fileName is absolute here, so we make it relative to cwd
|
|
107
|
+
// for more reasonable filenames. We convert leading ..'s
|
|
108
|
+
// to `__` so this doesn't escape the output directory.
|
|
109
|
+
return path.join(
|
|
110
|
+
generatedDirectory,
|
|
111
|
+
path
|
|
112
|
+
.relative(process.cwd(), path.dirname(fileName))
|
|
113
|
+
.replace(/\.\.\//g, '__/'),
|
|
114
|
+
);
|
|
115
|
+
} else {
|
|
116
|
+
return path.join(path.dirname(fileName), generatedDirectory);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
103
120
|
export const generateTypeFiles = (
|
|
104
121
|
fileName: string,
|
|
105
122
|
schema: Schema,
|
|
106
123
|
document: DocumentNode,
|
|
107
124
|
options: Options,
|
|
108
125
|
) => {
|
|
109
|
-
const generatedDir =
|
|
110
|
-
path.dirname(fileName),
|
|
111
|
-
options.generatedDirectory ?? '__generated__',
|
|
112
|
-
);
|
|
126
|
+
const generatedDir = getGeneratedDir(fileName, options);
|
|
113
127
|
const indexFile = path.join(generatedDir, 'index.js');
|
|
114
128
|
|
|
115
129
|
if (!fs.existsSync(generatedDir)) {
|