1 | import commandLineUsage from 'command-line-usage';
|
2 | import { execSync } from 'child_process';
|
3 | import { getRegistryUrl } from '../../shared/npm';
|
4 | import axios from 'axios';
|
5 |
|
6 | module.exports = (ctx: any) => {
|
7 |
|
8 | async function showToolVersion() {
|
9 |
|
10 | const sections = [
|
11 | {
|
12 | header: 'Your environment information',
|
13 | content: 'Show something very important.'
|
14 | },
|
15 | {
|
16 | header: 'Tools Version',
|
17 | optionList: [
|
18 | {
|
19 | name: 'node ',
|
20 | typeLabel: '{underline Version:}',
|
21 | description: executeSync('node -v')
|
22 | },
|
23 | {
|
24 | name: 'npm ',
|
25 | typeLabel: '{underline Version:}',
|
26 | description: executeSync('npm -v')
|
27 | },
|
28 | {
|
29 | name: 'tnpm ',
|
30 | typeLabel: '{underline Version:}',
|
31 | description: executeSync('tnpm -v')
|
32 | },
|
33 | {
|
34 | name: 'fef ',
|
35 | typeLabel: '{underline Version:}',
|
36 | description: executeSync('fef -v')
|
37 | },
|
38 | {
|
39 | name: 'Python ',
|
40 | typeLabel: '{underline Version:}',
|
41 | description: executeSync('python -c "import platform; print(platform.python_version())"')
|
42 | }
|
43 | ]
|
44 | },
|
45 | {
|
46 | header: 'Proxy config info',
|
47 | optionList: [
|
48 | {
|
49 | name: 'http_proxy ',
|
50 | typeLabel: '{underline info:}',
|
51 | description: executeSync('echo $http_proxy')
|
52 | },
|
53 | {
|
54 | name: 'npm_config_proxy ',
|
55 | typeLabel: '{underline info:}',
|
56 | description: executeSync('npm config get proxy')
|
57 | },
|
58 | {
|
59 | name: 'npm_config_registry ',
|
60 | typeLabel: '{underline info:}',
|
61 | description: executeSync('npm config get registry')
|
62 | },
|
63 | {
|
64 | name: 'npm user_config path ',
|
65 | typeLabel: '{underline info:}',
|
66 | description: executeSync('npm config get userconfig')
|
67 | },
|
68 | {
|
69 | name: 'iOA pac ',
|
70 | typeLabel: '{underline info:}',
|
71 | description: '@TODO'
|
72 | }
|
73 | ]
|
74 | },
|
75 | {
|
76 | header: 'Network access info',
|
77 | optionList: [
|
78 | {
|
79 | name: 'curl npm_config_registry ',
|
80 | typeLabel: '{underline info:}',
|
81 | description: await accessTnpmRegistry()
|
82 | }
|
83 | ]
|
84 | },
|
85 | ];
|
86 | const result = commandLineUsage(sections);
|
87 | return result;
|
88 | }
|
89 |
|
90 | function executeSync(command: string): string {
|
91 | let resultBuf: Buffer;
|
92 | try {
|
93 | resultBuf = execSync(command, { windowsHide: true });
|
94 | } catch(e) {
|
95 | return e.message;
|
96 | }
|
97 |
|
98 | const result = resultBuf.toString("utf8").trim();
|
99 | return result;
|
100 | };
|
101 |
|
102 | async function accessTnpmRegistry() {
|
103 | let tnpmRegistry = await getRegistryUrl('tnpm');
|
104 | tnpmRegistry = tnpmRegistry.trim().split('\n');
|
105 | tnpmRegistry = tnpmRegistry[tnpmRegistry.length-1];
|
106 |
|
107 | try {
|
108 | const response = await axios.get(tnpmRegistry);
|
109 |
|
110 | if(response.status === 200) {
|
111 | return 'access tnpm registry is ok!';
|
112 | } else {
|
113 | return 'access tnpm registry has error, http code: ${response.statusCode}';
|
114 | }
|
115 | } catch (error) {
|
116 | return 'access tnpm registry has error: ${error}';
|
117 | }
|
118 |
|
119 | }
|
120 |
|
121 | ctx.commander.register('doctor', 'environment information', () => {
|
122 | showToolVersion().then(result => {
|
123 | console.log(result);
|
124 | }).catch(error => {
|
125 | console.log('error:', error);
|
126 | });
|
127 | });
|
128 | }; |
\ | No newline at end of file |