Skip to content

Commit 91ef962

Browse files
authored
Merge pull request #619 from adobe/indentationInScripts
build: update script to detect the type of indentation in the file an…
2 parents 70bdab9 + 4f3a05c commit 91ef962

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

scripts/updateVersions.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ if (!newVersion) {
2727

2828
console.log(`Updating versions to: ${newVersion}`);
2929

30+
// Function to detect indentation from file content
31+
function detectIndentation(content) {
32+
const lines = content.split('\n');
33+
for (const line of lines) {
34+
if (line.trim() && line.startsWith(' ')) {
35+
// Find the first non-space character
36+
const match = line.match(/^(\s+)/);
37+
if (match) {
38+
return match[1];
39+
}
40+
}
41+
}
42+
// Default to 2 spaces if no indentation detected
43+
return ' ';
44+
}
45+
3046
// Find all package.json files in root and packages/*
3147
const rootDir = path.resolve(__dirname, '..');
3248
const packageJsonPaths = globSync('{package.json,packages/*/package.json}', {
@@ -40,6 +56,9 @@ packageJsonPaths.forEach((filePath) => {
4056
const packageJsonContent = fs.readFileSync(filePath, 'utf8');
4157
const packageJson = JSON.parse(packageJsonContent);
4258

59+
// Detect the original indentation
60+
const originalIndentation = detectIndentation(packageJsonContent);
61+
4362
// Update the main version
4463
if (packageJson.version) {
4564
packageJson.version = newVersion;
@@ -60,8 +79,10 @@ packageJsonPaths.forEach((filePath) => {
6079
});
6180

6281
// Write the updated package.json back to the file
63-
// Keep the same indentation (tab) and trailing newline as the original
64-
fs.writeFileSync(filePath, JSON.stringify(packageJson, null, '\t') + '\n', 'utf8');
82+
// Preserve the original indentation
83+
const indentSize = originalIndentation.length;
84+
const indent = ' '.repeat(indentSize);
85+
fs.writeFileSync(filePath, JSON.stringify(packageJson, null, indent) + '\n', 'utf8');
6586
} catch (error) {
6687
console.error(`Error processing file ${filePath}:`, error);
6788
}

0 commit comments

Comments
 (0)