-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: init (#878) * feat: init (#876) * feat: init (#871) * feat: update (#883) * Feat add codemod check to doctor package (#870) * feat: update * feat: update usage * feat: update interface * feat: update interface * feat: update interface * fix: component link (#884) * fix: component link * fix: import namespace * fix: types * chore: changelog * Feat doctor extension add codemod (#873) * feat: doctor * fix: build error (#885) * Fix: rax component docs not display (#887) * fix: rax component docs not display * fix: import name Co-authored-by: Hengchang Lu <[email protected]>
- Loading branch information
1 parent
98ae157
commit ba3d23b
Showing
65 changed files
with
1,166 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,6 @@ web/** | |
**/*.map | ||
**/*.ts | ||
|
||
node_modules/@types/** | ||
node_modules/terser | ||
node_modules/ts-loader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"iceworksDoctor.commands.doctor.title": "AppWorks: Doctor.", | ||
"iceworksDoctor.commands.scan.title": "AppWorks: Scan Your Project.", | ||
"iceworksDoctor.commands.fix.title": "AppWorks: Scan and Fix Your Project." | ||
"iceworksDoctor.commands.fix.title": "AppWorks: Scan and Fix Your Project.", | ||
"iceworksDoctor.commands.codemod.title": "AppWorks: Run a Codemod." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"iceworksDoctor.commands.doctor.title": "AppWorks: 打开质量检测仪", | ||
"iceworksDoctor.commands.scan.title": "AppWorks: 扫描代码", | ||
"iceworksDoctor.commands.fix.title": "AppWorks: 扫描并修复代码" | ||
"iceworksDoctor.commands.fix.title": "AppWorks: 扫描并修复代码", | ||
"iceworksDoctor.commands.codemod.title": "AppWorks: 运行 Codemod" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as vscode from 'vscode'; | ||
import * as semver from 'semver'; | ||
import { Doctor } from '@appworks/doctor'; | ||
import { projectPath } from '@appworks/project-service'; | ||
import parse from 'parse-package-name'; | ||
import setOutput from './setOutput'; | ||
import setDeprecatedPackage from './setDeprecatedPackage'; | ||
|
||
const doctor = new Doctor({ ignore: ['.vscode', '.ice', 'mocks', '.eslintrc.js', 'webpack.config.js'] }); | ||
|
||
const SCAN_OPTIONS = { | ||
disableESLint: true, | ||
disableMaintainability: true, | ||
disableRepeatability: true, | ||
}; | ||
|
||
export async function runCodemod(transform: string) { | ||
const result = await doctor.scan( | ||
projectPath, | ||
Object.assign({ transforms: [transform] }, SCAN_OPTIONS), | ||
); | ||
setOutput(result.codemod?.reports[0].output || ''); | ||
return result; | ||
} | ||
|
||
export async function activateCodemod(context: vscode.ExtensionContext) { | ||
const { env, window } = vscode; | ||
const isEn = env.language === 'en'; | ||
|
||
const deprecatedPackageConfig = {}; | ||
|
||
const reports = await doctor.scan(projectPath, SCAN_OPTIONS); | ||
const packageFile = path.join(projectPath, 'package.json'); | ||
const packageJSON = fs.existsSync(packageFile) ? JSON.parse(fs.readFileSync(packageFile, 'utf-8')) : {}; | ||
|
||
// Show notifaction | ||
(reports.codemod?.reports || []).forEach((codemod) => { | ||
const action = 'Run a Codemod'; | ||
|
||
if (codemod.npm_deprecate) { | ||
const { name, version } = parse(codemod.npm_deprecate); | ||
const dependence = (packageJSON.dependencies || {})[name] || (packageJSON.devDependencies || {})[name]; | ||
|
||
if (dependence && semver.satisfies(semver.coerce(dependence), version || '*')) { | ||
deprecatedPackageConfig[name] = { | ||
...codemod, | ||
name, | ||
version, | ||
}; | ||
} | ||
} | ||
const message = | ||
`${isEn ? codemod.title_en : codemod.title}: ` + | ||
`${isEn ? codemod.message_en : codemod.message} ` + | ||
`( [${isEn ? 'docs' : '文档'}](${codemod.docs}) )`; | ||
const showMessage = codemod.severity === 2 ? window.showErrorMessage : window.showWarningMessage; | ||
|
||
showMessage(message, action).then(async (item) => { | ||
// Run codemod | ||
if (item === action) { | ||
const result = await runCodemod(codemod.transform); | ||
|
||
// Remove fixed deprecated package | ||
if (result.codemod?.reports[0].npm_deprecate) { | ||
delete deprecatedPackageConfig[result.codemod?.reports[0].npm_deprecate]; | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
// Show deprecate package | ||
setDeprecatedPackage(deprecatedPackageConfig); | ||
vscode.window.onDidChangeActiveTextEditor(() => { | ||
setDeprecatedPackage(deprecatedPackageConfig); | ||
}, null, context.subscriptions); | ||
vscode.workspace.onDidChangeTextDocument(() => { | ||
setDeprecatedPackage(deprecatedPackageConfig); | ||
}, null, context.subscriptions); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import * as vscode from 'vscode'; | ||
|
||
interface IConfig { | ||
[key: string]: { | ||
docs: string; | ||
title: string; | ||
title_en: string; | ||
message: string; | ||
message_en: string; | ||
name: string; | ||
version: string; | ||
transform: string; | ||
}; | ||
} | ||
|
||
let decorationType; | ||
function getDecorationType(): vscode.TextEditorDecorationType { | ||
// Remove last decoration first | ||
decorationType && decorationType.dispose(); | ||
decorationType = vscode.window.createTextEditorDecorationType({ | ||
textDecoration: 'line-through', | ||
}); | ||
return decorationType; | ||
} | ||
|
||
export default function (deprecatedPackageConfig: IConfig) { | ||
const isEn = vscode.env.language === 'en'; | ||
const editor = vscode.window.activeTextEditor; | ||
|
||
if (!editor || Object.keys(deprecatedPackageConfig).length === 0) return; | ||
|
||
const text = editor.document.getText(); | ||
const decorationsArray: vscode.DecorationOptions[] = []; | ||
|
||
let matched; | ||
const reg = new RegExp(Object.keys(deprecatedPackageConfig).map((key) => `(["|'|\`]${key}["|'|\`])`).join('|'), 'g'); | ||
|
||
// eslint-disable-next-line | ||
while (matched = reg.exec(text || '')) { | ||
|
||
const config = deprecatedPackageConfig[matched[0].slice(1, -1)]; | ||
if (config) { | ||
const start = editor.document.positionAt(matched.index); | ||
const end = editor.document.positionAt(matched.index + matched[0].length); | ||
|
||
const range = new vscode.Range(start, end); | ||
const hoverMessage = new vscode.MarkdownString( | ||
`# ${isEn ? 'Codemod Suggestion' : 'Codemod 建议'} \n ` + | ||
`${isEn ? config.title_en : config.title}: ${isEn ? config.message_en : config.message} ` + | ||
`( [${isEn ? 'docs' : '文档'}](${config.docs}) ) \n\n ` + | ||
`[Run a Codemod](${vscode.Uri.parse( | ||
`command:doctor.codemod?${encodeURIComponent(JSON.stringify([{ transform: config.transform }]))}`, | ||
)})`, | ||
); | ||
hoverMessage.isTrusted = true; | ||
|
||
decorationsArray.push({ | ||
range, | ||
hoverMessage, | ||
}); | ||
} | ||
} | ||
|
||
editor.setDecorations(getDecorationType(), decorationsArray); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as vscode from 'vscode'; | ||
|
||
const channel = vscode.window.createOutputChannel('Doctor'); | ||
|
||
export default function (message: string) { | ||
channel.appendLine(message); | ||
channel.show(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"title": "Rax 组件工程升级", | ||
"title_en": "Rax component project upgrade", | ||
"message": "从 plugin-rax-component 升级到 plugin-component", | ||
"message_en": "upgrade from plugin-rax-component to plugin-component", | ||
"severity": 1, | ||
"npm_deprecate": "build-plugin-rax-component", | ||
"transform": "plugin-rax-component-to-component", | ||
"docs": "https://github.com/appworks-lab/codemod/tree/master/transforms/docs/plugin-rax-component-to-component.md", | ||
"mode": "run", | ||
"output": "Processing 20 files... \nSpawning 7 workers...\nRunning in dry mode, no files will be written! \nSending 3 files to free worker...\nSending 3 files to free worker...\nSending 3 files to free worker...\nSending 3 files to free worker...\nSending 3 files to free worker...\nSending 3 files to free worker...\nSending 2 files to free worker...\nAll done. \nResults: \n0 errors\n18 unmodified\n1 skipped\n1 ok\nTime elapsed: 1.014seconds " | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.