UNPKG

5.76 kBPlain TextView Raw
1#!/usr/bin/env node
2
3'use strict';
4
5const co = require('co');
6const chalk = require('chalk');
7const execSync = require('child_process').execSync;
8const exec = require('child_process').exec;
9const pkgFile = require('../package.json');
10const thunkify = require("thunkify");
11const tcpp = require('tcp-ping');
12const request = require('request');
13const ora = require('ora');
14const fs = require('fs');
15const path = require('path');
16const http = require('http');
17const ini = require('ini');
18
19
20/*
21// Nexus OSS 2.x Info
22// Npm official mirror: https://registry.npmjs.org
23const IPCOMPANY = '172.16.51.12';
24const YON_MIRROR = 'http://maven.yonyou.com/nexus/content/groups/NPM-Yonyou-Repository/';
25*/
26
27// Nexus OSS 3.3 Info
28const IPCOMPANY = '172.20.27.204';
29const YON_MIRROR = 'http://172.20.27.204:8081/repository/ynpm-all/';
30const DEAFAULT_MIRROR = 'https://registry.npm.taobao.org';
31const HOST_REGISTRY = 'http://172.20.27.204:8081/repository/ynpm-private/';
32const CDNJSON = 'http://iuap-design-cdn.oss-cn-beijing.aliyuncs.com/static/ynpm/ynpm.json'
33
34/**
35 * ynpm --version || ynpm -v
36 */
37const opt = process.argv[2];
38if (opt == '-v' || opt == '--version') {
39 console.log('Yonyou Package Manager(ynpm) : v' + pkgFile.version);
40 process.exit(0);
41}
42
43/**
44 * ynpm --help || ynpm -h
45 */
46if (opt == '-h' || opt == '--help' || opt == undefined) {
47 console.log(
48 `
49 Usage:
50 ----------------------------------------------------
51 ynpm install
52 ynpm install <pkg>
53 ynpm install <pkg>@<tag>
54 ynpm install <pkg>@<version>
55 ynpm install <pkg>@<version range>
56 ynpm install <folder>
57 ynpm install <tarball file>
58 ynpm install <tarball url>
59 ynpm install <git:// url>
60 ynpm install <github username>/<github project>
61
62 Options:
63 ----------------------------------------------------
64 --save, -S, --save-dev, -D: save installed dependencies into package.json
65 -g, --global: install devDependencies to global directory
66
67 Others:
68 ----------------------------------------------------
69 ynpm --registry: change default mirror address
70
71 `
72 );
73 process.exit(0);
74}
75
76const IP_Req = thunkify(request);
77const Ping = thunkify(tcpp.ping);
78const Exec = thunkify(exec);
79
80co(function* () {
81 //IP判断 - 耗时暂取消
82 /*
83 const IP_Response = yield IP_Req('https://api.ipify.org?format=json')
84 const IP_Body = IP_Response[IP_Response.length - 1]
85 const IP_ADDRESS = JSON.parse(IP_Body)['ip'];
86 const IP_REG = new RegExp(/^123\.103/);
87 if(IP_REG.test(IP_ADDRESS)){
88 console.log('内网')
89 }
90 */
91
92 // Ping内网
93 const Ping_Response = yield Ping({
94 address: IPCOMPANY,
95 port: 8081,
96 timeout: 50,
97 attempts: 1
98 })
99 let registry = Ping_Response.avg ? YON_MIRROR : DEAFAULT_MIRROR;
100
101 const argvs = process.argv;
102 const spinner = ora().start();
103 spinner.color = 'green';
104
105 let arg_install = `npm --registry=${registry} `;
106 if (argvs[2] == 'i' || argvs[2] == 'install') {
107 if (Ping_Response.avg) {
108 console.log(chalk.dim('Yonyou Mirror Downloading...\n'));
109 } else {
110 console.log(chalk.dim('CNPM Mirror Downloading...\n'));
111 }
112
113 const argv_part = argvs.slice(2).join(' ');
114 arg_install += argv_part;
115
116 // execSync(arg_install);
117 spinner.text = 'Installing package ⬇️...';
118 var data = yield Exec(arg_install);
119 console.log(chalk.bold('\n\nInstall Info:\n' + data[0]));
120 console.log(chalk.yellow('Warn Info:\n' + data[1]));
121 console.log(chalk.green(`√ Finish, Happy enjoy coding!`));
122
123 } else if (argvs[2] == 'publish' && argvs[3] == 'inner') {
124 // Get Publish Package Info
125 var packOrigin = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'))).name;
126 var packName = packOrigin.split('/')[0].replace("@", "");
127
128 // Get Data
129 var cdnRes = yield IP_Req(CDNJSON);
130 var jsonRes = JSON.parse(cdnRes[cdnRes.length - 1]);
131 console.log("=====jsonRes=====", jsonRes);
132 // Get User Info - using offical method - ini
133 var _auth;
134 var npmConfigReturn = yield Exec('npm get userconfig');
135 // npmConfigReturn: [ '/Users/AYA/.npmrc\n', '' ]
136 var npmUserConfig = npmConfigReturn[0].trim();
137 console.log("=====npmUserConfig=====", npmUserConfig);
138 var iniConfig = ini.parse(fs.readFileSync(npmUserConfig, 'utf-8'))
139 var parseAuth = new Buffer(iniConfig._auth, 'base64').toString().split(":")[0];
140
141 // Verify Publish Scoped
142 console.log(parseAuth + "==========");
143 if (jsonRes[parseAuth] && jsonRes[parseAuth].includes(packName)) {
144 console.log('Aviable: Pass Validation, Start to Publish...')
145 var arg_publish_inner = `npm --registry=${HOST_REGISTRY} publish`;
146 console.log("--arg_publish_inner-- : " + arg_publish_inner);
147 spinner.text = 'Publishing your package in Yonyou Local Area Net';
148 console.log("arg_publish_inner:" + arg_publish_inner);
149 // var data = yield Exec(arg_publish_inner);
150 } else if (jsonRes[parseAuth]) {
151 console.error(`Error: Overflow User Privilege, Publish Package Scoped with "@${jsonRes[parseAuth]}" or Contact Admin to Extend Privilege!`);
152 } else {
153 console.error("Error: Cant Find User, Please Use `npm config set _auth=base64String` or Contact Admin to Create User!");
154 }
155
156 } else if (argvs[2] == 'publish' && !argvs[3]) {
157 var arg_publish = `npm publish`;
158 spinner.text = 'Publishing your package on NPM Official Repos';
159 var data = yield Exec(arg_publish);
160
161 }
162 spinner.stop();
163 process.exit(0);
164
165
166}).catch(err => {
167 console.error(chalk.red('\n' + err));
168 // console.error(chalk.red(err.stack));
169 console.error(chalk.yellow('ynpm version: %s'), pkgFile.version);
170 console.error(chalk.yellow('ynpm args: %s'), process.argv.slice(2).join(' '));
171 process.exit(1);
172});