From 61633dbe7aa6785f4cf543ce0cbe8e16015748c2 Mon Sep 17 00:00:00 2001 From: Katsute <58778985+Katsute@users.noreply.github.com> Date: Thu, 31 Aug 2023 13:35:55 -0400 Subject: [PATCH] Add support for environmental variables (#196) --- README.md | 12 ++++++++++-- package.json | 2 +- src/lib/env.ts | 35 +++++++++++++++++++++++++++++++++++ src/lib/glob.ts | 24 +++++++++++++++--------- test/test.js | 2 +- 5 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 src/lib/env.ts diff --git a/README.md b/README.md index 1b569022..a4266486 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ Type `Background: Configuration` in the command pallette or press the **Backgrou configuration menu -#### ✱ Glob and URL Support +#### ✱ Glob, URL, and Environment Variable Support Add background images by file, folder, [glob](https://github.com/isaacs/node-glob#glob-primer), or URL. @@ -115,10 +115,18 @@ The order settings are saved in is: | Advanced | Type | Description | |---|:-:|---| |`background.useWindowOptionsForAllBackgrounds`|`boolean`|If enabled, all background images will use the options set for the windows background. This does not include the backgrounds, you still need to add background images separately.| -|`background.renderContentAboveBackground`|`boolean`|If enabled, content like images, pdfs, and markdown previews will render above the background.| +|`background.renderContentAboveBackground`|`boolean`|If enabled, content like images, PDFs, and markdown previews will render above the background.| |`background.smoothImageRendering`|`boolean`|If enabled, use smooth image rendering rather than pixelated rendering when resizing images.| |`background.CSS`|`string`|Apply raw CSS to VSCode.| +## 💻 Environment Variables + +| Variable | Description | +|---|---| +|`${vscode:workspace}`|Current VSCode project folder| +|`${user:home}`|Current user's home directory| +|`${...}`|System environment variable| + ## ⚠️ Known Issues #### ⚠️ (Mac) read-only file system diff --git a/package.json b/package.json index fca8f9ea..720d5800 100644 --- a/package.json +++ b/package.json @@ -283,7 +283,7 @@ "default": false }, "background.renderContentAboveBackground": { - "markdownDescription": "If enabled, content like images, pdfs, and markdown previews will render above the background.", + "markdownDescription": "If enabled, content like images, PDFs, and markdown previews will render above the background.", "order": 13, "type": "boolean", "default": false diff --git a/src/lib/env.ts b/src/lib/env.ts new file mode 100644 index 00000000..9f988f07 --- /dev/null +++ b/src/lib/env.ts @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2023 Katsute + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +import * as os from "os"; +import * as vscode from "vscode"; + +const home: string = os.homedir(); + +export const resolve: (str: string) => string = (str: string) => + str.replace(/\${(.*)}/g, (_, envvar) => { + if(envvar == "vscode:workspace" && vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 && vscode.workspace.workspaceFolders[0].uri){ + return vscode.workspace.workspaceFolders[0].uri.fsPath.toString(); + }else if(envvar == "user:home"){ + return home; + }else if(envvar in process.env){ + return process.env[envvar] || ''; + }else{ + return ''; + } + }); \ No newline at end of file diff --git a/src/lib/glob.ts b/src/lib/glob.ts index 41cb2926..ccb8fb2b 100644 --- a/src/lib/glob.ts +++ b/src/lib/glob.ts @@ -20,6 +20,8 @@ import { GlobOptions, globSync } from "glob"; import * as path from "path"; +import * as env from "../lib/env"; + import { extensions } from "../command/config/file"; import { unique } from "./unique"; @@ -43,21 +45,25 @@ export const count: (glob: string | string[]) => number = (glob: string | string for(const g of (Array.isArray(glob) ? glob.filter(unique) : [glob])) if(g.startsWith("https://")) i++; - else - globs.push(g); + else // need to normalize '/' ↓ so glob works properly + globs.push(env.resolve(g).replace(/\\/g, '/')); return i + (globSync(globs, options) as string[]).filter(filter).filter(unique).length; } export const resolve: (glob: string | string[]) => string[] = (glob: string | string[]) => { - let p: string[] = []; + let urls: string[] = []; let globs: string[] = []; - (Array.isArray(glob) ? glob.filter(unique) : [glob]).forEach(g => (g.startsWith("https://") ? p : globs).push(g)); + for(const g of (Array.isArray(glob) ? glob.filter(unique) : [glob])) + if(g.startsWith("https://")) + urls.push(g); + else // need to normalize '/' ↓ so glob works properly + globs.push(env.resolve(g).replace(/\\/g, '/')); - return p.concat((globSync(globs, options) as string[]) - .filter(filter) - .map(path => `vscode-file://vscode-app/${path.replace(/\\/gm, '/').replace(/^\/+/gm, "")}`)) - .filter(unique) - .map(path => '"' + path + '"'); + return urls.concat((globSync(globs, options) as string[]) + .filter(filter) // need to normalize '/' again ↓ because glob uses the wrong slash + .map(path => `vscode-file://vscode-app/${path.replace(/\\/g, '/').replace(/^\/+/g, "")}`)) + .filter(unique) + .map(path => '"' + path + '"'); } \ No newline at end of file diff --git a/test/test.js b/test/test.js index dfbfe0ca..c1686d35 100644 --- a/test/test.js +++ b/test/test.js @@ -5,7 +5,7 @@ const vscode = require("vscode"); const file = path.join(__dirname, "__testno__"); -const images = path.join(__dirname, "*.png").replace(/\\/gm, '/'); +const images = path.join(__dirname, "*.png").replace(/\\/g, '/'); module.exports = { run: () => new Promise(async () => {