Skip to content

Commit

Permalink
fix: index known files by default, index other files explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme committed Mar 3, 2025
1 parent 7220d00 commit 28c27c0
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 103 deletions.
64 changes: 44 additions & 20 deletions app/lib/file-context/__tests__/file-context-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ describe('FileContext', function() {

// when
await waitFor(() => {
fileContext.addFile(filePath, 'foo');
fileContext.addFile(filePath, {
localValue: 'foo'
});
});

// then
Expand All @@ -76,6 +78,28 @@ describe('FileContext', function() {
});


it('adding file with explicit processor', async function() {

// given
const filePath = path.resolve(__dirname, './fixtures/extensions/bpmn.unrecognized');

// when
await waitFor(() => {
fileContext.addFile(filePath, {
processor: 'bpmn'
});
});

// then
expectItems(fileContext, [
{
uri: toFileUrl(path.resolve(__dirname, './fixtures/extensions/bpmn.unrecognized')),
processor: 'bpmn'
}
]);
});


it('removing file', async function() {

// given
Expand Down Expand Up @@ -158,35 +182,35 @@ describe('FileContext', function() {
});


it('should NOT throw for unrecognized extension', async function() {
describe('error handling', function() {

// given
const filePath = path.resolve(__dirname, './fixtures/extensions/bpmn.unrecognized');
it('should NOT throw for unrecognized extension', async function() {

// when
const file = await fileContext.addFile(filePath);
// given
const filePath = path.resolve(__dirname, './fixtures/extensions/bpmn.unrecognized');

// then
expect(file).to.exist;
expect(file.metadata).to.have.property('type');
});
// when
const file = await fileContext.addFile(filePath);

// then
expect(file).to.exist;
expect(file.metadata).to.have.property('type');
});

it('should NOT throw for missing extension', async function() {

// given
const filePath = path.resolve(__dirname, './fixtures/extensions/no-extension');
it('should NOT throw for no extension', async function() {

// when
const file = await fileContext.addFile(filePath);
// given
const filePath = path.resolve(__dirname, './fixtures/extensions/no-extension');

// then
expect(file).to.exist;
expect(file.metadata).to.have.property('type');
});
// when
const file = await fileContext.addFile(filePath);

// then
expect(file).to.exist;
expect(file.metadata).to.have.property('type');
});

describe('broken files', function() {

it('should NOT throw for empty BPMN', async function() {

Expand Down
34 changes: 17 additions & 17 deletions app/lib/file-context/file-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,20 @@ module.exports = class FileContext extends EventEmitter {
* Add file.
*
* @param { string } uri
* @param { string } [localValue]
*/
addFile(uri, localValue) {
return this._indexer.add(toFileUrl(uri), localValue);
}

/**
* Update file.
* @param { { localValue?: string, processor?: string } } [options]
*
* @param { string } uri
* @returns { Promise<undefined> }
*/
updateFile(uri) {
return this.addFile(toFileUrl(uri));
addFile(uri, options) {
return this._indexer.add(toFileUrl(uri), options);
}

/**
* Remove file.
*
* @param { string } uri
*
* @returns { undefined }
*/
removeFile(uri) {
return this._indexer.remove(toFileUrl(uri));
Expand All @@ -106,19 +101,24 @@ module.exports = class FileContext extends EventEmitter {
/**
* Handle file opened.
*
* @param { { uri: string, value: string } } fileProps
* @param { string } uri
* @param { { processor?: string } }
*
* @returns { Promise<undefined> }
*/
fileOpened(fileProps) {
return this._indexer.fileOpened(fileProps);
fileOpened(uri, options) {
return this._indexer.fileOpened(uri, options);
}

/**
* Handle file content changed.
*
* @param { { uri: string, value: string } } fileProps
* @param { string } uri
* @param { string } value
* @param { { processor?: string } } [options]
*/
fileContentChanged(fileProps) {
return this._indexer.fileContentChanged(fileProps);
fileContentChanged(uri, value, options) {
return this._indexer.fileContentChanged(uri, value, options);
}

/**
Expand Down
53 changes: 29 additions & 24 deletions app/lib/file-context/indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,24 @@ module.exports = class Indexer {

/**
* @param { string } uri
* @param { string } [localValue]
* @param { { localValue?: string, processor?: string } } [options]
*
* @returns { Promise<undefined> }
*/
add(uri, localValue) {
add(uri, options = {}) {
uri = toFileUrl(uri);

this._logger.info('indexer:add', uri, localValue);
const {
localValue,
processor
} = options;

this._logger.info('indexer:add', uri, localValue, processor);

let indexItem = this.items.get(uri);

if (!indexItem) {
indexItem = createIndexItem({ uri, localValue });
indexItem = createIndexItem({ uri, localValue, processor });

this.items.set(uri, indexItem);
}
Expand All @@ -126,35 +133,30 @@ module.exports = class Indexer {
/**
* Notify file opened
*
* @param { { uri: string, value: string } } fileProps
* @param { string } uri
* @param { { processor?: string } } [options]
*
* @returns { Promise<undefined> }
*/
fileOpened(fileProps) {

const {
uri,
value
} = fileProps;

fileOpened(uri, options = {}) {
this._emit('file-opened', uri);

return this.add(toFileUrl(uri), value);
return this.add(toFileUrl(uri), options);
}

/**
* Notify file content changed
*
* @param { { uri: string, value: string } } fileProps
* @param { string } uri
* @param { string } value
* @param { { processor?: string } } [options]
*
* @returns { Promise<undefined> }
*/
fileContentChanged(fileProps) {

const {
uri,
value
} = fileProps;

fileContentChanged(uri, value, options = {}) {
this._emit('file-content-changed', uri);

return this.add(toFileUrl(uri), value);
return this.add(toFileUrl(uri), value, options);
}

/**
Expand Down Expand Up @@ -345,7 +347,8 @@ module.exports = class Indexer {
/**
* @param { {
* uri: string,
* localValue?: string
* localValue?: string,
* processor?: string
* } } item
*
* @return {IndexItem}
Expand All @@ -355,6 +358,7 @@ function createIndexItem(item) {
const {
uri,
localValue,
processor,
...rest
} = item;

Expand All @@ -373,7 +377,8 @@ function createIndexItem(item) {
this.localValue = value;
},
file,
localValue
localValue,
processor
};

}
10 changes: 10 additions & 0 deletions app/lib/file-context/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ module.exports = class Processor {
process(item) {
this._logger.info('processor:process', item.uri);

if (item.processor) {
const processor = this._processors.find(processor => processor.id === item.processor);

if (processor) {
return processor.process(item);
}

this._logger.warn('processor:process', `Processor with id ${ item.processor } not found`);
}

const processor = this._processors.find(processor => processor.extensions.includes(getFileExtension(item.file.path)));

if (!processor) {
Expand Down
3 changes: 2 additions & 1 deletion app/lib/file-context/processors/bpmnProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const {
} = require('./util');

module.exports = {
extensions: [ '.bpmn', '.xml' ],
id: 'bpmn',
extensions: [ '.bpmn' ],
process: async (item) => {
let rootElement, ids, linkedIds;

Expand Down
1 change: 1 addition & 0 deletions app/lib/file-context/processors/dmnProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
} = require('./util');

module.exports = {
id: 'dmn',
extensions: [ '.dmn' ],
process: async (item) => {
let rootElement, ids;
Expand Down
1 change: 1 addition & 0 deletions app/lib/file-context/processors/formProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
const assert = require('node:assert');

module.exports = {
id: 'form',
extensions: [ '.form' ],
process: async (item) => {
let formId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

module.exports = {
id: 'processApplication',
extensions: [ '.process-application' ],
process: async (item) => {
return {
Expand Down
1 change: 1 addition & 0 deletions app/lib/file-context/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type IndexItem = {
file: File,
localValue?: any,
metadata: Metadata,
processor?: string,
uri: string,
value: any,
[key: string]: any
Expand Down
25 changes: 10 additions & 15 deletions app/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,26 +224,30 @@ renderer.on('system-clipboard:write-text', function(options, done) {
});

// file context //////////
renderer.on('file-context:add-root', function(filePath, done) {
renderer.on('file-context:add-root', function(options, done) {
const { filePath } = options;

fileContext.addRoot(filePath);

done(null);
});

renderer.on('file-context:remove-root', function(filePath, done) {
renderer.on('file-context:remove-root', function(options, done) {
const { filePath } = options;

fileContext.removeRoot(filePath);

done(null);
});

renderer.on('file-context:file-opened', function(filePath, value, done) {
fileContext.fileOpened({ uri: toFileUrl(filePath), value });
renderer.on('file-context:file-opened', function(filePath, options, done) {
fileContext.fileOpened(toFileUrl(filePath), options);

done(null);
});

renderer.on('file-context:file-content-changed', function(filePath, value, done) {
fileContext.fileContentChanged({ uri: toFileUrl(filePath), value });
renderer.on('file-context:file-content-changed', function(filePath, value, options, done) {
fileContext.fileContentChanged(toFileUrl(filePath), value, options);

done(null);
});
Expand All @@ -254,15 +258,6 @@ renderer.on('file-context:file-closed', function(filePath, done) {
done(null);
});

renderer.on('file:content-changed', function(filePath, contents, done) {
fileContext.fileContentChanged({
uri: toFileUrl(filePath),
value: contents
});

done(null);
});

// filesystem //////////

renderer.on('file:read', function(filePath, options = {}, done) {
Expand Down
Loading

0 comments on commit 28c27c0

Please sign in to comment.