Skip to content

Commit

Permalink
refactor consolidating options
Browse files Browse the repository at this point in the history
  • Loading branch information
coffeephile committed Feb 17, 2025
1 parent 579d7e4 commit 5d93a69
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/app/commands/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 61 additions & 0 deletions src/init/index.test.ts
Original file line number Diff line number Diff line change
@@ -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')`,
},
]);
});
});
30 changes: 18 additions & 12 deletions src/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import inquirer from "inquirer";

const DEFAULT_ENDPOINT_URL = "https://localhost:9000";

const addAction: (options: CreateAppOptions) => Promise<void> = async (
options,
) => {
// collect required information via prompt if not specified via parameter
async function makeOptions(options: CreateAppOptions) {
const promptItems = [];

if (!options.channelId) {
Expand Down Expand Up @@ -45,8 +42,8 @@ const addAction: (options: CreateAppOptions) => Promise<void> = 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;
Expand All @@ -57,25 +54,34 @@ const addAction: (options: CreateAppOptions) => Promise<void> = async (
? promptInputs.endpointUrl
: DEFAULT_ENDPOINT_URL;

return options
}

export const initAction: (options: CreateAppOptions) => Promise<void> = 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",
});

// 4. print instructions on how to run locally
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.}/
`);
Expand All @@ -98,5 +104,5 @@ export const installInitCommands = (program: Command) => {
"-e, --endpoint-url <endpointUrl>",
"The endpoint URL of the LIFF app. Must be 'https://'",
)
.action(addAction);
.action(initAction);
};

0 comments on commit 5d93a69

Please sign in to comment.