-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathapiSpecs.js
More file actions
120 lines (106 loc) · 3.89 KB
/
apiSpecs.js
File metadata and controls
120 lines (106 loc) · 3.89 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
const fs = require('fs');
const path = require('path');
const { dialog, ipcMain } = require('electron');
const { normalizeAndResolvePath } = require('../utils/filesystem');
const { generateUidBasedOnHash } = require('../utils/common');
const {
addApiSpecToWorkspace,
readWorkspaceConfig,
getWorkspaceUid
} = require('../utils/workspace-config');
const DEFAULT_WORKSPACE_NAME = 'My Workspace';
const normalizeWorkspaceConfig = (config) => {
return {
...config,
name: config.info?.name,
type: config.info?.type,
collections: config.collections || [],
apiSpecs: config.specs || []
};
};
const prepareWorkspaceConfigForClient = (workspaceConfig, isDefault) => {
if (isDefault) {
return {
...workspaceConfig,
name: workspaceConfig.name || DEFAULT_WORKSPACE_NAME,
type: 'default'
};
}
return workspaceConfig;
};
const openApiSpecDialog = async (win, watcher, options = {}) => {
const { filePaths } = await dialog.showOpenDialog(win, {
properties: ['openFile', 'createFile']
});
if (filePaths && filePaths[0]) {
const resolvedPath = normalizeAndResolvePath(filePaths[0]);
try {
await openApiSpec(win, watcher, resolvedPath, options);
} catch (err) {
console.error(`[ERROR] Cannot open API spec: "${resolvedPath}"`);
}
}
};
const openApiSpec = async (win, watcher, apiSpecPath, options = {}) => {
try {
const uid = generateUidBasedOnHash(apiSpecPath);
if (options.workspacePath) {
const workspaceFilePath = path.join(options.workspacePath, 'workspace.yml');
if (fs.existsSync(workspaceFilePath)) {
const workspaceConfig = readWorkspaceConfig(options.workspacePath);
const specs = workspaceConfig.specs || [];
const specName = path.basename(apiSpecPath, path.extname(apiSpecPath));
const existingSpec = specs.find((a) => {
if (!a.path) return false;
const existingPath = path.isAbsolute(a.path)
? a.path
: path.resolve(options.workspacePath, a.path);
return existingPath === apiSpecPath || a.name === specName;
});
if (!existingSpec) {
await addApiSpecToWorkspace(options.workspacePath, {
name: specName,
path: apiSpecPath
});
const updatedConfig = readWorkspaceConfig(options.workspacePath);
const normalizedConfig = normalizeWorkspaceConfig(updatedConfig);
const workspaceUid = getWorkspaceUid(options.workspacePath);
const isDefault = workspaceUid === 'default';
const configForClient = prepareWorkspaceConfigForClient(normalizedConfig, isDefault);
win.webContents.send('main:workspace-config-updated', options.workspacePath, workspaceUid, configForClient);
}
}
}
if (!watcher.hasWatcher(apiSpecPath)) {
ipcMain.emit('main:apispec-opened', win, apiSpecPath, uid, options.workspacePath);
} else {
win.webContents.send('main:apispec-tree-updated', 'addFile', {
pathname: apiSpecPath,
uid: uid,
raw: require('fs').readFileSync(apiSpecPath, 'utf8'),
name: require('path').basename(apiSpecPath, require('path').extname(apiSpecPath)),
filename: require('path').basename(apiSpecPath),
json: (() => {
const ext = require('path').extname(apiSpecPath).toLowerCase();
const content = require('fs').readFileSync(apiSpecPath, 'utf8');
if (ext === '.yaml' || ext === '.yml') {
return require('js-yaml').load(content);
} else if (ext === '.json') {
return JSON.parse(content);
}
return null;
})()
});
}
} catch (err) {
if (!options.dontSendDisplayErrors) {
win.webContents.send('main:display-error', {
error: err.message || 'An error occurred while opening the apiSpec'
});
}
}
};
module.exports = {
openApiSpec,
openApiSpecDialog
};