Skip to content

Commit

Permalink
fix: disable greedy arrays (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
hongaar authored Mar 8, 2023
1 parent 64bef9e commit 5f400f2
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 22 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ cd my-monorepo
yarn moker use prettier doctoc semantic-release

# create workspaces
yarn moker add server --template express
yarn moker add client --template cra
yarn moker add shared --template lib
yarn moker add cli --template bandersnatch
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 Down
17 changes: 11 additions & 6 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"version": "0.11.2",
"description": "The moker CLI",
"repository": "https://github.com/hongaar/moker",
"author": "[email protected]",
"license": "MIT",
"author": "[email protected]",
"type": "module",
"main": "dist/index.js",
"types": "types/index.d.ts",
Expand All @@ -14,11 +14,12 @@
"types"
],
"scripts": {
"start": "node moker.js",
"prepublish": "yarn build && cp ../../README.md .",
"clean": "rm -rf dist && rm -rf types",
"build": "yarn clean && tsc",
"build:watch": "tsc --watch"
"build:watch": "tsc --watch",
"clean": "rm -rf dist && rm -rf types",
"prepublish": "yarn build && cp ../../README.md .",
"start": "node moker.js",
"test": "NODE_OPTIONS='--loader=ts-node/esm --no-warnings' node--test test/*.test.ts | NODE_OPTIONS='--loader=ts-node/esm --no-warnings' tap --no-coverage"
},
"dependencies": {
"@mokr/core": "workspace:*",
Expand All @@ -28,11 +29,15 @@
},
"devDependencies": {
"@types/node": "18.14.6",
"tap": "16.3.4",
"test": "3.3.0",
"ts-node": "10.9.1",
"typescript": "4.9.5"
},
"moker": {
"plugins": [
"typescript"
"typescript",
"test"
]
}
}
6 changes: 5 additions & 1 deletion packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { assertNodeVersion, assertYarnVersion } from "@mokr/core";
import { program } from "bandersnatch";
import * as commands from "./commands/index.js";

const cli = program().prompt("moker > ");
const cli = program({
parserConfiguration: {
"greedy-arrays": false,
},
}).prompt("moker > ");

// @ts-ignore
Object.values(commands).forEach((command) => cli.add(command));
Expand Down
80 changes: 80 additions & 0 deletions packages/cli/test/add.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { createDirectory, isDirectory, writePackage } from "@mokr/core";
import { setFastFail } from "@mokr/core/src/io.js";
import assert from "node:assert";
import { temporaryDirectory } from "tempy";
import test from "test";
import cli from "../src/cli.js";

/* @ts-expect-error */
const { beforeEach, before, after, it } = test;

let tempDir: string;

before(() => {
setFastFail(true);
});

after(() => {
setFastFail(false);
});

beforeEach(() => {
tempDir = temporaryDirectory();
});

it("should only run wihtin monorepos", async () => {
await assert.rejects(cli.run(`add --cwd ${tempDir} foo`), {
name: "Error",
message: "Execute this command from within a monorepo",
});
});

it("needs a workspace configuration", async () => {
await createDirectory({ directory: `${tempDir}/.git` });
await writePackage({
directory: tempDir,
data: {
moker: { scoped: true },
},
});

await assert.rejects(cli.run(`add --cwd ${tempDir} foo`), {
name: "Error",
message: "No workspace configuration found in package.json",
});
});

it("should add a workspace", async () => {
const tempDir = temporaryDirectory();

await createDirectory({ directory: `${tempDir}/.git` });
await writePackage({
directory: tempDir,
data: {
workspaces: ["packages/*"],
moker: { scoped: true },
},
});

await cli.run(`add --cwd ${tempDir} foo`);
});

it(
"should not confuse templates and workspace names",
{ only: true },
async () => {
const tempDir = temporaryDirectory();

await createDirectory({ directory: `${tempDir}/.git` });
await writePackage({
directory: tempDir,
data: {
workspaces: ["packages/*"],
moker: { scoped: true },
},
});

await cli.run(`add --cwd ${tempDir} --template bar foo`);
assert(await isDirectory({ directory: `${tempDir}/packages/foo` }));
}
);
10 changes: 10 additions & 0 deletions packages/core/src/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Log =
type: "error";
};

let fastFail = false;
let messages: Log[];
let encounteredErrors: boolean;

Expand Down Expand Up @@ -75,6 +76,10 @@ export function resetState() {
resetEncounteredErrors();
}

export function setFastFail(value: boolean) {
fastFail = value;
}

export async function flushLogs() {
for (const { message, type } of messages) {
type === "warning"
Expand All @@ -98,6 +103,11 @@ export async function task<T>(title: string, callback: () => Promise<T>) {
spinner.fail();
logError(error);
flushLogs();

if (fastFail) {
throw error;
}

return [null, error as Error] as const;
}

Expand Down
36 changes: 25 additions & 11 deletions packages/core/src/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,32 @@ const queues = {
remove: new Map<string, Set<string>>(),
};

export async function runYarnCmd(
args: string[],
{ directory }: { directory: string }
) {
return exec("yarn", args, {
cwd: directory,
env: {
...process.env,
// Reset NODE_OPTIONS='--loader=ts-node/esm' when running from tests
NODE_OPTIONS: undefined,
},
});
}

export async function initYarnExistingRepo({ directory }: DirOption) {
await exec("git", ["init", "--initial-branch", "main"], { cwd: directory });

await exec("yarn", ["set", "version", "latest"], { cwd: directory });
await runYarnCmd(["set", "version", "latest"], { directory });

await setupYarn({ directory });
}

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

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

await setupYarn({ directory });
}
Expand All @@ -62,7 +76,7 @@ export async function addYarnPlugin({
directory: string;
name: string;
}) {
await exec("yarn", ["plugin", "import", name], { cwd: directory });
await runYarnCmd(["plugin", "import", name], { directory });
}

export async function removeYarnPlugin({
Expand All @@ -72,7 +86,7 @@ export async function removeYarnPlugin({
directory: string;
name: string;
}) {
await exec("yarn", ["plugin", "remove", name], { cwd: directory });
await runYarnCmd(["plugin", "remove", name], { directory });
}

export async function installDependency({
Expand All @@ -92,7 +106,7 @@ export async function installDependency({
...identifier.map((id) => (dev ? `--dev ${id}` : id)),
];

await exec("yarn", installArgs, { cwd: directory });
await runYarnCmd(installArgs, { directory });
}

export function enqueueInstallDependency({
Expand Down Expand Up @@ -150,14 +164,14 @@ export async function runDependencyQueues({ directory }: DirOption) {

if (!dependencies.size && !devDependencies.size && !removeDependencies.size) {
// Nothing to install or remove
await exec("yarn", [], { cwd: directory });
await runYarnCmd([], { directory });
return;
}

if (dependencies.size) {
const installArgs = ["add", "--exact", ...Array.from(dependencies)];

await exec("yarn", installArgs, { cwd: directory });
await runYarnCmd(installArgs, { directory });

queues.install.set(directory, new Set());
}
Expand All @@ -170,15 +184,15 @@ export async function runDependencyQueues({ directory }: DirOption) {
...Array.from(devDependencies),
];

await exec("yarn", installArgs, { cwd: directory });
await runYarnCmd(installArgs, { directory });

queues.installDev.set(directory, new Set());
}

if (removeDependencies.size) {
const removeArgs = ["remove", ...Array.from(removeDependencies)];

await exec("yarn", removeArgs, { cwd: directory });
await runYarnCmd(removeArgs, { directory });

queues.remove.set(directory, new Set());
}
Expand All @@ -187,8 +201,8 @@ export async function runDependencyQueues({ directory }: DirOption) {
}

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

Expand Down
3 changes: 3 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5192,6 +5192,9 @@ __metadata:
"@mokr/templates": "workspace:*"
"@types/node": 18.14.6
bandersnatch: 1.11.1
tap: 16.3.4
test: 3.3.0
ts-node: 10.9.1
typescript: 4.9.5
bin:
moker: moker.js
Expand Down

0 comments on commit 5f400f2

Please sign in to comment.