Skip to content

Commit

Permalink
feat: reinstall (#92)
Browse files Browse the repository at this point in the history
* feat: reinstall

* refactor: tasks
  • Loading branch information
hongaar committed Dec 11, 2022
1 parent fe85aa7 commit dc38447
Show file tree
Hide file tree
Showing 18 changed files with 213 additions and 83 deletions.
2 changes: 2 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ plugins:
spec: "@yarnpkg/plugin-workspace-tools"
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"
- path: .yarn/plugins/@yarnpkg/plugin-typescript.cjs
spec: "@yarnpkg/plugin-typescript"

yarnPath: .yarn/releases/yarn-3.3.0.cjs
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ yarn moker add --template cra client
- [`cra` _repo or workspace_](#cra-_repo-or-workspace_)
- [`express` _repo or workspace_](#express-_repo-or-workspace_)
- [`lib` _repo or workspace_](#lib-_repo-or-workspace_)
- [Commands](#commands)
- [Contributing](#contributing)
- [Roadmap](#roadmap)
- [Development](#development)
Expand Down Expand Up @@ -287,6 +288,10 @@ Scaffolds a simple [express](https://expressjs.com) HTTP app with the
A plain shared library template with the [typescript](#typescript-workspace) and
[jest](#jest-workspace) plugins.

# Commands

See `moker --help` for a list of available commands.

# Contributing

Contributions are very welcome!
Expand Down
9 changes: 4 additions & 5 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
### TODOs

| 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#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 |
| 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 |
9 changes: 7 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"scripts": {
"start": "node moker.js",
"prepublish": "yarn clean && yarn build && cp ../../README.md .",
"prepublish": "yarn build && cp ../../README.md .",
"clean": "rm -rf dist && rm -rf types",
"build": "yarn clean && tsc",
"build:watch": "tsc --watch"
Expand All @@ -27,7 +27,12 @@
"bandersnatch": "1.11.1"
},
"devDependencies": {
"@types/node": "18.11.12",
"@types/node": "18.11.13",
"typescript": "4.9.4"
},
"moker": {
"plugins": [
"typescript"
]
}
}
45 changes: 17 additions & 28 deletions packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {
addWorkspace,
applyTemplate,
installPlugin,
isMonorepo,
loadAllPlugins,
runDependencyQueues,
task,
} from "@mokr/core";
import { addWorkspace, applyTemplate, isMonorepo, task } from "@mokr/core";
import { command } from "bandersnatch";
import { resolve } from "node:path";
import {
addPlugin,
format,
loadPlugins,
updateDependencies,
} from "../tasks.js";

export const add = command("add")
.description("Add a workspace to a monorepo")
Expand All @@ -30,14 +29,16 @@ export const add = command("add")
default: process.cwd(),
})
.action(async ({ name, template, plugin, cwd }) => {
if (!(await isMonorepo({ directory: cwd }))) {
const directory = resolve(cwd);

if (!(await isMonorepo({ directory }))) {
throw new Error("Execute this command from within a monorepo");
}

for (const workspaceName of name) {
const workspaceDirectory = await task(
`Add workspace ${workspaceName}`,
() => addWorkspace({ directory: cwd, name: workspaceName })
() => addWorkspace({ directory, name: workspaceName })
);

if (template) {
Expand All @@ -47,25 +48,13 @@ export const add = command("add")
}

for (const name of plugin) {
await task(`Add plugin ${name}`, () =>
installPlugin({ directory: workspaceDirectory, name })
);
await addPlugin({ directory: workspaceDirectory, name });
}

await task(`Load plugins`, () =>
loadAllPlugins({ directory: workspaceDirectory })
);
await task(`Update dependencies`, () =>
runDependencyQueues({ directory: workspaceDirectory })
);
await loadPlugins({ directory: workspaceDirectory });

await updateDependencies({ directory: workspaceDirectory });
}

// await asyncForEach(name, async (name) => {
// const directory = path.join(monorepo.directory, "packages", name);
// const spinner = ora(`Creating workspace ${name}...`).start();
// await new Workspace(monorepo, directory).create(
// workspace[template as keyof typeof workspace]
// );
// spinner.succeed(`Created workspace ${name}`);
// });
await format({ directory });
});
20 changes: 12 additions & 8 deletions packages/cli/src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import {
DEFAULT_LICENSE,
DEFAULT_SCOPED,
DEFAULT_WORKSPACES_DIRECTORY,
installPlugin,
isReadableAndWritableDirectory,
loadAllPlugins,
runDependencyQueues,
task,
} from "@mokr/core";
import { command } from "bandersnatch";
import { resolve } from "node:path";
import {
addPlugin,
format,
loadPlugins,
updateDependencies,
} from "../tasks.js";

export const create = command("create")
.description("Create a new repo")
Expand Down Expand Up @@ -73,11 +76,12 @@ export const create = command("create")
}

for (const name of plugin) {
await task(`Add plugin ${name}`, () =>
installPlugin({ directory, name })
);
await addPlugin({ directory, name });
}

await task(`Load plugins`, () => loadAllPlugins({ directory }));
await task(`Update dependencies`, () => runDependencyQueues({ directory }));
await loadPlugins({ directory });

await updateDependencies({ directory });

await format({ directory });
});
1 change: 1 addition & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./add.js";
export * from "./create.js";
export * from "./list.js";
export * from "./reinstall.js";
export * from "./reload.js";
export * from "./remove.js";
export * from "./use.js";
10 changes: 6 additions & 4 deletions packages/cli/src/commands/list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getPlugins, listWorkspaces } from "@mokr/core";
import { getPlugins, getWorkspaces } from "@mokr/core";
import { command } from "bandersnatch";
import { resolve } from "node:path";

export const list = command("list")
.description("List plugins and workspaces in a monorepo")
Expand All @@ -8,15 +9,16 @@ export const list = command("list")
default: process.cwd(),
})
.action(async ({ cwd }) => {
const plugins = await getPlugins({ directory: cwd });
const workspaces = await listWorkspaces({ directory: cwd });
const directory = resolve(cwd);

const plugins = await getPlugins({ directory });
const workspaces = await getWorkspaces({ directory });

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

// @todo: list workspaces using https://yarnpkg.com/cli/workspaces/list
console.log(
`Workspaces:
${workspaces.map(({ location, name }) => `- ${name} (${location})\n`).join("")}`
Expand Down
70 changes: 70 additions & 0 deletions packages/cli/src/commands/reinstall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
getPlugins,
getWorkspaces,
installPlugin,
isMonorepo,
loadAllPlugins,
logWarning,
task,
} from "@mokr/core";
import { command } from "bandersnatch";
import { resolve } from "node:path";
import { REINSTALL_WARNING } from "../constants.js";
import { format, updateDependencies } from "../tasks.js";

export const reinstall = command("reinstall")
.hidden()
.description("Reinstalls all plugins in the current directory")
.option("all", {
type: "boolean",
description: "Also reinstalls plugins of all workspaces in a monorepo",
})
.option("cwd", {
description: "Directory to use as the current working directory",
default: process.cwd(),
})
.action(async ({ all, cwd }) => {
const directory = resolve(cwd);
const plugins = await getPlugins({ directory });

for (const name of plugins) {
await task(`Reinstall plugin ${name}`, async () => {
await installPlugin({ directory, name });
});
}

await task(`Load plugins`, () => loadAllPlugins({ directory }));

if (all && (await isMonorepo({ directory }))) {
const workspaces = await getWorkspaces({ directory });

for (const workspace of workspaces) {
const workspaceDirectory = resolve(directory, workspace.location);
const plugins = await getPlugins({ directory: workspaceDirectory });

// Skip root
if (workspaceDirectory === directory) {
continue;
}

for (const name of plugins) {
await task(
`Reinstall plugin ${name} of ${workspace.name}`,
async () => {
await installPlugin({ directory: workspaceDirectory, name });
}
);
}

await task(`Load plugins of ${workspace.name}`, () =>
loadAllPlugins({ directory })
);
}
}

await updateDependencies({ directory });

await format({ directory });

logWarning(REINSTALL_WARNING);
});
14 changes: 9 additions & 5 deletions packages/cli/src/commands/reload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { loadAllPlugins, runDependencyQueues, task } from "@mokr/core";
import { command } from "bandersnatch";
import { resolve } from "node:path";
import { format, loadPlugins, updateDependencies } from "../tasks.js";

export const reload = command("reload")
.hidden()
Expand All @@ -9,8 +10,11 @@ export const reload = command("reload")
default: process.cwd(),
})
.action(async ({ cwd }) => {
await task(`Load plugins`, () => loadAllPlugins({ directory: cwd }));
await task(`Update dependencies`, () =>
runDependencyQueues({ directory: cwd })
);
const directory = resolve(cwd);

await loadPlugins({ directory });

await updateDependencies({ directory });

await format({ directory });
});
22 changes: 11 additions & 11 deletions packages/cli/src/commands/remove/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {
loadAllPlugins,
removePlugin,
runDependencyQueues,
task,
} from "@mokr/core";
import { removePlugin, task } from "@mokr/core";
import { command } from "bandersnatch";
import { resolve } from "node:path";
import { format, loadPlugins, updateDependencies } from "../../tasks.js";

export const remove = command("plugin")
.description("Remove a plugin from the monorepo or workspace")
Expand All @@ -17,14 +14,17 @@ export const remove = command("plugin")
default: process.cwd(),
})
.action(async ({ name, cwd }) => {
const directory = resolve(cwd);

for (const pluginName of name) {
await task(`Remove plugin ${pluginName}`, () =>
removePlugin({ directory: cwd, name: pluginName })
removePlugin({ directory, name: pluginName })
);
}

await task(`Load plugins`, () => loadAllPlugins({ directory: cwd }));
await task(`Update dependencies`, () =>
runDependencyQueues({ directory: cwd })
);
await loadPlugins({ directory });

await updateDependencies({ directory });

await format({ directory });
});
32 changes: 18 additions & 14 deletions packages/cli/src/commands/use.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import {
hasPlugin,
installPlugin,
loadAllPlugins,
runDependencyQueues,
task,
} from "@mokr/core";
import { hasPlugin, installPlugin, logWarning, task } from "@mokr/core";
import { command } from "bandersnatch";
import { resolve } from "node:path";
import { REINSTALL_WARNING } from "../constants.js";
import { format, loadPlugins, updateDependencies } from "../tasks.js";

export const use = command("use")
.description("Add plugin to monorepo or workspace")
.description("Install plugin in repo or workspace")
.argument("plugin", {
description: "Plugin name",
variadic: true,
Expand All @@ -22,18 +19,25 @@ export const use = command("use")
default: process.cwd(),
})
.action(async ({ plugin, reinstall, cwd }) => {
const directory = resolve(cwd);

for (const name of plugin) {
await task(`Add plugin ${name}`, async () => {
if (!reinstall && (await hasPlugin({ directory: cwd, name }))) {
if (!reinstall && (await hasPlugin({ directory, name }))) {
throw new Error(`Plugin ${name} is already installed`);
}

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

await task(`Load plugins`, () => loadAllPlugins({ directory: cwd }));
await task(`Update dependencies`, () =>
runDependencyQueues({ directory: cwd })
);
await loadPlugins({ directory });

await updateDependencies({ directory });

await format({ directory });

if (reinstall) {
logWarning(REINSTALL_WARNING);
}
});
2 changes: 2 additions & 0 deletions packages/cli/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const REINSTALL_WARNING =
"Please verify the changes made by reinstalling the plugin(s) as they may have reverted changes you've made to the plugins defaults.";
Loading

0 comments on commit dc38447

Please sign in to comment.