Skip to content

Commit

Permalink
feat: remove all yaml meta file, change them to JSON (#138)
Browse files Browse the repository at this point in the history
* feat: remove all yaml meta file, change them to JSON

* refactor(utils): loadMetaFile check condition

* refactor(utils): loadMetaFile arguments and pre-logic
  • Loading branch information
noahziheng authored Jul 14, 2022
1 parent 7161703 commit 8392260
Show file tree
Hide file tree
Showing 42 changed files with 145 additions and 128 deletions.
13 changes: 13 additions & 0 deletions exception.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"ARTUS:GLOBAL_TEST": {
"desc": "全局测试错误,仅用于单元测试",
"detailUrl": "https://github.com/artusjs/spec"
},
"ARTUS:GLOBAL_TEST_I18N": {
"desc": {
"zh": "全局测试错误,仅用于单元测试",
"en": "This is a test exception, only valid in unit-test"
},
"detailUrl": "https://github.com/artusjs/spec"
}
}
8 changes: 0 additions & 8 deletions exception.yaml

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"@artus/injection": "^0.3.1",
"@artus/pipeline": "^0.2.2",
"deepmerge": "^4.2.2",
"js-yaml": "^4.1.0",
"minimatch": "^5.0.1"
},
"ci": {
Expand Down
4 changes: 2 additions & 2 deletions src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export const DEFAULT_EXCLUDES = [
export const FRAMEWORK_PATTERN = 'framework.*';
export const PLUGIN_CONFIG_PATTERN = 'plugin.*';
export const CONFIG_PATTERN = 'config.*';
export const PLUGIN_META = ['meta.json', 'meta.yaml', 'meta.yml'];
export const PACKAGE_JSON = 'package.json';
export const EXCEPTION_FILE = 'exception.yaml';
export const PLUGIN_META_FILENAME = 'meta.json';
export const EXCEPTION_FILENAME = 'exception.json';

export const DEFAULT_LOADER_LIST_WITH_ORDER = [
'exception',
Expand Down
6 changes: 3 additions & 3 deletions src/loader/impl/exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ManifestItem, Loader, LoaderFindOptions } from '../types';
import { ExceptionItem } from '../../exception/types';
import { ExceptionHandler } from '../../exception';
import { loadMetaFile } from '../../utils/load_meta_file';
import { EXCEPTION_FILE } from '../../constant';
import { EXCEPTION_FILENAME } from '../../constant';
import { isMatch } from '../../utils';

@DefineLoader('exception')
Expand All @@ -16,13 +16,13 @@ class ExceptionLoader implements Loader {
}

static async is(opts: LoaderFindOptions) {
return isMatch(opts.filename, EXCEPTION_FILE);
return isMatch(opts.filename, EXCEPTION_FILENAME);
}

async load(item: ManifestItem) {
const exceptionHandler = this.container.get(ExceptionHandler);
try {
const codeMap: Record<string, ExceptionItem> = await loadMetaFile<Record<string, ExceptionItem>>(item);
const codeMap: Record<string, ExceptionItem> = await loadMetaFile<Record<string, ExceptionItem>>(item.path);
for (const [errCode, exceptionItem] of Object.entries(codeMap)) {
exceptionHandler.registerCode(errCode, exceptionItem);
}
Expand Down
7 changes: 4 additions & 3 deletions src/loader/impl/plugin_meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { DefineLoader } from '../decorator';
import { ManifestItem, Loader, LoaderFindOptions } from '../types';
import { loadMetaFile } from '../../utils/load_meta_file';
import { PluginMetadata } from '../../plugin/types';
import { PLUGIN_META } from '../../constant';
import { PLUGIN_META_FILENAME } from '../../constant';
import { isMatch } from '../../utils';

@DefineLoader('plugin-meta')
class PluginMetaLoader implements Loader {
Expand All @@ -14,11 +15,11 @@ class PluginMetaLoader implements Loader {
}

static async is(opts: LoaderFindOptions): Promise<boolean> {
return PLUGIN_META.includes(opts.filename);
return isMatch(opts.filename, PLUGIN_META_FILENAME);
}

async load(item: ManifestItem) {
const pluginMeta: PluginMetadata = await loadMetaFile<PluginMetadata>(item);
const pluginMeta: PluginMetadata = await loadMetaFile<PluginMetadata>(item.path);
this.container.set({
id: `pluginMeta_${pluginMeta.name}`,
value: pluginMeta,
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type PluginMap = Map<string, BasePlugin>;

export class BasePlugin implements Plugin {
static getPath(packageName: string): string {
return path.resolve(require.resolve(`${packageName}/package.json`), '..');
return path.resolve(require.resolve(packageName), '..');
}

public name: string;
Expand Down
36 changes: 9 additions & 27 deletions src/plugin/impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';
import { BasePlugin } from './base';
import { loadMetaFile } from '../utils/load_meta_file';
import { exisis } from '../utils/fs';
import { PLUGIN_META_FILENAME } from '../constant';

export class ArtusPlugin extends BasePlugin {
async init() {
Expand All @@ -22,34 +23,15 @@ export class ArtusPlugin extends BasePlugin {
if (!await exisis(this.importPath)) {
throw new Error(`load plugin <${this.name}> import path ${this.importPath} is not exists.`);
}

let find = false;
const fileNameList = [
'meta.yaml',
'meta.yml',
'meta.json',
];
for (const fileName of fileNameList) {
const metaFilePath = path.resolve(this.importPath, fileName);
try {
if (!await exisis(metaFilePath)) {
continue;
}
this.metadata = await loadMetaFile({
path: metaFilePath,
extname: path.extname(metaFilePath),
filename: fileName,
});
this.metaFilePath = metaFilePath;
find = true;
break;
} catch (e) {
throw new Error(`load plugin <${this.name}> failed, err: ${e}`);
const metaFilePath = path.resolve(this.importPath, PLUGIN_META_FILENAME);
try {
if (!await exisis(metaFilePath)) {
throw new Error(`load plugin <${this.name}> import path ${this.importPath} can't find meta file.`);
}
}

if (!find) {
throw new Error(`load plugin <${this.name}> import path ${this.importPath} can't find meta file.`);
this.metadata = await loadMetaFile(metaFilePath);
this.metaFilePath = metaFilePath;
} catch (e) {
throw new Error(`load plugin <${this.name}> failed, err: ${e}`);
}
}
}
2 changes: 1 addition & 1 deletion src/scanner/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Scanner {
loaderListGenerator: (defaultLoaderList: string[]) => defaultLoaderList,
...options,
exclude: DEFAULT_EXCLUDES.concat(options.exclude ?? []),
extensions: [...new Set(this.moduleExtensions.concat(options.extensions ?? [], ['.yaml']))],
extensions: [...new Set(this.moduleExtensions.concat(options.extensions ?? []))],
};
}

Expand Down
5 changes: 2 additions & 3 deletions src/scanner/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Container } from '@artus/injection';
import {
ArtusInjectEnum,
DEFAULT_LOADER,
PLUGIN_META,
PLUGIN_META_FILENAME,
} from '../constant';
import { LoaderFactory, ManifestItem } from '../loader';
import { WalkOptions } from './types';
Expand Down Expand Up @@ -38,8 +38,7 @@ export class ScanUtils {
const itemStat = await fs.stat(realPath);
if (itemStat.isDirectory()) {
// ignore plugin dir
// TODO: 怎么判断是否是插件文件夹
if (this.exist(realPath, PLUGIN_META)) {
if (this.exist(realPath, [PLUGIN_META_FILENAME])) {
continue;
}
await ScanUtils.walk(realPath, options);
Expand Down
36 changes: 6 additions & 30 deletions src/utils/load_meta_file.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
import yaml from 'js-yaml';
import { readFile } from 'fs/promises';
import { ManifestItem } from '../loader/types';
import compatibleRequire from './compatible_require';

type ParserFunction = <T = Record<string, any>>(content: string) => T;

const YamlParser: ParserFunction = <T = Record<string, any>>(content: string) => {
return yaml.load(content, {
json: true,
}) as T;
};
const JsonParser: ParserFunction = <T = Record<string, any>>(content: string) => {
return JSON.parse(content) as T;
};

export const loadMetaFile = async <T = Record<string, any>>(item: ManifestItem): Promise<T> => {
let parserFunc: ParserFunction;
if (item.extname === '.yaml' || item.extname === '.yml') {
parserFunc = YamlParser;
} else if (item.extname === '.json') {
parserFunc = JsonParser;
} else {
throw new Error(`[Artus-Loader] Unsupported file extension: ${item.extname} in ${item.path}`);
}
const content = await readFile(item.path, {
encoding: 'utf-8',
});
if (!content) {
throw new Error(`[Artus-Loader] File content is empty in ${item.path}.`);
export const loadMetaFile = async <T = Record<string, any>>(path: string): Promise<T> => {
const metaObject = await compatibleRequire(path);
if (!metaObject || typeof metaObject !== 'object') {
throw new Error(`[loadMetaFile] ${path} is not a valid json file.`);
}
const resultMap = parserFunc<T>(content);
return resultMap;
return metaObject;
};
13 changes: 13 additions & 0 deletions test/fixtures/app_koa_with_ts/src/exception.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"ARTUS:GLOBAL_TEST": {
"desc": "全局测试错误,仅用于单元测试",
"detailUrl": "https://github.com/artusjs/spec"
},
"ARTUS:GLOBAL_TEST_I18N": {
"desc": {
"zh": "全局测试错误,仅用于单元测试",
"en": "This is a test exception, only valid in unit-test"
},
"detailUrl": "https://github.com/artusjs/spec"
}
}
8 changes: 0 additions & 8 deletions test/fixtures/app_koa_with_ts/src/exception.yaml

This file was deleted.

3 changes: 3 additions & 0 deletions test/fixtures/app_koa_with_ts/src/mysql_plugin/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "mysql"
}
1 change: 0 additions & 1 deletion test/fixtures/app_koa_with_ts/src/mysql_plugin/meta.yaml

This file was deleted.

7 changes: 7 additions & 0 deletions test/fixtures/app_koa_with_ts/src/redis_plugin/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "redis",
"exclude": [
"not_to_be_scanned_dir",
"not_to_be_scanned_file.ts"
]
}
4 changes: 0 additions & 4 deletions test/fixtures/app_koa_with_ts/src/redis_plugin/meta.yaml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "testDuplicate"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "base"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "mysql",
"configDir": "src/custom_config",
"dependencies": [
{
"name": "base"
}
]
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "mysql"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "redis"
}

This file was deleted.

12 changes: 6 additions & 6 deletions test/fixtures/exception_with_ts_yaml/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ async function main() {
source: 'app',
},
{
path: path.resolve(__dirname, '../../../exception.yaml'),
extname: '.yaml',
filename: 'exception.yaml',
path: path.resolve(__dirname, '../../../exception.json'),
extname: '.json',
filename: 'exception.json',
loader: 'exception',
source: 'app',
},
{
path: path.resolve(__dirname, './exception.yaml'),
extname: '.yaml',
filename: 'exception.yaml',
path: path.resolve(__dirname, './exception.json'),
extname: '.json',
filename: 'exception.json',
loader: 'exception',
source: 'app',
},
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/exception_with_ts_yaml/exception.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"APP:TEST_ERROR": {
"desc": "这是一个测试用的错误",
"detailUrl": "https://github.com/artusjs"
}
}
3 changes: 0 additions & 3 deletions test/fixtures/exception_with_ts_yaml/exception.yaml

This file was deleted.

12 changes: 12 additions & 0 deletions test/fixtures/plugins/plugin_a/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "plugin-a",
"dependencies": [
{
"name": "plugin-b"
},
{
"name": "plugin-c",
"optional": true
}
]
}
5 changes: 0 additions & 5 deletions test/fixtures/plugins/plugin_a/meta.yaml

This file was deleted.

8 changes: 8 additions & 0 deletions test/fixtures/plugins/plugin_b/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "plugin-b",
"dependencies": [
{
"name": "plugin-c"
}
]
}
3 changes: 0 additions & 3 deletions test/fixtures/plugins/plugin_b/meta.yaml

This file was deleted.

3 changes: 3 additions & 0 deletions test/fixtures/plugins/plugin_c/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "plugin-c"
}
1 change: 0 additions & 1 deletion test/fixtures/plugins/plugin_c/meta.yaml

This file was deleted.

Loading

0 comments on commit 8392260

Please sign in to comment.