Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env sh

node scripts/ensure-crlf.js || exit 1
npm run localization
node scripts/localization-extract.js --precommit || exit 1
npm run precommit
79 changes: 77 additions & 2 deletions scripts/localization-extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/

const vscodel10n = require("@vscode/l10n-dev");
const { execFileSync } = require("child_process");
const fs = require("fs").promises;
const path = require("path");
const logger = require("./terminal-logger");
Expand All @@ -19,6 +20,56 @@ const EXTENSION_CONFIG = {
"data-workspace": "data-workspace",
};

const EXTENSION_INPUTS = {
mssql: ["extensions/mssql/src/", "extensions/mssql/package.nls.json"],
"sql-database-projects": [
"extensions/sql-database-projects/src/",
"extensions/sql-database-projects/package.nls.json",
],
"data-workspace": [
"extensions/data-workspace/src/",
"extensions/data-workspace/package.nls.json",
],
};
Comment on lines +23 to +33

const LOCALIZATION_SCRIPT_INPUTS = [
"scripts/localization-extract.js",
"scripts/file-utils.js",
"scripts/terminal-logger.js",
];

function getStagedFiles() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is the linter being limited to just staged files? something that could be shared between the two?

const output = execFileSync(
"git",
["diff", "--cached", "--name-only", "--diff-filter=ACMRD", "-z"],
{ encoding: "buffer" },
);

return output
.toString("utf8")
.split("\0")
.map((file) => file.replace(/\\/g, "/").trim())
.filter(Boolean);
}

function matchesInput(file, input) {
return input.endsWith("/") ? file.startsWith(input) : file === input;
}

function getAffectedExtensionsForPrecommit() {
const stagedFiles = getStagedFiles();

if (stagedFiles.some((file) => LOCALIZATION_SCRIPT_INPUTS.includes(file))) {
return Object.keys(EXTENSION_CONFIG);
}

return Object.entries(EXTENSION_INPUTS)
.filter(([, inputs]) =>
stagedFiles.some((file) => inputs.some((input) => matchesInput(file, input))),
)
.map(([extension]) => extension);
}

/**
* Scans the src directory of an extension for TypeScript files and extracts their content
* @param {string} extensionPath - Path to the extension directory
Expand Down Expand Up @@ -173,18 +224,42 @@ async function extractLocalizationStrings() {
}
}

async function extractLocalizationForPrecommit() {
const affectedExtensions = getAffectedExtensionsForPrecommit();
if (!affectedExtensions.length) {
console.log("No staged localization inputs; skipping localization extraction.");
return;
}

for (const extensionDir of affectedExtensions) {
await extractLocalizationForExtension(extensionDir, EXTENSION_CONFIG[extensionDir]);
}
}

module.exports = {
extractLocalizationStrings,
extractLocalizationForExtension,
extractLocalizationForPrecommit,
getL10nJson,
};

if (require.main === module) {
// Check if a specific extension is requested via command line args
const args = process.argv.slice(2);
const specificExtension = args[0];
const precommit = args.includes("--precommit");
const specificExtension = args.find((arg) => !arg.startsWith("--"));

if (specificExtension) {
if (precommit) {
extractLocalizationForPrecommit()
.then(() => {
logger.success("Script completed successfully!");
process.exit(0);
})
.catch((error) => {
logger.error(`Script failed: ${error.message}`);
process.exit(1);
});
} else if (specificExtension) {
// Extract for specific extension
const xliffName = EXTENSION_CONFIG[specificExtension];
if (!xliffName) {
Expand Down
Loading