mirror of
https://github.com/JosunLP/BrowserExtensionTemplate.git
synced 2025-10-14 16:10:10 +00:00
Merge branch 'feature/vite' into dev
This commit is contained in:
commit
1723196db6
18 changed files with 127 additions and 220 deletions
|
@ -1,10 +0,0 @@
|
|||
// @ts-ignore
|
||||
const fs = require("fs");
|
||||
|
||||
function removeExport(file: string) {
|
||||
let content = fs.readFileSync(file, "utf8");
|
||||
content = content.replace("export {};", "");
|
||||
fs.writeFileSync(file, content);
|
||||
}
|
||||
|
||||
removeExport("./dist/js/background.js");
|
|
@ -1,86 +0,0 @@
|
|||
const path = require('path');
|
||||
// @ts-ignore
|
||||
const fs = require('fs');
|
||||
|
||||
const appConfigJson = JSON.parse(fs.readFileSync('./app.config.json', 'utf8'));
|
||||
const DEPLOY_ENTRY = "./public/";
|
||||
const DEPLOY_TARGET = "./dist/";
|
||||
|
||||
function deleteFolderRecursive(path: string) {
|
||||
if (fs.existsSync(path)) {
|
||||
fs.readdirSync(path).forEach(function (file: string) {
|
||||
const curPath = path + "/" + file;
|
||||
if (fs.lstatSync(curPath).isDirectory()) {
|
||||
deleteFolderRecursive(curPath);
|
||||
} else {
|
||||
fs.unlinkSync(curPath);
|
||||
}
|
||||
});
|
||||
fs.rmdirSync(path);
|
||||
}
|
||||
}
|
||||
|
||||
function findHtmlFilesRecursive(source: string): string[] {
|
||||
let files: string[] = [];
|
||||
const dir = fs.readdirSync(source);
|
||||
dir.forEach(function (file: string) {
|
||||
const sourceFile = path.join(source, file);
|
||||
const stat = fs.lstatSync(sourceFile);
|
||||
if (stat.isDirectory()) {
|
||||
files = files.concat(findHtmlFilesRecursive(sourceFile));
|
||||
} else {
|
||||
if (path.extname(sourceFile) == '.html') {
|
||||
files.push(sourceFile);
|
||||
}
|
||||
}
|
||||
});
|
||||
return files;
|
||||
}
|
||||
|
||||
function replaceKeywordsInHtmlFile(file: string) {
|
||||
let content = fs.readFileSync(file, 'utf8');
|
||||
const pairs = appConfigJson.htmlTemplatePairs;
|
||||
pairs.forEach(function (pair: object) {
|
||||
// @ts-ignore
|
||||
content = content.replaceAll(pair.key, pair.value);
|
||||
});
|
||||
file = file.replace("public\\", DEPLOY_TARGET);
|
||||
fs.writeFileSync(file, content);
|
||||
}
|
||||
|
||||
function buildHtmlFiles(source: string) {
|
||||
const files = findHtmlFilesRecursive(source);
|
||||
files.forEach(function (file: string) {
|
||||
replaceKeywordsInHtmlFile(file);
|
||||
});
|
||||
}
|
||||
|
||||
function mkdirSync(path: string) {
|
||||
try {
|
||||
fs.mkdirSync(path);
|
||||
} catch (e: any) {
|
||||
if (e.code != 'EEXIST') throw e;
|
||||
}
|
||||
}
|
||||
|
||||
function copyFiles(source: string, target: string) {
|
||||
const files = fs.readdirSync(source);
|
||||
files.forEach(function (file: string) {
|
||||
const sourceFile = path.join(source, file);
|
||||
const targetFile = path.join(target, file);
|
||||
const stat = fs.lstatSync(sourceFile);
|
||||
if (stat.isDirectory()) {
|
||||
mkdirSync(targetFile);
|
||||
copyFiles(sourceFile, targetFile);
|
||||
} else {
|
||||
fs.writeFileSync(targetFile, fs.readFileSync(sourceFile));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
deleteFolderRecursive(DEPLOY_TARGET);
|
||||
mkdirSync(DEPLOY_TARGET);
|
||||
copyFiles(DEPLOY_ENTRY, DEPLOY_TARGET);
|
||||
buildHtmlFiles(DEPLOY_ENTRY);
|
||||
|
||||
console.log("Deployed to " + DEPLOY_TARGET);
|
72
tools/parse.ts
Normal file
72
tools/parse.ts
Normal file
|
@ -0,0 +1,72 @@
|
|||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
const appConfig = JSON.parse(fs.readFileSync("./app.config.json", "utf8"));
|
||||
const DEPLOY_TARGET = "./dist/";
|
||||
|
||||
function findCssFileNames(source: string): string[] {
|
||||
let files: string[] = [];
|
||||
const dir = fs.readdirSync(source);
|
||||
dir.forEach(function (file: string) {
|
||||
const sourceFile = path.join(source, file);
|
||||
const stat = fs.lstatSync(sourceFile);
|
||||
if (stat.isDirectory()) {
|
||||
files = files.concat(findCssFileNames(sourceFile));
|
||||
} else {
|
||||
if (path.extname(sourceFile) == ".css") {
|
||||
files.push(file);
|
||||
}
|
||||
}
|
||||
});
|
||||
return files;
|
||||
}
|
||||
|
||||
function findHtmlFilesRecursive(source: string): string[] {
|
||||
let files: string[] = [];
|
||||
const dir = fs.readdirSync(source);
|
||||
dir.forEach(function (file: string) {
|
||||
const sourceFile = path.join(source, file);
|
||||
const stat = fs.lstatSync(sourceFile);
|
||||
if (stat.isDirectory()) {
|
||||
files = files.concat(findHtmlFilesRecursive(sourceFile));
|
||||
} else {
|
||||
if (path.extname(sourceFile) == ".html") {
|
||||
files.push(sourceFile);
|
||||
}
|
||||
}
|
||||
});
|
||||
return files;
|
||||
}
|
||||
|
||||
function replaceKeywordsInHtmlFile(file: string) {
|
||||
let content = fs.readFileSync(file, "utf8");
|
||||
const pairs: { key: string; value: string }[] = appConfig.htmlTemplatePairs;
|
||||
pairs.forEach(function (pair: { key: string; value: string }) {
|
||||
//@ts-ignore
|
||||
content = content.replaceAll(pair.key, pair.value);
|
||||
});
|
||||
file = file.replace("public\\", DEPLOY_TARGET);
|
||||
fs.writeFileSync(file, content);
|
||||
}
|
||||
|
||||
function buildHtmlFiles(source: string) {
|
||||
const files = findHtmlFilesRecursive(source);
|
||||
files.forEach(function (file: string) {
|
||||
replaceKeywordsInHtmlFile(file);
|
||||
});
|
||||
}
|
||||
|
||||
findCssFileNames(DEPLOY_TARGET).forEach((file: string) => {
|
||||
const files = findHtmlFilesRecursive(DEPLOY_TARGET);
|
||||
files.forEach(function (htmlFile: string) {
|
||||
let content = fs.readFileSync(htmlFile, "utf8");
|
||||
content = content.replace(
|
||||
"</head>",
|
||||
`<link rel="stylesheet" href="./assets/${file}">\n</head>`
|
||||
);
|
||||
fs.writeFileSync(htmlFile, content);
|
||||
});
|
||||
});
|
||||
|
||||
buildHtmlFiles(DEPLOY_TARGET);
|
||||
|
||||
console.log("Parsed Files: ", findHtmlFilesRecursive(DEPLOY_TARGET));
|
Loading…
Add table
Add a link
Reference in a new issue