Skip to content

Commit

Permalink
feat: generate base code
Browse files Browse the repository at this point in the history
  • Loading branch information
gronxb committed Sep 14, 2024
1 parent c69c2f8 commit 9839ff6
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { syncIcons } from "../utils/generateIcon";
import { addIcon } from "../utils//config";
import { addIcons } from "../utils//config";
import { generateBaseCode } from "../utils/generateBaseCode";

export const add = async (iconName: string) => {
export const add = async (iconNames: string[]) => {
try {
const config = await addIcon(iconName);
const config = await addIcons(iconNames);
await generateBaseCode(config);
await syncIcons(config);
} catch (error) {
if (error instanceof Error) {
Expand Down
2 changes: 2 additions & 0 deletions src/commands/sync.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { syncIcons } from "../utils/generateIcon";
import { loadConfig } from "../utils/config";
import { generateBaseCode } from "../utils/generateBaseCode";

export const sync = async () => {
try {
const config = await loadConfig();
await generateBaseCode(config);
await syncIcons(config);
} catch (error) {
if (error instanceof Error) {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ program.command("init").description("Initialize the project").action(init);

program
.command("add")
.description("Add an icon to the project")
.argument("<icon-name>", "The name of the icon to add")
.description("Add icons to the project")
.argument("<icon-names...>", "The names of the icons to add")
.action(add);

program
Expand Down
4 changes: 2 additions & 2 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export const loadConfig = async () => {
return config;
};

export const addIcon = async (iconName: string) => {
export const addIcons = async (iconNames: string[]) => {
const config = await loadConfig();
const updatedIcons = uniq([...config.icons, iconName]);
const updatedIcons = uniq([...config.icons, ...iconNames]);
config.icons = updatedIcons;

return initConfig(config);
Expand Down
3 changes: 3 additions & 0 deletions src/utils/cwd.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { dirname } from "path";
import { fileURLToPath } from "url";
import { findPackageRoot } from "workspace-tools";

export const getCwd = () => findPackageRoot(process.cwd())!;
export const getDirname = () => dirname(fileURLToPath(import.meta.url));
24 changes: 24 additions & 0 deletions src/utils/generateBaseCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import path from "path";
import type { Config } from "./config";
import { getCwd, getDirname } from "./cwd";
import fs from "fs/promises";

export const generateBaseCode = async (config: Config) => {
const iconBase = config.typescript ? "iconBase.tsx" : "iconBase.jsx";
const iconContext = config.typescript ? "iconContext.tsx" : "iconContext.jsx";

const templatesPath = path.join(getDirname(), "..", "templates");
const outputPath = path.join(getCwd(), config.outputPath);

await fs.mkdir(outputPath, { recursive: true });
await Promise.all([
fs.copyFile(
path.join(templatesPath, iconBase),
path.join(outputPath, iconBase)
),
fs.copyFile(
path.join(templatesPath, iconContext),
path.join(outputPath, iconContext)
),
]);
};
2 changes: 1 addition & 1 deletion src/utils/generateIcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const generateIconCode = async (
}
code = `
import { ${[...svgTagSet].join(", ")} } from "react-native-svg"
import { type IconBaseProps, GenIcon } from "./iconBase";
import { ${typescript ? "type IconBaseProps, " : ""}GenIcon } from "./iconBase";
${code}
`;
Expand Down

0 comments on commit 9839ff6

Please sign in to comment.