1 | const path = require('path');
|
2 | const fs = require('fs')
|
3 | const shelljs = require('shelljs');
|
4 | const debug = require('debug')('debug:util')
|
5 | const readPkgUp = require('read-pkg-up');
|
6 | const log = require('./util/log');
|
7 | const boxen = require('boxen');
|
8 |
|
9 | const config = require('../bin/config');
|
10 | const WHITE_LIST = config.WHITE_LIST;
|
11 | const PLUGIN_REGEXP = new RegExp(config.PLUGIN_REGEXP);
|
12 |
|
13 | const getPluginNameAndPath = (root) => {
|
14 | const list = [];
|
15 | let filePath = '';
|
16 | const pushPlugin = (name, filePath) => {
|
17 | if (isPluginName(name) && fs.existsSync(filePath)) {
|
18 | list.push({
|
19 | name,
|
20 | path: filePath
|
21 | });
|
22 | }
|
23 | }
|
24 |
|
25 | fs.readdirSync(root).forEach(name => {
|
26 | if (WHITE_LIST.indexOf(name) === -1) {
|
27 | filePath = path.join(root, name, './index.js');
|
28 | pushPlugin(name, filePath);
|
29 | } else {
|
30 | const depthRoot = path.join(root, name);
|
31 | fs.readdirSync(depthRoot).forEach(subName => {
|
32 | filePath = path.join(depthRoot, subName, './index.js');
|
33 | pushPlugin([name, subName].join('/'), filePath);
|
34 | })
|
35 | }
|
36 | })
|
37 |
|
38 | return list;
|
39 | }
|
40 |
|
41 | const isPluginName = (name = '') => {
|
42 | return PLUGIN_REGEXP.test(name)
|
43 | }
|
44 |
|
45 | const getGitUserInfo = () => {
|
46 | const username = shelljs.exec('git config --get user.name', {
|
47 | silent: true
|
48 | }).output.trim();
|
49 | const email = shelljs.exec('git config --get user.email', {
|
50 | silent: true
|
51 | }).output.trim();
|
52 |
|
53 | return {
|
54 | username,
|
55 | email
|
56 | }
|
57 | }
|
58 |
|
59 |
|
60 | const getAbcJson = (path) => {
|
61 | let abc = {};
|
62 | try {
|
63 | abc = fs.existsSync(path) ? JSON.parse(fs.readFileSync(path, 'utf8')) : {};
|
64 | } catch(err) {
|
65 | log.error(err);
|
66 | process.exit(1);
|
67 | }
|
68 |
|
69 | return abc;
|
70 |
|
71 | }
|
72 |
|
73 | const getPackageInfo = async (cwdPath) => {
|
74 | try {
|
75 | return await readPkgUp({
|
76 | cwd: cwdPath
|
77 | })
|
78 | } catch(err) {
|
79 | return null;
|
80 | }
|
81 |
|
82 | }
|
83 |
|
84 | const publishDirBoxed = (publishDir) => {
|
85 | console.log(boxen(
|
86 | `
|
87 | Your Publish Page Address:
|
88 |
|
89 | ${publishDir}
|
90 | `,
|
91 | {
|
92 | borderColor: 'blue',
|
93 | padding: {
|
94 | left: 2,
|
95 | right: 2
|
96 | }
|
97 | }))
|
98 | }
|
99 |
|
100 | const getNpmRoot = () => {
|
101 | const npmRoot = shelljs.exec('npm root -g', {
|
102 | silent: true
|
103 | }).stdout;
|
104 |
|
105 | return npmRoot.trim();
|
106 | }
|
107 |
|
108 |
|
109 | module.exports = {
|
110 | isPluginName,
|
111 | getPluginNameAndPath,
|
112 | getGitUserInfo,
|
113 | getAbcJson,
|
114 | getPackageInfo,
|
115 | publishDirBoxed,
|
116 | getNpmRoot
|
117 | } |
\ | No newline at end of file |