Add verbose and reload commands (#297)
* Add verbose and reload commands * Adds docs * Adds a changeset * Update witty-colts-fix.md Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
This commit is contained in:
parent
c6d3456968
commit
1d930ffdf4
6 changed files with 58 additions and 9 deletions
5
.changeset/witty-colts-fix.md
Normal file
5
.changeset/witty-colts-fix.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Adds [`--verbose`](https://github.com/snowpackjs/astro/blob/main/docs/cli.md#--verbose) and [`--reload`](https://github.com/snowpackjs/astro/blob/main/docs/cli.md#--reload) flags to the `astro` CLI.
|
|
@ -20,6 +20,14 @@ The root is used for finding the Astro configuration file.
|
|||
astro --project-root examples/snowpack dev
|
||||
```
|
||||
|
||||
#### `--reload`
|
||||
|
||||
Clears the cache (dependencies are built within Astro apps).
|
||||
|
||||
#### `--verbose`
|
||||
|
||||
Enables verbose logging, which is helpful when debugging an issue.
|
||||
|
||||
#### `--version`
|
||||
|
||||
Print the Astro version number and exit.
|
||||
|
|
|
@ -9,15 +9,20 @@ import yargs from 'yargs-parser';
|
|||
import { loadConfig } from './config.js';
|
||||
import { build } from './build.js';
|
||||
import devServer from './dev.js';
|
||||
import { reload } from './reload.js';
|
||||
|
||||
const { readFile } = fsPromises;
|
||||
const buildAndExit = async (...args: Parameters<typeof build>) => {
|
||||
const ret = await build(...args);
|
||||
process.exit(ret);
|
||||
};
|
||||
const reloadAndExit = async () => {
|
||||
const ret = await reload();
|
||||
process.exit(ret);
|
||||
};
|
||||
|
||||
type Arguments = yargs.Arguments;
|
||||
type cliCommand = 'help' | 'version' | 'dev' | 'build';
|
||||
type cliCommand = 'help' | 'version' | 'dev' | 'build' | 'reload';
|
||||
interface CLIState {
|
||||
cmd: cliCommand;
|
||||
options: {
|
||||
|
@ -25,6 +30,7 @@ interface CLIState {
|
|||
sitemap?: boolean;
|
||||
port?: number;
|
||||
config?: string;
|
||||
reload?: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -50,6 +56,10 @@ function resolveArgs(flags: Arguments): CLIState {
|
|||
case 'build':
|
||||
return { cmd: 'build', options };
|
||||
default:
|
||||
if (flags.reload) {
|
||||
return { cmd: 'reload', options };
|
||||
}
|
||||
|
||||
return { cmd: 'help', options };
|
||||
}
|
||||
}
|
||||
|
@ -66,6 +76,8 @@ function printHelp() {
|
|||
--config <path> Specify the path to the Astro config file.
|
||||
--project-root <path> Specify the path to the project root folder.
|
||||
--no-sitemap Disable sitemap generation (build only).
|
||||
--reload Clean the cache, reinstalling dependencies.
|
||||
--verbose Enable verbose logging
|
||||
--version Show the version number and exit.
|
||||
--help Show this help message.
|
||||
`);
|
||||
|
@ -84,22 +96,23 @@ function mergeCLIFlags(astroConfig: AstroConfig, flags: CLIState['options']) {
|
|||
}
|
||||
|
||||
/** Handle `astro run` command */
|
||||
async function runCommand(rawRoot: string, cmd: (a: AstroConfig) => Promise<void>, options: CLIState['options']) {
|
||||
async function runCommand(rawRoot: string, cmd: (a: AstroConfig, options: any) => Promise<void>, options: CLIState['options']) {
|
||||
try {
|
||||
const projectRoot = options.projectRoot || rawRoot;
|
||||
const astroConfig = await loadConfig(projectRoot, options.config);
|
||||
mergeCLIFlags(astroConfig, options);
|
||||
|
||||
return cmd(astroConfig);
|
||||
return cmd(astroConfig, options);
|
||||
} catch (err) {
|
||||
console.error(colors.red(err.toString() || err));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
const cmdMap = new Map([
|
||||
const cmdMap = new Map<string, (a: AstroConfig, opts?: any) => Promise<void>>([
|
||||
['build', buildAndExit],
|
||||
['dev', devServer],
|
||||
['reload', reloadAndExit]
|
||||
]);
|
||||
|
||||
/** The primary CLI action */
|
||||
|
@ -118,11 +131,20 @@ export async function cli(args: string[]) {
|
|||
process.exit(0);
|
||||
break;
|
||||
}
|
||||
case 'reload': {
|
||||
await reloadAndExit();
|
||||
break;
|
||||
}
|
||||
case 'build':
|
||||
case 'dev': {
|
||||
if(flags.reload) {
|
||||
await reload();
|
||||
}
|
||||
|
||||
const cmd = cmdMap.get(state.cmd);
|
||||
if (!cmd) throw new Error(`Error running ${state.cmd}`);
|
||||
runCommand(flags._[3], cmd, state.options);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ import 'source-map-support/register.js';
|
|||
import type { AstroConfig } from './@types/astro';
|
||||
import type { LogOptions } from './logger.js';
|
||||
|
||||
import { logger as snowpackLogger } from 'snowpack';
|
||||
import { green } from 'kleur/colors';
|
||||
import http from 'http';
|
||||
import path from 'path';
|
||||
|
@ -13,9 +12,6 @@ import { stopTimer } from './build/util';
|
|||
|
||||
const hostname = '127.0.0.1';
|
||||
|
||||
// Disable snowpack from writing to stdout/err.
|
||||
snowpackLogger.level = 'silent';
|
||||
|
||||
const logging: LogOptions = {
|
||||
level: 'debug',
|
||||
dest: defaultLogDestination,
|
||||
|
|
18
packages/astro/src/reload.ts
Normal file
18
packages/astro/src/reload.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
import type { LogOptions } from './logger';
|
||||
import { clearCache } from 'snowpack';
|
||||
import { defaultLogDestination, debug, error, info, parseError } from './logger.js';
|
||||
|
||||
const logging: LogOptions = {
|
||||
level: 'debug',
|
||||
dest: defaultLogDestination,
|
||||
};
|
||||
|
||||
export async function reload() {
|
||||
try {
|
||||
info(logging, 'reload', `Clearing the cache...`);
|
||||
await clearCache();
|
||||
return 0;
|
||||
} catch {
|
||||
return 1;
|
||||
}
|
||||
}
|
|
@ -45,7 +45,7 @@ type LoadResultError = { statusCode: 500 } & ({ type: 'parse-error'; error: Comp
|
|||
export type LoadResult = (LoadResultSuccess | LoadResultNotFound | LoadResultRedirect | LoadResultError) & { collectionInfo?: CollectionInfo };
|
||||
|
||||
// Disable snowpack from writing to stdout/err.
|
||||
snowpackLogger.level = 'silent';
|
||||
snowpackLogger.level = process.argv.includes('--verbose') ? 'debug' : 'silent';
|
||||
|
||||
/** Pass a URL to Astro to resolve and build */
|
||||
async function load(config: RuntimeConfig, rawPathname: string | undefined): Promise<LoadResult> {
|
||||
|
|
Loading…
Reference in a new issue