Skip to content

Commit

Permalink
feat(scanner): add parsed pluginConfig to manifest (#197)
Browse files Browse the repository at this point in the history
* feat(scanner): add parsed pluginConfig to manifest

* test(scanner): support windows unit-test with path
  • Loading branch information
noahziheng authored Sep 15, 2022
1 parent bc7fdbb commit e3c7fde
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/loader/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Container } from '@artus/injection';
import { ScanPolicy } from '../constant';
import { PluginConfigItem } from '../plugin/types';

interface Manifest {
items: ManifestItem[];
pluginConfig?: Record<string, PluginConfigItem>;
relative?: boolean;
}

Expand Down
13 changes: 11 additions & 2 deletions src/scanner/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,18 @@ export class Scanner {
// 4. scan all file in app
await this.walk(root, this.formatWalkOptions('app', root, ''));

const relative = this.options.useRelativePath;
if (relative) {
for (const [pluginName, pluginConfigItem] of Object.entries(pluginConfig)) {
if (pluginConfigItem.path) {
pluginConfig[pluginName].path = path.relative(root, pluginConfigItem.path);
}
}
}
const result: Manifest = {
items: this.getItemsFromMap(this.options.useRelativePath, root, env),
relative: this.options.useRelativePath,
pluginConfig,
items: this.getItemsFromMap(relative, root, env),
relative,
};
return result;
}
Expand Down
10 changes: 10 additions & 0 deletions test/scanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ describe('test/scanner.test.ts', () => {
expect(manifest.items.filter(item => item.unitName === 'redis').length).toBe(2);
expect(manifest.items.filter(item => item.unitName === 'mysql').length).toBe(0);
expect(manifest.items.filter(item => item.source === 'app').length).toBe(8);
expect(manifest.pluginConfig).toStrictEqual({
redis: { enable: true, path: path.join('src', 'redis_plugin') },
mysql: { enable: false, path: path.join('src', 'mysql_plugin') },
testDuplicate: { enable: false, path: path.join('..', '..', '..', 'node_modules', '@artus', 'injection', 'lib') },
});

const { dev: devManifest } = scanResults;
// console.log('devManifest', devManifest);
Expand All @@ -36,6 +41,11 @@ describe('test/scanner.test.ts', () => {
expect(devManifest.items.filter(item => item.loader === 'config').length).toBe(2);
expect(devManifest.items.filter(item => item.loader === 'plugin-meta').length).toBe(2);
expect(devManifest.items.find(item => item.unitName === 'testDuplicate')).toBeDefined();
expect(devManifest.pluginConfig).toStrictEqual({
redis: { enable: true, path: path.join('src', 'redis_plugin') },
mysql: { enable: false, path: path.join('src', 'mysql_plugin') },
testDuplicate: { enable: true, path: path.join('src', 'test_duplicate_plugin') },
});
});

it('should scan module with custom loader', async () => {
Expand Down

0 comments on commit e3c7fde

Please sign in to comment.