-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate-schemas.js
More file actions
executable file
·200 lines (158 loc) · 5.12 KB
/
Copy pathcreate-schemas.js
File metadata and controls
executable file
·200 lines (158 loc) · 5.12 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env node
/* eslint-disable no-console, no-use-before-define, no-param-reassign */
const getopts = require('getopts');
const fs = require('fs');
const execSync = require('child_process').execSync;
const jp = require('jsonpath');
const globalOptions = getopts(process.argv.slice(2), {
boolean: ['help', 'fetch', 'rewrite', 'overlay'],
alias: {
h: 'help',
},
default: {
fetch: false,
rewrite: false,
overlay: false,
}
});
if (globalOptions.help || globalOptions._.length !== 1) {
console.error(`Usage: ${process.argv[1]} [--(no-)fetch] [--(no-)rewrite] [--(no-)overlay] schemaconf.json`);
process.exit(1);
}
const configName = globalOptions._[0];
const globalConfig = parseSchema(configName);
createSchemas(globalConfig, globalOptions);
process.exit(0);
function createSchemas(config, options) {
config.forEach(moduleConfig => createModuleSchemas(moduleConfig, options));
}
function createModuleSchemas(moduleConfig, options) {
const { module, release, ramlPath, copyFiles, overlays } = moduleConfig;
if (options.fetch) {
system(`rm -rf ${module}`);
}
if (!fs.existsSync(module)) {
obtainSchemas(module, release, ramlPath);
}
if (options.rewrite || options.overlay) {
if (copyFiles) {
copyFiles.forEach(entry => {
system(`cp -r ${entry} ${module}/ramls/`);
});
}
if (overlays) {
Object.keys(overlays).sort().forEach(schemaName => {
if (options.overlay) {
handleOverlaysForSchema(module, schemaName, overlays[schemaName]);
} else {
console.log(` Skipping overlays for schema ${schemaName}`);
}
});
}
}
}
function obtainSchemas(module, release, ramlPath) {
console.log(`Obtaining schemas for ${module} ${release}`);
// There may be a better way to do this, but cloning the source from
// a well-known GitHub organization, checking out the relevant
// release tag, and removing all but the `raml` directory will
// suffice for now. It's fragile, though.
system(`git clone --recurse-submodules https://github.com/folio-org/${module}`);
process.chdir(module);
system(`git checkout --quiet ${release}`);
system('git submodule update --init --recursive');
process.chdir('..');
system(`mv ${module}/${ramlPath || 'ramls'} ramls`);
system(`rm -rf ${module}`);
system(`mkdir ${module}`);
system(`mv ramls ${module}`);
}
function handleOverlaysForSchema(module, schemaName, schemaOverlays) {
console.log(` Handling overlays for schema ${schemaName}`);
const schema = parseSchema(`${module}/ramls/${schemaName}`);
Object.keys(schemaOverlays).sort().forEach(jsonPath => {
const overlay = schemaOverlays[jsonPath];
handleOverlay(schema, jsonPath, overlay);
});
writeSchema(`${module}/ramls/${schemaName}`, schema);
}
function handleOverlay(schema, jsonPath, overlay) {
console.log(` Handling overlay at ${jsonPath}`);
if (typeof overlay === 'string') {
overlay = expandOverlaySummary(overlay);
}
const res = jsonPath.match(/(.*)\.(.*)/);
let basePath, insertAs;
if (res) {
basePath = `$.properties.${res[1]}`;
insertAs = res[2];
} else {
basePath = '$.properties';
insertAs = jsonPath;
}
const target = jp.query(schema, basePath);
if (target.length === 0) {
console.warn(`*** could not find basePath ${basePath}`);
} else {
console.log(` -- jsonPath='${jsonPath}' -> (${basePath}, ${insertAs})`);
target[0][insertAs] = overlay;
}
}
// Example: "callnumbertype.json call-number-types?id=itemLevelCallNumberTypeId callNumberTypes.0"
function expandOverlaySummary(summary) {
const regexp = /^(.*?) (.*?)\?(.*?)=(.*?) (.*)$/;
const res = summary.match(regexp);
if (!res) {
throw Error(`bad overlay summary: '${summary}'`);
}
// eslint-disable-next-line no-unused-vars
const [__UNUSED, schemaRef, linkBase, linkToField, linkFromFieldAndArgs, includedElement] = res;
let linkFromField, extraArgs;
const res2 = linkFromFieldAndArgs.match(/^(.*)?&(.*)/);
if (res2) {
linkFromField = res2[1];
extraArgs = res2[2];
} else {
linkFromField = linkFromFieldAndArgs;
}
const virtualFields = {
'readonly': true,
'folio:isVirtual': true,
'folio:linkBase': linkBase,
'folio:linkFromField': linkFromField,
'folio:linkToField': linkToField,
'folio:includedElement': includedElement,
};
if (extraArgs !== undefined) {
virtualFields['folio:extraArgs'] = extraArgs;
}
if (schemaRef.endsWith('[]')) {
return {
'type': 'array',
'items': {
'type': 'object',
'$ref': schemaRef.substring(0, schemaRef.length - 2)
},
...virtualFields
};
} else {
return {
'type': 'object',
'folio:$ref': schemaRef,
...virtualFields
};
}
}
function parseSchema(fileName) {
const schemaText = fs.readFileSync(fileName, 'utf8');
return JSON.parse(schemaText);
}
function writeSchema(fileName, schemaObj) {
const schemaText = JSON.stringify(schemaObj, null, 2);
fs.writeFileSync(fileName, schemaText, 'utf8');
}
function system(command) {
const output = execSync(command);
console.log(` -- ${command}`);
process.stdout.write(output);
}