From e3a2fa7dd18541f3f697bf3955bbed5a887e02d4 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Fri, 13 Dec 2019 20:25:02 +0200 Subject: [PATCH] Create packages/package.json on server start --- src/command-line/index.js | 29 +++++++++++++++++++++++++++++ src/command-line/install.js | 24 ------------------------ src/plugins/packages/index.js | 2 +- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/command-line/index.js b/src/command-line/index.js index 46a2428d..e9e81f13 100644 --- a/src/command-line/index.js +++ b/src/command-line/index.js @@ -2,6 +2,7 @@ const log = require("../log"); const fs = require("fs"); +const fsextra = require("fs-extra"); const path = require("path"); const colors = require("chalk"); const program = require("commander"); @@ -36,6 +37,9 @@ try { // fs.statSync will throw if config.js does not exist (e.g. first run) } +// Create packages/package.json +createPackagesFolder(); + // Merge config key-values passed as CLI options into the main config Helper.mergeConfig(Helper.config, program.config); @@ -62,6 +66,31 @@ if (program.rawArgs.length < 3) { program.help(); } +function createPackagesFolder() { + const packagesPath = Helper.getPackagesPath(); + const packagesConfig = path.join(packagesPath, "package.json"); + + // Create node_modules folder, otherwise yarn will start walking upwards to find one + fsextra.ensureDirSync(path.join(packagesPath, "node_modules")); + + // Create package.json with private set to true, if it doesn't exist already + if (!fs.existsSync(packagesConfig)) { + fs.writeFileSync( + packagesConfig, + JSON.stringify( + { + private: true, + description: + "Packages for The Lounge. All packages in node_modules directory will be automatically loaded.", + dependencies: {}, + }, + null, + "\t" + ) + ); + } +} + function verifyFileOwner() { if (!process.getuid) { return; diff --git a/src/command-line/install.js b/src/command-line/install.js index 96b4f45b..2d7dc704 100644 --- a/src/command-line/install.js +++ b/src/command-line/install.js @@ -12,8 +12,6 @@ program .on("--help", Utils.extraHelp) .action(function(packageName) { const fs = require("fs"); - const fsextra = require("fs-extra"); - const path = require("path"); const packageJson = require("package-json"); if (!fs.existsSync(Helper.getConfigPath())) { @@ -44,28 +42,6 @@ program log.info(`Installing ${colors.green(json.name + " v" + json.version)}...`); - const packagesPath = Helper.getPackagesPath(); - const packagesConfig = path.join(packagesPath, "package.json"); - - // Create node_modules folder, otherwise yarn will start walking upwards to find one - fsextra.ensureDirSync(path.join(packagesPath, "node_modules")); - - // Create package.json with private set to true, if it doesn't exist already - if (!fs.existsSync(packagesConfig)) { - fs.writeFileSync( - packagesConfig, - JSON.stringify( - { - private: true, - description: - "Packages for The Lounge. All packages in node_modules directory will be automatically loaded.", - }, - null, - "\t" - ) - ); - } - return Utils.executeYarnCommand("add", "--exact", `${json.name}@${json.version}`) .then(() => { log.info( diff --git a/src/plugins/packages/index.js b/src/plugins/packages/index.js index 56898a76..11c2dffc 100644 --- a/src/plugins/packages/index.js +++ b/src/plugins/packages/index.js @@ -74,7 +74,7 @@ function getEnabledPackages(packageJson) { const json = JSON.parse(fs.readFileSync(packageJson, "utf-8")); return Object.keys(json.dependencies); } catch (e) { - // + log.error(`Failed to read packages/package.json: ${colors.red(e)}`); } return [];