From 76399311bfcdae14c90e0c0e309615f852f55f85 Mon Sep 17 00:00:00 2001 From: Minggang Wang Date: Thu, 30 Nov 2017 16:38:28 +0800 Subject: [PATCH] Remove the redundant code used to load an interface Fix #211 --- index.js | 11 +---------- lib/interface_loader.js | 18 +++++++++++------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index d4d1e14c..8438ff20 100644 --- a/index.js +++ b/index.js @@ -166,16 +166,7 @@ let rcl = { * @return {object} - the object of the required package/interface. */ require(name) { - if (typeof (name.package) === 'string' && typeof (name.type) === 'string' && typeof (name.message) === 'string') { - return loader.loadInterface(name.package, name.type, name.message); - } - - if (name.indexOf('/') !== -1) { - let [packageName, type, messageName] = name.split('/'); - return loader.loadInterface(packageName, type, messageName); - } - - return loader.loadInterfaceInPackage(name); + return loader.loadInterface(name); }, /** diff --git a/lib/interface_loader.js b/lib/interface_loader.js index 469fcdb7..b38b8665 100644 --- a/lib/interface_loader.js +++ b/lib/interface_loader.js @@ -46,18 +46,22 @@ let interfaceLoader = { return this.loadInterface(packageName, type, messageName); } + // Suppose the name is a package, and traverse the path to collect the IDL files. + let packagePath = path.join(generator.generatedRoot, name); + + // eslint-disable-next-line + let interfaces = fs.readdirSync(packagePath); + if (interfaces.length > 0) { + return this.loadInterfaceByPath(packagePath, interfaces); + } + throw new TypeError('A string argument in expected in "package/type/message" format'); }, - loadInterfaceInPackage(packageName) { - let packagePath = path.join(generator.generatedRoot, packageName); - // The files under the package folder are limited, so it will not cost too - // much time and result in blocking the main thread. - // eslint-disable-next-line - let files = fs.readdirSync(packagePath); + loadInterfaceByPath(packagePath, interfaces) { let interfaceInfos = []; - files.forEach((file) => { + interfaces.forEach((file) => { let results = file.match(/\w+__(\w+)__(\w+).js$/); let type = results[1]; let name = results[2];