-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
66 lines (58 loc) · 1.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const R = require('ramda')
const { EOL } = require('os')
const {
name,
parseSchema,
parseMessageDefinition,
schemaMessageDefinitions,
writeFileSafe,
filesFromDir,
log,
noNullElementArray,
generate,
env,
prepareFile,
cp,
match,
} = require('./lib')
const generators = require('./generators')
function parse(
messageDefinitionDir = env('MESSAGE_DEFINITION_DIR', 'schema/message-schema-definitions'),
schema = env('SCHEMA', '/schema/integration-schema.gql')
) {
schema = parseSchema(process.cwd() + (process.env.SCHEMA || '/schema/integration-schema.gql'))
const messageFiles = filesFromDir(messageDefinitionDir, ['graphql', 'gql'])
const schemaMessageDefs = schemaMessageDefinitions(schema)
log(EOL + EOL + `[INFO] Processing message Definitions:`)
const messages = noNullElementArray(
R.map((messageFileName) => {
const message = parseMessageDefinition(`${process.cwd()}/${messageFileName}`)
return match(name(message))
.on(
(n) => schemaMessageDefs.includes(n),
(n) => {
log(` - ${n} (${messageFileName})`)
message.fileName = messageFileName
return message
}
)
.otherwise((n) => {
log(EOL + `[ERROR] ${n} is not defined in the schema` + EOL)
return null
})
}, messageFiles)
)
const code = generate(messages, schema, generators, messageDefinitionDir)
R.forEach(
(file) =>
writeFileSafe(...prepareFile(file.outputFileName, file.toDir, file.path, file.extension, file.code, messageDefinitionDir)),
code.filter(Boolean)
)
R.forEach(({ staticDir, toDir }) => {
cp(staticDir, toDir)
}, generators)
}
// Make runnable through npm
if (!module.parent) parse()
// Or runnable in other scripts
module.exports = parse