UNPKG

8.24 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11const path = require("path");
12const child_process_1 = require("child_process");
13const getLatestVersion = require("latest-version");
14const semver = require("semver");
15const ora = require("ora");
16const util_1 = require("../../util");
17exports.default = (ctx) => {
18 ctx.registerCommand({
19 name: 'update',
20 fn() {
21 return __awaiter(this, void 0, void 0, function* () {
22 const { appPath, configPath } = ctx.paths;
23 const { chalk, fs, shouldUseCnpm, shouldUseYarn, PROJECT_CONFIG, UPDATE_PACKAGE_LIST } = ctx.helper;
24 const { version, updateType } = ctx.runOpts;
25 const pkgPath = path.join(appPath, 'package.json');
26 const pkgName = util_1.getPkgItemByKey('name');
27 function getTargetVersion() {
28 return __awaiter(this, void 0, void 0, function* () {
29 let targetTaroVersion;
30 // base on current project taro versions, not base on @tarojs/cli verison
31 const currentTaroVersion = require(pkgPath).dependencies['@tarojs/taro'];
32 if (version) {
33 targetTaroVersion = semver.clean(version);
34 }
35 else {
36 // update to current lastest major.x.x
37 try {
38 targetTaroVersion = yield getLatestVersion(pkgName, {
39 version: semver.major(currentTaroVersion).toString()
40 });
41 }
42 catch (e) {
43 targetTaroVersion = yield getLatestVersion(pkgName);
44 }
45 }
46 if (!semver.valid(targetTaroVersion)) {
47 console.log(chalk.red('命令错误:无效的 version ~'));
48 throw Error('无效的 version!');
49 }
50 return targetTaroVersion;
51 });
52 }
53 function info() {
54 console.log(chalk.red('命令错误:'));
55 console.log(`${chalk.green('taro update self [version]')} 更新 Taro 开发工具 taro-cli 到指定版本或当前主版本的最新版本`);
56 console.log(`${chalk.green('taro update project [version]')} 更新项目所有 Taro 相关依赖到指定版本或当前主版本的最新版本`);
57 }
58 function updateSelf() {
59 return __awaiter(this, void 0, void 0, function* () {
60 let command;
61 const targetTaroVersion = yield getTargetVersion();
62 if (shouldUseCnpm()) {
63 command = `cnpm i -g @tarojs/cli@${targetTaroVersion}`;
64 }
65 else {
66 command = `npm i -g @tarojs/cli@${targetTaroVersion}`;
67 }
68 const child = child_process_1.exec(command);
69 const spinner = ora('即将将 Taro 开发工具 taro-cli 更新到最新版本...').start();
70 child.stdout.on('data', function (data) {
71 console.log(data);
72 spinner.stop();
73 });
74 child.stderr.on('data', function (data) {
75 console.log(data);
76 spinner.stop();
77 });
78 });
79 }
80 function updateProject() {
81 return __awaiter(this, void 0, void 0, function* () {
82 if (!configPath || !fs.existsSync(configPath)) {
83 console.log(chalk.red(`找不到项目配置文件${PROJECT_CONFIG},请确定当前目录是Taro项目根目录!`));
84 process.exit(1);
85 }
86 const packageMap = require(pkgPath);
87 const version = yield getTargetVersion();
88 // 获取 NervJS 版本
89 const nervJSVersion = `^${yield getLatestVersion('nervjs')}`;
90 // 更新 @tarojs/* 版本和 NervJS 版本
91 Object.keys(packageMap.dependencies).forEach((key) => {
92 if (UPDATE_PACKAGE_LIST.indexOf(key) !== -1) {
93 if (key.includes('nerv')) {
94 packageMap.dependencies[key] = nervJSVersion;
95 }
96 else if (key.includes('react-native')) {
97 // delete old version react-native,and will update when run taro build
98 delete packageMap.dependencies[key];
99 }
100 else {
101 packageMap.dependencies[key] = version;
102 }
103 }
104 });
105 Object.keys(packageMap.devDependencies).forEach((key) => {
106 if (UPDATE_PACKAGE_LIST.indexOf(key) !== -1) {
107 if (key.includes('nerv')) {
108 packageMap.devDependencies[key] = nervJSVersion;
109 }
110 else {
111 packageMap.devDependencies[key] = version;
112 }
113 }
114 });
115 // 写入package.json
116 try {
117 yield fs.writeJson(pkgPath, packageMap, { spaces: '\t' });
118 console.log(chalk.green('更新项目 package.json 成功!'));
119 console.log();
120 }
121 catch (err) {
122 console.error(err);
123 }
124 let command;
125 if (shouldUseYarn()) {
126 command = 'yarn';
127 }
128 else if (shouldUseCnpm()) {
129 command = 'cnpm install';
130 }
131 else {
132 command = 'npm install';
133 }
134 const child = child_process_1.exec(command);
135 const spinner = ora('即将将项目所有 Taro 相关依赖更新到最新版本...').start();
136 child.stdout.on('data', function (data) {
137 spinner.stop();
138 console.log(data);
139 });
140 child.stderr.on('data', function (data) {
141 spinner.stop();
142 console.log(data);
143 });
144 });
145 }
146 if (!updateType)
147 return info();
148 if (updateType === 'self')
149 return updateSelf();
150 if (updateType === 'project')
151 return updateProject();
152 info();
153 });
154 }
155 });
156};