|
1 | 1 | import { bold } from "colorette";
|
2 | 2 |
|
3 |
| -import { logLabeledSuccess } from "../utils"; |
4 | 3 | import { Command } from "../command";
|
5 |
| -import { Site, createSite } from "../hosting/api"; |
6 |
| -import { promptOnce } from "../prompt"; |
7 |
| -import { FirebaseError } from "../error"; |
8 |
| -import { requirePermissions } from "../requirePermissions"; |
9 |
| -import { needProjectId } from "../projectUtils"; |
| 4 | +import { interactiveCreateHostingSite } from "../hosting/interactive"; |
| 5 | +import { last, logLabeledSuccess } from "../utils"; |
10 | 6 | import { logger } from "../logger";
|
| 7 | +import { needProjectId } from "../projectUtils"; |
| 8 | +import { Options } from "../options"; |
| 9 | +import { requirePermissions } from "../requirePermissions"; |
| 10 | +import { Site } from "../hosting/api"; |
| 11 | +import { FirebaseError } from "../error"; |
11 | 12 |
|
12 | 13 | const LOG_TAG = "hosting:sites";
|
13 | 14 |
|
14 | 15 | export const command = new Command("hosting:sites:create [siteId]")
|
15 | 16 | .description("create a Firebase Hosting site")
|
16 | 17 | .option("--app <appId>", "specify an existing Firebase Web App ID")
|
17 | 18 | .before(requirePermissions, ["firebasehosting.sites.update"])
|
18 |
| - .action( |
19 |
| - async ( |
20 |
| - siteId: string, |
21 |
| - options: any // eslint-disable-line @typescript-eslint/no-explicit-any |
22 |
| - ): Promise<Site> => { |
23 |
| - const projectId = needProjectId(options); |
24 |
| - const appId = options.app; |
25 |
| - if (!siteId) { |
26 |
| - if (options.nonInteractive) { |
27 |
| - throw new FirebaseError( |
28 |
| - `"siteId" argument must be provided in a non-interactive environment` |
29 |
| - ); |
30 |
| - } |
31 |
| - siteId = await promptOnce( |
32 |
| - { |
33 |
| - type: "input", |
34 |
| - message: "Please provide an unique, URL-friendly id for the site (<id>.web.app):", |
35 |
| - validate: (s) => s.length > 0, |
36 |
| - } // Prevents an empty string from being submitted! |
37 |
| - ); |
38 |
| - } |
39 |
| - if (!siteId) { |
40 |
| - throw new FirebaseError(`"siteId" must not be empty`); |
41 |
| - } |
| 19 | + .action(async (siteId: string, options: Options & { app: string }): Promise<Site> => { |
| 20 | + const projectId = needProjectId(options); |
| 21 | + const appId = options.app; |
| 22 | + |
| 23 | + if (options.nonInteractive && !siteId) { |
| 24 | + throw new FirebaseError(`${bold(siteId)} is required in a non-interactive environment`); |
| 25 | + } |
42 | 26 |
|
43 |
| - let site: Site; |
44 |
| - try { |
45 |
| - site = await createSite(projectId, siteId, appId); |
46 |
| - } catch (e: any) { |
47 |
| - if (e.status === 409) { |
48 |
| - throw new FirebaseError( |
49 |
| - `Site ${bold(siteId)} already exists in project ${bold(projectId)}.`, |
50 |
| - { original: e } |
51 |
| - ); |
52 |
| - } |
53 |
| - throw e; |
54 |
| - } |
| 27 | + const site = await interactiveCreateHostingSite(siteId, appId, options); |
| 28 | + siteId = last(site.name.split("/")); |
55 | 29 |
|
56 |
| - logger.info(); |
57 |
| - logLabeledSuccess( |
58 |
| - LOG_TAG, |
59 |
| - `Site ${bold(siteId)} has been created in project ${bold(projectId)}.` |
60 |
| - ); |
61 |
| - if (appId) { |
62 |
| - logLabeledSuccess( |
63 |
| - LOG_TAG, |
64 |
| - `Site ${bold(siteId)} has been linked to web app ${bold(appId)}` |
65 |
| - ); |
66 |
| - } |
67 |
| - logLabeledSuccess(LOG_TAG, `Site URL: ${site.defaultUrl}`); |
68 |
| - logger.info(); |
69 |
| - logger.info( |
70 |
| - `To deploy to this site, follow the guide at https://firebase.google.com/docs/hosting/multisites.` |
71 |
| - ); |
72 |
| - return site; |
| 30 | + logger.info(); |
| 31 | + logLabeledSuccess( |
| 32 | + LOG_TAG, |
| 33 | + `Site ${bold(siteId)} has been created in project ${bold(projectId)}.` |
| 34 | + ); |
| 35 | + if (appId) { |
| 36 | + logLabeledSuccess(LOG_TAG, `Site ${bold(siteId)} has been linked to web app ${bold(appId)}`); |
73 | 37 | }
|
74 |
| - ); |
| 38 | + logLabeledSuccess(LOG_TAG, `Site URL: ${site.defaultUrl}`); |
| 39 | + logger.info(); |
| 40 | + logger.info( |
| 41 | + `To deploy to this site, follow the guide at https://firebase.google.com/docs/hosting/multisites.` |
| 42 | + ); |
| 43 | + return site; |
| 44 | + }); |
0 commit comments