Skip to content

Commit

Permalink
Bug fixes and improvements (#5)
Browse files Browse the repository at this point in the history
Co-authored-by: Reny Paul <[email protected]>
  • Loading branch information
renypaul and Reny Paul authored Nov 30, 2024
1 parent 273f160 commit 1a2fdf9
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 54 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
v0.8.2
- Bug fixes

v0.8.1
- Improved handing of type of data
- Bug fixes
Expand Down
87 changes: 56 additions & 31 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { execSync } = require('child_process');
const Parser = require('./parser');
const Terragrunt = require('./terragrunt');

let linkDecator = vscode.window.createTextEditorDecorationType({
let linkDecorator = vscode.window.createTextEditorDecorationType({
textDecoration: 'underline',
overviewRulerColor: 'blue',
overviewRulerLane: vscode.OverviewRulerLane.Right,
Expand Down Expand Up @@ -83,6 +83,7 @@ class TerragruntNav {
getCodePath = '';
tfInfo = {};
lastModulePath = null;
lastHCLFile = null;

constructor(context) {
const repoCacheDir = process.platform === 'win32' ? process.env.USERPROFILE : process.env.HOME;
Expand All @@ -94,9 +95,7 @@ class TerragruntNav {

if (featureToggles['ReplaceStrings'] === undefined) {
featureToggles['ReplaceStrings'] = true;
vscode.workspace
.getConfiguration('terragrunt-navigator')
.update('featureToggles', featureToggles, vscode.ConfigurationTarget.Global);
setImmediate(() => this.updateSetting(config, 'featureToggles', featureToggles));
}
this.replaceStrings = featureToggles['ReplaceStrings'];

Expand All @@ -105,14 +104,10 @@ class TerragruntNav {
this.replacementStrings.push({ find: '', replace: '' });
}

this.quickReplaceStringsCount = vscode.workspace
.getConfiguration('terragrunt-navigator')
.get('quickReplaceStringsCount');
this.quickReplaceStringsCount = config.get('quickReplaceStringsCount');
if (this.quickReplaceStringsCount === undefined || this.quickReplaceStringsCount < 1) {
this.quickReplaceStringsCount = 1;
vscode.workspace
.getConfiguration('terragrunt-navigator')
.update('quickReplaceStringsCount', this.quickReplaceStringsCount, vscode.ConfigurationTarget.Global);
setImmediate(() => this.updateSetting(config, 'quickReplaceStringsCount', this.quickReplaceStringsCount));
}

const extensionPath = context.extensionPath;
Expand All @@ -139,6 +134,10 @@ class TerragruntNav {
}
}

async updateSetting(config, key, value) {
await config.update(key, value, vscode.ConfigurationTarget.Global);
}

provideDocumentLinks(document, token) {
let links = [];
let linkDecorations = [];
Expand All @@ -164,7 +163,7 @@ class TerragruntNav {
}

if (vscode.window.activeTextEditor) {
vscode.window.activeTextEditor.setDecorations(linkDecator, linkDecorations);
vscode.window.activeTextEditor.setDecorations(linkDecorator, linkDecorations);
vscode.window.activeTextEditor.setDecorations(lhsDecorator, lhsDecorations);
vscode.window.activeTextEditor.setDecorations(rhsDecorator, rhsDecorations);
vscode.window.activeTextEditor.setDocumentLinks(links);
Expand All @@ -181,24 +180,30 @@ class TerragruntNav {
if (!ranges?.hasOwnProperty(key)) {
continue;
}
let value = configs[key];
let range = ranges[key];
if (Array.isArray(value) && Array.isArray(range)) {
this.updateDecorations(decorations, value, range[range.length - 1]);
for (let i = 0; i < value.length; i++) {
let v = value[i];
let r = range[i];
if (typeof v === 'object' && v !== null) {
this.decorateKeys(decorations, v, r);
} else {
this.updateDecorations(decorations, v, r);
}
}
} else if (typeof value === 'object' && value !== null) {
this.updateDecorations(decorations, value, range);
this.decorateKeys(decorations, value, range);
this.processKey(decorations, configs[key], ranges[key]);
}
}

processKey(decorations, value, range) {
if (Array.isArray(value) && Array.isArray(range)) {
this.updateDecorations(decorations, value, range[range.length - 1]);
this.processArray(decorations, value, range);
} else if (typeof value === 'object' && value !== null) {
this.updateDecorations(decorations, value, range);
this.decorateKeys(decorations, value, range);
} else {
this.updateDecorations(decorations, value, range);
}
}

processArray(decorations, valueArray, rangeArray) {
for (let i = 0; i < valueArray.length; i++) {
let v = valueArray[i];
let r = rangeArray[i];
if (typeof v === 'object' && v !== null) {
this.decorateKeys(decorations, v, r);
} else {
this.updateDecorations(decorations, value, range);
this.updateDecorations(decorations, v, r);
}
}
}
Expand Down Expand Up @@ -279,18 +284,38 @@ class TerragruntNav {
const filePath = vscode.window.activeTextEditor.document.uri.fsPath;
try {
const baseDir = path.dirname(filePath);
let fileName = path.basename(filePath);
if (this.lastModulePath === baseDir) {
console.log('Already read terragrunt config for ' + baseDir);
return;
let allowedNames = [fileName, null];
let revalidate = true;
if (fileName.endsWith('.hcl')) {
if (allowedNames.includes(this.lastHCLFile)) {
this.lastHCLFile = fileName;
revalidate = false;
}
} else {
revalidate = false;
}

if (!revalidate) {
console.log('Already read terragrunt config for ' + baseDir);
return;
}
}
this.lastModulePath = baseDir;
if (fileName.endsWith('.hcl')) {
this.lastHCLFile = fileName;
} else {
this.lastHCLFile = null;
}

this.tfInfo = {
freshStart: true,
printTree: false,
traverse: Parser.traverse,
};
console.log('Reading config for ' + filePath);
if (path.basename(filePath) === 'main.tf') {
if (fileName === 'main.tf') {
let varFile = filePath.replace('main.tf', 'variables.tf');
if (fs.existsSync(varFile)) {
console.log('Reading variables for main.tf ' + varFile);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@
"compile": "java -jar /usr/local/lib/antlr-4.13.2-complete.jar -Dlanguage=JavaScript hclLexer.g4 hclParser.g4 -o generated -Xexact-output-dir -visitor && npx babel generated --out-dir generated-cjs",
"test": "node ./parser.js tests/env/region/cluster/terragrunt.hcl"
},
"version": "0.8.1"
"version": "0.8.2"
}
59 changes: 38 additions & 21 deletions terragrunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@ function find_in_parent_folders(fileName = null) {
fileName = 'terragrunt.hcl';
}
let currentDir = path.dirname(this.path.root);
while (currentDir !== '/') {
const isWindows = process.platform === 'win32';

while (true) {
let filePath = path.join(currentDir, fileName);
if (fs.existsSync(filePath)) {
return filePath;
}
currentDir = path.dirname(currentDir);

let parentDir = path.dirname(currentDir);
if (
currentDir === parentDir ||
(isWindows && currentDir.match(/^[a-zA-Z]:\\$/)) ||
(!isWindows && currentDir === '/')
) {
break;
}
currentDir = parentDir;
}
return null;
}
Expand Down Expand Up @@ -97,25 +108,31 @@ function fetch_key_info(configs, ranges, depth = 0) {

for (let key in configs) {
if (ranges?.hasOwnProperty(key)) {
let value = configs[key];
let range = ranges[key];
if (Array.isArray(value) && Array.isArray(range)) {
print_key_info(value, range[range.length - 1], depth);
for (let i = 0; i < value.length; i++) {
let v = value[i];
let r = range[i];
if (typeof v === 'object' && v !== null) {
fetch_key_info(v, r, depth + 1);
} else {
print_key_info(v, r, depth + 1);
}
}
} else if (typeof value === 'object' && value !== null) {
print_key_info(value, range, depth);
fetch_key_info(value, range, depth + 1);
} else {
print_key_info(value, range, depth);
}
process_key_info(configs[key], ranges[key], depth);
}
}
}

function process_key_info(value, range, depth) {
if (Array.isArray(value) && Array.isArray(range)) {
process_array_info(value, range, depth);
} else if (typeof value === 'object' && value !== null) {
print_key_info(value, range, depth);
fetch_key_info(value, range, depth + 1);
} else {
print_key_info(value, range, depth);
}
}

function process_array_info(value, range, depth) {
print_key_info(value, range[range.length - 1], depth);
for (let i = 0; i < value.length; i++) {
let v = value[i];
let r = range[i];
if (typeof v === 'object' && v !== null) {
fetch_key_info(v, r, depth + 1);
} else {
print_key_info(v, r, depth + 1);
}
}
}
Expand Down

0 comments on commit 1a2fdf9

Please sign in to comment.