Skip to content

Commit 12ce779

Browse files
TheZokeredgarmueller
authored andcommitted
Outsource vscode and theia code to the tooling package (#6)
1 parent 8f3c074 commit 12ce779

File tree

3 files changed

+195
-345
lines changed

3 files changed

+195
-345
lines changed

jsonforms-tooling-common/src/index.ts

+167-59
Original file line numberDiff line numberDiff line change
@@ -8,86 +8,116 @@ import * as Ajv from 'ajv';
88
import { sep } from 'path';
99
const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
1010

11+
export enum Project {
12+
Example = 'example',
13+
Seed = 'seed',
14+
}
15+
1116
/*
12-
* Clones a git repository and runs npm install on it
13-
* @param {string} repo the name of the repo that should be cloned
14-
* @param {string} path to the folder, where the repo should be cloned into
15-
* @param {function} callback forwards the current status to the caller
17+
* Receives the data from the editor and calls the install methos
18+
* @param {any} editorInstance the instance of the editor
19+
* @param {string} path the arguments passed to the editor call
20+
* @param {string} project the project, that should be installed
1621
*/
17-
export const cloneAndInstall = (
18-
repo: string,
19-
path: string,
20-
callback: (id: string, result: string, type?: string) => void,
21-
name?: string) => {
22-
let url = '';
23-
switch (repo) {
24-
case 'example':
25-
url = 'https://github.com/eclipsesource/make-it-happen-react';
26-
break;
27-
case 'seed':
28-
url = 'https://github.com/eclipsesource/jsonforms-react-seed';
29-
break;
30-
default:
31-
return;
22+
export const createProject = (editorInstance: any, path: string, project: string) => {
23+
if (!path) {
24+
editorInstance.window.showOpenDialog(editorInstance.OpenDialogOptions = {
25+
canSelectMany: false,
26+
canSelectFolders: true,
27+
canSelectFiles: false,
28+
openLabel: 'Select folder',
29+
}).then((fileUri: any) => {
30+
if (fileUri && fileUri[0].fsPath) {
31+
asyncCreateProject(editorInstance, fileUri[0].fsPath, project);
32+
} else {
33+
showMessage(editorInstance, 'Please select a empty folder', 'err');
34+
return;
35+
}
36+
});
37+
} else {
38+
asyncCreateProject(editorInstance, path, project);
3239
}
33-
const git = simplegit();
34-
callback('start-cloning', 'Starting to clone repo');
35-
git.clone(url, path)
36-
.then(() => {
37-
callback('finished-cloning', 'Finished to clone repo');
38-
callback('npm-install', 'Running npm install');
39-
const result = cp.spawnSync(npm, ['install'], {
40-
cwd: path,
41-
});
42-
callback('signal', result.signal);
43-
})
44-
.catch((err: any) => { callback('error', err.message, 'err'); });
4540
};
4641

4742
/**
4843
* Generates the default UI Schema from a json schema
49-
* @param {string} path path to the json schema file
50-
* @param {function} callback forwards the current status to the caller
44+
* @param {any} editorInstance the instance of the editor
45+
* @param {string} path the arguments passed to the editor call
5146
*/
52-
export const generateUISchema = (
53-
path: string,
54-
name: string,
55-
callback: (id: string, result: string, type?: string) => void) => {
56-
// Read JSON Schema file
57-
readFile(path, 'utf8', (readError, data) => {
58-
if (readError.message) {
59-
callback('error', readError.message, 'err');
60-
return;
61-
}
62-
63-
const jsonSchema = JSON.parse(data);
64-
validateJSONSchema(jsonSchema, (validateError?: string) => {
65-
if (validateError) {
66-
callback('error', validateError, 'err');
47+
export const generateUISchema = (editorInstance: any, path: string) => {
48+
if (!path) {
49+
editorInstance.window.showOpenDialog(editorInstance.OpenDialogOptions = {
50+
canSelectMany: false,
51+
canSelectFolders: false,
52+
canSelectFiles: true,
53+
openLabel: 'Select schema',
54+
filters: {
55+
'Json Files': ['json'],
56+
},
57+
}).then((fileUri: any) => {
58+
if (fileUri && fileUri[0].fsPath) {
59+
asyncGenerateUiSchema(editorInstance, fileUri[0].fsPath);
60+
} else {
61+
showMessage('Please select a json schema file', 'err');
6762
return;
6863
}
64+
});
65+
} else {
66+
asyncGenerateUiSchema(editorInstance, path);
67+
}
68+
};
6969

70-
const jsonUISchema = jsonforms.generateDefaultUISchema(jsonSchema);
71-
72-
// Check if windows or linux filesystem
73-
let newPath = path.substring(0, path.lastIndexOf(sep));
74-
newPath = newPath + sep + name;
70+
/**
71+
* Async Generate UI Schema
72+
* @param {any} editorInstance the instance of the editor
73+
* @param {string} path the path to the schema file
74+
*/
75+
const asyncGenerateUiSchema = (editorInstance: any, path: string) => {
76+
editorInstance.window.showInputBox(editorInstance.InputBoxOptions = {
77+
prompt: 'Label: ',
78+
placeHolder: 'Enter a filename for your UI Schema (default: ui-schema.json)',
79+
}).then((name: string) => {
80+
let fileName = name;
81+
if (!fileName) {
82+
fileName = 'ui-schema.json';
83+
}
84+
showMessage(editorInstance, `Generating UI Schema: ${path}`);
85+
// Read JSON Schema file
86+
readFile(path, 'utf8', (readError, data) => {
87+
if (readError.message) {
88+
showMessage(editorInstance, readError.message, 'err');
89+
return;
90+
}
7591

76-
// Write UI Schema file
77-
writeFile(newPath, JSON.stringify(jsonUISchema, null, 2), writeError => {
78-
if (writeError.message) {
79-
callback('error', writeError.message, 'err');
92+
const jsonSchema = JSON.parse(data);
93+
validateJSONSchema(jsonSchema, (validateError?: string) => {
94+
if (validateError) {
95+
showMessage(editorInstance, validateError, 'err');
8096
return;
8197
}
82-
callback('success', 'Successfully generated UI schema');
98+
99+
const jsonUISchema = jsonforms.generateDefaultUISchema(jsonSchema);
100+
101+
// Check if windows or linux filesystem
102+
let newPath = path.substring(0, path.lastIndexOf(sep));
103+
newPath = newPath + sep + name;
104+
105+
// Write UI Schema file
106+
writeFile(newPath, JSON.stringify(jsonUISchema, null, 2), writeError => {
107+
if (writeError.message) {
108+
showMessage(editorInstance, writeError.message, 'err');
109+
return;
110+
}
111+
showMessage(editorInstance, 'Successfully generated UI schema');
112+
});
83113
});
84114
});
85115
});
86116
};
87117

88118
/**
89119
* Validate a given JSON Schema
90-
* @param {string} path path to the json schema file
120+
* @param {Object} schema the json schema, that will be validated
91121
* @param {function} callback forwards the current status to the caller
92122
*/
93123
const validateJSONSchema = (schema: Object, callback: (err?: string) => void) => {
@@ -99,3 +129,81 @@ const validateJSONSchema = (schema: Object, callback: (err?: string) => void) =>
99129
callback(error.message);
100130
}
101131
};
132+
133+
/**
134+
* Show message within the editor
135+
* @param {any} editorInstance the instance of the editor
136+
* @param {string} message the message that should be displayed
137+
* @param {string} type the optional type of the message
138+
*/
139+
const showMessage = (editorInstance: any, message: string, type?: string) => {
140+
switch (type) {
141+
case 'err':
142+
editorInstance.window.showErrorMessage(message);
143+
break;
144+
case 'war':
145+
editorInstance.window.showWarningMessage(message);
146+
break;
147+
default:
148+
editorInstance.window.showInformationMessage(message);
149+
}
150+
};
151+
152+
/**
153+
* Async Creating Project
154+
* @param {any} editorInstance the instance of the editor
155+
* @param {string} path the path to the project folder
156+
* @param {string} project the project, that will be created
157+
*/
158+
const asyncCreateProject = (editorInstance: any, path: string, project: string) => {
159+
let url = '';
160+
switch (project) {
161+
case Project.Example:
162+
url = 'https://github.com/eclipsesource/make-it-happen-react';
163+
break;
164+
case Project.Seed:
165+
url = 'https://github.com/eclipsesource/jsonforms-react-seed';
166+
break;
167+
default:
168+
return;
169+
}
170+
if (project === Project.Example) {
171+
showMessage(editorInstance, `Creating example project: ${path}`);
172+
cloneAndInstall(editorInstance, url, path);
173+
return;
174+
}
175+
176+
editorInstance.window.showInputBox(editorInstance.InputBoxOptions = {
177+
prompt: 'Label: ',
178+
placeHolder: `Enter a name for your ${project} project`,
179+
}).then((name: any) => {
180+
let projectName = name;
181+
if (!name) {
182+
projectName = `jsonforms-${project}`;
183+
} else {
184+
showMessage(editorInstance, `Creating ${project} project: ${path}`);
185+
cloneAndInstall(editorInstance, url, path, projectName);
186+
}
187+
});
188+
};
189+
190+
/**
191+
* Async Clone And Install
192+
* @param {any} editorInstance the instance of the editor
193+
* @param {string} url the url to the project repository
194+
* @param {string} path the path to the project folder
195+
* @param {string} projectName the name of the project
196+
*/
197+
const cloneAndInstall = (editorInstance: any, url: string, path: string, projectName?: string) => {
198+
const git = simplegit();
199+
git.clone(url, path)
200+
.then(() => {
201+
showMessage(editorInstance, 'Finished to clone repo');
202+
showMessage(editorInstance, 'Running npm install');
203+
const result = cp.spawnSync(npm, ['install'], {
204+
cwd: path,
205+
});
206+
showMessage(editorInstance, result.signal);
207+
})
208+
.catch((err: any) => { showMessage(editorInstance, err.message, 'err'); });
209+
};

0 commit comments

Comments
 (0)