import { type Path, join, strings } from "@angular-devkit/core";
import {
type Rule,
type Source,
apply,
mergeWith,
move,
template,
url,
} from "@angular-devkit/schematics";
import { toKebabCase } from "@utils/formatting";
import { resolvePackageName } from "@utils/name";
import { DEFAULT_APP_NAME, DEFAULT_LANGUAGE } from "~/defaults";
import { type MySchematicOptions } from "./my-schematic.options";
import { type MySchematicSchema } from "./my-schematic.schema";
const transform = (schema: MySchematicSchema): MySchematicOptions => {
const name = resolvePackageName(toKebabCase(schema.name?.toString() ?? DEFAULT_APP_NAME));
return {
name,
language: schema.language ?? DEFAULT_LANGUAGE,
};
};
const generate = (options: MySchematicOptions, path: string): Source => {
return apply(url(join("./files" as Path, options.language)), [
template({
...strings,
...options,
}),
move(path),
]);
};
export const main = (schema: MySchematicSchema): Rule => {
const options = transform(schema);
return mergeWith(generate(options, schema.directory ?? options.name));
};