Skip to content

Commit

Permalink
Add support for multiple files
Browse files Browse the repository at this point in the history
Use an object as config where each key-value is a file to be written.
  • Loading branch information
grgar committed Mar 19, 2024
1 parent 598f59c commit dbf2bca
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

# dotfiles

Configure dotfiles from your VS Code settings so they're always available.

## Extension Settings

* `dotfiles.directory`: Base directory path for all config files to be written.
* `dotfiles.files`: Files to be written to the configured directory, where key is the relative path to the file and value is the content of the file.
18 changes: 6 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"displayName": "dotfiles",
"publisher": "grg",
"description": "Apply dotfiles from settings",
"version": "0.0.1",
"version": "0.1.0",
"preview": true,
"engines": {
"vscode": "^1.74.0"
},
Expand All @@ -29,17 +30,10 @@
"markdownDescription": "Path to config directory. If unset, uses `$XDG_CONFIG_DIR` with fallback to `$HOME/.config`.",
"order": 1
},
"dotfiles.file": {
"type": "string",
"default": "fish/conf.d/vscode.fish",
"markdownDescription": "Path to config file relative to `#dotfiles.directory#` directory.",
"dotfiles.files": {
"type": "object",
"markdownDescription": "Files to apply where key is file path relative to `#dotfiles.directory#` and value is file content.",
"order": 2
},
"dotfiles.content": {
"type": "string",
"description": "Content of config file.",
"editPresentation": "multilineText",
"order": 3
}
}
}
Expand All @@ -53,7 +47,7 @@
"test": "vscode-test"
},
"devDependencies": {
"@types/vscode": "^1.87.0",
"@types/vscode": "^1.74.0",
"@types/mocha": "^10.0.6",
"@types/node": "18.x",
"@typescript-eslint/eslint-plugin": "^7.0.2",
Expand Down
29 changes: 15 additions & 14 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ const configNamespace = "dotfiles";

async function apply() {
const config = vscode.workspace.getConfiguration(configNamespace);
const directory = config.get<string>("directory") || process.env["XDG_CONFIG_DIR"] || path.join(process.env["HOME"]!, ".config");
const filePath = path.join(directory, config.get<string>("file") || "fish/conf.d/vscode.fish");
fs.stat(filePath, (err, _) => {
if (err) {
vscode.window.showWarningMessage(`${filePath} does not exist`);
return;
}
const data = config.get<string>("content");
if (!data) {
vscode.window.showInformationMessage("No content to write");
return;
}
fs.writeFile(filePath, data, { encoding: "utf-8", mode: "w" }, () => { });
});
const directory = config.get<string>("directory", process.env["XDG_CONFIG_DIR"] ?? path.join(process.env["HOME"]!, ".config"));
const files = Object.entries(config.get<object>("files", {}));
for (const [file, content] of files) {
const filePath = path.join(directory, file);
fs.stat(filePath, (err, _) => {
if (err) {
vscode.window.showWarningMessage(`${filePath} does not exist`);
return;
}
if (!content) {
return;
}
fs.writeFile(filePath, content, { encoding: "utf-8", mode: "w" }, () => { });
});
}
}

export function activate(context: vscode.ExtensionContext) {
Expand Down

0 comments on commit dbf2bca

Please sign in to comment.