create-iconfont-component
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -5,2 +5,2 @@ #!/usr/bin/env node | ||
require("../lib/index"); | ||
require("../lib/index").cli(); |
@@ -0,11 +1,11 @@ | ||
import { ConfigSchema, ConfigType } from "./createConfig"; | ||
declare class CreateIconfont { | ||
configFileName: string; | ||
iconfontOpenApiUrl: string; | ||
isExistIconfontJson(pwd: string): boolean; | ||
getIconfontJson(pwd: string): CreateIconfontComponent.ConfigSchema; | ||
checkIconfontJson(config: CreateIconfontComponent.ConfigSchema): boolean; | ||
createIconfont(config: CreateIconfontComponent.ConfigSchema): void; | ||
getIconfontData(configItem: CreateIconfontComponent.ConfigType): Promise<void>; | ||
hasDuplicate(config: ConfigSchema, key: keyof ConfigType): boolean; | ||
deleteFile(outFile: string, projectName: string): void; | ||
createFile(config: ConfigType, data: any[]): void; | ||
createIconfont(config: ConfigSchema): void; | ||
getIconfontData(configItem: ConfigType): Promise<unknown>; | ||
} | ||
declare const _default: CreateIconfont; | ||
export default _default; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs_extra_1 = __importDefault(require("fs-extra")); | ||
const path_1 = __importDefault(require("path")); | ||
const ajv_1 = __importDefault(require("ajv")); | ||
const config_json_1 = __importDefault(require("./schema/config.json")); | ||
const axios_1 = __importDefault(require("axios")); | ||
const ajv = new ajv_1.default(); | ||
const fs = require("fs-extra"); | ||
const path = require("path"); | ||
const axios_1 = require("axios"); | ||
const signale_1 = require("signale"); | ||
const ejs = require("ejs"); | ||
const logger = new signale_1.Signale(); | ||
var FileExt; | ||
(function (FileExt) { | ||
FileExt["Typescript_react"] = "tsx"; | ||
FileExt["Typescript_vue"] = "vue"; | ||
FileExt["JavaScript_react"] = "js"; | ||
FileExt["JavaScript_vue"] = "vue"; | ||
})(FileExt || (FileExt = {})); | ||
function toPascalCase(str) { | ||
return ("Icon" + | ||
str | ||
.replace(/[-_]/g, " ") | ||
.split(" ") | ||
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) | ||
.join("") | ||
.replace(/\s+/g, "")); | ||
} | ||
class CreateIconfont { | ||
constructor() { | ||
this.configFileName = "iconfont.json"; | ||
this.iconfontOpenApiUrl = "https://www.iconfont.cn/open/project/detail.json"; | ||
this.iconfontOpenApiUrl = "http://www.iconfont.cn/open/project/detail.json"; | ||
} | ||
isExistIconfontJson(pwd) { | ||
return fs_extra_1.default.existsSync(path_1.default.resolve(pwd, this.configFileName)); | ||
hasDuplicate(config, key) { | ||
return config.some((item, index) => config.findIndex((i) => i[key] === item[key]) !== index); | ||
} | ||
getIconfontJson(pwd) { | ||
return fs_extra_1.default.readJSONSync(path_1.default.resolve(pwd, this.configFileName)); | ||
deleteFile(outFile, projectName) { | ||
return fs.removeSync(path.resolve(process.cwd(), outFile, projectName)); | ||
} | ||
checkIconfontJson(config) { | ||
const validator = ajv.compile(config_json_1.default); | ||
const validationResult = validator(config); | ||
if (validationResult) { | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
createFile(config, data) { | ||
const { projectName, componentPath: outFile, svgClass, projectType, projectLanguage } = config; | ||
logger.info(`开始写入${projectName}组件`); | ||
const ejsTemplate = fs.readFileSync(path.resolve(__dirname, `../template/${projectLanguage}/${projectType}/index.ejs`), "utf-8"); | ||
ejs | ||
.render(ejsTemplate, { icons: data?.map((item) => toPascalCase(item.font_class)) }, { async: true }) | ||
.then((text) => { | ||
return fs.outputFileSync(path.resolve(process.cwd(), outFile, projectName, `index.${FileExt[`${projectLanguage}_${projectType}`]}`), text); | ||
}); | ||
data.forEach((item) => { | ||
const ejsTemplate = fs.readFileSync(path.resolve(__dirname, `../template/${projectLanguage}/${projectType}/icon.ejs`), "utf-8"); | ||
ejs | ||
.render(ejsTemplate, { name: toPascalCase(item.font_class), svgPath: item.svg, svgClass }, { async: true }) | ||
.then((text) => { | ||
return fs.outputFileSync(path.resolve(process.cwd(), outFile, projectName, `${toPascalCase(item.font_class)}.${FileExt[`${projectLanguage}_${projectType}`]}`), text); | ||
}); | ||
}); | ||
} | ||
createIconfont(config) { | ||
config.forEach((item) => __awaiter(this, void 0, void 0, function* () { | ||
yield this.getIconfontData(item); | ||
})); | ||
if (this.hasDuplicate(config, "projectName")) { | ||
logger.error("项目名重复"); | ||
process.exit(0); | ||
} | ||
config.forEach((item) => { | ||
logger.info(`开始获取 项目名称:${item.projectName} -- iconfontId:${item.iconfontId}数据`); | ||
this.getIconfontData(item) | ||
.then((result) => { | ||
logger.info(`成功获取 项目名称:${item.projectName} -- iconfontId:${item.iconfontId}数据`); | ||
this.deleteFile(item.componentPath, item.projectName); | ||
this.createFile(item, result.icons || []); | ||
logger.success(`完成写入${item.projectName}组件`); | ||
}) | ||
.catch((e) => { | ||
logger.error(e?.message || `获取iconFont数据失败`); | ||
}); | ||
}); | ||
} | ||
getIconfontData(configItem) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const result = yield axios_1.default.get(`${this.iconfontOpenApiUrl}?pid=${configItem.iconfontId}`); | ||
const data = result.data.data; | ||
console.log(`data`); | ||
} | ||
catch (error) { | ||
} | ||
return new Promise((resolve, reject) => { | ||
axios_1.default.get(`${this.iconfontOpenApiUrl}?pid=${configItem.iconfontId}`) | ||
.then((result) => { | ||
const { code, data } = result.data; | ||
if (code !== 200) { | ||
logger.error(`${configItem.iconfontId} 获取数据失败`); | ||
reject(`拉取失败 项目名称:${configItem.projectName} -- iconfontId:${configItem.iconfontId}`); | ||
} | ||
else { | ||
resolve(data); | ||
} | ||
}) | ||
.catch(() => reject(`拉取失败 项目名称:${configItem.projectName} -- iconfontId:${configItem.iconfontId}`)); | ||
}); | ||
@@ -57,0 +87,0 @@ } |
@@ -1,1 +0,14 @@ | ||
export {}; | ||
import { ErrorObject } from "ajv"; | ||
import { ConfigSchema } from "./createConfig"; | ||
export declare class CreateIconfontComponentController { | ||
configFileName: string; | ||
isExistIconfontJson(pwd: string): boolean; | ||
getIconfontJson(pwd: string): ConfigSchema; | ||
checkIconfontJson(config: ConfigSchema): { | ||
result: boolean; | ||
error: ErrorObject<string, Record<string, any>, unknown>[] | null | undefined; | ||
}; | ||
constructor(); | ||
init(): void; | ||
} | ||
export declare const cli: () => CreateIconfontComponentController; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const createIconfont_1 = __importDefault(require("./createIconfont")); | ||
if (createIconfont_1.default.isExistIconfontJson(process.cwd())) { | ||
const config = createIconfont_1.default.getIconfontJson(process.cwd()); | ||
if (createIconfont_1.default.checkIconfontJson(config)) { | ||
console.log("iconfont.json 规范"); | ||
createIconfont_1.default.createIconfont(config); | ||
exports.cli = exports.CreateIconfontComponentController = void 0; | ||
const createIconfont_1 = require("./createIconfont"); | ||
const fs = require("fs-extra"); | ||
const path = require("path"); | ||
const ajv_1 = require("ajv"); | ||
const config_json_schema_1 = require("./schema/config.json.schema"); | ||
const createConfig_1 = require("./createConfig"); | ||
const signale_1 = require("signale"); | ||
const logger = new signale_1.Signale(); | ||
const ajv = new ajv_1.default(); | ||
class CreateIconfontComponentController { | ||
isExistIconfontJson(pwd) { | ||
return fs.existsSync(path.resolve(pwd, this.configFileName)); | ||
} | ||
getIconfontJson(pwd) { | ||
return fs.readJSONSync(path.resolve(pwd, this.configFileName)); | ||
} | ||
checkIconfontJson(config) { | ||
const validator = ajv.compile(config_json_schema_1.default); | ||
const result = validator(config); | ||
return { result, error: validator.errors }; | ||
} | ||
constructor() { | ||
this.configFileName = "iconfont.json"; | ||
this.init(); | ||
} | ||
init() { | ||
if (this.isExistIconfontJson(process.cwd())) { | ||
logger.info("开始读取本地配置文件"); | ||
const config = this.getIconfontJson(process.cwd()); | ||
const checkResult = this.checkIconfontJson(config); | ||
if (checkResult.result) { | ||
createIconfont_1.default.createIconfont(config); | ||
} | ||
else { | ||
logger.error(checkResult.error); | ||
process.exit(0); | ||
} | ||
} | ||
else { | ||
logger.info("开始创建配置文件"); | ||
createConfig_1.default.config().then((config) => { | ||
createIconfont_1.default.createIconfont([config]); | ||
}); | ||
} | ||
} | ||
} | ||
else { | ||
console.log("不存在 iconfont.json"); | ||
} | ||
console.log(process.cwd()); | ||
exports.CreateIconfontComponentController = CreateIconfontComponentController; | ||
const cli = () => new CreateIconfontComponentController(); | ||
exports.cli = cli; |
101
package.json
{ | ||
"name": "create-iconfont-component", | ||
"version": "0.0.5", | ||
"description": "create-iconfont-component", | ||
"bin": { | ||
"create-iconfont-component": "bin/index.js" | ||
}, | ||
"files": [ | ||
"bin", | ||
"lib", | ||
"package.json", | ||
"*.md" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mu-kang/create-iconfont-component.git" | ||
}, | ||
"keywords": [ | ||
"iconfont", | ||
"iconfont-component", | ||
"create-iconfont" | ||
], | ||
"author": "mu-kang", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mu-kang/create-iconfont-component/issues" | ||
}, | ||
"homepage": "https://github.com/mu-kang/create-iconfont-component#readme", | ||
"main": "lib/index.js", | ||
"typings": "lib/index.d.ts", | ||
"scripts": { | ||
"dev": "tsc --build --watch", | ||
"build": "tsc --build" | ||
}, | ||
"peerDependencies": { | ||
"fs-extra": "^10.1.0" | ||
}, | ||
"dependencies": { | ||
"ajv": "^8.17.1", | ||
"axios": "^1.7.9", | ||
"chalk": "^4.1.2", | ||
"commander": "^9.2.0", | ||
"signale": "1.4.0" | ||
}, | ||
"devDependencies": { | ||
"@types/fs-extra": "*", | ||
"@types/node": "*" | ||
} | ||
"name": "create-iconfont-component", | ||
"version": "0.0.6", | ||
"description": "create-iconfont-component", | ||
"bin": { | ||
"create-iconfont-component": "bin/index.js" | ||
}, | ||
"files": [ | ||
"bin", | ||
"lib", | ||
"template", | ||
"package.json", | ||
"*.md" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mu-kang/create-iconfont-component.git" | ||
}, | ||
"keywords": [ | ||
"iconfont", | ||
"iconfont-component", | ||
"create-iconfont" | ||
], | ||
"author": "mu-kang", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mu-kang/create-iconfont-component/issues" | ||
}, | ||
"homepage": "https://github.com/mu-kang/create-iconfont-component#readme", | ||
"main": "lib/index.js", | ||
"typings": "lib/index.d.ts", | ||
"scripts": { | ||
"dev": "tsc --build --watch", | ||
"clean": "rimraf lib && rimraf tsconfig.tsbuildinfo", | ||
"build": "npm run clean && tsc --build" | ||
}, | ||
"peerDependencies": { | ||
"fs-extra": "^10.1.0" | ||
}, | ||
"dependencies": { | ||
"ajv": "^8.17.1", | ||
"axios": "^1.7.9", | ||
"chalk": "^4.1.2", | ||
"commander": "^9.5.0", | ||
"ejs": "^3.1.10", | ||
"inquirer": "^12.2.0", | ||
"signale": "1.4.0" | ||
}, | ||
"devDependencies": { | ||
"@types/ejs": "^3.1.5", | ||
"@types/fs-extra": "^11.0.4", | ||
"@types/node": "^22.10.2", | ||
"@types/signale": "^1.4.7", | ||
"rimraf": "^6.0.1" | ||
} | ||
} |
# create-iconfont-component | ||
create-iconfont-component | ||
本项目是根据 [Iconfont](https://www.iconfont.cn) 上的图标在项目中生成 `React` 或者 `Vue` 组件,方便大家使用。 | ||
## 使用方式一(推荐): | ||
1. 在项目根目录新增 `iconfont.json` 文件 | ||
```json | ||
[ | ||
{ | ||
// 项目名称 | ||
"projectName": "project", | ||
// iconfont 项目id | ||
"iconfontId": 000, | ||
// 项目使用框架 react | vue | ||
"projectType": "react", | ||
// 组件生成位置 相对位置 | ||
"componentPath": "./dist", | ||
// 项目使用的 语言 Typescript | JavaScript | ||
"projectLanguage": "Typescript", | ||
// 是否给 svg 添加 class | ||
"svgClass": "demo" | ||
} | ||
] | ||
``` | ||
2. 安装 `create-iconfont-component` | ||
```bash | ||
npm install -D create-iconfont-component | ||
``` | ||
3. 在项目 `package.json` 中新增 | ||
```json | ||
"scripts": { | ||
... | ||
"iconfont": "create-iconfont-component" | ||
}, | ||
``` | ||
## 使用方式二: | ||
1. 执行以下命令 | ||
```bash | ||
npx create-iconfont-component | ||
``` | ||
2. 按照提示填写信息 | ||
```text | ||
✔ 请输入项目名称 projecta | ||
✔ 请输入Iconfont项目Id 0 | ||
✔ 请选择项目应用框架 React | ||
✔ 项目开发语言 Typescript | ||
✔ 请输入组件生成位置 ./dist | ||
✔ 请输入svg className前缀 | ||
✔ 是否生成预览文件 是 | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
21
387
58
20279
8
5
+ Addedejs@^3.1.10
+ Addedinquirer@^12.2.0
+ Added@inquirer/checkbox@4.0.4(transitive)
+ Added@inquirer/confirm@5.1.1(transitive)
+ Added@inquirer/core@10.1.2(transitive)
+ Added@inquirer/editor@4.2.1(transitive)
+ Added@inquirer/expand@4.0.4(transitive)
+ Added@inquirer/figures@1.0.9(transitive)
+ Added@inquirer/input@4.1.1(transitive)
+ Added@inquirer/number@3.0.4(transitive)
+ Added@inquirer/password@4.0.4(transitive)
+ Added@inquirer/prompts@7.2.1(transitive)
+ Added@inquirer/rawlist@4.0.4(transitive)
+ Added@inquirer/search@3.0.4(transitive)
+ Added@inquirer/select@4.0.4(transitive)
+ Added@inquirer/type@3.0.2(transitive)
+ Added@types/node@22.10.5(transitive)
+ Addedansi-escapes@4.3.2(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addedasync@3.2.6(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.112.0.1(transitive)
+ Addedchardet@0.7.0(transitive)
+ Addedcli-width@4.1.0(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedejs@3.1.10(transitive)
+ Addedemoji-regex@8.0.0(transitive)
+ Addedexternal-editor@3.1.0(transitive)
+ Addedfilelist@1.0.4(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedinquirer@12.3.0(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedjake@10.9.2(transitive)
+ Addedminimatch@3.1.25.1.6(transitive)
+ Addedmute-stream@2.0.0(transitive)
+ Addedos-tmpdir@1.0.2(transitive)
+ Addedrun-async@3.0.0(transitive)
+ Addedrxjs@7.8.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsignal-exit@4.1.0(transitive)
+ Addedstring-width@4.2.3(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedtmp@0.0.33(transitive)
+ Addedtslib@2.8.1(transitive)
+ Addedtype-fest@0.21.3(transitive)
+ Addedundici-types@6.20.0(transitive)
+ Addedwrap-ansi@6.2.0(transitive)
+ Addedyoctocolors-cjs@2.1.2(transitive)
Updatedcommander@^9.5.0