UNPKG

4.64 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.findNpm = exports.checkUpdate = void 0;
4const path_1 = require("path");
5const fs_1 = require("fs");
6const child_process_1 = require("child_process");
7const os_1 = require("os");
8const checkUpdate = (npm) => {
9 const startTime = Date.now();
10 const lockFile = path_1.join(os_1.tmpdir(), 'faascliupdate.lock');
11 if (fs_1.existsSync(lockFile)) {
12 const content = +fs_1.readFileSync(lockFile).toString();
13 // 更新提示 24 小时
14 if (startTime - content < 24 * 3600000) {
15 return;
16 }
17 }
18 fs_1.writeFileSync(lockFile, `${startTime}`);
19 const { registry } = exports.findNpm({ npm });
20 try {
21 const data = child_process_1.execSync(`npm ${registry ? `--registry=${registry}` : ''} view @midwayjs/cli dist-tags --json`, {
22 cwd: process.env.HOME,
23 }).toString();
24 const remoteVersion = JSON.parse(data)['latest'];
25 const remoteVersionNumber = versionToNumber(remoteVersion);
26 const currentVersion = require('../package.json').version;
27 const currentVersionNumber = versionToNumber(currentVersion);
28 if (remoteVersionNumber > currentVersionNumber) {
29 console.log();
30 console.log('*********************************************************');
31 console.log();
32 console.log(' find new version:');
33 console.log(` ${currentVersion} ==> ${remoteVersion}`);
34 console.log();
35 console.log(' please reinstall @midwayjs/cli module to update.');
36 console.log();
37 console.log(' npm i @midwayjs/cli -g');
38 console.log();
39 console.log('*********************************************************');
40 console.log();
41 }
42 }
43 catch (err) {
44 console.log('[ Midway ] check update error and skip', err.message);
45 }
46};
47exports.checkUpdate = checkUpdate;
48const versionToNumber = version => {
49 if (!version) {
50 return;
51 }
52 const versionList = version.split('.');
53 return ((versionList[0] || 0) * 10e6 +
54 (versionList[1] || 0) * 10e3 +
55 (versionList[2] || 0) * 1);
56};
57const findNpm = (argv) => {
58 let npm = 'npm';
59 let registry = '';
60 // 先找npm客户端
61 if (argv === null || argv === void 0 ? void 0 : argv.npm) {
62 npm = argv.npm;
63 }
64 else if (process.env.npm_config_user_agent &&
65 /yarn/.test(process.env.npm_config_user_agent)) {
66 npm = 'yarn';
67 }
68 else if (process.env.npm_execpath &&
69 /yarn/.test(process.env.npm_execpath)) {
70 npm = 'yarn';
71 }
72 else if (process.env.yarn_registry) {
73 npm = 'yarn';
74 }
75 else {
76 const npmList = ['cnpm'];
77 const currentPlatform = os_1.platform();
78 const cmd = npmList.find(cmd => {
79 if (currentPlatform === 'win32') {
80 if (cmd === 'cnpm') {
81 return;
82 }
83 // for windows
84 try {
85 const find = child_process_1.execSync(`where ${cmd}`).toString();
86 // windows的命令路径至少会有 C/D/E:\ 前缀
87 if (find.indexOf(':\\') !== -1) {
88 return cmd;
89 }
90 }
91 catch (_a) {
92 //
93 }
94 }
95 else {
96 // for mac/linux
97 try {
98 const find = child_process_1.execSync(`which ${cmd}`).toString();
99 // 没有找到not found
100 if (find.indexOf('not found') === -1) {
101 return cmd;
102 }
103 }
104 catch (_b) {
105 //
106 }
107 }
108 });
109 if (cmd) {
110 npm = cmd;
111 }
112 }
113 // registry
114 if ((argv === null || argv === void 0 ? void 0 : argv.registry) !== undefined) {
115 registry = argv.registry || '';
116 }
117 else if (npm === 'yarn' && process.env.yarn_registry) {
118 registry = process.env.yarn_registry;
119 }
120 else if (process.env.npm_config_registry) {
121 registry = process.env.npm_config_registry;
122 }
123 else {
124 // language is zh_CN
125 if (process.env.LANG === 'zh_CN.UTF-8') {
126 registry = 'https://registry.npm.taobao.org';
127 }
128 }
129 return {
130 cmd: `${npm}${registry ? ` --registry=${registry}` : ''}`,
131 npm,
132 registry,
133 };
134};
135exports.findNpm = findNpm;
136//# sourceMappingURL=utils.js.map
\No newline at end of file