From 73570c815a1809345679cf8b78e2fbb6605b3387 Mon Sep 17 00:00:00 2001 From: Peter Singh Date: Fri, 1 Apr 2022 00:46:10 +0000 Subject: [PATCH 01/11] =?UTF-8?q?So=20This=20works=20=F0=9F=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/astro/src/openDocs/open.js | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 packages/astro/src/openDocs/open.js diff --git a/packages/astro/src/openDocs/open.js b/packages/astro/src/openDocs/open.js new file mode 100644 index 000000000000..d18081e3588b --- /dev/null +++ b/packages/astro/src/openDocs/open.js @@ -0,0 +1,35 @@ +import { execa } from "execa"; + +const commands = ()=>{ + const isGitPod = Boolean(process.env.GITPOD_REPO_ROOT) + const platform = (isGitPod) ? 'gitpod' : process.platform + + switch (platform) { + case 'android': + case 'linux': + return ['xdg-open']; + case 'darwin': + return ['open']; + case 'win32': + return ['cmd', ['/c', 'start']]; + case 'gitpod': + return ['/ide/bin/remote-cli/gitpod-code', ['--openExternal']] + default: + throw new Error(`Sorry but it appears that your Platform: "${platform}" isn't supported.`); + } +} + + +const launch = url => new Promise((res,rej)=>{ + try{ + const [command,args=[]] = commands() + execa(command,[...args,encodeURI(url)]) + return res() + }catch(err){ + return rej(err) + } +}) + +launch('https://docs.astro.build/') +launch('https://rainsberger.ca') +launch('https://www.google.com') From 9645863bc554f6614d8381dc594777ce0d2da99d Mon Sep 17 00:00:00 2001 From: Peter Singh Date: Fri, 1 Apr 2022 00:46:25 +0000 Subject: [PATCH 02/11] need to add to the cli next --- packages/astro/src/openDocs/index.ts | 1 + 1 file changed, 1 insertion(+) create mode 100644 packages/astro/src/openDocs/index.ts diff --git a/packages/astro/src/openDocs/index.ts b/packages/astro/src/openDocs/index.ts new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/packages/astro/src/openDocs/index.ts @@ -0,0 +1 @@ + From 4a576b236e49e7075aec1d30d24a5ca8b1c4c5be Mon Sep 17 00:00:00 2001 From: Peter Singh Date: Fri, 1 Apr 2022 21:50:50 +0000 Subject: [PATCH 03/11] Renamed Files and Export Applied creditation to where I found the 'inspiration' for this application. --- .../src/{openDocs/open.js => browser/index.ts} | 13 ++++++++----- packages/astro/src/openDocs/index.ts | 1 - 2 files changed, 8 insertions(+), 6 deletions(-) rename packages/astro/src/{openDocs/open.js => browser/index.ts} (72%) delete mode 100644 packages/astro/src/openDocs/index.ts diff --git a/packages/astro/src/openDocs/open.js b/packages/astro/src/browser/index.ts similarity index 72% rename from packages/astro/src/openDocs/open.js rename to packages/astro/src/browser/index.ts index d18081e3588b..5984eab49394 100644 --- a/packages/astro/src/openDocs/open.js +++ b/packages/astro/src/browser/index.ts @@ -1,5 +1,9 @@ import { execa } from "execa"; +/** + * Credit: Azhar22 + * @see https://github.com/azhar22k/ourl/blob/master/index.js + */ const commands = ()=>{ const isGitPod = Boolean(process.env.GITPOD_REPO_ROOT) const platform = (isGitPod) ? 'gitpod' : process.platform @@ -15,12 +19,12 @@ const commands = ()=>{ case 'gitpod': return ['/ide/bin/remote-cli/gitpod-code', ['--openExternal']] default: - throw new Error(`Sorry but it appears that your Platform: "${platform}" isn't supported.`); + throw new Error(`Sorry but it appears that your Platform: "${platform}" isn't supported. \nTo view Astro's Docs visit:\nhttps://docs.astro.build`); } } -const launch = url => new Promise((res,rej)=>{ +const browser = url => new Promise((res,rej)=>{ try{ const [command,args=[]] = commands() execa(command,[...args,encodeURI(url)]) @@ -30,6 +34,5 @@ const launch = url => new Promise((res,rej)=>{ } }) -launch('https://docs.astro.build/') -launch('https://rainsberger.ca') -launch('https://www.google.com') +export default browser + diff --git a/packages/astro/src/openDocs/index.ts b/packages/astro/src/openDocs/index.ts deleted file mode 100644 index 8b137891791f..000000000000 --- a/packages/astro/src/openDocs/index.ts +++ /dev/null @@ -1 +0,0 @@ - From 90496012c9669715657d4d16c61acf97dcfbfb1b Mon Sep 17 00:00:00 2001 From: Peter Singh Date: Fri, 1 Apr 2022 21:51:26 +0000 Subject: [PATCH 04/11] applied `astro docs` to cli --- packages/astro/src/cli/index.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts index df2d2c2a89a7..f57641e40e6c 100644 --- a/packages/astro/src/cli/index.ts +++ b/packages/astro/src/cli/index.ts @@ -11,13 +11,14 @@ import build from '../core/build/index.js'; import add from '../core/add/index.js'; import devServer from '../core/dev/index.js'; import preview from '../core/preview/index.js'; +import browser from '../browser/index.js'; import { check } from './check.js'; import { loadConfig } from '../core/config.js'; import { printHelp, formatErrorMessage, formatConfigErrorMessage } from '../core/messages.js'; import { createSafeError } from '../core/util.js'; type Arguments = yargs.Arguments; -type CLICommand = 'help' | 'version' | 'add' | 'dev' | 'build' | 'preview' | 'reload' | 'check'; +type CLICommand = 'help' | 'version' | 'add' | 'docs' | 'dev' | 'build' | 'preview' | 'reload' | 'check'; /** Display --help flag */ function printAstroHelp() { @@ -26,6 +27,7 @@ function printAstroHelp() { headline: 'Futuristic web development tool.', commands: [ ['add', 'Add an integration to your configuration.'], + ['docs', 'Launch Astro\'s Doc site from the terminal '], ['dev', 'Run Astro in development mode.'], ['build', 'Build a pre-compiled production-ready site.'], ['preview', 'Preview your build locally before deploying.'], @@ -144,7 +146,13 @@ export async function cli(args: string[]) { return throwAndExit(err); } } - + case 'docs':{ + try{ + browser('https://docs.astro.build/') + }catch(err){ + throwAndExit(err) + } + } default: { throw new Error(`Error running ${cmd}`); } From dfcf6451a10c42bdb200966c011494156d38ad22 Mon Sep 17 00:00:00 2001 From: Peter Singh Date: Fri, 1 Apr 2022 22:25:16 +0000 Subject: [PATCH 05/11] =?UTF-8?q?Trying=20to=20add=20to=20CLI,=20Not=20wor?= =?UTF-8?q?king=20=F0=9F=A4=B7=E2=80=8D=E2=99=82=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/astro/src/cli/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts index f57641e40e6c..31d1b58c0491 100644 --- a/packages/astro/src/cli/index.ts +++ b/packages/astro/src/cli/index.ts @@ -148,8 +148,8 @@ export async function cli(args: string[]) { } case 'docs':{ try{ - browser('https://docs.astro.build/') - }catch(err){ + await browser('https://docs.astro.build/') + }catch(err){ throwAndExit(err) } } From bc69133d630285aa943a1cded282b5508ea6cd14 Mon Sep 17 00:00:00 2001 From: Peter Singh Date: Fri, 1 Apr 2022 22:25:40 +0000 Subject: [PATCH 06/11] Converted into async method, --- packages/astro/src/browser/index.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/astro/src/browser/index.ts b/packages/astro/src/browser/index.ts index 5984eab49394..d53c3e85a321 100644 --- a/packages/astro/src/browser/index.ts +++ b/packages/astro/src/browser/index.ts @@ -4,7 +4,7 @@ import { execa } from "execa"; * Credit: Azhar22 * @see https://github.com/azhar22k/ourl/blob/master/index.js */ -const commands = ()=>{ +const commands =():any=>{ const isGitPod = Boolean(process.env.GITPOD_REPO_ROOT) const platform = (isGitPod) ? 'gitpod' : process.platform @@ -23,16 +23,15 @@ const commands = ()=>{ } } - -const browser = url => new Promise((res,rej)=>{ +//@ts-ignore +const browser = async(url:any):any=>{ try{ const [command,args=[]] = commands() - execa(command,[...args,encodeURI(url)]) - return res() + return execa(command,[...args,encodeURI(url)]) }catch(err){ - return rej(err) + throw Error(`${err}`) } -}) +} export default browser From 9f75b5a4eb42f622a0cfd5593e24eb5cba5f50b9 Mon Sep 17 00:00:00 2001 From: Peter Singh Date: Sat, 2 Apr 2022 00:31:06 +0000 Subject: [PATCH 07/11] =?UTF-8?q?=F0=9F=8E=86=F0=9F=8E=86=20It=20works!!!?= =?UTF-8?q?=20=F0=9F=A5=B3=F0=9F=8E=89=F0=9F=A5=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Embarrasing as it is I totally missed the part where logic was to be in. --- packages/astro/src/cli/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts index 31d1b58c0491..26e00a0ed67d 100644 --- a/packages/astro/src/cli/index.ts +++ b/packages/astro/src/cli/index.ts @@ -27,7 +27,7 @@ function printAstroHelp() { headline: 'Futuristic web development tool.', commands: [ ['add', 'Add an integration to your configuration.'], - ['docs', 'Launch Astro\'s Doc site from the terminal '], + ['docs', 'Launch Astro\'s Doc site from the terminal. '], ['dev', 'Run Astro in development mode.'], ['build', 'Build a pre-compiled production-ready site.'], ['preview', 'Preview your build locally before deploying.'], @@ -60,6 +60,7 @@ async function printVersion() { function resolveCommand(flags: Arguments): CLICommand { const cmd = flags._[2] as string; if (cmd === 'add') return 'add'; + if (cmd === 'docs') return 'docs'; if (flags.version) return 'version'; else if (flags.help) return 'help'; @@ -149,6 +150,7 @@ export async function cli(args: string[]) { case 'docs':{ try{ await browser('https://docs.astro.build/') + process.exit(0) }catch(err){ throwAndExit(err) } From e1df3bd7c3e08a6d13567c9a0355a3ca826c2e44 Mon Sep 17 00:00:00 2001 From: Peter Singh Date: Mon, 4 Apr 2022 14:50:13 +0000 Subject: [PATCH 08/11] Moved `docs` cmd to `supportedCommands` --- packages/astro/src/cli/index.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts index 26e00a0ed67d..a9df1f304b05 100644 --- a/packages/astro/src/cli/index.ts +++ b/packages/astro/src/cli/index.ts @@ -27,7 +27,7 @@ function printAstroHelp() { headline: 'Futuristic web development tool.', commands: [ ['add', 'Add an integration to your configuration.'], - ['docs', 'Launch Astro\'s Doc site from the terminal. '], + ['docs', 'Launch Astro\'s Doc site directly from the terminal. '], ['dev', 'Run Astro in development mode.'], ['build', 'Build a pre-compiled production-ready site.'], ['preview', 'Preview your build locally before deploying.'], @@ -60,12 +60,10 @@ async function printVersion() { function resolveCommand(flags: Arguments): CLICommand { const cmd = flags._[2] as string; if (cmd === 'add') return 'add'; - if (cmd === 'docs') return 'docs'; - if (flags.version) return 'version'; else if (flags.help) return 'help'; - const supportedCommands = new Set(['dev', 'build', 'preview', 'check']); + const supportedCommands = new Set(['dev', 'build', 'preview', 'check', 'docs']); if (supportedCommands.has(cmd)) { return cmd as CLICommand; } @@ -149,7 +147,7 @@ export async function cli(args: string[]) { } case 'docs':{ try{ - await browser('https://docs.astro.build/') + await browser('https://docs.astro.build/')//Launches System Browser process.exit(0) }catch(err){ throwAndExit(err) From 955afe7f8bc59c6d1346a1582524d87d76762085 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Wed, 6 Apr 2022 11:54:33 -0500 Subject: [PATCH 09/11] refactor: cleanup docs command --- packages/astro/src/browser/index.ts | 37 ----------------------------- packages/astro/src/cli/browser.ts | 32 +++++++++++++++++++++++++ packages/astro/src/cli/index.ts | 15 ++++++------ 3 files changed, 40 insertions(+), 44 deletions(-) delete mode 100644 packages/astro/src/browser/index.ts create mode 100644 packages/astro/src/cli/browser.ts diff --git a/packages/astro/src/browser/index.ts b/packages/astro/src/browser/index.ts deleted file mode 100644 index d53c3e85a321..000000000000 --- a/packages/astro/src/browser/index.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { execa } from "execa"; - -/** - * Credit: Azhar22 - * @see https://github.com/azhar22k/ourl/blob/master/index.js - */ -const commands =():any=>{ - const isGitPod = Boolean(process.env.GITPOD_REPO_ROOT) - const platform = (isGitPod) ? 'gitpod' : process.platform - - switch (platform) { - case 'android': - case 'linux': - return ['xdg-open']; - case 'darwin': - return ['open']; - case 'win32': - return ['cmd', ['/c', 'start']]; - case 'gitpod': - return ['/ide/bin/remote-cli/gitpod-code', ['--openExternal']] - default: - throw new Error(`Sorry but it appears that your Platform: "${platform}" isn't supported. \nTo view Astro's Docs visit:\nhttps://docs.astro.build`); - } -} - -//@ts-ignore -const browser = async(url:any):any=>{ - try{ - const [command,args=[]] = commands() - return execa(command,[...args,encodeURI(url)]) - }catch(err){ - throw Error(`${err}`) - } -} - -export default browser - diff --git a/packages/astro/src/cli/browser.ts b/packages/astro/src/cli/browser.ts new file mode 100644 index 000000000000..ad803513e347 --- /dev/null +++ b/packages/astro/src/cli/browser.ts @@ -0,0 +1,32 @@ +import type { ExecaChildProcess } from 'execa'; +import { execa } from 'execa'; + +/** + * Credit: Azhar22 + * @see https://github.com/azhar22k/ourl/blob/master/index.js + */ +const getPlatformSpecificCommand = (): [string]|[string, string[]] => { + const isGitPod = Boolean(process.env.GITPOD_REPO_ROOT); + const platform = isGitPod ? 'gitpod' : process.platform; + + switch (platform) { + case 'android': + case 'linux': + return ['xdg-open']; + case 'darwin': + return ['open']; + case 'win32': + return ['cmd', ['/c', 'start']]; + case 'gitpod': + return ['/ide/bin/remote-cli/gitpod-code', ['--openExternal']]; + default: + throw new Error( + `It looks like your platform ("${platform}") isn't supported!\nTo view Astro's docs, please visit https://docs.astro.build` + ); + } +}; + +export async function openInBrowser(url: string): Promise { + const [command, args = []] = getPlatformSpecificCommand(); + return execa(command, [...args, encodeURI(url)]); +}; diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts index a9df1f304b05..d893a81465e3 100644 --- a/packages/astro/src/cli/index.ts +++ b/packages/astro/src/cli/index.ts @@ -11,8 +11,8 @@ import build from '../core/build/index.js'; import add from '../core/add/index.js'; import devServer from '../core/dev/index.js'; import preview from '../core/preview/index.js'; -import browser from '../browser/index.js'; import { check } from './check.js'; +import { openInBrowser } from './browser.js'; import { loadConfig } from '../core/config.js'; import { printHelp, formatErrorMessage, formatConfigErrorMessage } from '../core/messages.js'; import { createSafeError } from '../core/util.js'; @@ -145,14 +145,15 @@ export async function cli(args: string[]) { return throwAndExit(err); } } - case 'docs':{ - try{ - await browser('https://docs.astro.build/')//Launches System Browser - process.exit(0) - }catch(err){ - throwAndExit(err) + + case 'docs': { + try { + return await openInBrowser('https://docs.astro.build/') + } catch (err) { + return throwAndExit(err) } } + default: { throw new Error(`Error running ${cmd}`); } From 5dfa66f0ceb75b317bd9f008bd4c14cd1d940285 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Wed, 6 Apr 2022 11:58:10 -0500 Subject: [PATCH 10/11] chore: add changeset --- .changeset/strange-avocados-double.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/strange-avocados-double.md diff --git a/.changeset/strange-avocados-double.md b/.changeset/strange-avocados-double.md new file mode 100644 index 000000000000..e6379955dab5 --- /dev/null +++ b/.changeset/strange-avocados-double.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Add `astro docs` command which opens the Astro docs in your preferred browser. From c5408cd8a408a26d1b9dcc309cd469de4521ff91 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Wed, 6 Apr 2022 11:58:31 -0500 Subject: [PATCH 11/11] chore: rename browser to open --- packages/astro/src/cli/index.ts | 2 +- packages/astro/src/cli/{browser.ts => open.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename packages/astro/src/cli/{browser.ts => open.ts} (100%) diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts index d893a81465e3..5d6612b43404 100644 --- a/packages/astro/src/cli/index.ts +++ b/packages/astro/src/cli/index.ts @@ -12,7 +12,7 @@ import add from '../core/add/index.js'; import devServer from '../core/dev/index.js'; import preview from '../core/preview/index.js'; import { check } from './check.js'; -import { openInBrowser } from './browser.js'; +import { openInBrowser } from './open.js'; import { loadConfig } from '../core/config.js'; import { printHelp, formatErrorMessage, formatConfigErrorMessage } from '../core/messages.js'; import { createSafeError } from '../core/util.js'; diff --git a/packages/astro/src/cli/browser.ts b/packages/astro/src/cli/open.ts similarity index 100% rename from packages/astro/src/cli/browser.ts rename to packages/astro/src/cli/open.ts