Skip to content

Commit

Permalink
Merge branch 'main' into eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
mldangelo committed Oct 3, 2023
2 parents 2af3bb1 + eddbc79 commit bca9bd4
Show file tree
Hide file tree
Showing 25 changed files with 1,288 additions and 345 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ This repository provides a self-hosted integration for Smile ID on the Web. It e
- `.gitignore`: Specifies files to ignore in git. These include [./dist](./dist) and [./build](./build).
- `.nvmrc`: Node Version Manager configuration file.
- `.nycrc`: [NYC](https://github.com/istanbuljs/nyc) configuration file.
- `esbuild.js`: [esbuild](https://esbuild.github.io/) configuration file.

## Testing

Expand Down
111 changes: 111 additions & 0 deletions esbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const fs = require("fs");
const path = require("path");
const esbuild = require("esbuild");
const rimraf = require("rimraf");

/**
* Ensures a directory exists. If not, creates it.
* @param {string} dirPath - The path to the directory.
*/
const ensureDirSync = (dirPath) => {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
};

/**
* Recursively copies files from source to destination with a filter option.
* @param {string} srcDir - The source directory.
* @param {string} destDir - The destination directory.
* @param {Function} filterFn - A function to filter which files to copy.
*/
const copySync = (srcDir, destDir, filterFn) => {
const entries = fs.readdirSync(srcDir, { withFileTypes: true });

// eslint-disable-next-line no-restricted-syntax
for (const entry of entries) {
const srcPath = path.join(srcDir, entry.name);
const destPath = path.join(destDir, entry.name);

if (entry.isDirectory()) {
ensureDirSync(destPath);
copySync(srcPath, destPath, filterFn);
} else if (!filterFn || filterFn(entry.name)) {
fs.copyFileSync(srcPath, destPath);
}
}
};

/**
* Copies specific files from source to destination based on a pattern.
* @param {string} srcDir - The source directory.
* @param {string} pattern - A wildcard pattern to filter files. e.g., '*.html'
* @param {string} destDir - The destination directory.
*/
const copyFiles = (srcDir, pattern, destDir) => {
const files = fs
.readdirSync(srcDir)
.filter((file) => file.match(new RegExp(pattern.replace("*", ".*"))));
files.forEach((file) => {
const srcPath = path.join(srcDir, file);
const destPath = path.join(destDir, file);
fs.copyFileSync(srcPath, destPath);
});
};

/**
* Performs preparatory tasks for production mode.
* Clears the 'dist' directory, ensures it exists,
* and copies necessary files.
*/
const prebuildDist = () => {
rimraf.sync("dist");
ensureDirSync("dist");
copySync("src", "dist", (file) => !file.endsWith(".js"));
};

/**
* Performs preparatory tasks for development mode.
* Clears the 'build' directory, ensures it exists,
* copies necessary files, and copies HTML pages from cypress.
*/
const prebuild = () => {
rimraf.sync("build");
ensureDirSync("build");
copySync("src", "build", (file) => !file.endsWith(".js"));
copyFiles("cypress/pages", "*.html", "build");
};

const files = fs.readdirSync("src/js").filter((file) => file.endsWith(".js"));

if (process.env.NODE_ENV === "development") {
prebuild();
} else {
prebuildDist();
}

const devOptions = {
bundle: true,
};

const prodOptions = {
bundle: true,
minify: true,
};

files.forEach((file) => {
const baseName = path.basename(file, ".js");
if (process.env.NODE_ENV === "development") {
esbuild.build({
...devOptions,
entryPoints: [`src/js/${file}`],
outfile: `build/js/${baseName}.min.js`,
});
} else {
esbuild.build({
...prodOptions,
entryPoints: [`src/js/${file}`],
outfile: `dist/js/${baseName}.min.js`,
});
}
});
13 changes: 0 additions & 13 deletions libs/jszip.min.js

This file was deleted.

9 changes: 0 additions & 9 deletions libs/validate.min.js

This file was deleted.

Loading

0 comments on commit bca9bd4

Please sign in to comment.