Replace xhtml locale keys that only exist in ftl
This commit is contained in:
		
							parent
							
								
									ab845ddb35
								
							
						
					
					
						commit
						4d7f61ee0b
					
				@ -18,6 +18,9 @@ import details from "../package.json" assert { type: "json" };
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const { name, author, description, homepage, version, config } = details;
 | 
					const { name, author, description, homepage, version, config } = details;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const localeMessage = new Set();
 | 
				
			||||||
 | 
					const localeMessageMiss = new Set();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function copyFileSync(source, target) {
 | 
					function copyFileSync(source, target) {
 | 
				
			||||||
  var targetFile = target;
 | 
					  var targetFile = target;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -87,8 +90,12 @@ function dateFormat(fmt, date) {
 | 
				
			|||||||
function addAddonRefToFlt(fltContent) {
 | 
					function addAddonRefToFlt(fltContent) {
 | 
				
			||||||
  const lines = fltContent.split("\n");
 | 
					  const lines = fltContent.split("\n");
 | 
				
			||||||
  const prefixedLines = lines.map((line) => {
 | 
					  const prefixedLines = lines.map((line) => {
 | 
				
			||||||
    if (line.match(/^(?<message>[a-zA-Z]\S*)([ ]*=[ ]*)(?<pattern>.*)$/gm)) {
 | 
					    // https://regex101.com/r/lQ9x5p/1
 | 
				
			||||||
      // https://regex101.com/r/lQ9x5p/1
 | 
					    const match = line.match(
 | 
				
			||||||
 | 
					      /^(?<message>[a-zA-Z]\S*)([ ]*=[ ]*)(?<pattern>.*)$/m
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					    if (match) {
 | 
				
			||||||
 | 
					      localeMessage.add(match.groups.message);
 | 
				
			||||||
      return `${config.addonRef}-${line}`;
 | 
					      return `${config.addonRef}-${line}`;
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      return line;
 | 
					      return line;
 | 
				
			||||||
@ -97,6 +104,21 @@ function addAddonRefToFlt(fltContent) {
 | 
				
			|||||||
  return prefixedLines.join("\n");
 | 
					  return prefixedLines.join("\n");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function replaceLocaleIdInXHtml(input) {
 | 
				
			||||||
 | 
					  const matchs = [...input.matchAll(/(data-l10n-id)="(\S*)"/g)];
 | 
				
			||||||
 | 
					  matchs.map((match) => {
 | 
				
			||||||
 | 
					    if (localeMessage.has(match[2])) {
 | 
				
			||||||
 | 
					      input = input.replace(
 | 
				
			||||||
 | 
					        match[0],
 | 
				
			||||||
 | 
					        `${match[1]}="${config.addonRef}-${match[2]}"`
 | 
				
			||||||
 | 
					      );
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      localeMessageMiss.add(match[1]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					  return input;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function main() {
 | 
					async function main() {
 | 
				
			||||||
  const t = new Date();
 | 
					  const t = new Date();
 | 
				
			||||||
  const buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", t);
 | 
					  const buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", t);
 | 
				
			||||||
@ -142,9 +164,6 @@ async function main() {
 | 
				
			|||||||
  );
 | 
					  );
 | 
				
			||||||
  replaceTo.push(...Object.values(config));
 | 
					  replaceTo.push(...Object.values(config));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  replaceFrom.push(/(data-l10n-id=")(\S*")/gm);
 | 
					 | 
				
			||||||
  replaceTo.push(`$1${config.addonRef}-$2`);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  const optionsAddon = {
 | 
					  const optionsAddon = {
 | 
				
			||||||
    files: [
 | 
					    files: [
 | 
				
			||||||
      join(buildDir, "**/*.xhtml"),
 | 
					      join(buildDir, "**/*.xhtml"),
 | 
				
			||||||
@ -166,14 +185,28 @@ async function main() {
 | 
				
			|||||||
    processor: [addAddonRefToFlt],
 | 
					    processor: [addAddonRefToFlt],
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const replaceResultXhtml = sync({
 | 
				
			||||||
 | 
					    files: [join(buildDir, "addon/**/*.xhtml")],
 | 
				
			||||||
 | 
					    processor: [replaceLocaleIdInXHtml],
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  console.log(
 | 
					  console.log(
 | 
				
			||||||
    "[Build] Run replace in ",
 | 
					    "[Build] Run replace in ",
 | 
				
			||||||
    replaceResult
 | 
					    replaceResult
 | 
				
			||||||
      .filter((f) => f.hasChanged)
 | 
					      .filter((f) => f.hasChanged)
 | 
				
			||||||
      .map((f) => `${f.file} : ${f.numReplacements} / ${f.numMatches}`),
 | 
					      .map((f) => `${f.file} : ${f.numReplacements} / ${f.numMatches}`),
 | 
				
			||||||
    replaceResultFlt.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`)
 | 
					    replaceResultFlt.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`),
 | 
				
			||||||
 | 
					    replaceResultXhtml.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`)
 | 
				
			||||||
  );
 | 
					  );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (localeMessageMiss.size !== 0) {
 | 
				
			||||||
 | 
					    console.warn(
 | 
				
			||||||
 | 
					      `[Build] [Warn] Fluent message [${new Array(
 | 
				
			||||||
 | 
					        ...localeMessageMiss
 | 
				
			||||||
 | 
					      )}] do not exsit in addon's locale files.`
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  console.log("[Build] Replace OK");
 | 
					  console.log("[Build] Replace OK");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Walk the builds/addon/locale folder's sub folders and rename *.ftl to addonRef-*.ftl
 | 
					  // Walk the builds/addon/locale folder's sub folders and rename *.ftl to addonRef-*.ftl
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user