-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b7dd11
commit 3f15fbf
Showing
7 changed files
with
538 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': major | ||
--- | ||
|
||
The `astro check` command now requires an external package `@astrojs/check` and an install of `typescript` in your project. This was done in order to make the main `astro` package smaller and give more flexibility to users in regard to the version of TypeScript they use. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import boxen from 'boxen'; | ||
import { execa } from 'execa'; | ||
import { bold, cyan, dim, magenta } from 'kleur/colors'; | ||
import { createRequire } from 'node:module'; | ||
import ora from 'ora'; | ||
import prompts from 'prompts'; | ||
import whichPm from 'which-pm'; | ||
import { debug, info, type LogOptions } from '../core/logger/core.js'; | ||
|
||
type GetPackageOptions = { | ||
skipAsk?: boolean; | ||
cwd?: string; | ||
}; | ||
|
||
export async function getPackage<T>( | ||
packageName: string, | ||
logging: LogOptions, | ||
options: GetPackageOptions, | ||
otherDeps: string[] = [] | ||
): Promise<T | undefined> { | ||
const require = createRequire(options.cwd ?? process.cwd()); | ||
|
||
let packageImport; | ||
try { | ||
require.resolve(packageName); | ||
|
||
// The `require.resolve` is required as to avoid Node caching the failed `import` | ||
packageImport = await import(packageName); | ||
} catch (e) { | ||
info( | ||
logging, | ||
'', | ||
`To continue, Astro requires the following dependency to be installed: ${bold(packageName)}.` | ||
); | ||
const result = await installPackage([packageName, ...otherDeps], options, logging); | ||
|
||
if (result) { | ||
packageImport = await import(packageName); | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
|
||
return packageImport as T; | ||
} | ||
|
||
function getInstallCommand(packages: string[], packageManager: string) { | ||
switch (packageManager) { | ||
case 'npm': | ||
return { pm: 'npm', command: 'install', flags: [], dependencies: packages }; | ||
case 'yarn': | ||
return { pm: 'yarn', command: 'add', flags: [], dependencies: packages }; | ||
case 'pnpm': | ||
return { pm: 'pnpm', command: 'add', flags: [], dependencies: packages }; | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
async function installPackage( | ||
packageNames: string[], | ||
options: GetPackageOptions, | ||
logging: LogOptions | ||
): Promise<boolean> { | ||
const cwd = options.cwd ?? process.cwd(); | ||
const packageManager = (await whichPm(cwd)).name ?? 'npm'; | ||
const installCommand = getInstallCommand(packageNames, packageManager); | ||
|
||
if (!installCommand) { | ||
return false; | ||
} | ||
|
||
const coloredOutput = `${bold(installCommand.pm)} ${installCommand.command}${[ | ||
'', | ||
...installCommand.flags, | ||
].join(' ')} ${cyan(installCommand.dependencies.join(' '))}`; | ||
const message = `\n${boxen(coloredOutput, { | ||
margin: 0.5, | ||
padding: 0.5, | ||
borderStyle: 'round', | ||
})}\n`; | ||
info( | ||
logging, | ||
null, | ||
`\n ${magenta('Astro will run the following command:')}\n ${dim( | ||
'If you skip this step, you can always run it yourself later' | ||
)}\n${message}` | ||
); | ||
|
||
let response; | ||
if (options.skipAsk) { | ||
response = true; | ||
} else { | ||
response = ( | ||
await prompts({ | ||
type: 'confirm', | ||
name: 'askToContinue', | ||
message: 'Continue?', | ||
initial: true, | ||
}) | ||
).askToContinue; | ||
} | ||
|
||
if (Boolean(response)) { | ||
const spinner = ora('Installing dependencies...').start(); | ||
try { | ||
await execa( | ||
installCommand.pm, | ||
[installCommand.command, ...installCommand.flags, ...installCommand.dependencies], | ||
{ cwd: cwd } | ||
); | ||
spinner.succeed(); | ||
|
||
return true; | ||
} catch (err) { | ||
debug('add', 'Error installing dependencies', err); | ||
spinner.fail(); | ||
|
||
return false; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} |
Oops, something went wrong.