|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const { execSync } = require('child_process'); |
| 6 | +const readline = require('readline'); |
| 7 | + |
| 8 | +// Check if AUTO_INSTALL_EXTRA_DEPS is explicitly set to false |
| 9 | +if (process.env.AUTO_INSTALL_EXTRA_DEPS === 'false') { |
| 10 | + console.log('AUTO_INSTALL_EXTRA_DEPS is set to false, skipping script execution'); |
| 11 | + process.exit(0); |
| 12 | +} |
| 13 | + |
| 14 | +// Get the workspace root directory |
| 15 | +const workspaceRoot = process.cwd(); |
| 16 | + |
| 17 | +// Check if AUTO_INSTALL_EXTRA_DEPS environment variable is set to bypass prompts |
| 18 | +const autoMode = process.env.AUTO_INSTALL_EXTRA_DEPS === 'true'; |
| 19 | + |
| 20 | +// Create readline interface for user prompts |
| 21 | +const rl = readline.createInterface({ |
| 22 | + input: process.stdin, |
| 23 | + output: process.stdout |
| 24 | +}); |
| 25 | + |
| 26 | +// Function to prompt user for confirmation |
| 27 | +function promptUser(question) { |
| 28 | + return new Promise((resolve) => { |
| 29 | + rl.question(question, (answer) => { |
| 30 | + resolve(answer.toLowerCase().startsWith('y')); |
| 31 | + }); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +// Function to find all package.json files in the apps directory |
| 36 | +function findPackageJsonFiles(directory) { |
| 37 | + const appsDir = path.join(workspaceRoot, directory); |
| 38 | + |
| 39 | + // Check if the directory exists |
| 40 | + if (!fs.existsSync(appsDir)) { |
| 41 | + console.log(`Directory ${directory} does not exist, skipping...`); |
| 42 | + return []; |
| 43 | + } |
| 44 | + |
| 45 | + const packageJsonFiles = []; |
| 46 | + |
| 47 | + // Read all items in the directory |
| 48 | + const items = fs.readdirSync(appsDir); |
| 49 | + |
| 50 | + for (const item of items) { |
| 51 | + const itemPath = path.join(appsDir, item); |
| 52 | + const stat = fs.statSync(itemPath); |
| 53 | + |
| 54 | + if (stat.isDirectory()) { |
| 55 | + // Check if this directory has a package.json |
| 56 | + const packageJsonPath = path.join(itemPath, 'package.json'); |
| 57 | + if (fs.existsSync(packageJsonPath)) { |
| 58 | + packageJsonFiles.push(packageJsonPath); |
| 59 | + // Skip searching in subdirectories once a package.json is found |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + // Only search subdirectories if no package.json was found in this directory |
| 64 | + packageJsonFiles.push(...findPackageJsonFiles(path.join(directory, item))); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return packageJsonFiles; |
| 69 | +} |
| 70 | + |
| 71 | +// Find all package.json files in the apps directory |
| 72 | +const packageJsonFiles = findPackageJsonFiles('apps'); |
| 73 | + |
| 74 | +if (packageJsonFiles.length === 0) { |
| 75 | + console.log('No package.json files found in the apps directory'); |
| 76 | + process.exit(0); |
| 77 | +} |
| 78 | + |
| 79 | +console.log(`Found ${packageJsonFiles.length} package.json files in the apps directory`); |
| 80 | + |
| 81 | +// Process each package.json file |
| 82 | +async function processPackageJsonFiles() { |
| 83 | + for (const packageJsonPath of packageJsonFiles) { |
| 84 | + try { |
| 85 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); |
| 86 | + |
| 87 | + // Check if extraDependencies exists |
| 88 | + if (packageJson.extraDependencies && Object.keys(packageJson.extraDependencies).length > 0) { |
| 89 | + console.log(`Found extraDependencies in ${packageJsonPath}, installing...`); |
| 90 | + |
| 91 | + // Process each dependency individually |
| 92 | + for (const [pkg, version] of Object.entries(packageJson.extraDependencies)) { |
| 93 | + const dependencyToInstall = `${pkg}@${version}`; |
| 94 | + |
| 95 | + // Prompt user if not in auto mode |
| 96 | + let shouldInstall = autoMode; |
| 97 | + if (!autoMode) { |
| 98 | + shouldInstall = await promptUser(` |
| 99 | +Do you want to install ${dependencyToInstall}? (y/n): |
| 100 | +Note that these packages are dual-licensed by Blocknotejs |
| 101 | +under AGPL-3.0 or a proprietary license. If you choose |
| 102 | +to install them, please ensure you fulfill your licensing |
| 103 | +obligations with respect to BlockNoteJS |
| 104 | +`); |
| 105 | + } |
| 106 | + |
| 107 | + if (shouldInstall) { |
| 108 | + // Install the dependency using npm install |
| 109 | + try { |
| 110 | + console.log(`Installing: ${dependencyToInstall}`); |
| 111 | + execSync(`npm install --no-save --ignore-scripts --no-audit ${dependencyToInstall}`, { stdio: 'inherit' }); |
| 112 | + console.log(`Extra dependency ${dependencyToInstall} installed successfully`); |
| 113 | + } catch (error) { |
| 114 | + console.error(`Failed to install extra dependency ${dependencyToInstall}:`, error.message); |
| 115 | + // Continue with other dependencies even if one fails |
| 116 | + } |
| 117 | + } else { |
| 118 | + console.log(`Skipping installation of ${dependencyToInstall}`); |
| 119 | + } |
| 120 | + } |
| 121 | + } else { |
| 122 | + console.log(`No extraDependencies found in ${packageJsonPath}, skipping installation`); |
| 123 | + } |
| 124 | + } catch (error) { |
| 125 | + console.error(`Error reading or parsing ${packageJsonPath}:`, error.message); |
| 126 | + // Continue with other package.json files even if one fails |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + console.log('Finished processing all package.json files'); |
| 131 | + rl.close(); |
| 132 | +} |
| 133 | + |
| 134 | +// Run the async function |
| 135 | +processPackageJsonFiles().catch(error => { |
| 136 | + console.error('An error occurred:', error); |
| 137 | + rl.close(); |
| 138 | + process.exit(1); |
| 139 | +}); |
0 commit comments