Repository: artusjs/core
Pull request #222 (2022-12-09 12:51:54)

fix: compatible for no config
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md

感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试，必要时请附上性能测试。
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] `npm test` passes
- [x] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [x] commit message follows commit guidelines

##### Affected core subsystem(s)
<!-- Provide affected core subsystem(s). -->


##### Description of change
<!-- Provide a description of the change below this comment. -->

当 config 目录不存在时，scan 不报错

<!--
- any feature?
- close https://github.com/eggjs/egg/ISSUE_URL
-->

codecov-commenter: # [Codecov](https://codecov.io/gh/artusjs/core/pull/222?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
Base: **92.42**% // Head: **92.66**% // Increases project coverage by **`+0.24%`** :tada:
> Coverage data is based on head [(`3ae8ef5`)](https://codecov.io/gh/artusjs/core/pull/222?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) compared to base [(`02e6a28`)](https://codecov.io/gh/artusjs/core/commit/02e6a2846ea54cb27812dcada673985e892b692c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).
> Patch coverage: 100.00% of modified lines in pull request are covered.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master     #222      +/-   ##
==========================================
+ Coverage   92.42%   92.66%   +0.24%     
==========================================
  Files          55       55              
  Lines        1294     1296       +2     
  Branches      216      218       +2     
==========================================
+ Hits         1196     1201       +5     
+ Misses         98       95       -3     
```


| [Impacted Files](https://codecov.io/gh/artusjs/core/pull/222?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [src/scanner/scan.ts](https://codecov.io/gh/artusjs/core/pull/222/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL3NjYW5uZXIvc2Nhbi50cw==) | `95.94% <100.00%> (+1.42%)` | :arrow_up: |
| [src/utils/fs.ts](https://codecov.io/gh/artusjs/core/pull/222/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL3V0aWxzL2ZzLnRz) | `100.00% <0.00%> (+10.00%)` | :arrow_up: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/artusjs/core/pull/222?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).


Patch:
diff --git a/src/plugin/impl.ts b/src/plugin/impl.ts
index 5226886..d02584d 100644
--- a/src/plugin/impl.ts
+++ b/src/plugin/impl.ts
@@ -1,6 +1,6 @@
 import path from 'path';
 import { loadMetaFile } from '../utils/load_meta_file';
-import { exisis } from '../utils/fs';
+import { exists } from '../utils/fs';
 import { PLUGIN_META_FILENAME } from '../constant';
 import { PluginConfigItem, PluginCreateOptions, PluginMap, PluginMetadata, PluginType } from './types';
 import { getPackagePath } from './common';
@@ -68,12 +68,12 @@ export class Plugin implements PluginType {
 
   private async checkAndLoadMetadata() {
     // check import path
-    if (!await exisis(this.importPath)) {
+    if (!await exists(this.importPath)) {
       throw new Error(`load plugin <${this.name}> import path ${this.importPath} is not exists.`);
     }
     const metaFilePath = path.resolve(this.importPath, PLUGIN_META_FILENAME);
     try {
-      if (!await exisis(metaFilePath)) {
+      if (!await exists(metaFilePath)) {
         throw new Error(`load plugin <${this.name}> import path ${this.importPath} can't find meta file.`);
       }
       this.metadata = await loadMetaFile(metaFilePath);
diff --git a/src/scanner/scan.ts b/src/scanner/scan.ts
index 016a54b..6bb0960 100644
--- a/src/scanner/scan.ts
+++ b/src/scanner/scan.ts
@@ -18,6 +18,7 @@ import ConfigurationHandler, { ConfigObject } from '../configuration';
 import { FrameworkConfig, FrameworkHandler } from '../framework';
 import { PluginType, PluginFactory } from '../plugin';
 import { ScanUtils } from './utils';
+import { exists } from '../utils/fs';
 import { PluginConfigItem, PluginMetadata } from '../plugin/types';
 import { getConfigMetaFromFilename } from '../loader/utils/config_file_meta';
 import { Application } from '../types';
@@ -67,7 +68,8 @@ export class Scanner {
     if (Array.isArray(envs) && envs.length) {
       return envs;
     }
-    const configFileList = await fs.readdir(path.resolve(root, configDir));
+    const absoluteConfigDir = path.resolve(root, configDir);
+    const configFileList = (await exists(absoluteConfigDir)) ? await fs.readdir(absoluteConfigDir) : [];
     const envSet: Set<string> = new Set([ARTUS_DEFAULT_CONFIG_ENV.DEFAULT]);
     for (const configFilename of configFileList) {
       if (configFilename.endsWith('.d.ts')) {
@@ -169,7 +171,7 @@ export class Scanner {
       return {};
     }
     const root = path.resolve(baseDir, configDir);
-    const configFileList = await fs.readdir(root);
+    const configFileList = (await exists(root)) ? await fs.readdir(root) : [];
     const container = new Container(ArtusInjectEnum.DefaultContainerName);
     container.set({ type: ConfigurationHandler });
     container.set({
diff --git a/src/utils/fs.ts b/src/utils/fs.ts
index 6b6f687..2939cac 100644
--- a/src/utils/fs.ts
+++ b/src/utils/fs.ts
@@ -1,6 +1,6 @@
 import fs from 'fs/promises';
 
-export async function exisis(path: string): Promise<boolean> {
+export async function exists(path: string): Promise<boolean> {
   try {
     await fs.access(path);
   } catch {


Test patch:
diff --git a/test/fixtures/app_without_config/config/.gitkeep b/test/fixtures/app_without_config/config/.gitkeep
deleted file mode 100644
index e69de29..0000000
diff --git a/test/scanner.test.ts b/test/scanner.test.ts
index 7d1a2fc..dbb487d 100644
--- a/test/scanner.test.ts
+++ b/test/scanner.test.ts
@@ -94,4 +94,11 @@ describe('test/scanner.test.ts', () => {
     expect(manifest.items).toBeDefined();
     expect(manifest.items.length).toBe(3);
   });
+
+  it('should not throw when scan application without configdir', async () => {
+    const scanner = new Scanner({ needWriteFile: false, extensions: ['.ts', '.js', '.json'] });
+    const scanResults = await scanner.scan(path.resolve(__dirname, './fixtures/app_without_config'));
+    const { default: manifest } = scanResults;
+    expect(manifest.items.find(item => item.loader === 'config')).toBeUndefined();
+  });
 });