|
| 1 | +import {execSync} from 'child_process'; |
| 2 | +import {fileURLToPath} from 'url'; |
| 3 | +import fs from 'fs-extra'; |
| 4 | +import os from 'os'; |
| 5 | +import path from 'path'; |
| 6 | +import * as terser from 'terser'; |
| 7 | + |
| 8 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 9 | +const neoPath = path.resolve(__dirname, '../'); |
| 10 | + |
| 11 | +const languages = ['bash', 'css', 'javascript', 'json', 'scss', 'xml']; |
| 12 | +const outputDir = path.resolve(neoPath, 'dist/highlight'); |
| 13 | +const outputFile = path.join(outputDir, 'highlight.custom.js'); |
| 14 | +const minOutputFile = path.join(outputDir, 'highlight.custom.min.js'); |
| 15 | +const tempDir = path.resolve(neoPath, 'tmp/highlightjs'); |
| 16 | + |
| 17 | +const gitCmd = os.platform().startsWith('win') ? 'git.exe' : 'git'; |
| 18 | +const nodeCmd = os.platform().startsWith('win') ? 'node.exe' : 'node'; |
| 19 | +const npmCmd = os.platform().startsWith('win') ? 'npm.cmd' : 'npm'; |
| 20 | + |
| 21 | +async function main() { |
| 22 | + console.log('Building custom highlight.js bundle...'); |
| 23 | + |
| 24 | + try { |
| 25 | + // 1. Clean up the temporary directory |
| 26 | + console.log(`Cleaning up ${tempDir}`); |
| 27 | + fs.removeSync(tempDir); |
| 28 | + fs.ensureDirSync(tempDir); |
| 29 | + |
| 30 | + // 2. Clone the highlight.js repository |
| 31 | + console.log(`Cloning highlight.js into ${tempDir}`); |
| 32 | + const cloneCommand = `${gitCmd} clone --depth 1 https://github.com/highlightjs/highlight.js.git ${tempDir}`; |
| 33 | + execSync(cloneCommand, { stdio: 'inherit' }); |
| 34 | + |
| 35 | + // 3. Install dependencies |
| 36 | + console.log(`Installing dependencies in ${tempDir}`); |
| 37 | + const installCommand = `${npmCmd} install`; |
| 38 | + execSync(installCommand, { cwd: tempDir, stdio: 'inherit' }); |
| 39 | + |
| 40 | + // 4. Run the build script |
| 41 | + console.log('Running build script...'); |
| 42 | + const buildCommand = [ |
| 43 | + nodeCmd, |
| 44 | + 'tools/build.js', |
| 45 | + '-n', |
| 46 | + ...languages |
| 47 | + ].join(' '); |
| 48 | + |
| 49 | + execSync(buildCommand, { cwd: tempDir, stdio: 'inherit' }); |
| 50 | + |
| 51 | + // 5. Copy and minify the generated bundle |
| 52 | + const generatedFile = path.join(tempDir, 'build/highlight.js'); |
| 53 | + console.log(`Copying ${generatedFile} to ${outputFile}`); |
| 54 | + const bundleContent = fs.readFileSync(generatedFile, 'utf-8'); |
| 55 | + fs.ensureDirSync(outputDir); |
| 56 | + fs.writeFileSync(outputFile, bundleContent); |
| 57 | + |
| 58 | + console.log(`Minifying ${outputFile}`); |
| 59 | + const minifiedContent = await terser.minify(bundleContent); |
| 60 | + fs.writeFileSync(minOutputFile, minifiedContent.code); |
| 61 | + |
| 62 | + |
| 63 | + // 6. Clean up the temporary directory |
| 64 | + console.log(`Cleaning up ${tempDir}`); |
| 65 | + fs.removeSync(tempDir); |
| 66 | + |
| 67 | + console.log(`Custom highlight.js bundle created at: ${outputFile}`); |
| 68 | + console.log(`Minified highlight.js bundle created at: ${minOutputFile}`); |
| 69 | + } catch (error) { |
| 70 | + console.error(`Error building highlight.js bundle: ${error}`); |
| 71 | + // In case of error, leave the temporary directory for inspection |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +main(); |
0 commit comments