From 00b898d9ea949f6cdcc7f9e17ac08ab4805139bd Mon Sep 17 00:00:00 2001 From: Louis Christopher Date: Fri, 17 Dec 2021 10:13:56 +0530 Subject: [PATCH] Override namespace from package config --- lib/install.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/install.js b/lib/install.js index 7b48ef4..99f37cd 100644 --- a/lib/install.js +++ b/lib/install.js @@ -48,17 +48,38 @@ const installGlobal = function (packageName, registry, namespace, cluster, optio exports.installGlobal = installGlobal; +/** + * Get package.json object from path + * @param path + * @returns {Object} + */ +const getPackageObject = function (path) { + const packageJSONFilePath = `${path}/package.json`; + const packageObject = JSON.parse(fs.readFileSync(packageJSONFilePath, "utf-8")); + return packageObject; +}; + /** * Get Package name from path * @param path * @returns {string} */ const packageNameFromPath = function (path) { - const packageJSONFilePath = `${path}/package.json`; - const packageObject = JSON.parse(fs.readFileSync(packageJSONFilePath, "utf-8")); + const packageObject = getPackageObject(path); return `${packageObject.name}@${packageObject.version}`; }; +/** + * Get Package namespace from path + * @param path + * @returns {string} + */ +const packageNamespaceFromPath = function (path) { + const packageObject = getPackageObject(path); + if (!packageObject.config || !packageObject.config.namespace) return null; + return packageObject.config.namespace; +}; + /** * Install a package * @@ -85,6 +106,8 @@ const install = function (packageName, registry, namespace, save, cluster, optio } } + namespace = packageNamespaceFromPath(dirPath) || namespace; + let parentPackageName = packageName; return npmInstall(dirPath, packageName, registry, npmSaveParam) .then((_) => {