UNPKG

4.61 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const chalk = require('chalk');
4const request = require('request');
5const Table = require('tty-table');
6const ta = require('time-ago');
7
8exports = module.exports = (program) => {
9 request({
10 url: 'https://raw.githubusercontent.com/wepyjs/wepy-templates/2.0.x/meta.json',
11 headers: {
12 'User-Agent': 'wepy-cli'
13 }
14 }, (err, res, body) => {
15 if (!body) {
16 console.error('Something wrong with your network');
17 return;
18 }
19 if (body.message) {
20 console.error(body.messge);
21 return;
22 }
23 let official, github;
24 try {
25 body = JSON.parse(body);
26 official = body.official;
27 github = body.github;
28 } catch (e) {
29 console.error('Something wrong with your network');
30 }
31 if (!program.github && Array.isArray(official)) {
32 console.log('\n Available official templates:\n');
33 /*
34 official.forEach(repo => {
35
36 console.log(
37 ' ' + chalk.yellow('♚') +
38 ' ' + chalk.blue(repo.name) +
39 ' - ' + repo.description);
40 });
41 */
42 let tableHead = [
43 {
44 value: 'Name',
45 width: 20,
46 color: 'blue'
47 },
48 {
49 value: 'Description',
50 width: 60,
51 align: 'left',
52 paddingLeft: 2,
53 key: 'description'
54 }
55 ];
56 let rows = [];
57 official.forEach(repo => {
58 rows.push([repo.name, repo.description]);
59 });
60
61 let offical = Table(tableHead, rows, {
62 borderStyle: 2
63 });
64 console.log(` e.g., wepy init ${rows[0][0]} myproject`);
65 console.log(offical.render());
66 }
67 if (Array.isArray(github)) {
68 console.log('\n Available github projects:\n');
69
70 let tableHead = [
71 {
72 value: 'Repository',
73 width: 30,
74 color: 'blue',
75 key: 'repo'
76 },
77 {
78 value: 'Stars',
79 width: 8,
80 key: 'star'
81 },
82 {
83 value: 'Description',
84 width: 60,
85 align: 'left',
86 paddingLeft: 2,
87 key: 'description'
88 },
89 {
90 value: 'Last Updated',
91 width: 25,
92 key: 'last_update',
93 formatter: function (v) {
94 let date = new Date(v);
95 if (date.toString() === 'Invalid Date') {
96 return '----';
97 } else {
98 return ta.ago(v);
99 }
100 }
101 }
102 ];
103
104 let map = tableHead.map(v => v.key);
105
106 let showItems = [];
107 let rows = [];
108 let MAX_COUNT = program.github ? 0 : 5;
109 if (MAX_COUNT && github.length > MAX_COUNT) {
110 for (let i = 0, l = github.length; i < l; i++) {
111 if (i >= MAX_COUNT)
112 break;
113 showItems.push(github[i]);
114 }
115 } else {
116 showItems = github;
117 }
118 showItems.forEach(repo => {
119 let row = [];
120 map.forEach((title, i) => {
121 row.push(repo[title] || '');
122 });
123 rows.push(row);
124 });
125 if (MAX_COUNT && github.length > MAX_COUNT) {
126 rows.push(['....', '..', '....', '....']);
127 }
128
129 let githubTable = Table(tableHead, rows, {
130 borderStyle: 2
131 });
132 console.log(` e.g., wepy init ${rows[0][0]} myproject`);
133 console.log(githubTable.render());
134
135 if (MAX_COUNT && github.length > MAX_COUNT) {
136 console.log(chalk.gray(` use 'wepy list --github' to see all github projects`));
137 }
138 if (program.github) {
139 console.log(chalk.gray(` You can registe your project from here: https://github.com/wepyjs/wepy_templates`));
140 }
141 console.log('\n');
142 }
143 });
144}