Skip to content

Commit

Permalink
feat: reload option for use command
Browse files Browse the repository at this point in the history
  • Loading branch information
hongaar committed Dec 10, 2022
1 parent 22b9733 commit fd7bfa3
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 21 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
| Filename | line # | TODO |
| :------------------------------------------------------------------------- | :----: | :------------------------------------------------------------ |
| [packages/core/src/yarnrc.ts](packages/core/src/yarnrc.ts#L23) | 23 | etc, fix later |
| [packages/cli/src/commands/list.ts](packages/cli/src/commands/list.ts#L15) | 15 | list workspaces using https://yarnpkg.com/cli/workspaces/list |
| [packages/cli/src/commands/list.ts](packages/cli/src/commands/list.ts#L19) | 19 | list workspaces using https://yarnpkg.com/cli/workspaces/list |
| [packages/plugins/src/jest/jest.ts](packages/plugins/src/jest/jest.ts#L31) | 31 | install jest without ts-jest |
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"prepublish": "yarn clean && yarn build && cp ../../README.md .",
"clean": "rm -rf dist && rm -rf types",
"build": "yarn clean && tsc",
"build:watch": "yarn build && tsc --watch"
"build:watch": "tsc --watch"
},
"dependencies": {
"@mokr/core": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { command } from "bandersnatch";

export const add = command("add")
.description("Add a workspace to the monorepo")
.description("Add a workspace to a monorepo")
.argument("name", {
description: "Name of the workspace",
prompt: true,
Expand Down
13 changes: 10 additions & 3 deletions packages/cli/src/commands/list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPlugins } from "@mokr/core";
import { getPlugins, listWorkspaces } from "@mokr/core";
import { command } from "bandersnatch";

export const list = command("list")
Expand All @@ -9,9 +9,16 @@ export const list = command("list")
})
.action(async ({ cwd }) => {
const plugins = await getPlugins({ directory: cwd });
const workspaces = await listWorkspaces({ directory: cwd });

console.log(`Plugins: ${plugins.join(", ")}`);
console.log(
`Plugins:
${plugins.map((plugin) => `- ${plugin}\n`).join("")}`
);

// @todo: list workspaces using https://yarnpkg.com/cli/workspaces/list
console.log(`Workspaces: ...`);
console.log(
`Workspaces:
${workspaces.map(({ location, name }) => `- ${name} (${location})\n`).join("")}`
);
});
2 changes: 1 addition & 1 deletion packages/cli/src/commands/reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { command } from "bandersnatch";

export const reload = command("reload")
.hidden()
.description("Reload plugins in monorepo or workspace")
.description("Reload plugins in repo or workspace")
.option("cwd", {
description: "Directory to use as the current working directory",
default: process.cwd(),
Expand Down
17 changes: 13 additions & 4 deletions packages/cli/src/commands/use.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
hasPlugin,
installPlugin,
loadAllPlugins,
runDependencyQueues,
Expand All @@ -12,15 +13,23 @@ export const use = command("use")
description: "Plugin name",
variadic: true,
})
.option("reinstall", {
type: "boolean",
description: "Re-install plugin even if it is already installed",
})
.option("cwd", {
description: "Directory to use as the current working directory",
default: process.cwd(),
})
.action(async ({ plugin, cwd }) => {
.action(async ({ plugin, reinstall, cwd }) => {
for (const name of plugin) {
await task(`Add plugin ${name}`, () =>
installPlugin({ directory: cwd, name })
);
await task(`Add plugin ${name}`, async () => {
if (!reinstall && (await hasPlugin({ directory: cwd, name }))) {
throw new Error(`Plugin ${name} is already installed`);
}

await installPlugin({ directory: cwd, name });
});
}

await task(`Load plugins`, () => loadAllPlugins({ directory: cwd }));
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"prepublish": "yarn build",
"clean": "rm -rf dist && rm -rf types",
"build": "yarn clean && tsc",
"build:watch": "yarn build && tsc --watch",
"build:watch": "tsc --watch",
"test": "jest",
"test:watch": "jest --watch"
},
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/json.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import deepmerge from "deepmerge";

import { isReadableAndWritableFile, readFile, writeFile } from "./file.js";
import {
deepmerge,
isPlainObject,
JSONValue,
StringableJSONValue,
} from "./utils/types.js";
} from "./utils/index.js";

export async function readJson<T extends JSONValue>({
path,
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,10 @@ export async function validateType({
}

export async function installPlugin({ directory, name }: PluginOptions) {
if (await hasPlugin({ directory, name })) {
throw new Error(`Plugin ${name} is already installed`);
}

const plugin = await importPlugin({ directory, name });

await plugin.install({ directory });

await writePackage({
directory,
data: {
Expand All @@ -138,6 +135,7 @@ export async function removePlugin({ directory, name }: PluginOptions) {
const plugin = await importPlugin({ directory, name });

await plugin.remove({ directory });

await updatePackage({
directory,
merge: (existingData) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./assertions.js";
export * from "./exec.js";
export * from "./merge.js";
export * from "./string.js";
export * from "./types.js";
11 changes: 11 additions & 0 deletions packages/core/src/utils/merge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ogDeepmerge from "deepmerge";

export function deepmerge<T>(source1: Partial<T>, source2: Partial<T>) {
return ogDeepmerge<T>(source1, source2, {
arrayMerge,
});
}

function arrayMerge<T>(source1: T[], source2: T[]) {
return [...new Set([...source1, ...source2])];
}
13 changes: 13 additions & 0 deletions packages/core/src/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,16 @@ export async function runDependencyQueues({ directory }: DirOption) {
queues.remove.set(directory, new Set());
}
}

export async function listWorkspaces({ directory }: DirOption) {
const { stdout } = await exec("yarn", ["workspaces", "list", "--json"], {
cwd: directory,
});
const workspaces: { location: string; name: string }[] = [];

for (const line of stdout.split("\n").filter((item) => item)) {
workspaces.push(JSON.parse(line.trim()));
}

return workspaces;
}
2 changes: 1 addition & 1 deletion packages/plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"prepublish": "yarn build",
"clean": "rm -rf dist && rm -rf types",
"build": "yarn clean && tsc",
"build:watch": "yarn build && tsc --watch"
"build:watch": "tsc --watch"
},
"dependencies": {
"@mokr/core": "workspace:*"
Expand Down
2 changes: 1 addition & 1 deletion packages/templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"prepublish": "yarn build",
"clean": "rm -rf dist && rm -rf types",
"build": "yarn clean && tsc",
"build:watch": "yarn build && tsc --watch"
"build:watch": "tsc --watch"
},
"dependencies": {
"@mokr/core": "workspace:*"
Expand Down

0 comments on commit fd7bfa3

Please sign in to comment.