mit-license/test.js
Richie Bendall f0a7eb433f
chore: Change promises back to await
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
2019-06-21 20:13:50 +12:00

66 lines
1.8 KiB
JavaScript

const path = require('path');
const fs = require('fs');
const CSS = require('css');
const {
validDomainId
} = require('./routes/utils');
const {
promisify
} = require('util');
const readFile = promisify(fs.readFile);
const readdir = promisify(fs.readdir);
const hasFlag = require('has-flag')
let errored = false;
function report(content, fix) {
errored = true;
console.error(content);
if (fix && hasFlag("--fix")) fix()
}
(async () => {
const users = await readdir('users');
await users.forEach(async user => {
if (!user.endsWith('json')) report(`${user} is not a json file`, () => fs.unlink(path.join('users', user), () => {}))
if (!validDomainId(user.replace(".json", ""))) report(`${user} is not a valid domain id.`)
try {
const data = await readFile(path.join('users', user), "utf8")
try {
const u = JSON.parse(data);
if (!u.locked && !u.copyright) report(`Copyright not specified in ${user}`)
const stringified = JSON.stringify(u, 0, 2)
if (data !== stringified) report(`Non-regular formatting in ${user}`, () => fs.writeFile(path.join('users', user), stringified, () => {}))
} catch ({
message
}) {
report(`Invalid JSON in ${user} (${message})`)
}
} catch ({
message
}) {
report(`Unable to read ${user} (${message})`)
}
});
const themes = await readdir('themes');
await themes.forEach(async theme => {
if (theme.endsWith('css')) {
try {
const data = await readFile(path.join('themes', theme), "utf8")
try {
CSS.parse(data);
} catch ({
message
}) {
report(`Invalid CSS in ${theme} (${message})`)
}
} catch ({
message
}) {
report(`Unable to read ${theme} (${message})`)
}
}
});
if (errored) process.exit(1);
})()