Skip to content

Commit

Permalink
Change JS linter to ESlint
Browse files Browse the repository at this point in the history
Previous linter `lynt` had vulnerable dependencies that were not
addressed for some time. ESLint is better maintained and a popular
project. This commit fixes also linting errors from ESLint.
  • Loading branch information
undergroundwires committed Aug 17, 2021
1 parent e34c3b0 commit 26b9c52
Show file tree
Hide file tree
Showing 7 changed files with 2,507 additions and 6,692 deletions.
13 changes: 13 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
env:
browser: true
commonjs: true
es6: true
extends: airbnb-base
globals:
Atomics: readonly
SharedArrayBuffer: readonly
parserOptions:
ecmaVersion: 2018
rules: {
"no-use-before-define": 0
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ You're welcome to contribute your theme to the project in [`./src/themes`](./src

## Distributed files

The repository and deployed packages includes a [`dist/`](./dist) folder that adds polyfills to the files and distributes them as:
The repository and deployed packages includes a `dist/` folder that adds polyfills to the files and distributes them as:

- minified (`.min.js`, `.min.css`) files for production usage
- non-minified (`.js`, `.css`) files for debugging
Expand Down
149 changes: 76 additions & 73 deletions compile.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,100 @@
/* eslint-disable no-console */

const fs = require('fs');
const babel = require("@babel/core");
const path = require("path");
const babel = require('@babel/core');
const path = require('path');
const ClosureCompiler = require('google-closure-compiler').jsCompiler;
const CleanCSS = require('clean-css');

const outputFolder = 'dist';
const inputFile = './src/ez-consent.js';
const cssFolder = './src/themes/';

(async function() {
await deleteFolderRecursiveAsync(outputFolder);
await fs.promises.mkdir(outputFolder, { recursive: true });
const noModulesCode = (await babel.transformFileAsync(inputFile, {
plugins: ["remove-import-export"]
})).code;
await Promise.all([
compileWithClosureAsync(noModulesCode, path.join(outputFolder, path.basename(inputFile)), {
compilation_level: 'SIMPLE',
formatting: 'PRETTY_PRINT',
env: 'BROWSER',
debug: true
}),
compileWithClosureAsync(noModulesCode, path.join(outputFolder, removeExtension(path.basename(inputFile)) + ".min.js"), {
compilation_level: 'SIMPLE',
env: 'BROWSER'
}),
minifyCssAsync(cssFolder, path.join(outputFolder, 'themes'))
]);
})().catch(err => {
console.error(err);
process.exit(-1);
(async () => {
await deleteFolderRecursiveAsync(outputFolder);
await fs.promises.mkdir(outputFolder, { recursive: true });
const noModulesCode = (await babel.transformFileAsync(inputFile, {
plugins: ['remove-import-export'],
})).code;
await Promise.all([
compileWithClosureAsync(noModulesCode, path.join(outputFolder, path.basename(inputFile)), {
compilation_level: 'SIMPLE',
formatting: 'PRETTY_PRINT',
env: 'BROWSER',
debug: true,
}),
compileWithClosureAsync(noModulesCode, path.join(outputFolder, `${removeExtension(path.basename(inputFile))}.min.js`), {
compilation_level: 'SIMPLE',
env: 'BROWSER',
}),
minifyCssAsync(cssFolder, path.join(outputFolder, 'themes')),
]);
})().catch((err) => {
console.error(err);
process.exit(-1);
});


async function minifyCssAsync(srcDir, targetDir) {
const files = await fs.promises.readdir(srcDir);
const cleanCSS = new CleanCSS();
return Promise.all(files.map(async (name) => {
const filePath = path.join(srcDir, name);
// Copy as it is
const nonMinifiedTarget = path.join(targetDir, name);
await copyFile(filePath, nonMinifiedTarget);
console.log(`CSS copied to "${nonMinifiedTarget}"`);
// Minify content
const content = await fs.promises.readFile(filePath, 'utf-8');
const minified = cleanCSS.minify(content).styles;
const targetPath = path.join(targetDir, removeExtension(path.basename(name)) + ".min.css");
await fs.promises.writeFile(targetPath, minified);
console.log(`CSS minified to "${targetPath}"`);
}));
const files = await fs.promises.readdir(srcDir);
const cleanCSS = new CleanCSS();
return Promise.all(files.map(async (name) => {
const filePath = path.join(srcDir, name);
// Copy as it is
const nonMinifiedTarget = path.join(targetDir, name);
await copyFile(filePath, nonMinifiedTarget);
console.log(`CSS copied to "${nonMinifiedTarget}"`);
// Minify content
const content = await fs.promises.readFile(filePath, 'utf-8');
const minified = cleanCSS.minify(content).styles;
const targetPath = path.join(targetDir, `${removeExtension(path.basename(name))}.min.css`);
await fs.promises.writeFile(targetPath, minified);
console.log(`CSS minified to "${targetPath}"`);
}));
}

async function copyFile(src, dest) {
await fs.promises.mkdir(path.dirname(dest), {recursive: true});
await fs.promises.copyFile(src, dest);
await fs.promises.mkdir(path.dirname(dest), { recursive: true });
await fs.promises.copyFile(src, dest);
}

function removeExtension(str) {
return str.slice(0, -path.extname(str).length);
return str.slice(0, -path.extname(str).length);
}

async function compileWithClosureAsync(code, output, options) {
return new Promise((resolve, reject) => {
new ClosureCompiler(options).run([{
path: './',
src: code,
sourceMap: null
}], async (exitCode, stdOut, stdErr) => {
if(stdErr) {
console.error('[ERROR] Google Closure', stdErr);
}
if(exitCode !== 0) {
reject(new Error('Unexpected exit code : ' + exitCode));
}
for (const fileResult of stdOut) {
console.log(`JS compiled to "${output}", with options: ${JSON.stringify(options)}`);
await fs.promises.writeFile(output, fileResult.src);
}
resolve();
});
})
return new Promise((resolve, reject) => {
new ClosureCompiler(options).run([{
path: './',
src: code,
sourceMap: null,
}], async (exitCode, stdOut, stdErr) => {
if (stdErr) {
console.error('[ERROR] Google Closure', stdErr);
}
if (exitCode !== 0) {
reject(new Error(`Unexpected exit code : ${exitCode}`));
}
await Promise.all(stdOut.map(async (fileResult) => {
console.log(`JS compiled to "${output}", with options: ${JSON.stringify(options)}`);
await fs.promises.writeFile(output, fileResult.src);
}));
resolve();
});
});
}


async function deleteFolderRecursiveAsync(path) {
if (!fs.existsSync(path)) {
return;
}
for (const entry of await fs.promises.readdir(path)) {
const curPath = path + "/" + entry;
if ((await fs.promises.lstat(curPath)).isDirectory())
{await deleteFolderRecursiveAsync(curPath);}
else await fs.promises.unlink(curPath);
async function deleteFolderRecursiveAsync(folderPath) {
if (!fs.existsSync(folderPath)) {
return;
}
const directoryContents = await fs.promises.readdir(folderPath);
await Promise.all(directoryContents.map(async (innerFilePath) => {
const innerPath = `${folderPath}/${innerFilePath}`;
if ((await fs.promises.lstat(innerPath)).isDirectory()) {
await deleteFolderRecursiveAsync(innerPath);
} else {
await fs.promises.unlink(innerPath);
}
await fs.promises.rmdir(path);
}));
await fs.promises.rmdir(path);
}
Binary file modified img/gitops.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 26b9c52

Please sign in to comment.