Skip to content

Commit

Permalink
feat: angular (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
hongaar authored Oct 18, 2024
1 parent cccf6d1 commit 5220f05
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 15 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ yarn moker add --template bandersnatch cli
- [`typescript`](#typescript)
- [`xv`](#xv)
- [Available templates](#available-templates)
- [`angular`](#angular)
- [`bandersnatch`](#bandersnatch)
- [`common`](#common)
- [`cra`](#cra)
Expand Down Expand Up @@ -447,6 +448,13 @@ script to the repo or both the workspace and the monorepo.
# Available templates
## `angular`
_Scope: repo or workspace_
Uses the [Angular CLI](https://angular.dev/tools/cli) to scaffold an Angular app
(web client).
## `bandersnatch`
_Scope: repo or workspace_
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"scripts": {
"build": "yarn workspaces foreach --all --topological --verbose run build",
"build:watch": "yarn workspaces foreach --all --parallel --interlaced run build:watch",
"build:watch": "yarn workspaces foreach --all --parallel --jobs unlimited --interlaced run build:watch",
"build:clean": "yarn workspaces foreach --all --topological --verbose run build:clean",
"doctoc": "doctoc README.md",
"format": "prettier --write --ignore-unknown .",
Expand Down
32 changes: 20 additions & 12 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 tasks: Ora[] = [];
let messages: Log[];
let encounteredErrors: boolean;

Expand Down Expand Up @@ -80,10 +81,10 @@ export async function flushLogs() {
type === "warning"
? writeWarning(message)
: type === "debug"
? writeDebug(message)
: type === "error"
? writeError(message)
: writeInfo(message);
? writeDebug(message)
: type === "error"
? writeError(message)
: writeInfo(message);
}
resetMessages();
}
Expand All @@ -94,26 +95,29 @@ export async function task<T>(
) {
const spinner = ora(title).start();
let result: T;
let error: any;

tasks.push(spinner);

try {
result = await callback(spinner);
} catch (error: any) {
spinner.fail();
logError(error);
flushLogs();

return [null, error as Error] as const;
} catch (err: any) {
error = err;
logError(err);
}

if (messages.filter(({ type }) => type === "warning").length > 0) {
if (error) {
spinner.fail();
} else if (messages.filter(({ type }) => type === "warning").length > 0) {
spinner.warn();
} else {
spinner.succeed();
}

flushLogs();
tasks = tasks.filter((task) => task !== spinner);

return [result, null] as const;
return error ? ([null, error as Error] as const) : ([result!, null] as const);
}

export function getMessages() {
Expand All @@ -127,3 +131,7 @@ export function containsMessage(search: string) {
export function hasEncounteredErrors() {
return encounteredErrors;
}

export function getLastTask() {
return tasks[tasks.length - 1];
}
1 change: 1 addition & 0 deletions packages/core/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type TemplateOptions = {
};

const CORE_TEMPLATES = [
"angular",
"bandersnatch",
"common",
"cra",
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/utils/exec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import childProcess, { ChildProcess } from "node:child_process";
import { debug } from "../io.js";
import { debug, getLastTask } from "../io.js";

function childAwaiter(child: ChildProcess): Promise<number> {
return new Promise(function (resolve, reject) {
Expand All @@ -19,6 +19,10 @@ export async function exec(
) {
debug(`spawning "${cmd} ${args.join(" ")}" in "${cwd}"`);

if (io === "passthrough") {
getLastTask()?.stop();
}

const child = childProcess.spawn(cmd, args, {
shell: true,
stdio: io === "passthrough" ? "inherit" : "pipe",
Expand All @@ -43,6 +47,10 @@ export async function exec(

const status = await childAwaiter(child);

if (io === "passthrough") {
getLastTask()?.start();
}

const result = { status, stdout, stderr };

if (status !== 0) {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type DirOption = {
};

const DEFAULT_INITIAL_VERSION = "0.0.0";
const README_FILENAME = "README.md";

export async function addWorkspace({
directory,
Expand Down Expand Up @@ -60,7 +61,7 @@ export async function writeReadme({
name,
}: DirOption & { name?: string }) {
await writeFile({
path: join(directory, "README.md"),
path: join(directory, README_FILENAME),
contents: `# ${name ?? basename(directory)}`,
});
}
Expand Down
44 changes: 44 additions & 0 deletions packages/templates/src/angular.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
PluginType,
exec,
readPackage,
removeDirectory,
writePackage,
type TemplateArgs,
} from "@mokr/core";
import { basename, dirname } from "node:path";

async function apply({ directory }: TemplateArgs) {
const oldPackage = await readPackage({ directory });

await removeDirectory({ directory });

await exec(
"yarn",
[
"dlx",
"@angular/cli",
"new",
"--package-manager",
"yarn",
"--force",
"--skip-git",
"--skip-install",
basename(directory),
],
{
cwd: dirname(directory),
io: "passthrough",
},
);

delete oldPackage.scripts?.["build"];
delete oldPackage.scripts?.["test"];

await writePackage({ directory, data: oldPackage });
}

export const angular = {
type: PluginType.RepoOrWorkspace,
apply,
};
1 change: 1 addition & 0 deletions packages/templates/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./angular.js";
export * from "./bandersnatch.js";
export * from "./common.js";
export * from "./cra.js";
Expand Down

0 comments on commit 5220f05

Please sign in to comment.