Skip to content

Commit 8b09991

Browse files
authored
Merge pull request #85 from h-kishi/feat/glob_pattern
2 parents 7c18f11 + 57a707e commit 8b09991

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"cfn-resolver-lib": "^1.1.7",
3030
"dataloader": "^2.0.0",
3131
"fb-watchman": "^2.0.1",
32+
"globby": "^11.0.3",
3233
"jest": "^26.6.3",
3334
"lodash": "^4.17.20",
3435
"merge-graphql-schemas": "^1.5.8"

src/__tests__/getAppSyncConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ describe('getAppSyncConfig', () => {
66
const config = {
77
name: 'myAPI',
88
authenticationType: 'API_KEY',
9+
schema: '*.graphql',
910
defaultMappingTemplates: {
1011
request: 'default.request.vtl',
1112
response: 'default.response.vtl',

src/getAppSyncConfig.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import fs from 'fs';
55
import { forEach, isNil, first } from 'lodash';
66
import path from 'path';
77
import { mergeTypes } from 'merge-graphql-schemas';
8+
import * as globby from 'globby';
89
import directLambdaRequest from './templates/direct-lambda.request.vtl';
910
import directLambdaResponse from './templates/direct-lambda.response.vtl';
1011

@@ -35,9 +36,26 @@ export default function getAppSyncConfig(context, appSyncConfig) {
3536
});
3637
};
3738

39+
const toAbsolutePath = (basePath, filePath) =>
40+
path.isAbsolute(filePath) ? filePath : path.join(basePath, filePath);
41+
42+
const globFilePaths = (basePath, filePaths) => {
43+
return filePaths
44+
.map((filePath) => {
45+
const paths = globby.sync(toAbsolutePath(basePath, filePath));
46+
if (path.isAbsolute(filePath)) {
47+
return paths;
48+
} else {
49+
// For backward compatibility with FileMap, revert to relative path
50+
return paths.map((p) => path.relative(basePath, p));
51+
}
52+
})
53+
.flat();
54+
};
55+
3856
const getFileMap = (basePath, filePath) => ({
3957
path: filePath,
40-
content: fs.readFileSync(path.join(basePath, filePath), {
58+
content: fs.readFileSync(toAbsolutePath(basePath, filePath), {
4159
encoding: 'utf8',
4260
}),
4361
});
@@ -208,8 +226,9 @@ export default function getAppSyncConfig(context, appSyncConfig) {
208226
const schemaPaths = Array.isArray(cfg.schema)
209227
? cfg.schema
210228
: [cfg.schema || 'schema.graphql'];
211-
const schemas = schemaPaths.map((schemaPath) =>
212-
getFileMap(context.serverless.config.servicePath, schemaPath),
229+
const basePath = context.serverless.config.servicePath;
230+
const schemas = globFilePaths(basePath, schemaPaths).map((schemaPath) =>
231+
getFileMap(basePath, schemaPath),
213232
);
214233
const schema = {
215234
path: first(schemas).path,

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4030,7 +4030,7 @@ [email protected]:
40304030
merge2 "^1.3.0"
40314031
slash "^3.0.0"
40324032

4033-
globby@^11.0.0, globby@^11.0.1:
4033+
globby@^11.0.0, globby@^11.0.1, globby@^11.0.3:
40344034
version "11.0.3"
40354035
resolved "https://registry.yarnpkg.com/globby/-/globby-11.0.3.tgz#9b1f0cb523e171dd1ad8c7b2a9fb4b644b9593cb"
40364036
integrity sha512-ffdmosjA807y7+lA1NM0jELARVmYul/715xiILEjo3hBLPTcirgQNnXECn5g3mtR8TOLCVbkfua1Hpen25/Xcg==

0 commit comments

Comments
 (0)