Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const glob = require("glob");

const mp = path.join(__dirname, "meta.json");

const meta = JSON.parse(fs.readFileSync(mp, "utf-8"));
const meta = JSON.parse(fs.readFileSync(mp, {encoding: "utf-8"}));

let files = [];

Expand All @@ -42,7 +42,7 @@ let licenses = {};
// sort so top level (newer) dependency licenses overwrite transient (potentially older) dependency licenses
for(const license of files.sort((a, b) => b.length - a.length)){
const name = license.split("node_modules").splice(-1)[0].split('/').slice(0, -1).join('/').substring(1);
licenses[name] = fs.readFileSync(license, "utf-8").trim();
licenses[name] = fs.readFileSync(license, {encoding: "utf-8"}).trim();
}

let out = "# Licenses\n\n---";
Expand All @@ -53,5 +53,5 @@ for(const k of Object.keys(licenses).sort()){
out += `\n\n${licenses[k]}`;
}

fs.writeFileSync(path.join(__dirname, "LICENSES.txt"), out, "utf-8");
fs.writeFileSync(path.join(__dirname, "LICENSES.txt"), out, {encoding: "utf-8"});
fs.rmSync(mp);
307 changes: 176 additions & 131 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"theme": "dark"
},
"publisher": "Katsute",
"version": "1.0.11",
"version": "2.0.0",
"private": true,
"engines": {
"vscode": "^1.96.0"
Expand Down Expand Up @@ -135,7 +135,7 @@
"homepage": "https://github.com/KatsuteDev/Settings-Repository#readme",
"devDependencies": {
"@types/adm-zip": "0.5.7",
"@types/node": "22.10.1",
"@types/node": "22.10.2",
"@types/vscode": "1.96.0",
"@vscode/vsce": "3.2.1",
"adm-zip": "0.5.16",
Expand Down
11 changes: 5 additions & 6 deletions src/command/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import * as vscode from "vscode";

import * as fs from "fs";
import * as os from "os";

import { isNull, isValidJson } from "../lib/is";
Expand Down Expand Up @@ -53,7 +52,7 @@ const crypt: Crypt = new Crypt(os.hostname());
//

export const mask: (s: string, c: credentials) => string = (s: string, c: credentials) => {
return s.replace(new RegExp(c.auth, "gm"), "***");
return `${s}`.replace(new RegExp(c.auth, "gm"), "***");
}

export const authenticate: () => void = () => {
Expand Down Expand Up @@ -85,13 +84,13 @@ export const authenticate: () => void = () => {

logger.info(`Updated authentication: ${username}`);

fs.writeFileSync(
files.write(
dist.credentials,
`{
"login": "${username}",
"auth": "${crypt.encrypt(password)}"
}`,
"utf-8");
}`
);
});
});
}
Expand All @@ -101,7 +100,7 @@ export const authorization: () => credentials | undefined = () => {

if(!files.isFile(dist.credentials)) return undefined;

const json: string = fs.readFileSync(dist.credentials, "utf-8");
const json: string = files.read(dist.credentials)!;

if(!isValidJson(json)) return undefined;

Expand Down
76 changes: 49 additions & 27 deletions src/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import { isNotNull, isNull, isValidJson } from "./lib/is";

//

export type Profile = {
location: string;
name: string;
}

export class Distribution {

public readonly Code: string;
Expand All @@ -36,7 +41,9 @@ export class Distribution {
public readonly keybindings: string;
public readonly locale: string;
public readonly settings: string;
public readonly Snippets: string;
public readonly snippets: string;
public readonly storage: string;
public readonly profiles: string;
public readonly dotVscode?: string;
public readonly Extensions?: string;
public readonly ExtensionsObsolete?: string;
Expand All @@ -52,7 +59,9 @@ export class Distribution {
this.keybindings = path.join(this.User, "keybindings.json");
this.locale = path.join(this.User, "locale.json");
this.settings = path.join(this.User, "settings.json");
this.Snippets = path.join(this.User, "snippets");
this.snippets = path.join(this.User, "snippets");
this.storage = path.join(this.User, "globalStorage", "storage.json");
this.profiles = path.join(this.User, "profiles");

const exts: vscode.Extension<any>[] = vscode.extensions.all.filter(e => !e.packageJSON.isBuiltin);

Expand All @@ -69,7 +78,9 @@ export class Distribution {
├ keybindings: ${this.keybindings}
├ locale: ${this.locale}
├ settings: ${this.settings}
└ Snippets: ${this.Snippets}
├ snippets: ${this.snippets}
├ storage: ${this.storage}
└ profiles: ${this.profiles}
.vscode: ${this.dotVscode}
├ Extensions: ${this.Extensions}
├ ExtensionsObsolete: ${this.ExtensionsObsolete}
Expand All @@ -80,7 +91,7 @@ export class Distribution {
public getExtensions(): string | undefined {
if(!files.isDirectory(this.Extensions)) return undefined;

const dotObsolete: string = this.ExtensionsObsolete && fs.existsSync(this.ExtensionsObsolete) ? fs.readFileSync(this.ExtensionsObsolete, "utf-8") : "{}";
const dotObsolete: string = files.read(this.ExtensionsObsolete) ?? "{}";
const obsolete: {[id: string]: boolean} = isValidJson(dotObsolete) ? JSON.parse(dotObsolete) : {};
const uninstalled: string[] = Object.entries(obsolete).filter(([k, v]) => v).map(([k, v]) => k);

Expand All @@ -95,7 +106,7 @@ export class Distribution {

if(!files.isFile(pkgFile)) continue;

const json: string = fs.readFileSync(pkgFile, "utf-8");
const json: string = files.read(pkgFile) ?? "";

if(!isValidJson(json)) continue;

Expand Down Expand Up @@ -136,7 +147,7 @@ ${json.slice(0, -2)}
public updateExtensions(): void { // we cannot handle enable/disable at the moment, see <https://github.com/microsoft/vscode/issues/15466#issuecomment-724147661>
if(!files.isDirectory(this.Extensions) || !files.isFile(this.extensions)) return;

const json: string = fs.readFileSync(this.extensions, "utf-8");
const json: string = files.read(this.extensions) ?? "";

const extensions: [{
identifier: string,
Expand All @@ -163,7 +174,7 @@ ${json.slice(0, -2)}

if(!files.isFile(pkgFile)) continue;

const json: string = fs.readFileSync(pkgFile, "utf-8");
const json: string = files.read(pkgFile) ?? "";

if(!isValidJson(json)) continue;

Expand All @@ -185,21 +196,36 @@ ${json.slice(0, -2)}
}
}

public getSettings(): string | undefined {
return files.isFile(this.settings)
? fs.readFileSync(this.settings, "utf-8").trim()
: undefined;
public getProfiles(): Profile[] | undefined {
if(files.isFile(this.storage)){
const content = (files.read(this.storage) ?? "").trim();
if(isValidJson(content)){
const obj = JSON.parse(content).userDataProfiles;
return Array.isArray(obj) ? obj : [];
}
}
return undefined;
}

public writeProfiles(profiles: Profile[]) {
if(files.isFile(this.storage)){
const content = (files.read(this.storage) ?? "").trim();
if(isValidJson(content)){
const obj: {userDataProfiles: Profile[]} = JSON.parse(content);
obj.userDataProfiles = [...(obj.userDataProfiles ?? []), ...(profiles ?? [])].filter((o, i, s) => s.findIndex(o => o.location === o.location) === i);
setTimeout(() => {
files.write(this.storage, JSON.stringify(obj, null, 4));
logger.info("Finished deferred profile write");
}, 1000 * 20);
}else{
logger.error("Storage was malformed");
}
}
}

private static readonly ctrl: RegExp = /(?<=^\s*"key"\s*:\s*".*)\bctrl\b(?=.*",?$)/gmi; // ⌃ ctrl
private static readonly cmd: RegExp = /(?<=^\s*"key"\s*:\s*".*)\bcmd\b(?=.*",?$)/gmi; // ⌘ cmd

public getKeybindings(): string | undefined {
return files.isFile(this.keybindings)
? fs.readFileSync(this.keybindings, "utf-8").trim()
: undefined;
}

public formatKeybindings(keybindings: string, ctrl: "ctrl" | "cmd" = "ctrl"): string {
return keybindings.replace(ctrl === "ctrl" ? Distribution.cmd : Distribution.ctrl, ctrl); // ⌃ ctrl ↔ ⌘ cmd
}
Expand All @@ -208,38 +234,34 @@ ${json.slice(0, -2)}
if(!files.isFile(this.keybindings)) return;

// replace local keybindings with OS specific keybinds
fs.writeFileSync(this.keybindings, this.formatKeybindings(fs.readFileSync(this.keybindings, "utf-8"), this.macos ? "cmd" : "ctrl"));
files.write(this.keybindings, this.formatKeybindings(files.read(this.keybindings)!, this.macos ? "cmd" : "ctrl"));
}

private static readonly locale: RegExp = /(?<=^\s*"locale"\s*:\s*")[\w-]+(?=")/mi;

public getLocale(): string | undefined {
if(!files.isFile(this.argv)) return undefined;

const argv: string = fs.readFileSync(this.argv!, "utf-8");
const argv: string = files.read(this.argv)!;

const match: RegExpExecArray | null = Distribution.locale.exec(argv);

return match && match.length > 0
? `{
"locale": "${match[0]}"
}`
: undefined;
return match && match.length > 0 ? JSON.stringify({locale: match[0]}, null, 4) : undefined;
}

public updateLocale(): void {
if(!files.isFile(this.argv) || !files.isFile(this.locale)) return;

const argv: string = fs.readFileSync(this.argv!, "utf-8");
const argv: string = files.read(this.argv)!;

const json: string = fs.readFileSync(this.locale, "utf-8");
const json: string = files.read(this.locale)!;

if(!isValidJson(json)) return;

const locale: any = JSON.parse(json);

if(isNotNull(locale.locale))
fs.writeFileSync(this.argv!, argv.replace(Distribution.locale, locale.locale).trim());
files.write(this.argv!, argv.replace(Distribution.locale, locale.locale).trim());
}

}
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const deactivate: () => Promise<void> = async () => {

export const notify: () => void = () => {
const select: string = "Reload";
vscode.window.showWarningMessage("Settings have been modified, a reload is required to see changes. Some changes require a full restart.", select, "Ignore").then((value?: string) => {
vscode.window.showWarningMessage("Settings have been modified, a reload is required to see changes. Profile changes require a full restart.", select, "Ignore").then((value?: string) => {
if(value === select)
vscode.commands.executeCommand("workbench.action.reloadWindow");
});
Expand Down
17 changes: 16 additions & 1 deletion src/lib/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,25 @@ export const copyRecursiveSync: (src: string | undefined | null, dest: string) =
for(const file of fs.readdirSync(src!) || []){
const fsPath: string = path.join(src!, file);
if(isFile(fsPath))
fs.copyFileSync(fsPath, path.join(dest, file));
copy(fsPath, path.join(dest, file));
else if(isDirectory(fsPath)){
fs.mkdirSync(path.join(dest, file));
copyRecursiveSync(fsPath, path.join(dest, file));
}
}
}

export const read: (src: string | undefined | null) => string | undefined = (src: string | undefined | null) => {
if(isFile(src))
return fs.readFileSync(src!, {encoding: "utf-8"});
else
return undefined;
}

export const copy: (src: string | undefined | null, dest: string | undefined | null) => void = (src: string | undefined | null, dest: string | undefined | null) => {
isFile(src) && isNotNull(dest) && fs.copyFileSync(src!, dest!);
}

export const write: (src: string | undefined | null, content: string | undefined | null) => void = (src: string | undefined | null, content: string | undefined | null) => {
isNotNull(src) && isNotNull(content) && fs.writeFileSync(src!, content!, {encoding: "utf-8"});
}
2 changes: 1 addition & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const warning: string = '⚠️';
const logger: vscode.OutputChannel = vscode.window.createOutputChannel("Settings Repository", "log");

const indent: (message: string) => string = (message: string) => {
return message.replace(/^/gm, " ").substring(6)
return `${message}`.replace(/^/gm, " ").substring(6)
}

export const log: (message: string) => void = (message: string) => {
Expand Down
Loading
Loading