Skip to content

Commit

Permalink
feat: devcontainer plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
hongaar committed Nov 24, 2022
1 parent 743784a commit f76b37f
Show file tree
Hide file tree
Showing 21 changed files with 768 additions and 54 deletions.
17 changes: 3 additions & 14 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
{
"name": "Node.js & TypeScript",
"image": "mcr.microsoft.com/devcontainers/typescript-node:16-bullseye",

// Features to add to the dev container. More info: https://containers.dev/implementors/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "yarn install",

// Configure tool-specific properties.
"features": {},
"forwardPorts": [],
"postCreateCommand": "yarn install",
"customizations": {
"vscode": {
"extensions": ["esbenp.prettier-vscode"]
}
}

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
pull_request:

jobs:
test:
ci:
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/format-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- main

jobs:
release:
format-check:
name: release
runs-on: ubuntu-latest
steps:
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# moker

[![npm](https://img.shields.io/npm/v/moker)](https://www.npmjs.com/package/moker)
# moker [![npm](https://img.shields.io/npm/v/moker)](https://www.npmjs.com/package/moker)

**No more struggles setting up monorepo tooling. Kick-start monorepos and
workspaces fast:**
Expand Down Expand Up @@ -263,6 +261,7 @@ plugins in the monorepo:
- `prettier`
- `husky`
- `lint-staged`
- `semantic-release`
- `github-actions`
- `devcontainer`
Expand Down Expand Up @@ -293,8 +292,8 @@ Contributions are very welcome!
## Roadmap
- [ ] github-actions plugin
- [ ] devcontainer plugin
- [x] github-actions plugin
- [x] devcontainer plugin
- [ ] leasot (todos) plugin
- [ ] doctoc plugin
- [ ] Add LICENSE file to monorepo
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"lint-staged",
"prettier",
"semantic-release",
"github-actions"
"github-actions",
"devcontainer"
]
},
"packageManager": "[email protected]",
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export async function removeDirectory({ directory }: DirOption) {
return fs.promises.rm(directory, { recursive: true });
}

/**
* Recursively create directory.
*/
export async function createDirectory({ directory }: DirOption) {
return fs.promises.mkdir(directory, { recursive: true });
}
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/file.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import fs from "node:fs";
import os from "node:os";
import { dirname } from "node:path";
import { createDirectory } from "./directory.js";

export async function readFile({ path }: { path: string }) {
return fs.promises.readFile(path, "utf8");
}

/**
* Write text file with UTF-8 encoding. The destination directory is created
* first recursively.
*/
export async function writeFile({
path,
contents,
}: {
path: string;
contents: string;
}) {
await createDirectory({ directory: dirname(path) });

return fs.promises.writeFile(path, `${contents.trim()}${os.EOL}`, "utf8");
}

export async function copyFile({ from, to }: { from: string; to: string }) {
await createDirectory({ directory: dirname(to) });

return fs.promises.copyFile(from, to);
}

Expand Down
7 changes: 1 addition & 6 deletions packages/core/src/monorepo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { join } from "node:path";
import {
createDirectory,
isReadableAndWritableDirectory,
} from "./directory.js";
import { isReadableAndWritableDirectory } from "./directory.js";
import { hasPackage, Package, readPackage, writePackage } from "./package.js";
import { enqueueInstallDependency, initYarn } from "./yarn.js";

Expand Down Expand Up @@ -41,8 +38,6 @@ export async function createMonorepo({
throw new Error(`${directory} already exists`);
}

await createDirectory({ directory });

await initYarn({ directory });

await writePackage({
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const CORE_PLUGINS = [
"typescript",
"jest",
"semantic-release",
"dependabot",
];

export function isPlugin(plugin: unknown): plugin is Plugin {
Expand Down
7 changes: 1 addition & 6 deletions packages/core/src/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { dirname, join } from "node:path";
import { pkgUp } from "pkg-up";
import {
createDirectory,
isReadableAndWritableDirectory,
} from "./directory.js";
import { isReadableAndWritableDirectory } from "./directory.js";
import { writeFile } from "./file.js";
import { getScoped, getWorkspacesDirectory } from "./monorepo.js";
import { readPackage, writePackage } from "./package.js";
Expand Down Expand Up @@ -31,8 +28,6 @@ export async function addWorkspace({
const isScoped = getScoped({ pkg });
const packageName = isScoped ? `@${pkg.name}/${name}` : name;

await createDirectory({ directory: workspaceDirectory });

await writePackage({
directory: workspaceDirectory,
append: false,
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ const queues = {

export async function initYarn({ directory }: DirOption) {
await createDirectory({ directory });

await exec("yarn", ["init", "-2"], { cwd: directory });

await writeYarnrc({ directory, data: { nodeLinker: "node-modules" } });

await writeGitignore({ directory, lines: GITIGNORE_LINES, append: false });

await addYarnPlugin({ directory, name: "interactive-tools" });

await addYarnPlugin({ directory, name: "workspace-tools" });

await addYarnPlugin({ directory, name: "version" });
}

Expand Down
45 changes: 45 additions & 0 deletions packages/plugins/src/devcontainer/devcontainer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { hasPlugin, PluginArgs, PluginType } from "@mokr/core";
import {
removeDevcontainerJson,
writeDevcontainerJson,
} from "./devcontainerJson.js";

async function install({ directory }: PluginArgs) {
await writeDevcontainerJson({
directory,
data: {
name: "Node.js & TypeScript",
image: "mcr.microsoft.com/devcontainers/typescript-node:16-bullseye",
features: {},
forwardPorts: [],
postCreateCommand: "yarn install",
customizations: {},
},
});
}

async function remove({ directory }: PluginArgs) {
await removeDevcontainerJson({ directory });
}

async function load({ directory }: PluginArgs) {
if (await hasPlugin({ directory, name: "prettier" })) {
await writeDevcontainerJson({
directory,
data: {
customizations: {
vscode: {
extensions: ["esbenp.prettier-vscode"],
},
},
},
});
}
}

export const devcontainer = {
type: PluginType.Monorepo,
install,
remove,
load,
};
Loading

0 comments on commit f76b37f

Please sign in to comment.