Skip to content
This repository was archived by the owner on Jun 3, 2021. It is now read-only.

Commit 2b7d7d7

Browse files
committed
update repair logic
1 parent 8379e4c commit 2b7d7d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2071
-1749
lines changed

.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
.vscode/
2-
31
node_modules/
42

53
# coverage

.vscode/launch.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceFolder}/bin/weex.js",
12+
"args": [
13+
"debug"
14+
],
15+
"env": {
16+
"WEEX_CORE_PATH": "/Users/kw/github/weex-toolkit/packages/@weex/core"
17+
}
18+
}
19+
]
20+
}

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "node_modules/typescript/lib"
3+
}

lib/cli.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ const cli = async (processArgv) => {
105105
(new Cli(coreData)).start()
106106
}
107107
catch(error) {
108-
console.log(error, 555)
109108
// await prepare(repair)
110109
}
111110
}
@@ -127,18 +126,32 @@ const prepare = async (command, config) => {
127126
}
128127

129128
if (command === 'repair') {
130-
const repairModule = argv.first;
129+
const repairModule = argv._[1];
131130
if (repairModule) {
132131
const arg = repairModule.split('@');
133-
coreName = arg[0] || config.coreName;
134-
coreVersion = arg[1] || 'latest';
132+
if (arg.length > 1) {
133+
coreVersion = arg.pop();
134+
coreName = arg.join('@');
135+
136+
}
137+
else {
138+
coreName = arg;
139+
coreVersion = 'latest';
140+
}
135141
}
136142
else {
143+
coreName = config.coreName
137144
coreVersion = 'latest'
138145
}
139-
needInstall = true
140-
debug(`start repair ${config.coreName}`)
141-
console.log(colors.yellow(`Start repair ${config.coreName}, please wait ...`));
146+
// If repair module isn't the core module, pipe argv to the @weex-cli/core
147+
if (coreName !== config.coreName) {
148+
needInstall = false;
149+
}
150+
else {
151+
needInstall = true;
152+
debug(`start repair ${config.coreName}`)
153+
console.log(colors.yellow(`Start repair ${config.coreName}, please wait ...`));
154+
}
142155
}
143156
else {
144157
// checking if there has weex-cli/core
@@ -184,6 +197,7 @@ const prepare = async (command, config) => {
184197
force: argv.force || argv.f,
185198
registry: config.registry
186199
});
200+
return ;
187201
}
188202
}
189203

lib/trash.js

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const path = require('path');
44
const spawn = require('cross-spawn');
55

66
module.exports = function(file) {
7-
console.log(process.execPath, path.join(__dirname, '_trash.js'))
87
try {
98
spawn(
109
process.execPath,

lib/utils.js

-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ const _install = async (name, version, opts) => {
118118
debug('use move');
119119
const trashPath = opts.trash + '_core_' + t;
120120
await fse.move(realDir, trashPath);
121-
console.log(trashPath)
122121
require('./trash')(trashPath);
123122
}
124123
}

packages/@weex/core/src/cli/cli.ts

+149-76
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,9 @@
1-
import { build, fs, IParameters, http, install } from '../index'
1+
import { build, fs, IParameters, http, install, logger, strings } from '../index'
22
import { parseParams } from '../toolbox/parameter-tools'
33
import * as path from 'path'
44

55
const debug = require('debug')('weex:core')
66

7-
// export class Cli {
8-
// private cli: any;
9-
// constructor(options: {
10-
// brand?: string
11-
// help?: any
12-
// defaultCommand?: any
13-
// version?: any
14-
// plugin?: {
15-
// value?: string
16-
// options?: any
17-
// }
18-
// plugins?: {
19-
// value?: string
20-
// options?: any
21-
// }
22-
// exclude?: string[]
23-
// } = {}) {
24-
// // create a CLI runtime
25-
// this.cli = build(options.brand || 'weex')
26-
// .src(__dirname)
27-
// .help(options.help)
28-
// .version(options.version)
29-
// .exclude(options.exclude)
30-
// .defaultCommand(options.defaultCommand);
31-
32-
// if (options.plugins && options.plugins.value) {
33-
// this.cli = this.cli.plugins(options.plugins.value, options.plugins.options)
34-
// }
35-
36-
// if (options.plugin) {
37-
// if (Array.isArray(options.plugin)) {
38-
// options.plugin.forEach(p => {
39-
// this.cli = this.cli.plugin(p.value, p.options)
40-
// })
41-
// }
42-
// else {
43-
// this.cli = this.cli.plugin(options.plugin.value, options.plugin.options)
44-
// }
45-
// }
46-
// }
47-
48-
// async start(argv?: string[] | string) {
49-
// const params = parseParams(argv);
50-
// // run the cli
51-
// const toolbox = await this.cli.run(argv)
52-
// // send it back (for testing, mostly)
53-
// return toolbox
54-
// }
55-
// }
567
enum ModType {
578
PLUGIN,
589
EXTENSION,
@@ -150,23 +101,40 @@ export class Cli {
150101
}
151102

152103
async start() {
153-
// run the cli
154-
// const toolbox = await this.cli.run(this.rawArgv)
155-
// // send it back (for testing, mostly)
156-
// return toolbox
157104
const command = this.argv.array[0]
158-
159105
if (this.cliOptions.modules) {
160106
this.plugins = this.pickPlugins(this.cliOptions.modules)
161107
} else {
162-
fs.file(path.join(this.cliOptions.moduleRoot, this.cliOptions.moduleName), {
163-
mode: '777',
164-
jsonIndent: 2,
165-
content: JSON.stringify({ mods: {}, last_update_time: new Date().getTime() }),
166-
})
108+
fs.write(path.join(this.cliOptions.moduleRoot, this.cliOptions.moduleName), { mods: {}, last_update_time: new Date().getTime() })
167109
}
110+
if (command === 'repair') {
111+
debug(`Do repair`);
112+
const repairModule = this.argv.array[1];
113+
let repairName;
114+
let repairVersion;
115+
if (repairModule) {
116+
const args = repairModule.split('@')
117+
if (repairModule.slice(0,1) === '@') {
118+
119+
}
120+
else {
168121

169-
if (command) {
122+
}
123+
try {
124+
await this.repairPackage(repairModule);
125+
debug(`repair ${repairModule} successed!`);
126+
logger.success(`\nRepair ${repairModule} successed!`)
127+
}
128+
catch(e) {
129+
this.analyzer('repair', e.stack, {module: repairModule})
130+
}
131+
}
132+
else {
133+
logger.error('Need to specify the repaired module')
134+
}
135+
return ;
136+
}
137+
else if (command) {
170138
const plugin = this.searchPlugin(command, this.plugins)
171139
let commands = []
172140
let type = ModType.EXTENSION
@@ -175,7 +143,6 @@ export class Cli {
175143
if (!res.error) {
176144
const packages: any = await this.installPackage(`@weex-cli/${command}`, 'latest', {
177145
root: this.cliOptions.moduleRoot,
178-
trash: this.cliOptions.trash,
179146
registry: this.cliOptions.registry,
180147
})
181148
for (let i = 0; i < packages.length; i++) {
@@ -216,11 +183,7 @@ export class Cli {
216183
}
217184
}
218185
// update module file
219-
fs.file(path.join(this.cliOptions.moduleRoot, this.cliOptions.moduleName), {
220-
mode: '777',
221-
jsonIndent: 2,
222-
content: JSON.stringify({ mods: this.cliOptions.modules.mods, last_update_time: new Date().getTime() }),
223-
})
186+
fs.write(path.join(this.cliOptions.moduleRoot, this.cliOptions.moduleName), { mods: this.cliOptions.modules.mods, last_update_time: new Date().getTime() })
224187
}
225188
}
226189
if (this.plugins.length > 0) {
@@ -229,13 +192,83 @@ export class Cli {
229192
})
230193
}
231194
}
195+
232196
// run the cli
233197
const toolbox = await this.cli.create().run(this.rawArgv)
234198
// send it back (for testing, mostly)
235199
return toolbox
236200
}
237201

238-
async installPackage(name, version, options, result = []) {
202+
async repairPackage(mod: string) {
203+
let modVersion;
204+
let modName;
205+
let commands = [];
206+
let type = ModType.EXTENSION;
207+
const first = mod.slice(0,1);
208+
// check for origin npm package
209+
if (first === '@') {
210+
const arg = mod.split('@');
211+
if (arg.length > 2) {
212+
modVersion = arg.pop();
213+
modName = arg.join('@');
214+
}
215+
else {
216+
modName = arg.join('@');
217+
modVersion = 'latest';
218+
}
219+
}
220+
else {
221+
const arg = mod.split('@');
222+
if (arg.length > 1) {
223+
modVersion = arg.pop();
224+
modName = arg.join('@');
225+
}
226+
else {
227+
modName = arg[0];
228+
modVersion = 'latest';
229+
}
230+
}
231+
const packages: any = await this.installPackage(modName, modVersion, {
232+
root: this.cliOptions.moduleRoot,
233+
registry: this.cliOptions.registry,
234+
})
235+
for (let i = 0; i < packages.length; i++) {
236+
const commandBasePath = path.join(packages[i].root, 'commands')
237+
const commandFiles: string[] = fs.list(commandBasePath) || []
238+
commandFiles.forEach(file => {
239+
let content
240+
try {
241+
content = require(path.join(commandBasePath, file))
242+
} catch (e) {
243+
debug(`Check module error with: ${e.stack}`)
244+
// try prev version
245+
}
246+
commands.push({
247+
name: content.name || '',
248+
alias: content.alias || '',
249+
showed: typeof content.dashed === 'boolean' ? !content.dashed : true,
250+
description: content.description || '',
251+
})
252+
type = ModType.PLUGIN
253+
})
254+
if (commands.length > 0) {
255+
this.cliOptions.modules.mods[packages[i].package.name] = {
256+
type: type,
257+
version: packages[i].package.version,
258+
next_version: '',
259+
is_next: true,
260+
changelog: packages[i].changelog || '',
261+
local: packages[i].root,
262+
commands: commands,
263+
}
264+
}
265+
}
266+
debug(`save modjson: ${JSON.stringify(this.cliOptions.modules.mods)}`)
267+
// update module file
268+
fs.write(path.join(this.cliOptions.moduleRoot, this.cliOptions.moduleName), { mods: this.cliOptions.modules.mods, last_update_time: new Date().getTime() })
269+
}
270+
271+
async installPackage(name: string, version: string, options: any, result:any = []) {
239272
const info: any = await install(name, version || 'latest', options)
240273
if (Array.isArray(info.package.extensionDependencies)) {
241274
let len = info.package.extensionDependencies.length
@@ -247,14 +280,48 @@ export class Cli {
247280
return [info]
248281
}
249282

250-
async suggestPackage(command, registry) {
283+
async suggestPackage(command: string, registry: string) {
251284
const npmApi = http.create({
252285
baseURL: `${registry}/@weex-cli/`,
253286
})
254287
const res = await npmApi.get(`${command}`)
255288
return res.data
256289
}
257290

291+
async analyzer(type: string, stack: string, options?: any) {
292+
if (type === 'repair') {
293+
if (/404 status/.test(stack)) {
294+
const innerMods = [
295+
'@weex-cli/debug',
296+
'@weex-cli/generator',
297+
'@weex-cli/build',
298+
'@weex-cli/preview'
299+
]
300+
let score;
301+
let tempScore;
302+
let suggestName;
303+
innerMods.forEach(mod => {
304+
tempScore = strings.strSimilarity2Number(mod, options.module);
305+
if (!score) {
306+
score = tempScore;
307+
suggestName = mod;
308+
}
309+
else if (tempScore < score) {
310+
score = tempScore;
311+
suggestName = mod;
312+
}
313+
})
314+
logger.warn(`Module "${options.module}" not found, do you mean "${suggestName}"?`)
315+
}
316+
}
317+
else {
318+
const logPath = path.join(process.cwd(), '.weex-error.log')
319+
fs.write(logPath, stack);
320+
logger.warn(`Unkown issue, see error stack on logPath.`)
321+
logger.warn(`To fix this, you can create a issue on https://github.com/weexteam/weex-toolkit/issues.`)
322+
}
323+
}
324+
258325
pickPlugins(modules: ModData): PluginItem[] {
259326
if (!modules) return []
260327
let plugins = []
@@ -271,14 +338,20 @@ export class Cli {
271338
}
272339

273340
searchPlugin(command: string, mods: PluginItem[]): PluginItem | boolean {
274-
mods.forEach((mod: PluginItem) => {
275-
mod.commands.forEach(cmd => {
276-
if (cmd.name === command || cmd.alias === command) {
277-
return mod
278-
}
341+
if (mods.length > 0) {
342+
let result
343+
mods.forEach((mod: PluginItem) => {
344+
mod.commands.forEach(cmd => {
345+
if (cmd.name === command || cmd.alias === command) {
346+
result = mod
347+
}
348+
})
279349
})
280-
})
281-
return false
350+
return result;
351+
} else {
352+
return false
353+
}
354+
282355
}
283356
}
284357

0 commit comments

Comments
 (0)