解决package.json解析异常问题;解决vsix打包问题

This commit is contained in:
zhangyun 2024-09-29 17:13:36 +08:00
parent 580b41b78e
commit a0fa458417
4 changed files with 1530 additions and 1021 deletions

View File

@ -444,6 +444,7 @@
"language.scriptTitle ":"Parse the script in package.json, you can click the following button to execute each script",
"language.debugTitle": "Parse luanch.json, you can click thie following button to debug",
"language.parseTaskError": "Parse tasks.json error: {0}",
"language.parseError": "Parse {0} error: {1}",
"language.selectLibraryFindLib": "You have selected the {0} directory, which contains the lib/lib64 path. You can replace it with {1}. Do you want to replace it? ",
"language.selectLibraryFindLibDetail": "You have selected the {0} directory, which contains the lib/lib64 path. Do you want to change it to {1}? ",

View File

@ -445,6 +445,7 @@
"language.scriptTitle": "解析package.json的script可以点击下列按钮执行各个脚本",
"language.debugTitle": "解析luanch.json可以点击下列按钮执行调试",
"language.parseTaskError": "解析任务出错:{0}",
"language.parseError": "解析{0}出错:{1}",
"language.selectLibraryFindLib": "您选择了 {0} 目录包含lib/lib64路径可以替换为 {1},是否替换?",
"language.selectLibraryFindLibDetail": "您选择了 {0} 目录包含lib/lib64路径是否修改选择为 {1}",

View File

@ -34,43 +34,18 @@ export interface TaskItem {
}
function removeComments(jsonString: string) {
return jsonString.replace(/\/\/.*|\/\*[^]*?\*\//g, '').trim();
// return jsonString.replace(/\/\/.*|\/\*[^]*?\*\//g, '').trim();
return jsonString.replace(/\/\/[^\n]*|\/\*[^]*?\*\//g, '').trim();
}
// 解析 tasks.json 文件
// export function parseTasksJson(rootPath: string): TaskItem[] {
// const tasksFile = path.join(rootPath, '.vscode', 'tasks.json');
// if (!fs.existsSync(tasksFile)) {
// return [];
// }
// const rawData = fs.readFileSync(tasksFile, 'utf-8');
// const jsonData = JSON.parse(rawData);
// if (!jsonData.tasks || !Array.isArray(jsonData.tasks)) {
// vscode.window.showErrorMessage('Invalid tasks.json format');
// return [];
// }
// return jsonData.tasks.map((task: any) => ({
// label: task.label,
// command: task.command,
// args: task.args || [],
// type: task.type,
// problemMatcher: task.problemMatcher,
// group: task.group,
// options: task.options,
// presentation: task.presentation
// }));
// }
export function parseTasksJson(workspacePath: string): TaskItem[] {
const tasksFilePath = path.join(workspacePath, '.vscode', 'tasks.json');
if (!fs.existsSync(tasksFilePath)) {
return [];
}
try{
const tasksFilePath = path.join(workspacePath, '.vscode', 'tasks.json');
if (!fs.existsSync(tasksFilePath)) {
return [];
}
const cleanJson = removeComments(fs.readFileSync(tasksFilePath, 'utf-8'));
const tasksJson = JSON.parse(cleanJson);
const tasks = tasksJson.tasks || [];
@ -103,25 +78,33 @@ export function parseTasksJson(workspacePath: string): TaskItem[] {
// 解析 package.json 中的 scripts
export function parsePackageJsonScripts(rootPath: string): TaskItem[] {
const packageFile = path.join(rootPath, 'package.json');
if (!fs.existsSync(packageFile)) {
return [];
try{
const packageFile = path.join(rootPath, 'package.json');
if (!fs.existsSync(packageFile)) {
return [];
}
// package.json里面不允许有注释但会有http://的情况使用上面的替换处理会出bug
const rawData = fs.readFileSync(packageFile, 'utf-8');
const jsonData = JSON.parse(rawData);
if (!jsonData.scripts || typeof jsonData.scripts !== 'object') {
return [];
}
return Object.keys(jsonData.scripts).map(script => ({
from: TaskFrom.script,
label: `npm: ${script}`,
command: 'npm',
args: ['run', script],
type: 'shell'
}));
}catch(err : any){
return [{
from: TaskFrom.task,
error: localize("language.parseError", "package.json", err.message)
}];
}
const cleanJson = removeComments(fs.readFileSync(packageFile, 'utf-8'));
const jsonData = JSON.parse(cleanJson);
if (!jsonData.scripts || typeof jsonData.scripts !== 'object') {
return [];
}
return Object.keys(jsonData.scripts).map(script => ({
from: TaskFrom.script,
label: `npm: ${script}`,
command: 'npm',
args: ['run', script],
type: 'shell'
}));
}
export function generateTaskPreview(task : any) {
@ -336,7 +319,7 @@ export function parseLaunchJson(workspacePath: string): TaskItem[] {
}catch(err: any){
return [{
from: TaskFrom.debug,
error: localize("language.parseTaskError", err.message)
error: localize("language.parseError", "launch.json", err.message)
}];
}
}

2464
yarn.lock

File diff suppressed because it is too large Load Diff