Add onFileChanged event hander
This commit is contained in:
Родитель
7e6fd8fa12
Коммит
039047d7a4
|
@ -0,0 +1,27 @@
|
|||
import { join } from 'path';
|
||||
import Project from 'ts-morph';
|
||||
|
||||
import MezzuriteComponent from '../models/MezzuriteComponent';
|
||||
import processFile from '../utilities/processFile';
|
||||
|
||||
function onFileChanged (components: MezzuriteComponent[], filePath: string, project: Project): Promise<MezzuriteComponent[]> {
|
||||
let updatedComponents: MezzuriteComponent[] = null;
|
||||
return processFile(filePath, project)
|
||||
.then((changedComponent: MezzuriteComponent) => {
|
||||
if (changedComponent != null) {
|
||||
updatedComponents = components.map((component: MezzuriteComponent) => {
|
||||
if (join(changedComponent.filePath) === join(component.filePath)) {
|
||||
return changedComponent;
|
||||
} else {
|
||||
return component;
|
||||
}
|
||||
});
|
||||
}
|
||||
return updatedComponents;
|
||||
})
|
||||
.catch((error: Error) => {
|
||||
throw(error);
|
||||
});
|
||||
}
|
||||
|
||||
export default onFileChanged;
|
|
@ -0,0 +1,71 @@
|
|||
import Project from 'ts-morph';
|
||||
|
||||
import MezzuriteComponent from '../models/MezzuriteComponent';
|
||||
import * as processFile from '../utilities/processFile';
|
||||
import onFileChanged from './onFileChanged';
|
||||
|
||||
describe('onFileChanged.ts', () => {
|
||||
const components: MezzuriteComponent[] = [
|
||||
{
|
||||
checks: {
|
||||
test: true
|
||||
},
|
||||
filePath: 'filePath',
|
||||
name: 'name',
|
||||
type: 'componentType'
|
||||
},
|
||||
{
|
||||
checks: {
|
||||
test: true
|
||||
},
|
||||
filePath: 'differentFilePath',
|
||||
name: 'name',
|
||||
type: 'componentType'
|
||||
}
|
||||
];
|
||||
|
||||
const project = new Project({
|
||||
addFilesFromTsConfig: false
|
||||
});
|
||||
|
||||
it('should handle when processFile returns a null value', () => {
|
||||
Object.defineProperty(processFile, 'default', { value: jest.fn(() => new Promise((resolve) => resolve(null))) });
|
||||
return onFileChanged(components, 'filePath', project)
|
||||
.then((updatedComponents: MezzuriteComponent[]) => [
|
||||
expect(updatedComponents).toBeNull()
|
||||
]);
|
||||
});
|
||||
|
||||
it('should handle when processFile returns a component that already exists', () => {
|
||||
Object.defineProperty(processFile, 'default', { value: jest.fn(() => new Promise((resolve) => resolve(components[0]))) });
|
||||
return onFileChanged(components, 'filePath', project)
|
||||
.then((updatedComponents: MezzuriteComponent[]) => {
|
||||
expect(updatedComponents).toMatchObject(components);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle when processFile returns a component that already exists', () => {
|
||||
const updated: MezzuriteComponent = {
|
||||
checks: {
|
||||
test: false
|
||||
},
|
||||
filePath: 'filePath',
|
||||
name: 'name',
|
||||
type: 'componentType'
|
||||
};
|
||||
Object.defineProperty(processFile, 'default', { value: jest.fn(() => new Promise((resolve) => resolve(updated))) });
|
||||
return onFileChanged(components, 'filePath', project)
|
||||
.then((updatedComponents: MezzuriteComponent[]) => {
|
||||
expect(updatedComponents).toMatchObject([ updated, components[1] ]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle when processFile returns a component that already exists', () => {
|
||||
Object.defineProperty(processFile, 'default', { value: jest.fn(() => new Promise((resolve, reject) => reject(new Error('Error')))) });
|
||||
expect.assertions(1);
|
||||
return onFileChanged(components, 'filePath', project)
|
||||
.catch((error: Error) => {
|
||||
expect(error).toMatchObject(new Error('Error'));
|
||||
});
|
||||
});
|
||||
});
|
|
@ -3,6 +3,7 @@
|
|||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
* ------------------------------------------------------------------------------------------ */
|
||||
|
||||
import { join } from 'path';
|
||||
import Project from 'ts-morph';
|
||||
import {
|
||||
createConnection,
|
||||
|
@ -11,6 +12,7 @@ import {
|
|||
WorkspaceFolder
|
||||
} from 'vscode-languageserver';
|
||||
|
||||
import onFileChanged from './events/onFileChanged';
|
||||
import MezzuriteComponent from './models/MezzuriteComponent';
|
||||
import combineWorkspaceFolders from './utilities/combineWorkspaceFolders';
|
||||
import processFile from './utilities/processFile';
|
||||
|
@ -60,6 +62,22 @@ connection.onInitialized(() => {
|
|||
});
|
||||
});
|
||||
|
||||
connection.onNotification('custom/fileChanged', (filePath: string) => {
|
||||
onFileChanged(components, filePath, project)
|
||||
.then((updatedComponents: MezzuriteComponent[]) => {
|
||||
components = updatedComponents;
|
||||
connection.sendNotification('custom/mezzuriteComponents', { value: components });
|
||||
})
|
||||
.catch((error: Error) => console.warn(error.message));
|
||||
});
|
||||
|
||||
connection.onNotification('custom/fileDeleted', (filePath: string) => {
|
||||
components = components.filter((component: MezzuriteComponent) => {
|
||||
return join(component.filePath) !== join(filePath);
|
||||
});
|
||||
connection.sendNotification('custom/mezzuriteComponents', { value: components });
|
||||
});
|
||||
|
||||
documents.listen(connection);
|
||||
|
||||
connection.listen();
|
||||
|
|
Загрузка…
Ссылка в новой задаче