Skip to content

Commit

Permalink
feat: add and init
Browse files Browse the repository at this point in the history
  • Loading branch information
gronxb committed Sep 14, 2024
1 parent fe9c855 commit eb86ab1
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 7 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ https://react-icons.github.io/react-icons/

Check out the icons you want here and enter the following command:
```sh
> npx react-native-icons-builder add AiFillApple
> npx react-native-icons-builder@latest add AiFillApple
```

## Motivation
Expand All @@ -27,15 +27,13 @@ Check out the icons you want here and enter the following command:
* Basic
```sh
> npm install react-native-svg # or use pnpm or yarn
> npx react-native-icons-builder init
> npx react-native-icons-builder add AiFillApple # visit https://react-icons.github.io/react-icons/
> npx react-native-icons-builder@latest add AiFillApple # visit https://react-icons.github.io/react-icons/
```

* Shorthand
```sh
> npm install react-native-svg react-native-icons-builder # or use pnpm or yarn
> npm rn-icons init
> npx rn-icons add AiFillApple # visit https://react-icons.github.io/react-icons/
> npm run rn-icons add AiFillApple # visit https://react-icons.github.io/react-icons/
```


Expand Down
6 changes: 5 additions & 1 deletion src/commands/add.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { syncIcons } from "../utils/generateIcon";
import { addIcons } from "../utils//config";
import { addIcons, checkIfConfigExists } from "../utils//config";
import { generateBaseCode } from "../utils/generateBaseCode";
import { init } from "./init";

export const add = async (iconNames: string[]) => {
try {
if (!checkIfConfigExists()) {
await init();
}
const config = await addIcons(iconNames);
await generateBaseCode(config);
await syncIcons(config);
Expand Down
3 changes: 2 additions & 1 deletion src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const init = async () => {
);

try {
initConfig({
const result = await initConfig({
...config,
icons: [],
});
Expand All @@ -37,6 +37,7 @@ export const init = async () => {
"Click here to explore available icons",
"https://react-icons.github.io/react-icons/"
);
return result;
} catch (error) {
if (error instanceof Error) {
console.error(error.message);
Expand Down
6 changes: 6 additions & 0 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from "fs/promises";
import { z } from "zod";
import { getCwd } from "./cwd";
import { uniq } from "./uniq";
import { existsSync } from "fs";

export const configSchema = z.object({
typescript: z.boolean(),
Expand Down Expand Up @@ -44,3 +45,8 @@ export const initConfig = async (config: Config) => {
);
return data;
};

export const checkIfConfigExists = () => {
const cwd = getCwd();
return existsSync(path.join(cwd, "react-native-icons-builder.json"));
};
3 changes: 3 additions & 0 deletions src/utils/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export const log = {
success: (message: string) => {
console.log(pc.green(message));
},
save: (filename: string) => {
console.log(`${pc.blue(`${filename}`)} ${pc.green("saved successfully.")}`);
},
link: (message: string, link: string) => {
console.log(`\x1b]8;;${link}\x1b\\${message}\x1b]8;;\x1b\\`);
},
Expand Down
3 changes: 3 additions & 0 deletions src/utils/generateBaseCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from "path";
import type { Config } from "./config";
import { getCwd, getDirname } from "./cwd";
import fs from "fs/promises";
import { log } from "./console";

export const generateBaseCode = async (config: Config) => {
const iconBase = config.typescript ? "iconBase.tsx" : "iconBase.jsx";
Expand All @@ -21,4 +22,6 @@ export const generateBaseCode = async (config: Config) => {
path.join(outputPath, iconContext)
),
]);
log.save(path.join(config.outputPath, iconBase));
log.save(path.join(config.outputPath, iconContext));
};
2 changes: 2 additions & 0 deletions src/utils/generateIcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { type ModuleItem, parse, print } from "@swc/core";
import { groupIconsByPrefix } from "./groupIconsByPrefix";
import type { Config } from "./config";
import { getCwd } from "./cwd";
import { log } from "./console";

export const generateIconCode = async (
prefix: string,
Expand Down Expand Up @@ -112,6 +113,7 @@ const saveIcons = async (
await fs.mkdir($outputPath, { recursive: true });

await fs.writeFile(path.join($outputPath, filename), code, "utf8");
log.save(path.join(outputPath, filename));
};

export const syncIcons = async (config: Config) => {
Expand Down

0 comments on commit eb86ab1

Please sign in to comment.