diff --git a/src/app/commands/delete.test.ts b/src/app/commands/delete.test.ts index 08ebc66..ca67c40 100644 --- a/src/app/commands/delete.test.ts +++ b/src/app/commands/delete.test.ts @@ -99,7 +99,7 @@ describe("makeDeleteCommand", () => { expect(mockDeleteApp).not.toHaveBeenCalled(); }); - it("should not delete a LIFF app if user cancels the deletio", async () => { + it("should not delete a LIFF app if user cancels the deletion", async () => { vi.mocked(resolveChannel).mockResolvedValueOnce({ accessToken: "token", expiresIn: 3600, diff --git a/src/init/index.test.ts b/src/init/index.test.ts new file mode 100644 index 0000000..5c5330d --- /dev/null +++ b/src/init/index.test.ts @@ -0,0 +1,61 @@ +import { + afterEach, + describe, + expect, + it, + vi, +} from "vitest"; + +import inquire from "inquirer"; + +import { initAction } from "./index.js"; + +vi.mock("../channel/commands/add.js"); +vi.mock("../app/commands/create.js"); +vi.mock("child_process"); + +vi.mock("inquirer"); + +describe("initAction", () => { + afterEach(() => { + vi.restoreAllMocks(); + }); + + it("should prompt on no parameters given", async () => { + const promptInputs = { + channelId: "123", + }; + vi.mocked(inquire.prompt).mockResolvedValue(promptInputs); + + await initAction({ + name: '', + viewType: '', + endpointUrl: '', + }); + + expect(inquire.prompt).toHaveBeenCalled(); + expect(inquire.prompt).toHaveBeenCalledWith([ + { + type: "input", + name: "channelId", + message: "Channel ID?", + }, + { + type: "input", + name: "name", + message: "App name?", + }, + { + type: "list", + name: "viewType", + message: "View type?", + choices: ["compact", "tall", "full"], + }, + { + type: "input", + name: "endpointUrl", + message: `Endpoint URL? (leave empty for default 'https://localhost:9000')`, + }, + ]); + }); +}); \ No newline at end of file diff --git a/src/init/index.ts b/src/init/index.ts index 69a3a7d..183dcb5 100644 --- a/src/init/index.ts +++ b/src/init/index.ts @@ -6,10 +6,7 @@ import inquirer from "inquirer"; const DEFAULT_ENDPOINT_URL = "https://localhost:9000"; -const addAction: (options: CreateAppOptions) => Promise = async ( - options, -) => { - // collect required information via prompt if not specified via parameter +async function makeOptions(options: CreateAppOptions) { const promptItems = []; if (!options.channelId) { @@ -45,8 +42,8 @@ const addAction: (options: CreateAppOptions) => Promise = async ( }); } - const promptInputs = await inquirer.prompt<{ [key: string]: string }>( - promptItems, + const promptInputs = await inquirer.prompt<{ [key: string]: string; }>( + promptItems ); options.channelId = promptInputs.channelId ?? options.channelId; @@ -57,14 +54,23 @@ const addAction: (options: CreateAppOptions) => Promise = async ( ? promptInputs.endpointUrl : DEFAULT_ENDPOINT_URL; + return options +} + +export const initAction: (options: CreateAppOptions) => Promise = async ( + options, +) => { + // collect required information via prompt if not specified via parameter + const consolidatedOptions = await makeOptions(options); + // 1. add channel - await addChannelAction(options.channelId); + await addChannelAction(consolidatedOptions.channelId); // 2. create liff app (@ server) - const liffId = await createLiffApp(options); + const liffId = await createLiffApp(consolidatedOptions); // 3. create liff app (@ client) - execSync(`npx @line/create-liff-app ${options.name} -l ${liffId}`, { + execSync(`npx @line/create-liff-app ${consolidatedOptions.name} -l ${liffId}`, { stdio: "inherit", }); @@ -72,10 +78,10 @@ const addAction: (options: CreateAppOptions) => Promise = async ( console.info(`App ${liffId} successfully created. Now do the following: - 1. go to app directory: \`cd ${options.name}\` + 1. go to app directory: \`cd ${consolidatedOptions.name}\` 2. create certificate key files (e.g. \`mkcert localhost\`, see: https://developers.line.biz/en/docs/liff/liff-cli/#serve-operating-conditions ) 3. run LIFF app template using command above (e.g. \`npm run dev\` or \`yarn dev\`) - 4. open new terminal window, navigate to \`${options.name}\` directory + 4. open new terminal window, navigate to \`${consolidatedOptions.name}\` directory 5. run \`liff-cli serve -l ${liffId} -u http://localhost:\${PORT FROM STEP 3.}/\` 6. open browser and navigate to http://localhost:\${PORT FROM STEP 3.}/ `); @@ -98,5 +104,5 @@ export const installInitCommands = (program: Command) => { "-e, --endpoint-url ", "The endpoint URL of the LIFF app. Must be 'https://'", ) - .action(addAction); + .action(initAction); };