|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Copyright (c) HashiCorp, Inc. |
| 4 | + * SPDX-License-Identifier: MPL-2.0 |
| 5 | + */ |
| 6 | +import { |
| 7 | + intro, |
| 8 | + outro, |
| 9 | + confirm, |
| 10 | + select, |
| 11 | + spinner, |
| 12 | + isCancel, |
| 13 | + cancel, |
| 14 | + text, |
| 15 | + group, |
| 16 | + note, |
| 17 | +} from '@clack/prompts' |
| 18 | +import color from 'picocolors' |
| 19 | +import path from 'node:path' |
| 20 | +import fs from 'node:fs' |
| 21 | +import mkdirp from 'mkdirp' |
| 22 | + |
| 23 | +const WORKFLOW_TEMPLATE_FILE = 'template.yml' |
| 24 | +const WORKFLOW_FILE = 'next_bundle_analysis.yml' |
| 25 | + |
| 26 | +const DEFAULT_PACKAGE_CONFIG = { |
| 27 | + budget: 350, |
| 28 | + budgetPercentIncreaseRed: 20, |
| 29 | + minimumChangeThreshold: 0, |
| 30 | + buildOutputDirectory: '.next', |
| 31 | + showDetails: true, |
| 32 | +} |
| 33 | + |
| 34 | +const DEFAULT_WORKFLOW_CONFIG = { |
| 35 | + baseBranch: 'main', |
| 36 | + nodeVersion: 18, |
| 37 | + packageManager: 'npm', |
| 38 | + workingDirectory: './', |
| 39 | + buildCommand: './node_modules/.bin/next build', |
| 40 | + buildOutputDirectory: DEFAULT_PACKAGE_CONFIG.buildOutputDirectory, |
| 41 | +} |
| 42 | + |
| 43 | +async function number(opts) { |
| 44 | + const result = await text(opts) |
| 45 | + return Number.parseInt(result, 10) |
| 46 | +} |
| 47 | + |
| 48 | +async function writePackageJsonConfig(config) { |
| 49 | + // write the config values to package.json |
| 50 | + const packageJsonPath = path.join(process.cwd(), 'package.json') |
| 51 | + const packageJsonContent = JSON.parse( |
| 52 | + fs.readFileSync(packageJsonPath, 'utf-8') |
| 53 | + ) |
| 54 | + packageJsonContent.nextBundleAnalysis = { |
| 55 | + ...DEFAULT_PACKAGE_CONFIG, |
| 56 | + ...config, |
| 57 | + } |
| 58 | + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJsonContent, null, 2)) |
| 59 | +} |
| 60 | + |
| 61 | +async function writeWorkflowFile(config) { |
| 62 | + const packageJsonPath = new URL('package.json', import.meta.url) |
| 63 | + const packageJsonContent = JSON.parse( |
| 64 | + fs.readFileSync(packageJsonPath, 'utf-8') |
| 65 | + ) |
| 66 | + const templatePath = new URL(WORKFLOW_TEMPLATE_FILE, import.meta.url) |
| 67 | + const workflowsPath = path.join(process.cwd(), '.github/workflows') |
| 68 | + const workflowFilePath = path.join(workflowsPath, WORKFLOW_FILE) |
| 69 | + |
| 70 | + let template = fs.readFileSync(templatePath, 'utf-8') |
| 71 | + |
| 72 | + // Specify the latest version |
| 73 | + template = template.replace('{PACKAGE_VERSION}', packageJsonContent.version) |
| 74 | + |
| 75 | + // mkdir -p the .workflows directory |
| 76 | + mkdirp.sync(workflowsPath) |
| 77 | + |
| 78 | + const areInputsChangedFromDefault = !Object.keys( |
| 79 | + DEFAULT_WORKFLOW_CONFIG |
| 80 | + ).every((key) => DEFAULT_WORKFLOW_CONFIG[key] === config[key]) |
| 81 | + |
| 82 | + if (areInputsChangedFromDefault) { |
| 83 | + // update the base branch |
| 84 | + template = template.replace('- main', `- ${config.baseBranch}`) |
| 85 | + |
| 86 | + // Update the inputs |
| 87 | + template = template |
| 88 | + .replace('# with:', 'with:') |
| 89 | + .replace(/#\s+node-version:.+$/m, ` node-version: ${config.nodeVersion}`) |
| 90 | + .replace( |
| 91 | + /#\s+package-manager:.+$/m, |
| 92 | + ` package-manager: ${config.packageManager}` |
| 93 | + ) |
| 94 | + .replace( |
| 95 | + /#\s+working-directory:.+$/m, |
| 96 | + ` working-directory: ${config.workingDirectory}` |
| 97 | + ) |
| 98 | + .replace( |
| 99 | + /#\s+build-output-directory:.+$/m, |
| 100 | + ` build-output-directory: ${config.buildOutputDirectory}` |
| 101 | + ) |
| 102 | + .replace( |
| 103 | + /#\s+build-command:.+$/m, |
| 104 | + ` build-command: ${config.buildCommand}` |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + fs.writeFileSync(workflowFilePath, template) |
| 109 | +} |
| 110 | + |
| 111 | +async function main() { |
| 112 | + console.log('\n', color.inverse(color.bold(' nextjs-bundle-analysis ')), '\n') |
| 113 | + |
| 114 | + intro(color.inverse(' configuration ')) |
| 115 | + |
| 116 | + const packageConfig = await group({ |
| 117 | + budget: async () => { |
| 118 | + const setBudget = await confirm({ |
| 119 | + message: 'Would you like to set a performance budget?', |
| 120 | + }) |
| 121 | + |
| 122 | + if (setBudget) { |
| 123 | + return ( |
| 124 | + (await number({ |
| 125 | + message: `What would you like the maximum javascript on first load to be (in kb)? (default: ${DEFAULT_PACKAGE_CONFIG.budget})`, |
| 126 | + defaultValue: DEFAULT_PACKAGE_CONFIG.budget, |
| 127 | + })) * 1024 |
| 128 | + ) |
| 129 | + } |
| 130 | + |
| 131 | + return DEFAULT_PACKAGE_CONFIG.budget |
| 132 | + }, |
| 133 | + budgetPercentIncreaseRed: () => |
| 134 | + number({ |
| 135 | + message: `If you exceed this percentage of the budget or filesize, it will be highlighted in red (default: ${DEFAULT_PACKAGE_CONFIG.budgetPercentIncreaseRed})`, |
| 136 | + defaultValue: DEFAULT_PACKAGE_CONFIG.budgetPercentIncreaseRed, |
| 137 | + }), |
| 138 | + minimumChangeThreshold: () => |
| 139 | + number({ |
| 140 | + message: `If a page's size change is below this threshold (in bytes), it will be considered unchanged (default: ${DEFAULT_PACKAGE_CONFIG.minimumChangeThreshold})`, |
| 141 | + defaultValue: DEFAULT_PACKAGE_CONFIG.minimumChangeThreshold, |
| 142 | + }), |
| 143 | + buildOutputDirectory: () => |
| 144 | + text({ |
| 145 | + message: `Do you have a custom dist directory? (default: ${DEFAULT_PACKAGE_CONFIG.buildOutputDirectory})`, |
| 146 | + defaultValue: DEFAULT_PACKAGE_CONFIG.buildOutputDirectory, |
| 147 | + }), |
| 148 | + }) |
| 149 | + |
| 150 | + await writePackageJsonConfig(packageConfig) |
| 151 | + |
| 152 | + outro('✅ Bundle analysis config written to package.json') |
| 153 | + |
| 154 | + intro(color.inverse(' workflow file ')) |
| 155 | + |
| 156 | + const workflowConfig = await group({ |
| 157 | + baseBranch: () => |
| 158 | + text({ |
| 159 | + message: `What's your base branch? (default: ${DEFAULT_WORKFLOW_CONFIG.baseBranch})`, |
| 160 | + defaultValue: DEFAULT_WORKFLOW_CONFIG.baseBranch, |
| 161 | + }), |
| 162 | + nodeVersion: () => |
| 163 | + text({ |
| 164 | + message: `What node version are you using? (default: ${DEFAULT_WORKFLOW_CONFIG.nodeVersion})`, |
| 165 | + defaultValue: DEFAULT_WORKFLOW_CONFIG.nodeVersion, |
| 166 | + }), |
| 167 | + packageManager: () => |
| 168 | + select({ |
| 169 | + message: 'What package manager do you use?', |
| 170 | + options: [ |
| 171 | + { value: 'npm', label: 'npm', hint: 'default' }, |
| 172 | + { value: 'yarn', label: 'yarn' }, |
| 173 | + { value: 'pnpm', label: 'pnpm' }, |
| 174 | + ], |
| 175 | + defaultValue: DEFAULT_WORKFLOW_CONFIG.nodeVersion, |
| 176 | + }), |
| 177 | + workingDirectory: () => |
| 178 | + text({ |
| 179 | + message: `What directory does your app live in? (default: ${DEFAULT_WORKFLOW_CONFIG.workingDirectory})`, |
| 180 | + defaultValue: DEFAULT_WORKFLOW_CONFIG.workingDirectory, |
| 181 | + }), |
| 182 | + buildCommand: () => |
| 183 | + text({ |
| 184 | + message: "What's your build command? (default: next build)", |
| 185 | + defaultValue: DEFAULT_WORKFLOW_CONFIG.buildCommand, |
| 186 | + }), |
| 187 | + }) |
| 188 | + |
| 189 | + await writeWorkflowFile({ |
| 190 | + ...workflowConfig, |
| 191 | + buildOutputDirectory: packageConfig.buildOutputDirectory, |
| 192 | + }) |
| 193 | + |
| 194 | + outro( |
| 195 | + '✅ Workflow file written to .github/workflows/next-js-bundle-analysis.yml' |
| 196 | + ) |
| 197 | +} |
| 198 | + |
| 199 | +main() |
0 commit comments