提交 f035d04e65597518b122647ddd95d621e0bd77f8

作者 sorrycc
1 个父辈 6df46e04

chore: generate features

正在显示 1 个修改的文件 包含 36 行增加1 行删除
... ... @@ -2,6 +2,34 @@ const { readdirSync, readFileSync, writeFileSync, existsSync } = require('fs');
2 2 const { join, basename } = require('path');
3 3 const mkdirp = require('mkdirp');
4 4
  5 +function haveDependencies(pkg, depName) {
  6 + if (pkg.dependencies && pkg.dependencies[depName]) {
  7 + return true;
  8 + }
  9 + if (pkg.devDependencies && pkg.devDependencies[depName]) {
  10 + return true;
  11 + }
  12 + return false;
  13 +}
  14 +
  15 +const EXT_NAMES = ['.tsx', '.ts', '.jsx', '.js'];
  16 +
  17 +function getFile(cwd, fileName) {
  18 + for (const ext of EXT_NAMES) {
  19 + const file = join(cwd, `${fileName}${ext}`);
  20 + if (existsSync(file)) {
  21 + return file;
  22 + }
  23 + }
  24 +}
  25 +
  26 +function haveImport(cwd, name) {
  27 + const indexFile = getFile(cwd, 'src/index');
  28 + if (!indexFile) return false;
  29 + const content = readFileSync(indexFile, 'utf-8');
  30 + return content.includes(`'${name}'`) || content.includes(`"${name}"`);
  31 +}
  32 +
5 33 function parseJSON(root) {
6 34 const dirs = readdirSync(root);
7 35 const type = basename(root);
... ... @@ -11,6 +39,13 @@ function parseJSON(root) {
11 39 const pkg = require(join(absDirPath, 'package.json'));
12 40 const url = `https://github.com/umijs/umi-blocks/tree/master/${type}/${dir}`;
13 41 const img = `https://github.com/umijs/umi-blocks/blob/master/${type}/${dir}/snapshot.png?raw=true`;
  42 + const features = [];
  43 + if (haveDependencies(pkg, 'antd') || haveImport(absDirPath, 'antd')) {
  44 + features.push('antd');
  45 + }
  46 + if (getFile(absDirPath, 'src/model')) {
  47 + features.push('dva');
  48 + }
14 49 memo.push({
15 50 name: pkg.name,
16 51 description: pkg.description,
... ... @@ -18,7 +53,7 @@ function parseJSON(root) {
18 53 tags: [],
19 54 img,
20 55 previewUrl: '',
21   - features: [],
  56 + features,
22 57 });
23 58 return memo;
24 59 }, []);
... ...
注册登录 后发表评论