Skip to content

Commit

Permalink
feat: github action template (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
hongaar authored Dec 12, 2022
1 parent 55c58c3 commit 67345da
Show file tree
Hide file tree
Showing 41 changed files with 909 additions and 167 deletions.
1 change: 0 additions & 1 deletion .github/workflows/format-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ on:

jobs:
format-check:
name: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ on:

jobs:
release:
name: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
132 changes: 97 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ single-purpose repos, monorepos, monorepo workspaces and common tooling:**

```bash
# initialize a monorepo
yarn dlx moker create --monorepo my-repo
cd my-repo
yarn dlx moker create --monorepo my-monorepo
cd my-monorepo

# install common tools
yarn moker use prettier husky lint-staged doctoc semantic-release
yarn moker use prettier doctoc semantic-release

# create workspaces
yarn moker add --template express server
yarn moker add --template cra client
yarn moker add --template lib shared
yarn moker add --template bandersnatch cli
```

## Features
Expand All @@ -36,13 +38,18 @@ yarn moker add --template cra client

- [Getting started](#getting-started)
- [Prerequisites](#prerequisites)
- [Create monorepo](#create-monorepo)
- [Use plugins](#use-plugins)
- [Add workspace](#add-workspace)
- [Usage](#usage)
- [Creating a single-purpose repo](#creating-a-single-purpose-repo)
- [Creating a monorepo](#creating-a-monorepo)
- [Creating a monorepo workspace](#creating-a-monorepo-workspace)
- [Using plugins](#using-plugins)
- [Using templates](#using-templates)
- [Using plugins and templates together](#using-plugins-and-templates-together)
- [Available plugins](#available-plugins)
- [`dependabot` _repo_](#dependabot-_repo_)
- [`devcontainer` _repo_](#devcontainer-_repo_)
- [`doctoc` _repo_](#doctoc-_repo_)
- [`esbuild` _repo or workspace_](#esbuild-_repo-or-workspace_)
- [`github-actions` _repo_](#github-actions-_repo_)
- [`husky` _repo_](#husky-_repo_)
- [`jest` _repo or workspace_](#jest-_repo-or-workspace_)
Expand All @@ -56,6 +63,7 @@ yarn moker add --template cra client
- [`common` _repo_](#common-_repo_)
- [`cra` _repo or workspace_](#cra-_repo-or-workspace_)
- [`express` _repo or workspace_](#express-_repo-or-workspace_)
- [`github-actions` _repo_](#github-actions-_repo_-1)
- [`lib` _repo or workspace_](#lib-_repo-or-workspace_)
- [Commands](#commands)
- [Contributing](#contributing)
Expand All @@ -80,69 +88,108 @@ You will need Node v14+ and Yarn v2+ in order to use `moker`.
corepack prepare yarn@stable --activate
```

## Create monorepo
## Usage

Create a new monorepo:
`moker` is distributed as a NPM package, and can be run with `yarn dlx`:

```bash
yarn dlx moker <command>
```

> **Note**: Note that when we use `yarn dlx moker` to create a new repo, `moker`
> is added as a dependency to your new repo, so we can simply use `yarn moker`
> to execute commands from within the repo directory.
## Creating a single-purpose repo

Create a new repo:

```bash
yarn dlx moker create --monorepo my-repo
yarn dlx moker create my-repo
```

This will initialize a new monorepo in the `my-repo` directory.
This will initialize a new repo in the `my-repo` directory.

> **Note**: Note that we use `yarn dlx moker` to create a new monorepo. Once we
> are inside our monorepo, we can simply use `yarn moker` to execute commands.
## Creating a monorepo

Create a new monorepo:

```bash
yarn dlx moker create --monorepo my-monorepo
```

This will initialize a new monorepo in the `my-monorepo` directory.

> 🤓 _Default_: The monorepo is initiated with Yarn without Zero-Installs and in
> legacy `nodeLinker: node-modules` mode because a lot of packages are not yet
> compatible with PnP or require a workaround.
## Use plugins
## Creating a monorepo workspace

To add a new workspace (a.k.a. monorepo package) to your monorepo, run this from
within the monorepo directory:

```bash
yarn moker add my-workspace
```

Workspaces are added in a customizable subdirectory of the monorepo (the default
is `packages`).

## Using plugins

Of course you want additional tools installed. You can add plugins with:

```bash
yarn moker use prettier
```

Of course you want additional tools installed at the monorepo level, add them
with:
You can install multiple plugins at once:

```bash
cd my-repo
yarn moker use prettier husky lint-staged
yarn moker use prettier lint-staged husky
```

Plugins may work together. For example, `lint-staged` will install a pre-commit
hook which formats code if `prettier` and `husky` are installed. The order in
which plugins are added does not matter.

See the section [available plugins](#available-plugins) for a list of options.
> **Note**: Some plugins only work at the repo or workspace level, `moker` will
> warn you if you try to add a plugin at the wrong level.
> **Note**: To quickly get started with the most common plugins, use a monorepo
> template like so:
>
> ```bash
> yarn dlx moker create --template common my-repo
> ```
See the section [available plugins](#available-plugins) for a list of options.

## Add workspace
## Using templates

To add a new workspace (a.k.a. monorepo package) to your monorepo, use:
You can templates when creating repos:

```bash
yarn moker add my-workspace
yarn dlx moker create --template common my-repo
```

Workspaces are added in a customizable subdirectory of the monorepo (the default
is `packages`).

You can also use a workspace template, e.g.:
You can also use templates when creating workspaces:

```bash
yarn moker add --template lib shared
yarn moker add --template express server
yarn moker add --template cra client
yarn moker add --template bandersnatch cli
```

You can install multiple templates at once:

```bash
yarn dlx moker create --template common github-action my-action
```

See the section [available templates](#available-templates) for a list of
options.

## Using plugins and templates together

Plugins and templates can be used together, for example:

```bash
yarn dlx moker create --template express --use prettier my-repo
```

# Available plugins

## `dependabot` _repo_
Expand Down Expand Up @@ -173,6 +220,15 @@ This plugin adds a script to generate a table of contents for the README using

If you have the `husky` plugin installed, it will also add a pre-commit hook.

## `esbuild` _repo or workspace_

This plugin sets up [esbuild](https://esbuild.github.io) and adds a `build` and
`build:watch` script to the repo or both the workspace and the monorepo.

> 🤓 _Default_: If you have the `typescript` plugin installed as well, we'll
> assume that you want to build to a bundle instead of transpiled TypeScript. We
> will still use `tsc` for type checking.
## `github-actions` _repo_

This plugin creates a simple `ci.yml`
Expand Down Expand Up @@ -283,6 +339,12 @@ app (web client).
Scaffolds a simple [express](https://expressjs.com) HTTP app with the
[typescript](#typescript-workspace) and [jest](#jest-workspace) plugins.
## `github-actions` _repo_
Scaffolds a
[custom GitHub Action](https://docs.github.com/en/actions/creating-actions/about-custom-actions)
template.
## `lib` _repo or workspace_
A plain shared library template with the [typescript](#typescript-workspace) and
Expand All @@ -298,11 +360,11 @@ Contributions are very welcome!
## Roadmap
- [ ] Add LICENSE file to repo
- [ ] Support for `swc`/`esbuild`
- [ ] A compat lib (which builds cjs and mjs targets)
- [ ] Blog post / tutorial
- [ ] Docs for writing custom plugins / templates
- [x] Add LICENSE file to repo
- [x] Adapt for non-monorepo use-cases
- [x] github-actions plugin
- [x] devcontainer plugin
Expand Down
9 changes: 5 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### TODOs

| Filename | line # | TODO |
| :------------------------------------------------------------------------- | :----: | :--------------------------- |
| [packages/core/src/yarnrc.ts](packages/core/src/yarnrc.ts#L23) | 23 | etc, fix later |
| [packages/plugins/src/jest/jest.ts](packages/plugins/src/jest/jest.ts#L31) | 31 | install jest without ts-jest |
| Filename | line # | TODO |
| :------------------------------------------------------------------------- | :----: | :--------------------------------- |
| [packages/core/src/license.ts](packages/core/src/license.ts#L43) | 43 | Contents is not perfect yet, e.g.: |
| [packages/core/src/yarnrc.ts](packages/core/src/yarnrc.ts#L23) | 23 | etc, fix later |
| [packages/plugins/src/jest/jest.ts](packages/plugins/src/jest/jest.ts#L31) | 31 | install jest without ts-jest |
9 changes: 7 additions & 2 deletions packages/cli/moker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
import { writeError } from "@mokr/core";
import cli from "./dist/cli.js";

cli.runOrRepl().catch((err) => {
function errorHandler(err) {
if (process.env["DEBUG"]) {
console.debug(err);
} else {
writeError(`\n${String(err)}`);
}

process.exit(1);
});
}

cli.runOrRepl().catch(errorHandler);

// Compat layer for Node < 16
process.on("unhandledRejection", errorHandler);
2 changes: 1 addition & 1 deletion packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ Object.values(commands).forEach((command) => cli.add(command));

// Some assertions we always need
assertYarnVersion(3);
assertNodeVersion(16);
assertNodeVersion(14);

export default cli;
9 changes: 5 additions & 4 deletions packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export const add = command("add")
})
.option("template", {
description: "Use workspace template",
type: "string",
type: "array",
default: [] as string[],
})
.option("plugin", {
description: "Kick-start with this plugin",
Expand All @@ -41,9 +42,9 @@ export const add = command("add")
() => addWorkspace({ directory, name: workspaceName })
);

if (template) {
await task(`Apply template ${template}`, () =>
applyTemplate({ directory: workspaceDirectory, name: template })
for (const name of template) {
await task(`Apply template ${name}`, () =>
applyTemplate({ directory: workspaceDirectory, name })
);
}

Expand Down
Loading

0 comments on commit 67345da

Please sign in to comment.