Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Commit

Permalink
feat(create): add --workspace-install flag
Browse files Browse the repository at this point in the history
  • Loading branch information
pmbanugo committed Feb 13, 2022
1 parent e07616b commit 320d168
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 34 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Serverless functions allow developers to quickly implement and deploy functional
## Install

```bash
$ npm install -g kazi
npm i -g @kazi-faas/cli
```

## Pre-requisite
Expand All @@ -26,21 +26,26 @@ $ npm install -g kazi
2. pack CLI
3. Node 16.3.x (some commands may not run smoothly in versions below 16)

## Getting Started

> TODO: A short guide that shows the concept so users can get started on the topic in under 10 minutes.
## CLI

```
Commands
$ kazi create: Scaffold a new project
$ kazi list: List the functions deployed using kazi
$ kazi deploy: Deploy a function (only works if you're inside the function's directory)
$ kazi --help: Show help text
$ kazi --help: Show help text
Usage
$ kazi create <Function_Name>
Options
--registry (-r), Your registry namespace
--use-yarn, Use Yarn to install dependencies
--use-yarn, Use Yarn to install dependencies (default: false)
--workspace-install, Used to install the dependencies in a workspace i.e using the workspace's node_modules(default: false).
Examples
$ kazi create hello --registry=docker.io/pmbanugo
Expand Down
4 changes: 4 additions & 0 deletions source/cli.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const cli = meow(helpText, {
type: "boolean",
default: false,
},
workspaceInstall: {
type: "boolean",
default: false,
},
},
});

Expand Down
50 changes: 24 additions & 26 deletions source/commands/create-project.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import degit from "degit";
import { install as pkgInstall } from "pkg-install";
import { projectInstall } from "pkg-install";
import { join } from "path";
import { writeFile } from "fs/promises";

function stripPath(name: string) {
//removes directory name if it's included.
return name.split("/").pop();
}

export async function createConfigFile(name: string, registry: string) {
const config = {
name,
name: stripPath(name),
registry,
};
await writeFile(
Expand All @@ -14,16 +19,22 @@ export async function createConfigFile(name: string, registry: string) {
);
}

async function createPkgJson(name: string) {
export async function createPkgJson(name: string) {
const pkg = {
name,
name: stripPath(name),
description: "A function which responds to HTTP requests",
main: "index.js",
scripts: {
start: "micro",
dev: "micro-dev",
deploy: "kazi deploy",
},
dependencies: {
micro: "^9.3.4",
},
devDependencies: {
"micro-dev": "^3.0.0",
},
};

await writeFile(
Expand All @@ -46,29 +57,16 @@ export const clone = async (name: string) => {
await emitter.clone(`./${name}`);
};

export const install = async (name: string, useYarn: boolean = false) => {
export const install = async (
name: string,
useYarn: boolean,
workspaceInstall: boolean
) => {
const pkgManager = useYarn ? "yarn" : "npm";

await createPkgJson(name);
const dir = join(process.cwd(), name);

await pkgInstall(
{
micro: "^9.3.4",
},
{
cwd: dir,
prefer: pkgManager,
}
);
await pkgInstall(
{
"micro-dev": "^3.0.0",
},
{
cwd: dir,
prefer: pkgManager,
dev: true,
}
);
await projectInstall({
cwd: workspaceInstall ? undefined : dir,
prefer: pkgManager,
});
};
13 changes: 10 additions & 3 deletions source/components/Create.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React, { FC, useEffect, useState } from "react";
import { TaskList, Task } from "ink-task-list";
import { useInput, Text } from "ink";
import { clone, install, createConfigFile } from "../commands/create-project";
import {
clone,
install,
createConfigFile,
createPkgJson,
} from "../commands/create-project";

type State = "pending" | "loading" | "success" | "warning" | "error";
type List = Record<"install" | "create", { label: string; state: State }>;
Expand Down Expand Up @@ -37,7 +42,8 @@ const Create: FC<{
input: string[];
registryFlag?: string;
useYarn?: boolean;
}> = ({ input, registryFlag, useYarn }) => {
workspaceInstall?: boolean;
}> = ({ input, registryFlag, useYarn, workspaceInstall }) => {
const [tasks, setTasks] = useState<List>({
create: { label: "Create the project", state: "loading" },
install: { label: "Install project's dependencies", state: "pending" },
Expand All @@ -51,13 +57,14 @@ const Create: FC<{
if (name && registry && tasks.create.state === "loading") {
await clone(name);
await createConfigFile(name, registry);
await createPkgJson(name);

setTasks((state) => ({
create: { ...state.create, state: "success" },
install: { ...state.install, state: "loading" },
}));

await install(name, useYarn);
await install(name, Boolean(useYarn), Boolean(workspaceInstall));
setTasks((state) => ({
...state,
install: { ...state.install, state: "success" },
Expand Down
1 change: 0 additions & 1 deletion source/components/Deploy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ const Deploy: FC = () => {
deploy: { ...state.deploy, state: "success" },
}));
} catch (error) {
console.log({ error });
setTasks((state) => ({
...state,
deploy: { ...state.deploy, state: "error" },
Expand Down
3 changes: 2 additions & 1 deletion source/meow-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Usage
Options
--registry (-r), Your registry namespace
--use-yarn, Use Yarn to install dependencies
--use-yarn, Use Yarn to install dependencies (default: false)
--workspace-install, Used to install the dependencies in a workspace i.e using the workspace's node_modules(default: false).
Examples
$ kazi create hello --registry=docker.io/pmbanugo
Expand Down
4 changes: 4 additions & 0 deletions source/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const App: FC<{
useYarn: {
type: "boolean";
};
workspaceInstall: {
type: "boolean";
};
}>;
}> = ({ input, flags }) => {
if (input.length === 2 && input[0] === "create") {
Expand All @@ -24,6 +27,7 @@ const App: FC<{
input={input}
registryFlag={flags?.registry}
useYarn={flags?.useYarn}
workspaceInstall={flags?.workspaceInstall}
/>
);
}
Expand Down

0 comments on commit 320d168

Please sign in to comment.