Skip to content

Commit

Permalink
feat: stop-backend.js and update.js linux support (#712)
Browse files Browse the repository at this point in the history
* chore(dependabot.yml): update target-branch from "develop" to "dev" for npm package updates in /api, /client, and root directory

* feat: stop-backend.js and update.js linux support (#701)

* feat: stop-backend.js and update.js linux support

* feat: update.js sudo support

* chore(helpers.js): add deleteNodeModules function
feat(packages.js): add script to delete node_modules and install dependencies
refactor(update.js): remove unnecessary imports and use deleteNodeModules function
feat(package.json): add update:linux script to update with sudo

* chore(package.json): rename 'update:linux'  script to 'update:sudo'

* refactor(update.js): simplify downCommand and buildCommand by removing redundant use of sudo command, add sudo to single docker command

---------

Co-authored-by: Fuegovic <32828263+fuegovic@users.noreply.github.com>
  • Loading branch information
danny-avila and fuegovic authored Jul 27, 2023
1 parent d59a3f2 commit 777d640
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 20 deletions.
6 changes: 3 additions & 3 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version: 2
updates:
- package-ecosystem: "npm" # See documentation for possible values
directory: "/api" # Location of package manifests
target-branch: "develop"
target-branch: "dev"
versioning-strategy: increase-if-necessary
schedule:
interval: "weekly"
Expand All @@ -20,7 +20,7 @@ updates:
include: "scope"
- package-ecosystem: "npm" # See documentation for possible values
directory: "/client" # Location of package manifests
target-branch: "develop"
target-branch: "dev"
versioning-strategy: increase-if-necessary
schedule:
interval: "weekly"
Expand All @@ -33,7 +33,7 @@ updates:
include: "scope"
- package-ecosystem: "npm" # See documentation for possible values
directory: "/" # Location of package manifests
target-branch: "develop"
target-branch: "dev"
versioning-strategy: increase-if-necessary
schedule:
interval: "weekly"
Expand Down
11 changes: 11 additions & 0 deletions config/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Helper functions
* This allows us to give the console some colour when running in a terminal
*/
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const { execSync } = require('child_process');

Expand All @@ -28,6 +30,14 @@ function isDockerRunning() {
}
}

function deleteNodeModules(dir) {
const nodeModulesPath = path.join(dir, 'node_modules');
if (fs.existsSync(nodeModulesPath)) {
console.purple(`Deleting node_modules in ${dir}`);
fs.rmdirSync(nodeModulesPath, { recursive: true });
}
}

const silentExit = (code = 0) => {
console.log = () => {};
process.exit(code);
Expand All @@ -48,4 +58,5 @@ module.exports = {
askQuestion,
silentExit,
isDockerRunning,
deleteNodeModules,
};
26 changes: 26 additions & 0 deletions config/packages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { execSync } = require('child_process');
const path = require('path');

const { deleteNodeModules } = require('./helpers');

// Set the directories
const rootDir = path.resolve(__dirname, '..');
const directories = [
rootDir,
path.resolve(rootDir, 'packages', 'data-provider'),
path.resolve(rootDir, 'client'),
path.resolve(rootDir, 'api'),
];

(async () => {
// Delete all node_modules
directories.forEach(deleteNodeModules);

// Run npm cache clean --force
console.purple('Cleaning npm cache...');
execSync('npm cache clean --force', { stdio: 'inherit' });

// Install dependencies
console.purple('Installing dependencies...');
execSync('npm install', { stdio: 'inherit' });
})();
23 changes: 23 additions & 0 deletions config/stop-backend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// eslint-disable-next-line
const helpers = require('./helpers');
const { exec } = require('child_process');
const { promisify } = require('util');

const isWindows = process.platform === 'win32';
const execAsync = promisify(exec);

async function main() {
try {
if (isWindows) {
console.red('The backend process has been terminated');
await execAsync('taskkill /F /IM node.exe /T');
} else {
await execAsync('pkill -f api/server/index.js');
console.orange('The backend process has been terminated');
}
} catch (err) {
console.red('The backend process has been terminated', err.message);
}
}

main();
32 changes: 15 additions & 17 deletions config/update.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { execSync } = require('child_process');
const path = require('path');
const fs = require('fs');
const { askQuestion, isDockerRunning, silentExit } = require('./helpers');
const { askQuestion, isDockerRunning, deleteNodeModules, silentExit } = require('./helpers');

const config = {
localUpdate: process.argv.includes('-l'),
dockerUpdate: process.argv.includes('-d'),
useSingleComposeFile: process.argv.includes('-s'),
useSudo: process.argv.includes('--sudo'),
};

// Set the directories
Expand Down Expand Up @@ -47,24 +47,20 @@ async function validateDockerRunning() {
}
}

function deleteNodeModules(dir) {
const nodeModulesPath = path.join(dir, 'node_modules');
if (fs.existsSync(nodeModulesPath)) {
console.purple(`Deleting node_modules in ${dir}`);
execSync(`rd /s /q "${nodeModulesPath}"`, { stdio: 'inherit', shell: 'cmd.exe' });
}
}

(async () => {
const showWizard = !config.localUpdate && !config.dockerUpdate && !config.useSingleComposeFile;

if (showWizard) {
await updateConfigWithWizard();
}

await validateDockerRunning();
const { dockerUpdate, useSingleComposeFile: singleCompose } = config;
console.green(
'Starting update script, this may take a minute or two depending on your system and network.',
);

await validateDockerRunning();
const { dockerUpdate, useSingleComposeFile: singleCompose, useSudo } = config;
const sudo = useSudo ? 'sudo ' : '';
// Fetch latest repo
console.purple('Fetching the latest repo...');
execSync('git fetch origin', { stdio: 'inherit' });
Expand All @@ -79,7 +75,7 @@ function deleteNodeModules(dir) {

if (dockerUpdate) {
console.purple('Removing previously made Docker container...');
const downCommand = `docker-compose ${
const downCommand = `${sudo}docker-compose ${
singleCompose ? '-f ./docs/dev/single-compose.yml ' : ''
}down --volumes`;
console.orange(downCommand);
Expand All @@ -88,14 +84,14 @@ function deleteNodeModules(dir) {

const imageName = singleCompose ? 'librechat_single' : 'librechat';
try {
execSync(`docker rmi ${imageName}:latest`, { stdio: 'inherit' });
execSync(`${sudo}docker rmi ${imageName}:latest`, { stdio: 'inherit' });
} catch (e) {
console.purple('Failed to remove Docker image librechat:latest. It might not exist.');
}
console.purple('Removing all unused dangling Docker images...');
execSync('docker image prune -f', { stdio: 'inherit' });
execSync(`${sudo}docker image prune -f`, { stdio: 'inherit' });
console.purple('Building new LibreChat image...');
const buildCommand = `docker-compose ${
const buildCommand = `${sudo}docker-compose ${
singleCompose ? '-f ./docs/dev/single-compose.yml ' : ''
}build`;
console.orange(buildCommand);
Expand All @@ -119,7 +115,9 @@ function deleteNodeModules(dir) {

let startCommand = 'npm run backend';
if (dockerUpdate) {
startCommand = `docker-compose ${singleCompose ? '-f ./docs/dev/single-compose.yml ' : ''}up`;
startCommand = `${sudo}docker-compose ${
singleCompose ? '-f ./docs/dev/single-compose.yml ' : ''
}up`;
}
console.green('Your LibreChat app is now up to date! Start the app with the following command:');
console.purple(startCommand);
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
"update:local": "node config/update.js -l",
"update:docker": "node config/update.js -d",
"update:single": "node config/update.js -s",
"update:sudo": "node config/update.js --sudo",
"upgrade": "node config/upgrade.js",
"create-user": "node config/create-user.js",
"backend": "cross-env NODE_ENV=production node api/server/index.js",
"backend:dev": "cross-env NODE_ENV=development npx nodemon api/server/index.js",
"backend:stop": "node config/stop-backend.js",
"build:data-provider": "cd packages/data-provider && npm run build",
"frontend": "npm run build:data-provider && cd client && npm run build",
"frontend:ci": "npm run build:data-provider && cd client && npm run build:ci",
Expand Down

0 comments on commit 777d640

Please sign in to comment.