Refactor the functions in main into separate functions
This commit is contained in:
parent
4d7f61ee0b
commit
3e6b744307
@ -18,8 +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 t = new Date();
|
||||||
const localeMessageMiss = new Set();
|
const buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", new Date());
|
||||||
|
const buildDir = "build";
|
||||||
|
|
||||||
function copyFileSync(source, target) {
|
function copyFileSync(source, target) {
|
||||||
var targetFile = target;
|
var targetFile = target;
|
||||||
@ -87,69 +88,32 @@ function dateFormat(fmt, date) {
|
|||||||
return fmt;
|
return fmt;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addAddonRefToFlt(fltContent) {
|
function renameLocaleFiles() {
|
||||||
const lines = fltContent.split("\n");
|
const localeDir = join(buildDir, "addon/locale");
|
||||||
const prefixedLines = lines.map((line) => {
|
const localeFolders = readdirSync(localeDir, { withFileTypes: true })
|
||||||
// https://regex101.com/r/lQ9x5p/1
|
.filter((dirent) => dirent.isDirectory())
|
||||||
const match = line.match(
|
.map((dirent) => dirent.name);
|
||||||
/^(?<message>[a-zA-Z]\S*)([ ]*=[ ]*)(?<pattern>.*)$/m
|
|
||||||
|
for (const localeSubFolder of localeFolders) {
|
||||||
|
const localeSubDir = join(localeDir, localeSubFolder);
|
||||||
|
const localeSubFiles = readdirSync(localeSubDir, {
|
||||||
|
withFileTypes: true,
|
||||||
|
})
|
||||||
|
.filter((dirent) => dirent.isFile())
|
||||||
|
.map((dirent) => dirent.name);
|
||||||
|
|
||||||
|
for (const localeSubFile of localeSubFiles) {
|
||||||
|
if (localeSubFile.endsWith(".ftl")) {
|
||||||
|
renameSync(
|
||||||
|
join(localeSubDir, localeSubFile),
|
||||||
|
join(localeSubDir, `${config.addonRef}-${localeSubFile}`)
|
||||||
);
|
);
|
||||||
if (match) {
|
|
||||||
localeMessage.add(match.groups.message);
|
|
||||||
return `${config.addonRef}-${line}`;
|
|
||||||
} else {
|
|
||||||
return line;
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
return prefixedLines.join("\n");
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function replaceLocaleIdInXHtml(input) {
|
function replaceString() {
|
||||||
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() {
|
|
||||||
const t = new Date();
|
|
||||||
const buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", t);
|
|
||||||
const buildDir = "build";
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
`[Build] BUILD_DIR=${buildDir}, VERSION=${version}, BUILD_TIME=${buildTime}, ENV=${[
|
|
||||||
env.NODE_ENV,
|
|
||||||
]}`
|
|
||||||
);
|
|
||||||
|
|
||||||
clearFolder(buildDir);
|
|
||||||
|
|
||||||
copyFolderRecursiveSync("addon", buildDir);
|
|
||||||
|
|
||||||
copyFileSync("update-template.json", "update.json");
|
|
||||||
|
|
||||||
await build({
|
|
||||||
entryPoints: ["src/index.ts"],
|
|
||||||
define: {
|
|
||||||
__env__: `"${env.NODE_ENV}"`,
|
|
||||||
},
|
|
||||||
bundle: true,
|
|
||||||
target: "firefox102",
|
|
||||||
outfile: join(buildDir, "addon/chrome/content/scripts/index.js"),
|
|
||||||
// Don't turn minify on
|
|
||||||
// minify: true,
|
|
||||||
}).catch(() => exit(1));
|
|
||||||
|
|
||||||
console.log("[Build] Run esbuild OK");
|
|
||||||
|
|
||||||
const replaceFrom = [
|
const replaceFrom = [
|
||||||
/__author__/g,
|
/__author__/g,
|
||||||
/__description__/g,
|
/__description__/g,
|
||||||
@ -180,14 +144,45 @@ async function main() {
|
|||||||
|
|
||||||
const replaceResult = sync(optionsAddon);
|
const replaceResult = sync(optionsAddon);
|
||||||
|
|
||||||
|
const localeMessage = new Set();
|
||||||
|
const localeMessageMiss = new Set();
|
||||||
|
|
||||||
const replaceResultFlt = sync({
|
const replaceResultFlt = sync({
|
||||||
files: [join(buildDir, "addon/**/*.ftl")],
|
files: [join(buildDir, "addon/**/*.ftl")],
|
||||||
processor: [addAddonRefToFlt],
|
processor: (fltContent) => {
|
||||||
|
const lines = fltContent.split("\n");
|
||||||
|
const prefixedLines = lines.map((line) => {
|
||||||
|
// 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}`;
|
||||||
|
} else {
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return prefixedLines.join("\n");
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const replaceResultXhtml = sync({
|
const replaceResultXhtml = sync({
|
||||||
files: [join(buildDir, "addon/**/*.xhtml")],
|
files: [join(buildDir, "addon/**/*.xhtml")],
|
||||||
processor: [replaceLocaleIdInXHtml],
|
processor: (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;
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
@ -206,32 +201,45 @@ async function main() {
|
|||||||
)}] do not exsit in addon's locale files.`
|
)}] do not exsit in addon's locale files.`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function esbuild() {
|
||||||
|
await build({
|
||||||
|
entryPoints: ["src/index.ts"],
|
||||||
|
define: {
|
||||||
|
__env__: `"${env.NODE_ENV}"`,
|
||||||
|
},
|
||||||
|
bundle: true,
|
||||||
|
target: "firefox102",
|
||||||
|
outfile: join(buildDir, "addon/chrome/content/scripts/index.js"),
|
||||||
|
// Don't turn minify on
|
||||||
|
// minify: true,
|
||||||
|
}).catch(() => exit(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
console.log(
|
||||||
|
`[Build] BUILD_DIR=${buildDir}, VERSION=${version}, BUILD_TIME=${buildTime}, ENV=${[
|
||||||
|
env.NODE_ENV,
|
||||||
|
]}`
|
||||||
|
);
|
||||||
|
|
||||||
|
clearFolder(buildDir);
|
||||||
|
|
||||||
|
copyFolderRecursiveSync("addon", buildDir);
|
||||||
|
|
||||||
|
copyFileSync("update-template.json", "update.json");
|
||||||
|
|
||||||
|
await esbuild();
|
||||||
|
|
||||||
|
console.log("[Build] Run esbuild OK");
|
||||||
|
|
||||||
|
replaceString();
|
||||||
|
|
||||||
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
|
||||||
const localeDir = join(buildDir, "addon/locale");
|
renameLocaleFiles();
|
||||||
const localeFolders = readdirSync(localeDir, { withFileTypes: true })
|
|
||||||
.filter((dirent) => dirent.isDirectory())
|
|
||||||
.map((dirent) => dirent.name);
|
|
||||||
|
|
||||||
for (const localeSubFolder of localeFolders) {
|
|
||||||
const localeSubDir = join(localeDir, localeSubFolder);
|
|
||||||
const localeSubFiles = readdirSync(localeSubDir, {
|
|
||||||
withFileTypes: true,
|
|
||||||
})
|
|
||||||
.filter((dirent) => dirent.isFile())
|
|
||||||
.map((dirent) => dirent.name);
|
|
||||||
|
|
||||||
for (const localeSubFile of localeSubFiles) {
|
|
||||||
if (localeSubFile.endsWith(".ftl")) {
|
|
||||||
renameSync(
|
|
||||||
join(localeSubDir, localeSubFile),
|
|
||||||
join(localeSubDir, `${config.addonRef}-${localeSubFile}`)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("[Build] Addon prepare OK");
|
console.log("[Build] Addon prepare OK");
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user