0.0.7 • Published 4 months ago

figma-icons-cli v0.0.7

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

figma-icons-cli

A command-line interface for managing icons. 管理图标组件的命令行工具。 可同时拉取 iconfont/figma 图标到本地,生成组件。

特性

  • ⚙️ 配置灵活,同一项目内可配多个不同配置,根据需求灵活使用
  • 📠 可单独拉取 iconfont 或 figma 的图标,也可两者同时拉取
  • 🎯 多种输出类型可选,且可任意配置
  • 🌈 可自定义组件格式化方式
  • 🚀 可设置过滤函数,过滤不需要的图标
  • 🔄 提供前后图标数据差异对比,方便跟踪变化
  • 🤖 提供图标集成组件模板、预览页模板

安装依赖

npm install -D figma-icons-cli
yarn add -D figma-icons-cli

使用

初始化配置文件

icons init

在当前目录生成 icons.config.ts 配置文件。具体配置详见下方配置参数说明
参考:配置文件模板 注:配置参数可传入对象或者数组。

拉取图标

# configFile: 可传入配置文件,不传则默认取当前根目录下的icons.config.ts 配置文件
icons build [configFile]

使用图标

输出的 type=component 时

<template>
  <div>
    <IconArrowLeft style="font-size: 50px; color: red" />
    <icon-more style="font-size: 50px; color: red" />

    <!-- format=bg|img时 -->
    <IconArrowLeft size="50px" color="#2254f4" />
  </div>
</template>

<script lang="ts">
import { IconArrowLeft, IconMore } from "@/icons";

export default {
  components: {
    IconArrowLeft,
    IconMore,
  },
};
</script>

输出的 type=symbol 时

// 入口文件main.js
import { createApp } from "vue";
import App from "./App.vue";
import "./svg-icon/svg-icon"; // 输出的svg-icon.js文件
import SvgIcon from "./svg-icon/svg-icon.vue"; // 输出的svg-icon.vue文件

const app = createApp(App);
app.component(SvgIcon.name, SvgIcon); // 全局注册图标组件
app.mount("#app");
<template>
  <div>
    <svg-icon name="success-static" style="font-size: 50px; color: #2254f4" />
    <svg-icon name="error-fill" :style="{ fontSize: 50, color: '#2254f4' }" />
    <svg-icon name="edit" :style="{ fontSize: 50, color: '#2254f4' }" />
    <svg-icon name="add" :style="{ fontSize: 50, color: '#2254f4' }" />
  </div>
</template>

创建模板文件

如需要<Icon name=""/>这种方式的集成所有图标的组件,可创建相应模板,进行自定义;同时提供了图标预览页模板。

# 根据模板,创建文件。执行后可选:icon 集成所有图标的Icon组件模板, preview 图标预览页模板, both 同时创建两种模板文件
# outputDir: 输出文件目录,不传则默认取当前根目录
icons template [outputDir]

配置参数说明

参数名描述是否必填类型默认值
input输入配置InputOption | InputOption[]
output输出配置OutputOption |OutputOption[]

InputOption

参数名描述是否必填类型
type源数据类型'figma' | 'iconfont'
urlfigma 文档链接或 iconfont js 链接string
tokentype=figma 时需要, figma api 需要的 tokenstring
prefix图标名前缀,用来避免不同数据源有同名的图标string
modulestype=figma 时可选, 自定义选择 figma 模块内的图标进行输出string[]
filter过滤函数(icon: Icon, index: number, array: Array<Icon>) => boolean;
formatIconName自定义修改图标名称(name:string) => string;
formatIconType自定义图标类型,static 固定色、configurable 可变色(name:string) => 'configurable'|'static';

OutputOption

参数名描述是否必填类型默认值
type输出类型'diff' | 'svg' | 'json' | 'component' | 'symbol'''
formattype=component 时生效,选择预置的格式化方式'vue' | 'img' | 'bg'''
dir输出目录string'src'
dirnametype=component 时生效,输出图标组件存放目录名number'components'
filenametype=component 时生效,输出图标组件的入口文件名boolean'index.js'
tagformat=bg 时生效,自定义组件的标签string'src'
defaultColorformat=img | bg 时生效,图标组件默认颜色值string''
defaultSizeformat=img | bg 时生效,图标组件默认大小string | number20
prefix生成图标组件的前缀,如 arrow -> IconArrowstring'icon'
className图标组件上的 class 名,如 arrow -> 'icons icons-arrow'string'icons'
svgAttrformat=vue 时生效,自定义组件 svg 标签的属性string'aria-hidden="true" v-on="$listeners"'
styletype=symbol/component 时生效,自定义组件的 stylestring''
formatSvgtype=component 时生效,自定义格式化 svg 的函数(options: SvgFormatOptions) => string
formatComponenttype=component 时生效,自定义格式化组件的函数(options: ComponentFormatOptions) => string
formatExportStringtype=component 时生效,自定义格式化导出组件的字符串(options: ExportStringFormatOptions) => string

type 参数说明

描述
diff输出前后图标数据差异对比
svg输出所有图标 svg 文件
json集合所有图标数据的 json 文件,如需输出 diff 文件,json 文件也必须输出
component输出图标组件
symbol输出类似 iconfont symbol 方式的文件

format 参数说明

框架标签描述常用场景
vuevuesvg正常的 svg 标签组件
imgvueimg图标转化为 base64 输出到 img 标签的 src 参数中
bgvue自定义,默认'span'图标转化为 base64 输出到标签 style 中的 background-image 中微信小程序下不支持 svg 标签,可使用 text 标签输出图标组件

注:微信小程序使用bg的方式,因转化 base64 时使用到了 btoa 函数,但小程序环境下并没有该函数,故此需在全局提前注入 btoa 函数,此处提供一个 btoa 函数供参考:btoa

SvgFormatOptions

参数名描述类型
content图标 svg 的字符串string
name图标的名称string
className输出图标组件的类名string
type图标的类型string<"configurable" | "static">
svgAttrsvg 标签的属性string

ComponentFormatOptions

参数名描述类型
svgContentformatSvg 返回的 svg 字符串string
componentName输出成图标组件的组件名string
className输出图标组件的类名string
name图标名string
tagformat=bg 时生效,自定义组件的标签string
defaultColor图标组件默认颜色值string
defaultSize图标组件默认大小string | number
style自定义图标组件样式string

ExportStringFormatOptions

参数名描述类型
componentName图标组件的组件名string
iconDirName图标组件输出的目录名string
name图标组件的文件名string