forked from kendraio/kendraio-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.ts
90 lines (81 loc) · 3.41 KB
/
migrate.ts
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
import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'fs';
import { get} from 'lodash';
import { parse } from 'yaml';
mkdirSync('adapters');
const dir = readdirSync('prev-adapters');
const needMigration = dir.filter(key => !existsSync(`adapters/${key}`));
needMigration.forEach(key => {
const data = parse(readFileSync(`prev-adapters/${key}/adapter.yaml`, 'utf-8'));
const name = get(data, 'name', key);
const label = get(data, 'title', key);
const description = get(data, 'description', `Adapter for ${label}`);
const infoUrl = get(data, 'infoUrl', 'https://kendra.io');
const maintainer = get(data, 'maintainer', 'Kendraio');
const supportUrl = get(data, 'supportUrl', 'https://kendra.io');
const version = get(data, 'version', '0.0.0');
const uploads = get(data, 'uploads', false);
mkdirSync(`adapters/${key}`);
mkdirSync(`adapters/${key}/schemas`);
mkdirSync(`adapters/${key}/forms`);
mkdirSync(`adapters/${key}/configs`);
const hasSwagger = get(data, 'swagger', false);
let swagger: string|boolean = false;
if (!!hasSwagger) {
const swaggerParts = hasSwagger.split('/');
const swaggerName = swaggerParts[swaggerParts.length - 1];
swagger = `swagger/${swaggerName}`;
mkdirSync(`adapters/${key}/swagger`);
copyFileSync(hasSwagger, `adapters/${key}/${swagger}`);
}
const database = get(data, 'database', []).map((sourceSchema: string) => {
const schemaPathParts = sourceSchema.split('/');
const name = schemaPathParts[schemaPathParts.length - 1];
const schema = `schemas/${name}`;
copyFileSync(sourceSchema, `adapters/${key}/${schema}`);
return ({ name, schema });
});
const workflow = get(data, 'configs', []).map(sourceConfig => {
const configData = JSON.parse(readFileSync(sourceConfig, 'utf-8'));
const configPathParts = sourceConfig.split('/');
const fileName = configPathParts[configPathParts.length - 1];
const title = get(configData, 'title', `${sourceConfig}`);
const config = `configs/${fileName}`;
copyFileSync(sourceConfig, `adapters/${key}/${config}`);
return ({ title, config })
});
const sourceForms = get(data, 'forms', {});
const forms = Object.keys(sourceForms).map((formId: string) => {
const { jsonSchema, uiSchema } = sourceForms[formId];
const jsonSchemaData = JSON.parse(readFileSync(jsonSchema, 'utf-8'));
const title = get(jsonSchemaData, 'title', formId);
const jsonSchemaPathParts = jsonSchema.split('/');
const jsonSchemaName = jsonSchemaPathParts[jsonSchemaPathParts.length - 1];
const newJsonSchema = `schemas/${jsonSchemaName}`;
if (!existsSync(newJsonSchema)) {
copyFileSync(jsonSchema, `adapters/${key}/${newJsonSchema}`);
}
const uiSchemaPathParts = uiSchema.split('/');
const uiSchemaName = uiSchemaPathParts[uiSchemaPathParts.length - 1];
const newUiSchema = `forms/${uiSchemaName}`;
copyFileSync(uiSchema, `adapters/${key}/${newUiSchema}`);
return ({ formId, title, jsonSchema: newJsonSchema, uiSchema: newUiSchema });
});
try {
writeFileSync(`adapters/${key}/kendraio-adapter.json`, JSON.stringify({
name,
label,
description,
version,
infoUrl,
maintainer,
supportUrl,
database,
workflow,
forms,
...(uploads) ? { upload: { enabled: true }} : {},
...(!!swagger) ? { apis: [ swagger ]} : {}
}, null, 2));
} catch (e) {
console.error(e);
}
});