|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import fetch from 'node-fetch'; |
| 4 | +import AdmZip from 'adm-zip'; |
| 5 | +import ProgressBar from 'progress'; |
| 6 | +import ora from 'ora'; |
| 7 | +import chalk from 'chalk'; |
| 8 | + |
| 9 | +// TODO: Extract into a util fn as it's reused for theme setup |
| 10 | +async function downloadFile(url, outputPath) { |
| 11 | + const response = await fetch(url); |
| 12 | + |
| 13 | + if (!response.ok) { |
| 14 | + throw new Error(`Failed to download: ${response.statusText}`); |
| 15 | + } |
| 16 | + |
| 17 | + const fileStream = fs.createWriteStream(outputPath); |
| 18 | + const contentLength = response.headers.get('content-length'); |
| 19 | + let totalSize = parseInt(contentLength, 10); |
| 20 | + |
| 21 | + if (isNaN(totalSize)) { |
| 22 | + console.warn( |
| 23 | + 'Warning: Size of the file is unknown. Progress bar will not be shown.' |
| 24 | + ); |
| 25 | + } else { |
| 26 | + const progressBar = new ProgressBar('Downloading [:bar] :percent :etas', { |
| 27 | + complete: '=', |
| 28 | + incomplete: ' ', |
| 29 | + width: 40, |
| 30 | + total: totalSize, |
| 31 | + }); |
| 32 | + |
| 33 | + response.body.on('data', (chunk) => { |
| 34 | + progressBar.tick(chunk.length); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + await new Promise((resolve, reject) => { |
| 39 | + response.body.pipe(fileStream); |
| 40 | + response.body.on('error', reject); |
| 41 | + fileStream.on('finish', resolve); |
| 42 | + }); |
| 43 | +} |
| 44 | +// ... [other imports and functions] |
| 45 | + |
| 46 | +async function setup_plugin() { |
| 47 | + // TODO: Do this in setup_theme as well |
| 48 | + const zipName = 'trbs-framework.zip'; |
| 49 | + |
| 50 | + try { |
| 51 | + // Ensure the current working directory ends with plugins |
| 52 | + const pluginsEndPath = path.join('wp-content', 'plugins'); |
| 53 | + const cwd = process.cwd(); |
| 54 | + // TODO: Do this is setup_theme as well |
| 55 | + if (!cwd.endsWith(pluginsEndPath)) { |
| 56 | + console.log( |
| 57 | + chalk.red( |
| 58 | + '\nThis command needs to be run inside the wp-content/plugins folder.' |
| 59 | + ) |
| 60 | + ); |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + // Check if plugin directory already exists |
| 65 | + const newPluginPath = path.join('.', 'trbs-framework'); |
| 66 | + if (fs.existsSync(newPluginPath)) { |
| 67 | + console.log(chalk.red('\nPlugin already exists.')); |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + // Fetch the latest release URL from GitHub API |
| 72 | + const apiUrl = |
| 73 | + 'https://api.github.com/repos/webredone/trbs-framework-plugin/releases/latest'; |
| 74 | + const apiResponse = await fetch(apiUrl); |
| 75 | + const apiData = await apiResponse.json(); |
| 76 | + const zipUrl = apiData.zipball_url; |
| 77 | + |
| 78 | + // Download the zip file |
| 79 | + await downloadFile(zipUrl, zipName); |
| 80 | + |
| 81 | + // Unzip the file to a temporary directory |
| 82 | + const spinner = ora('Unzipping the file...').start(); |
| 83 | + const zip = new AdmZip(zipName); |
| 84 | + const tempExtractPath = path.join('.', 'temp-extract'); |
| 85 | + fs.mkdirSync(tempExtractPath, { recursive: true }); |
| 86 | + zip.extractAllTo(tempExtractPath, true); |
| 87 | + spinner.succeed('Unzipping completed'); |
| 88 | + |
| 89 | + // Determine the top-level directory name and move its contents |
| 90 | + const extractedFolders = fs.readdirSync(tempExtractPath); |
| 91 | + if (extractedFolders.length !== 1) { |
| 92 | + console.error(chalk.red('Unexpected structure in the zip file.')); |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + const topLevelDir = path.join(tempExtractPath, extractedFolders[0]); |
| 97 | + fs.renameSync(topLevelDir, newPluginPath); |
| 98 | + |
| 99 | + // Clean up temporary directory |
| 100 | + fs.rmSync(tempExtractPath, { recursive: true, force: true }); |
| 101 | + |
| 102 | + // Delete the .git directory from the new plugin folder |
| 103 | + const gitDirPath = path.join(newPluginPath, '.git'); |
| 104 | + if (fs.existsSync(gitDirPath)) { |
| 105 | + fs.rmSync(gitDirPath, { recursive: true, force: true }); |
| 106 | + } |
| 107 | + |
| 108 | + // Delete the zip file |
| 109 | + fs.unlinkSync(zipName); |
| 110 | + |
| 111 | + console.log( |
| 112 | + chalk.green( |
| 113 | + 'Plugin has been set up. Please activate it from the WordPress admin panel.' |
| 114 | + ) |
| 115 | + ); |
| 116 | + |
| 117 | + // Read package.json and extract Node version |
| 118 | + const packageJsonPath = path.join(newPluginPath, 'package.json'); |
| 119 | + if (fs.existsSync(packageJsonPath)) { |
| 120 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); |
| 121 | + const nodeVersion = packageJson.engines.node; |
| 122 | + console.log( |
| 123 | + `Please ensure you have Node version ${nodeVersion} installed.` |
| 124 | + ); |
| 125 | + console.log( |
| 126 | + 'Then, run "npm i" and "composer i" from the plugin root directory.' |
| 127 | + ); |
| 128 | + } else { |
| 129 | + console.log( |
| 130 | + chalk.yellow('Note: package.json not found in the plugin directory.') |
| 131 | + ); |
| 132 | + } |
| 133 | + } catch (error) { |
| 134 | + console.error(chalk.red(`Error setting up plugin: ${error.message}`)); |
| 135 | + return false; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +export default setup_plugin; |
0 commit comments