Skip to content

Commit

Permalink
feat: github-actions plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
hongaar committed Nov 24, 2022
1 parent cdb4dac commit 4c3f06a
Show file tree
Hide file tree
Showing 19 changed files with 270 additions and 67 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: ci

on:
push:
branches:
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- node-version: 14
node-lts: true
- node-version: 16
node-lts: true
- node-version: 18
node-lts: true
- node-version: 19
node-lts: false
continue-on-error: ${{ ! matrix.node-lts }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: yarn
- run: yarn install
- run: yarn build
- run: yarn test
19 changes: 19 additions & 0 deletions .github/workflows/format-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: format-check

on:
push:
branches:
- main

jobs:
release:
name: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: lts/*
cache: yarn
- run: yarn install
- run: yarn format-check
32 changes: 0 additions & 32 deletions .github/workflows/test.yml

This file was deleted.

1 change: 0 additions & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
workspaces = true
workspaces-update = false

2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
.yarn

# artifacts
lib/
dist/
types/
60 changes: 49 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ yarn moker add --template cra client
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [Getting started](#getting-started)
- [Getting started](#getting-started)
- [Prerequisites](#prerequisites)
- [Create monorepo](#create-monorepo)
- [Use plugins](#use-plugins)
Expand All @@ -49,7 +49,7 @@ yarn moker add --template cra client
- [`lint-staged` _monorepo_](#lint-staged-_monorepo_)
- [`prettier` _monorepo_](#prettier-_monorepo_)
- [`jest` _workspace_](#jest-_workspace_)
- [`semantic-release` _workspace_](#semantic-release-_workspace_)
- [`semantic-release` _monorepo_](#semantic-release-_monorepo_)
- [`typescript` _workspace_](#typescript-_workspace_)
- [Available templates](#available-templates)
- [`common` _monorepo_](#common-_monorepo_)
Expand All @@ -65,7 +65,7 @@ yarn moker add --template cra client

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Getting started
# Getting started

## Prerequisites

Expand Down Expand Up @@ -161,10 +161,21 @@ monorepo level.

## `github-actions` _monorepo_

**🚧 This plugin is a work in progress**
This plugin creates a simple `ci.yml`
[GitHub Actions](https://github.com/features/actions) workflow.

This plugin sets up [GitHub Actions](https://github.com/features/actions) at the
monorepo level.
If you have the `prettier` plugin installed, this will also setup a `lint.yml`
workflow.

If you have the `semantic-release` plugin installed, this will also setup a
`release.yml` workflow. This workflow needs these secrets to be added to your
repository:

- `GH_TOKEN`: a GitHub token with read/write access to your repository
- `NPM_TOKEN`: an NPM token with publish access to your packages

> 🤓 The workflows will use the `main` branch by default, but it is trivial to
> change this.
## `husky` _monorepo_

Expand Down Expand Up @@ -201,14 +212,41 @@ This plugin sets up [Prettier](https://prettier.io).
This plugin sets up [Jest](https://jestjs.io) and adds a `test` and `watch:test`
script to both the workspace and the monorepo.
## `semantic-release` _workspace_
## `semantic-release` _monorepo_
This plugin sets up
[semantic-release](https://semantic-release.gitbook.io/semantic-release/).
[semantic-release](https://semantic-release.gitbook.io/semantic-release/). It
uses a workaround so that it can be used in a monorepo, which is to set up a
`.npmrc` file containing:
```ini
workspaces = true
workspaces-update = false
```
This causes both `npm version` and `npm publish` to be run for each monorepo in
the `semantic-release` context.
Please note that the root repository is not published. Furthermore, make sure
that the root `package.json` doesn't contain:
```json
"private": true
```
Otherwise, the `semantic-release` process will skip the `publish` step.
> 🤓 The release configuration will use the `main` branch by default, but it is
> trivial to change this.
> ⚠️ The semantic-release plugin in our monorepo configuration is currently
> broken due to
> [this issue with their npm plugin](https://github.com/semantic-release/npm/pull/529)
> broken due to an issue with their npm plugin (see [semantic-release/npm#529])
> [this issue with their npm plugin]. Take a look at [patch-semantic-commit.js]
> in this repository for a workaround.
[semantic-release/npm#529]: https://github.com/semantic-release/npm/pull/529
[patch-semantic-commit.js]:
https://github.com/hongaar/moker/blob/main/scripts/patch-semantic-commit.js
## `typescript` _workspace_
Expand Down Expand Up @@ -257,7 +295,6 @@ Contributions are very welcome!
- [ ] github-actions plugin
- [ ] devcontainer plugin
- [ ] semantic-release plugin
- [ ] leasot (todos) plugin
- [ ] doctoc plugin
- [ ] Add LICENSE file to monorepo
Expand All @@ -266,6 +303,7 @@ Contributions are very welcome!
- [ ] Adapt for non-monorepo use-cases (?)
- [ ] Blog post / tutorial
- [ ] Docs for writing custom plugins / templates
- [x] semantic-release plugin
- [x] Port templates
- [x] Support for BYO plugins/templates
- [x] Remove plugins
Expand Down
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"plugins": [
"husky",
"lint-staged",
"prettier"
"prettier",
"semantic-release",
"github-actions"
]
},
"packageManager": "[email protected]",
Expand All @@ -25,19 +27,21 @@
"watch:test": "yarn workspaces foreach --parallel --interlaced run watch:test",
"postinstall": "husky install && node scripts/postinstall.js",
"format": "prettier --write --ignore-unknown .",
"format-check": "prettier --check --ignore-unknown .",
"doctoc": "doctoc README.md",
"leasot": "leasot --exit-nicely --reporter markdown --ignore \"**/node_modules\" \"**/*.ts\" > TODO.md"
"leasot": "leasot --exit-nicely --reporter markdown --ignore \"**/node_modules\" \"**/*.ts\" > TODO.md",
"release": "semantic-release"
},
"devDependencies": {
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/git": "^10.0.1",
"@semantic-release/changelog": "6.0.1",
"@semantic-release/git": "10.0.1",
"@types/prettier": "^2",
"doctoc": "^2.2.1",
"husky": "8.0.2",
"leasot": "^13.2.0",
"lint-staged": "13.0.3",
"prettier": "2.7.1",
"semantic-release": "^19.0.5"
"semantic-release": "19.0.5"
},
"lint-staged": {
"*": "prettier --write --ignore-unknown"
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export async function writeFile({
return fs.promises.writeFile(path, `${contents.trim()}${os.EOL}`, "utf8");
}

export async function copyFile({ from, to }: { from: string; to: string }) {
return fs.promises.copyFile(from, to);
}

export async function removeFile({ path }: { path: string }) {
return fs.promises.rm(path);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/monorepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export async function createMonorepo({
plugins: [],
},
scripts: {
publish: "yarn publish",
build: "echo 'not implemented'",
test: "echo 'not implemented'",
},
},
});
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export async function addWorkspace({
},
}
: {}),
moker: {
plugins: [],
},
},
});

Expand Down
3 changes: 2 additions & 1 deletion packages/plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"types": "types/index.d.ts",
"files": [
"dist",
"types"
"types",
"static"
],
"scripts": {
"prepublishOnly": "yarn build",
Expand Down
64 changes: 56 additions & 8 deletions packages/plugins/src/githubActions/githubActions.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,63 @@
import { PluginType } from "@mokr/core";
import {
copyFile,
createDirectory,
hasPlugin,
PluginArgs,
PluginType,
removeDirectory,
} from "@mokr/core";
import { join } from "node:path";
import { URL } from "node:url";

async function install() {
// jest -> test workflow
// semanticRelease -> release workflow
// prettier -> lint workflow
// typescript -> build workflow
const WORKFLOWS_DIRECTORY = ".github/workflows";

async function install({ directory }: PluginArgs) {
const workflowDirectory = join(directory, WORKFLOWS_DIRECTORY);

await createDirectory({ directory: workflowDirectory });

// Workspaces

// Since there's currently no way to detect plugins in workspaces, we always
// install the build and test workflows.

// We could build plugin detection in workspaces, but then we would need to
// load all plugins at the monorepo level when we modify plugins at the
// workspace level

await copyFile({
from: new URL("../../static/ci.yml", import.meta.url).pathname,
to: join(workflowDirectory, "ci.yml"),
});
}

async function remove() {}
async function remove({ directory }: PluginArgs) {
const workflowDirectory = join(directory, WORKFLOWS_DIRECTORY);

async function load() {}
await removeDirectory({ directory: workflowDirectory });
}

async function load({ directory }: PluginArgs) {
const workflowDirectory = join(directory, WORKFLOWS_DIRECTORY);

await createDirectory({ directory: workflowDirectory });

// Monorepo plugins

if (await hasPlugin({ directory, name: "semanticRelease" })) {
await copyFile({
from: new URL("../../static/release.yml", import.meta.url).pathname,
to: join(workflowDirectory, "release.yml"),
});
}

if (await hasPlugin({ directory, name: "prettier" })) {
await copyFile({
from: new URL("../../static/format-check.yml", import.meta.url).pathname,
to: join(workflowDirectory, "format-check.yml"),
});
}
}

export const githubActions = {
type: PluginType.Monorepo,
Expand Down
1 change: 1 addition & 0 deletions packages/plugins/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./githubActions/githubActions.js";
export * from "./husky/husky.js";
export * from "./jest/jest.js";
export * from "./lintStaged/lintStaged.js";
Expand Down
3 changes: 2 additions & 1 deletion packages/plugins/src/prettier/prettier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const PRETTIER_IGNORE = [
".yarn",
"",
"# artifacts",
"lib/",
"dist/",
"types/",
"",
];
Expand All @@ -41,6 +41,7 @@ async function install({ directory }: PluginArgs) {
data: {
scripts: {
format: "prettier --write --ignore-unknown .",
"format-check": "prettier --check --ignore-unknown .",
},
},
});
Expand Down
Loading

0 comments on commit 4c3f06a

Please sign in to comment.