|
| 1 | +import * as prompts from '@clack/prompts'; |
| 2 | +import chalk from 'chalk'; |
| 3 | +import detectIndent from 'detect-indent'; |
| 4 | +import { execa } from 'execa'; |
| 5 | +import fs from 'node:fs'; |
| 6 | +import path from 'node:path'; |
| 7 | +import whichpm from 'which-pm'; |
| 8 | +import type { Arguments } from 'yargs-parser'; |
| 9 | +import { pkg } from '../../pkg.js'; |
| 10 | +import { generateAstroConfig, parseAstroConfig, replaceArgs } from '../../utils/astro-config.js'; |
| 11 | +import { errorLabel, primaryLabel, printHelp } from '../../utils/messages.js'; |
| 12 | +import { updateWorkspaceVersions } from '../../utils/workspace-version.js'; |
| 13 | +import { DEFAULT_VALUES, type EjectOptions } from './options.js'; |
| 14 | + |
| 15 | +interface PackageJson { |
| 16 | + dependencies: Record<string, string>; |
| 17 | + devDependencies: Record<string, string>; |
| 18 | +} |
| 19 | + |
| 20 | +const TUTORIALKIT_VERSION = pkg.version; |
| 21 | +const REQUIRED_DEPENDENCIES = ['@tutorialkit/runtime', '@webcontainer/api', 'nanostores', '@nanostores/react']; |
| 22 | + |
| 23 | +export function ejectRoutes(flags: Arguments) { |
| 24 | + if (flags._[1] === 'help' || flags.help || flags.h) { |
| 25 | + printHelp({ |
| 26 | + commandName: `${pkg.name} eject`, |
| 27 | + usage: '[folder] [...options]', |
| 28 | + tables: { |
| 29 | + Options: [ |
| 30 | + [ |
| 31 | + '--force', |
| 32 | + `Overwrite existing files in the target directory without prompting (default ${chalk.yellow(DEFAULT_VALUES.force)})`, |
| 33 | + ], |
| 34 | + ['--defaults', 'Skip all the prompts and eject the routes using the defaults'], |
| 35 | + ], |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + return 0; |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + return _eject(flags); |
| 44 | + } catch (error) { |
| 45 | + console.error(`${errorLabel()} Command failed`); |
| 46 | + |
| 47 | + if (error.stack) { |
| 48 | + console.error(`\n${error.stack}`); |
| 49 | + } |
| 50 | + |
| 51 | + process.exit(1); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +async function _eject(flags: EjectOptions) { |
| 56 | + let folderPath = flags._[1] !== undefined ? String(flags._[1]) : undefined; |
| 57 | + |
| 58 | + if (folderPath === undefined) { |
| 59 | + folderPath = process.cwd(); |
| 60 | + } else { |
| 61 | + folderPath = path.resolve(process.cwd(), folderPath); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * First we make sure that the destination has the correct files |
| 66 | + * and that there won't be any files overwritten in the process. |
| 67 | + * |
| 68 | + * If there are any and `force` was not specified we abort. |
| 69 | + */ |
| 70 | + const { astroConfigPath, srcPath, pkgJsonPath, astroIntegrationPath, srcDestPath } = validateDestination( |
| 71 | + folderPath, |
| 72 | + flags.force, |
| 73 | + ); |
| 74 | + |
| 75 | + /** |
| 76 | + * We proceed with the astro configuration. |
| 77 | + * |
| 78 | + * There we must disable the default routes so that the |
| 79 | + * new routes that we're copying will be automatically picked up. |
| 80 | + */ |
| 81 | + const astroConfig = await parseAstroConfig(astroConfigPath); |
| 82 | + |
| 83 | + replaceArgs({ defaultRoutes: false }, astroConfig); |
| 84 | + |
| 85 | + fs.writeFileSync(astroConfigPath, generateAstroConfig(astroConfig)); |
| 86 | + |
| 87 | + // we copy all assets from the `default` folder into the `src` folder |
| 88 | + fs.cpSync(srcPath, srcDestPath, { recursive: true }); |
| 89 | + |
| 90 | + /** |
| 91 | + * Last, we ensure that the `package.json` contains the extra dependencies. |
| 92 | + * If any are missing we suggest to install the new dependencies. |
| 93 | + */ |
| 94 | + const pkgJsonContent = fs.readFileSync(pkgJsonPath, 'utf-8'); |
| 95 | + const indent = detectIndent(pkgJsonContent).indent || ' '; |
| 96 | + const pkgJson: PackageJson = JSON.parse(pkgJsonContent); |
| 97 | + |
| 98 | + const astroIntegrationPkgJson: PackageJson = JSON.parse( |
| 99 | + fs.readFileSync(path.join(astroIntegrationPath, 'package.json'), 'utf-8'), |
| 100 | + ); |
| 101 | + |
| 102 | + const newDependencies = []; |
| 103 | + |
| 104 | + for (const dep of REQUIRED_DEPENDENCIES) { |
| 105 | + if (!(dep in pkgJson.dependencies) && !(dep in pkgJson.devDependencies)) { |
| 106 | + pkgJson.dependencies[dep] = astroIntegrationPkgJson.dependencies[dep]; |
| 107 | + |
| 108 | + newDependencies.push(dep); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + updateWorkspaceVersions(pkgJson.dependencies, TUTORIALKIT_VERSION, (dependency) => |
| 113 | + REQUIRED_DEPENDENCIES.includes(dependency), |
| 114 | + ); |
| 115 | + |
| 116 | + if (newDependencies.length > 0) { |
| 117 | + fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, undefined, indent), { encoding: 'utf-8' }); |
| 118 | + |
| 119 | + console.log( |
| 120 | + primaryLabel('INFO'), |
| 121 | + `New dependencies added: ${newDependencies.join(', ')}. Install the new dependencies before proceeding.`, |
| 122 | + ); |
| 123 | + |
| 124 | + if (!flags.defaults) { |
| 125 | + const packageManager = (await whichpm(path.dirname(pkgJsonPath))).name; |
| 126 | + |
| 127 | + const answer = await prompts.confirm({ |
| 128 | + message: `Do you want to install those dependencies now using ${chalk.blue(packageManager)}?`, |
| 129 | + }); |
| 130 | + |
| 131 | + if (answer === true) { |
| 132 | + await execa(packageManager, ['install'], { cwd: folderPath, stdio: 'inherit' }); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +function validateDestination(folder: string, force: boolean) { |
| 139 | + assertExists(folder); |
| 140 | + |
| 141 | + const pkgJsonPath = assertExists(path.join(folder, 'package.json')); |
| 142 | + const astroConfigPath = assertExists(path.join(folder, 'astro.config.ts')); |
| 143 | + const srcDestPath = assertExists(path.join(folder, 'src')); |
| 144 | + |
| 145 | + const astroIntegrationPath = assertExists(path.resolve(folder, 'node_modules', '@tutorialkit', 'astro')); |
| 146 | + |
| 147 | + const srcPath = path.join(astroIntegrationPath, 'dist', 'default'); |
| 148 | + |
| 149 | + // check that there are no collision |
| 150 | + if (!force) { |
| 151 | + walk(srcPath, (relativePath) => { |
| 152 | + const destination = path.join(srcDestPath, relativePath); |
| 153 | + |
| 154 | + if (fs.existsSync(destination)) { |
| 155 | + throw new Error( |
| 156 | + `Eject aborted because '${destination}' would be overwritten by this command. Use ${chalk.yellow('--force')} to ignore this error.`, |
| 157 | + ); |
| 158 | + } |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + return { |
| 163 | + astroConfigPath, |
| 164 | + astroIntegrationPath, |
| 165 | + pkgJsonPath, |
| 166 | + srcPath, |
| 167 | + srcDestPath, |
| 168 | + }; |
| 169 | +} |
| 170 | + |
| 171 | +function assertExists(filePath: string) { |
| 172 | + if (!fs.existsSync(filePath)) { |
| 173 | + throw new Error(`${filePath} does not exists!`); |
| 174 | + } |
| 175 | + |
| 176 | + return filePath; |
| 177 | +} |
| 178 | + |
| 179 | +function walk(root: string, visit: (relativeFilePath: string) => void) { |
| 180 | + function traverse(folder: string, pathPrefix: string) { |
| 181 | + for (const filename of fs.readdirSync(folder)) { |
| 182 | + const filePath = path.join(folder, filename); |
| 183 | + const stat = fs.statSync(filePath); |
| 184 | + |
| 185 | + const relativeFilePath = path.join(pathPrefix, filename); |
| 186 | + |
| 187 | + if (stat.isDirectory()) { |
| 188 | + traverse(filePath, relativeFilePath); |
| 189 | + } else { |
| 190 | + visit(relativeFilePath); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + traverse(root, ''); |
| 196 | +} |
0 commit comments