@@ -20,17 +20,17 @@ const EXTENSION_CONFIG = {
2020 "data-workspace" : "data-workspace" ,
2121} ;
2222
23- const EXTENSION_INPUTS = {
24- mssql : [ "extensions/mssql/src/" , "extensions/mssql/ package.nls. json"] ,
25- "sql-database-projects" : [
26- "extensions/sql-database-projects/src/ ",
27- "extensions/sql-database-projects/package.nls.json ",
28- ] ,
29- "data-workspace" : [
30- "extensions/data-workspace/src/" ,
31- "extensions/data-workspace/package.nls.json" ,
32- ] ,
33- } ;
23+ const PRECOMMIT_FULL_EXTRACTION_INPUTS = [
24+ " package.json",
25+ "package-lock.json" ,
26+ "scripts/localization-extract.js ",
27+ "scripts/file-utils.js ",
28+ "scripts/terminal-logger.js" ,
29+ ] ;
30+
31+ function getExtensionInputs ( extensionDir ) {
32+ return [ `extensions/ ${ extensionDir } /src/` , `extensions/ ${ extensionDir } /package.nls.json` ] ;
33+ }
3434
3535function getStagedFiles ( ) {
3636 const output = execFileSync (
@@ -42,7 +42,7 @@ function getStagedFiles() {
4242 return output
4343 . toString ( "utf8" )
4444 . split ( "\0" )
45- . map ( ( file ) => file . replace ( / \\ / g, "/" ) . trim ( ) )
45+ . map ( ( file ) => file . replace ( / \\ / g, "/" ) )
4646 . filter ( Boolean ) ;
4747}
4848
@@ -57,11 +57,15 @@ function matchesInput(file, input) {
5757function getAffectedExtensionsForPrecommit ( ) {
5858 const stagedFiles = getStagedFiles ( ) ;
5959
60- return Object . entries ( EXTENSION_INPUTS )
61- . filter ( ( [ , inputs ] ) =>
62- stagedFiles . some ( ( file ) => inputs . some ( ( input ) => matchesInput ( file , input ) ) ) ,
63- )
64- . map ( ( [ extension ] ) => extension ) ;
60+ if ( stagedFiles . some ( ( file ) => PRECOMMIT_FULL_EXTRACTION_INPUTS . includes ( file ) ) ) {
61+ return Object . keys ( EXTENSION_CONFIG ) ;
62+ }
63+
64+ return Object . keys ( EXTENSION_CONFIG ) . filter ( ( extensionDir ) =>
65+ stagedFiles . some ( ( file ) =>
66+ getExtensionInputs ( extensionDir ) . some ( ( input ) => matchesInput ( file , input ) ) ,
67+ ) ,
68+ ) ;
6569}
6670
6771async function getL10nJsonFromFileContents ( fileContents ) {
@@ -132,35 +136,6 @@ function getStagedSourceFilesForExtension(extensionDir, stagedFiles) {
132136 ) ;
133137}
134138
135- async function getL10nJsonFromStagedSourceFiles ( extensionDir , stagedFiles ) {
136- logger . step ( "Scanning staged source files for localization strings..." ) ;
137-
138- const sourceFiles = getStagedSourceFilesForExtension ( extensionDir , stagedFiles ) ;
139- logger . info ( `Found ${ sourceFiles . length } staged TypeScript files to process` ) ;
140-
141- const fileContents = [ ] ;
142- for ( const file of sourceFiles ) {
143- try {
144- const content = getStagedFileContent ( file ) ;
145- if ( content ) {
146- fileContents . push ( {
147- contents : content ,
148- extension : file . endsWith ( ".tsx" ) ? ".tsx" : ".ts" ,
149- } ) ;
150- }
151- } catch ( error ) {
152- logger . warning ( `Failed to read staged file ${ file } : ${ error . message } ` ) ;
153- }
154- }
155-
156- logger . success ( `Successfully processed ${ fileContents . length } staged source files` ) ;
157- if ( ! fileContents . length ) {
158- return { } ;
159- }
160-
161- return await getL10nJsonFromFileContents ( fileContents ) ;
162- }
163-
164139async function readJsonFile ( filePath , stagedFiles = [ ] ) {
165140 const normalizedPath = filePath . replace ( / \\ / g, "/" ) ;
166141 const content = stagedFiles . includes ( normalizedPath )
@@ -169,6 +144,18 @@ async function readJsonFile(filePath, stagedFiles = []) {
169144 return JSON . parse ( content ) ;
170145}
171146
147+ async function readJsonFileOrEmptyIfMissing ( filePath , stagedFiles = [ ] ) {
148+ try {
149+ return await readJsonFile ( filePath , stagedFiles ) ;
150+ } catch ( error ) {
151+ if ( error . code === "ENOENT" ) {
152+ return { } ;
153+ }
154+
155+ throw error ;
156+ }
157+ }
158+
172159async function writeLocalizationOutputs ( extensionDir , xliffName , packageJSON , bundleJSON ) {
173160 const map = new Map ( ) ;
174161 map . set ( "package" , packageJSON ) ;
@@ -272,8 +259,11 @@ async function extractLocalizationStrings() {
272259async function extractLocalizationForPrecommit ( ) {
273260 const stagedFiles = getStagedFiles ( ) ;
274261 const affectedExtensions = getAffectedExtensionsForPrecommit ( ) ;
262+ const forceFullExtraction = stagedFiles . some ( ( file ) =>
263+ PRECOMMIT_FULL_EXTRACTION_INPUTS . includes ( file ) ,
264+ ) ;
275265 if ( ! affectedExtensions . length ) {
276- console . log ( "No staged localization inputs; skipping localization extraction." ) ;
266+ logger . info ( "No staged localization inputs; skipping localization extraction." ) ;
277267 return ;
278268 }
279269
@@ -282,28 +272,39 @@ async function extractLocalizationForPrecommit() {
282272 extensionDir ,
283273 EXTENSION_CONFIG [ extensionDir ] ,
284274 stagedFiles ,
275+ forceFullExtraction ,
285276 ) ;
286277 }
287278}
288279
289- async function extractLocalizationForExtensionPrecommit ( extensionDir , xliffName , stagedFiles ) {
280+ async function extractLocalizationForExtensionPrecommit (
281+ extensionDir ,
282+ xliffName ,
283+ stagedFiles ,
284+ forceFullExtraction = false ,
285+ ) {
290286 logger . header ( `Processing staged localization inputs for Extension: ${ extensionDir } ` ) ;
291287
292288 const extensionPath = path . resolve ( "extensions" , extensionDir ) ;
293289 const packageNlsPath = `extensions/${ extensionDir } /package.nls.json` ;
294290 const bundlePath = `extensions/${ extensionDir } /l10n/bundle.l10n.json` ;
295291
296292 try {
297- const existingBundleJSON = await readJsonFile ( bundlePath , stagedFiles ) . catch ( ( ) => ( { } ) ) ;
298- const stagedBundleJSON = await getL10nJsonFromStagedSourceFiles ( extensionDir , stagedFiles ) ;
299- const bundleJSON = { ...existingBundleJSON , ...stagedBundleJSON } ;
293+ const stagedSourceFiles = getStagedSourceFilesForExtension ( extensionDir , stagedFiles ) ;
294+ const bundleJSON =
295+ forceFullExtraction || stagedSourceFiles . length
296+ ? await getL10nJson ( extensionPath )
297+ : await readJsonFileOrEmptyIfMissing ( bundlePath , stagedFiles ) ;
300298
301299 logger . step ( "Loading package localization data..." ) ;
302- const packageJSON = await readJsonFile ( packageNlsPath , stagedFiles ) . catch ( ( error ) => {
300+ let packageJSON ;
301+ try {
302+ packageJSON = await readJsonFile ( packageNlsPath , stagedFiles ) ;
303+ logger . success ( "Loaded package.nls.json" ) ;
304+ } catch ( error ) {
303305 logger . warning ( `Could not load package.nls.json: ${ error . message } ` ) ;
304- return { } ;
305- } ) ;
306- logger . success ( "Loaded package.nls.json" ) ;
306+ packageJSON = { } ;
307+ }
307308
308309 const generatedFiles = await writeLocalizationOutputs (
309310 extensionDir ,
0 commit comments