Skip to content

Commit

Permalink
fix: compatible for no config (#222)
Browse files Browse the repository at this point in the history
* feat: compatible for no config

* fix: typo
  • Loading branch information
whxaxes authored Dec 12, 2022
1 parent 02e6a28 commit d790874
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/plugin/impl.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/scanner/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion src/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Empty file.
7 changes: 7 additions & 0 deletions test/scanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit d790874

Please sign in to comment.