mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-08-07 09:23:00 +02:00
chore(devx-improvements): Deps upgrades + Prettier
This commit is contained in:
+151
-102
@@ -17,10 +17,9 @@ import { SETTING_CUSTOM_SCRIPTS } from '../constants';
|
||||
import { existsAsync } from '../utils';
|
||||
|
||||
export class CustomScript {
|
||||
|
||||
/**
|
||||
* Retrieve all scripts
|
||||
* @returns
|
||||
* @returns
|
||||
*/
|
||||
public static async getScripts(): Promise<ICustomScript[]> {
|
||||
const scripts = Settings.get<ICustomScript[]>(SETTING_CUSTOM_SCRIPTS) || [];
|
||||
@@ -29,8 +28,8 @@ export class CustomScript {
|
||||
|
||||
/**
|
||||
* Run a script
|
||||
* @param script
|
||||
* @param path
|
||||
* @param script
|
||||
* @param path
|
||||
*/
|
||||
public static async run(script: ICustomScript, path: string | null = null): Promise<void> {
|
||||
const wsFolder = Folders.getWorkspaceFolder();
|
||||
@@ -40,7 +39,7 @@ export class CustomScript {
|
||||
|
||||
if (script.type === ScriptType.MediaFile || script.type === ScriptType.MediaFolder) {
|
||||
Telemetry.send(TelemetryEvent.runMediaScript);
|
||||
|
||||
|
||||
await CustomScript.runMediaScript(wsPath, path, script);
|
||||
} else {
|
||||
Telemetry.send(TelemetryEvent.runCustomScript);
|
||||
@@ -61,12 +60,16 @@ export class CustomScript {
|
||||
|
||||
/**
|
||||
* Run the script on the current file
|
||||
* @param wsPath
|
||||
* @param script
|
||||
* @param path
|
||||
* @returns
|
||||
* @param wsPath
|
||||
* @param script
|
||||
* @param path
|
||||
* @returns
|
||||
*/
|
||||
private static async singleRun(wsPath: string, script: ICustomScript, path: string | null = null): Promise<void> {
|
||||
private static async singleRun(
|
||||
wsPath: string,
|
||||
script: ICustomScript,
|
||||
path: string | null = null
|
||||
): Promise<void> {
|
||||
let articlePath: string | null = path;
|
||||
let article: ParsedFrontMatter | null = null;
|
||||
|
||||
@@ -81,14 +84,22 @@ export class CustomScript {
|
||||
}
|
||||
|
||||
if (articlePath && article) {
|
||||
return window.withProgress({
|
||||
location: ProgressLocation.Notification,
|
||||
title: `Executing: ${script.title}`,
|
||||
cancellable: false
|
||||
}, async () => {
|
||||
const output = await CustomScript.runScript(wsPath, article, articlePath as string, script);
|
||||
await CustomScript.showOutput(output, script, articlePath);
|
||||
});
|
||||
return window.withProgress(
|
||||
{
|
||||
location: ProgressLocation.Notification,
|
||||
title: `Executing: ${script.title}`,
|
||||
cancellable: false
|
||||
},
|
||||
async () => {
|
||||
const output = await CustomScript.runScript(
|
||||
wsPath,
|
||||
article,
|
||||
articlePath as string,
|
||||
script
|
||||
);
|
||||
await CustomScript.showOutput(output, script, articlePath);
|
||||
}
|
||||
);
|
||||
} else {
|
||||
Notifications.warning(`${script.title}: Article couldn't be retrieved.`);
|
||||
}
|
||||
@@ -96,9 +107,9 @@ export class CustomScript {
|
||||
|
||||
/**
|
||||
* Run the script on multiple files
|
||||
* @param wsPath
|
||||
* @param script
|
||||
* @returns
|
||||
* @param wsPath
|
||||
* @param script
|
||||
* @returns
|
||||
*/
|
||||
private static async bulkRun(wsPath: string, script: ICustomScript): Promise<void> {
|
||||
const folders = await Folders.getInfo();
|
||||
@@ -110,93 +121,121 @@ export class CustomScript {
|
||||
|
||||
let output: string[] = [];
|
||||
|
||||
window.withProgress({
|
||||
location: ProgressLocation.Notification,
|
||||
title: `Executing: ${script.title}`,
|
||||
cancellable: false
|
||||
}, async (progress, token) => {
|
||||
for await (const folder of folders) {
|
||||
if (folder.lastModified.length > 0) {
|
||||
for await (const file of folder.lastModified) {
|
||||
try {
|
||||
const article = await ArticleHelper.getFrontMatterByPath(file.filePath);
|
||||
if (article) {
|
||||
const crntOutput = await CustomScript.runScript(wsPath, article, file.filePath, script);
|
||||
if (crntOutput) {
|
||||
output.push(crntOutput);
|
||||
window.withProgress(
|
||||
{
|
||||
location: ProgressLocation.Notification,
|
||||
title: `Executing: ${script.title}`,
|
||||
cancellable: false
|
||||
},
|
||||
async (progress, token) => {
|
||||
for await (const folder of folders) {
|
||||
if (folder.lastModified.length > 0) {
|
||||
for await (const file of folder.lastModified) {
|
||||
try {
|
||||
const article = await ArticleHelper.getFrontMatterByPath(file.filePath);
|
||||
if (article) {
|
||||
const crntOutput = await CustomScript.runScript(
|
||||
wsPath,
|
||||
article,
|
||||
file.filePath,
|
||||
script
|
||||
);
|
||||
if (crntOutput) {
|
||||
output.push(crntOutput);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
// Skipping file
|
||||
}
|
||||
} catch (error) {
|
||||
// Skipping file
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await CustomScript.showOutput(output.join(`\n`), script);
|
||||
}
|
||||
|
||||
await CustomScript.showOutput(output.join(`\n`), script);
|
||||
});
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a script for a media file
|
||||
* @param wsPath
|
||||
* @param path
|
||||
* @param script
|
||||
* @returns
|
||||
* @param wsPath
|
||||
* @param path
|
||||
* @param script
|
||||
* @returns
|
||||
*/
|
||||
private static async runMediaScript(wsPath: string, path: string | null, script: ICustomScript): Promise<void> {
|
||||
private static async runMediaScript(
|
||||
wsPath: string,
|
||||
path: string | null,
|
||||
script: ICustomScript
|
||||
): Promise<void> {
|
||||
if (!path) {
|
||||
Notifications.error(`${script.title}: There was no folder or media path specified.`);
|
||||
return;
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
window.withProgress({
|
||||
location: ProgressLocation.Notification,
|
||||
title: `Executing: ${script.title}`,
|
||||
cancellable: false
|
||||
}, async () => {
|
||||
try {
|
||||
const output = await CustomScript.executeScript(script, wsPath, `"${wsPath}" "${path}"`);
|
||||
window.withProgress(
|
||||
{
|
||||
location: ProgressLocation.Notification,
|
||||
title: `Executing: ${script.title}`,
|
||||
cancellable: false
|
||||
},
|
||||
async () => {
|
||||
try {
|
||||
const output = await CustomScript.executeScript(
|
||||
script,
|
||||
wsPath,
|
||||
`"${wsPath}" "${path}"`
|
||||
);
|
||||
|
||||
await CustomScript.showOutput(output, script);
|
||||
|
||||
Dashboard.postWebviewMessage({
|
||||
command: DashboardCommand.mediaUpdate
|
||||
});
|
||||
await CustomScript.showOutput(output, script);
|
||||
|
||||
return;
|
||||
} catch (e) {
|
||||
Notifications.error(`${script.title}: ${(e as Error).message}`);
|
||||
return;
|
||||
Dashboard.postWebviewMessage({
|
||||
command: DashboardCommand.mediaUpdate
|
||||
});
|
||||
|
||||
return;
|
||||
} catch (e) {
|
||||
Notifications.error(`${script.title}: ${(e as Error).message}`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Script runner
|
||||
* @param wsPath
|
||||
* @param article
|
||||
* @param contentPath
|
||||
* @param script
|
||||
* @returns
|
||||
* @param wsPath
|
||||
* @param article
|
||||
* @param contentPath
|
||||
* @param script
|
||||
* @returns
|
||||
*/
|
||||
private static async runScript(wsPath: string, article: ParsedFrontMatter | null, contentPath: string, script: ICustomScript): Promise<string | null> {
|
||||
private static async runScript(
|
||||
wsPath: string,
|
||||
article: ParsedFrontMatter | null,
|
||||
contentPath: string,
|
||||
script: ICustomScript
|
||||
): Promise<string | null> {
|
||||
try {
|
||||
let articleData = "";
|
||||
if (os.type() === "Windows_NT") {
|
||||
const jsonData = JSON.stringify(article?.data)
|
||||
let articleData = '';
|
||||
if (os.type() === 'Windows_NT') {
|
||||
const jsonData = JSON.stringify(article?.data);
|
||||
articleData = `'${jsonData.replace(/"/g, `\"`)}'`;
|
||||
} else {
|
||||
articleData = JSON.stringify(article?.data).replace(/'/g, "%27");
|
||||
articleData = JSON.stringify(article?.data).replace(/'/g, '%27');
|
||||
articleData = `'${articleData}'`;
|
||||
}
|
||||
|
||||
const output = await CustomScript.executeScript(script, wsPath, `"${wsPath}" "${contentPath}" ${articleData}`);
|
||||
const output = await CustomScript.executeScript(
|
||||
script,
|
||||
wsPath,
|
||||
`"${wsPath}" "${contentPath}" ${articleData}`
|
||||
);
|
||||
return output;
|
||||
} catch (e) {
|
||||
if (typeof e === "string") {
|
||||
if (typeof e === 'string') {
|
||||
Notifications.error(`${script.title}: ${e}`);
|
||||
} else {
|
||||
Notifications.error(`${script.title}: ${(e as Error).message}`);
|
||||
@@ -207,10 +246,14 @@ export class CustomScript {
|
||||
|
||||
/**
|
||||
* Show/process the output of the script
|
||||
* @param output
|
||||
* @param script
|
||||
* @param output
|
||||
* @param script
|
||||
*/
|
||||
private static async showOutput(output: string | null, script: ICustomScript, articlePath?: string | null): Promise<void> {
|
||||
private static async showOutput(
|
||||
output: string | null,
|
||||
script: ICustomScript,
|
||||
articlePath?: string | null
|
||||
): Promise<void> {
|
||||
if (output) {
|
||||
try {
|
||||
const data = JSON.parse(output);
|
||||
@@ -221,7 +264,7 @@ export class CustomScript {
|
||||
|
||||
if (!articlePath) {
|
||||
if (!editor) return;
|
||||
|
||||
|
||||
articlePath = editor.document.uri.fsPath;
|
||||
article = ArticleHelper.getFrontMatter(editor);
|
||||
} else {
|
||||
@@ -246,14 +289,16 @@ export class CustomScript {
|
||||
throw new Error(`No frontmatter found.`);
|
||||
}
|
||||
} catch (error) {
|
||||
if (script.output === "editor") {
|
||||
ContentProvider.show(output, script.title, script.outputType || "text");
|
||||
if (script.output === 'editor') {
|
||||
ContentProvider.show(output, script.title, script.outputType || 'text');
|
||||
} else {
|
||||
window.showInformationMessage(`${script.title}: ${output}`, 'Copy output').then(value => {
|
||||
if (value === 'Copy output') {
|
||||
vscodeEnv.clipboard.writeText(output);
|
||||
}
|
||||
});
|
||||
window
|
||||
.showInformationMessage(`${script.title}: ${output}`, 'Copy output')
|
||||
.then((value) => {
|
||||
if (value === 'Copy output') {
|
||||
vscodeEnv.clipboard.writeText(output);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -263,17 +308,21 @@ export class CustomScript {
|
||||
|
||||
/**
|
||||
* Execute script
|
||||
* @param script
|
||||
* @param wsPath
|
||||
* @param args
|
||||
* @returns
|
||||
* @param script
|
||||
* @param wsPath
|
||||
* @param args
|
||||
* @returns
|
||||
*/
|
||||
public static async executeScript(script: ICustomScript, wsPath: string, args: string): Promise<string> {
|
||||
public static async executeScript(
|
||||
script: ICustomScript,
|
||||
wsPath: string,
|
||||
args: string
|
||||
): Promise<string> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const osType = os.type();
|
||||
|
||||
// Check the command to use
|
||||
let command = script.nodeBin || "node";
|
||||
let command = script.nodeBin || 'node';
|
||||
if (script.command && script.command !== CommandType.Node) {
|
||||
command = script.command;
|
||||
}
|
||||
@@ -282,19 +331,19 @@ export class CustomScript {
|
||||
if (script.script.includes(WORKSPACE_PLACEHOLDER)) {
|
||||
scriptPath = Folders.getAbsFilePath(script.script);
|
||||
}
|
||||
|
||||
|
||||
// Check if there is an environments overwrite required
|
||||
if (script.environments) {
|
||||
let crntType: EnvironmentType | null = null;
|
||||
if (osType === "Windows_NT") {
|
||||
crntType = "windows"
|
||||
} else if (osType === "Darwin") {
|
||||
crntType = "macos"
|
||||
if (osType === 'Windows_NT') {
|
||||
crntType = 'windows';
|
||||
} else if (osType === 'Darwin') {
|
||||
crntType = 'macos';
|
||||
} else {
|
||||
crntType = "linux"
|
||||
crntType = 'linux';
|
||||
}
|
||||
|
||||
const environment = script.environments.find(e => e.type === crntType);
|
||||
const environment = script.environments.find((e) => e.type === crntType);
|
||||
if (environment && environment.script && environment.command) {
|
||||
if (await CustomScript.validateCommand(environment.command)) {
|
||||
command = environment.command;
|
||||
@@ -306,7 +355,7 @@ export class CustomScript {
|
||||
}
|
||||
}
|
||||
|
||||
if (!await existsAsync(scriptPath)) {
|
||||
if (!(await existsAsync(scriptPath))) {
|
||||
reject(new Error(`Script not found: ${scriptPath}`));
|
||||
return;
|
||||
}
|
||||
@@ -332,8 +381,8 @@ export class CustomScript {
|
||||
|
||||
/**
|
||||
* Validate if the command is exists
|
||||
* @param command
|
||||
* @returns
|
||||
* @param command
|
||||
* @returns
|
||||
*/
|
||||
private static async validateCommand(command: string) {
|
||||
try {
|
||||
@@ -345,4 +394,4 @@ export class CustomScript {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user