From f871bc0fa28d0a1219ef3d078209eff18fdc854f Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Tue, 17 Dec 2024 13:46:48 +0530 Subject: [PATCH] feat: setup script --- package.json | 3 +- scripts/setup.js | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 scripts/setup.js diff --git a/package.json b/package.json index ebe1af53f..2c46fdd2c 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "ts-jest": "^29.0.5" }, "scripts": { + "setup": "node ./scripts/setup.js", "dev": "concurrently --kill-others \"npm run dev:web\" \"npm run dev:electron\"", "dev:web": "npm run dev --workspace=packages/bruno-app", "build:web": "npm run build --workspace=packages/bruno-app", @@ -51,6 +52,6 @@ "prepare": "husky install" }, "overrides": { - "rollup":"3.29.5" + "rollup": "3.29.5" } } diff --git a/scripts/setup.js b/scripts/setup.js new file mode 100644 index 000000000..47d679098 --- /dev/null +++ b/scripts/setup.js @@ -0,0 +1,95 @@ +const { execSync } = require('child_process'); +const fs = require('fs'); +const path = require('path'); + +const icons = { + clean: '๐Ÿงน', + delete: '๐Ÿ—‘๏ธ', + install: '๐Ÿ“ฆ', + build: '๐Ÿ”จ', + success: 'โœ…', + error: 'โŒ', + working: 'โšก' +}; + +const execCommand = (command, description) => { + try { + console.log(`\n${icons.working} ${description}...`); + execSync(command, { stdio: 'inherit' }); + console.log(`${icons.success} ${description} completed`); + } catch (error) { + console.error(`${icons.error} ${description} failed`); + throw error; + } +}; + +const glob = function (startPath, pattern) { + let results = []; + + // Ensure start path exists + if (!fs.existsSync(startPath)) { + return results; + } + + const files = fs.readdirSync(startPath); + for (const file of files) { + const filename = path.join(startPath, file); + const stat = fs.lstatSync(filename); + + // If directory, recurse into it + if (stat.isDirectory()) { + // Skip node_modules recursion to avoid unnecessary deep scanning + if (file === 'node_modules') { + if (file === pattern) { + results.push(filename); + } + continue; + } + results = results.concat(glob(filename, pattern)); + } + + // If file matches pattern, add to results + if (file === pattern) { + results.push(filename); + } + } + + return results; +}; + +async function setup() { + try { + // Clean up node_modules (if exists) + console.log(`\n${icons.clean} Cleaning up node_modules directories...`); + const nodeModulesPaths = glob('.', 'node_modules'); + for (const dir of nodeModulesPaths) { + console.log(`${icons.delete} Removing ${dir}`); + fs.rmSync(dir, { recursive: true, force: true }); + } + + // Install dependencies + execCommand('npm i --legacy-peer-deps', 'Installing dependencies'); + + // Build packages + execCommand('npm run build:graphql-docs', 'Building graphql-docs'); + execCommand('npm run build:bruno-query', 'Building bruno-query'); + execCommand('npm run build:bruno-common', 'Building bruno-common'); + + // Bundle JS sandbox libraries + execCommand( + 'npm run sandbox:bundle-libraries --workspace=packages/bruno-js', + 'Bundling JS sandbox libraries' + ); + + console.log(`\n${icons.success} Setup completed successfully!\n`); + } catch (error) { + console.error(`\n${icons.error} Setup failed:`); + console.error(error); + process.exit(1); + } +} + +setup().catch(error => { + console.error(error); + process.exit(1); +});