addons-linter/scripts/firefox-schema-import

64 строки
1.6 KiB
JavaScript
Executable File

#!/usr/bin/env node
let filePath;
const fs = require('fs');
const path = require('path');
// @babel/register has to be required here because mixing commonjs and
// ESM modules as currently being done from this script isn't something
// that nodejs native ESM module loading does actually support.
require('@babel/register');
const schemaImport = require('../src/schema/firefox-schemas-import');
const firefoxDir = 'tmp/firefox';
const importedDir = 'src/schema/imported';
const updatesDir = 'src/schema/updates';
const arg = process.argv[2];
try {
fs.statSync(arg);
filePath = arg;
} catch (e) {
// eslint-disable-next-line no-console
console.error(`File not found: ${arg}\nUsage: ${process.argv[1]} filePath`);
process.exit(1);
}
function emptyDir(dir, matching) {
fs.readdirSync(dir).forEach((file) => {
if (!matching || matching.test(file)) {
fs.unlinkSync(path.join(dir, file));
}
});
}
try {
fs.mkdirSync(firefoxDir);
} catch (e) {
// The folder already existed, remove any old schema files.
emptyDir(firefoxDir);
}
// Remove the old schema files.
emptyDir(importedDir, /.*\.json$/);
emptyDir(importedDir, /index\.js$/);
schemaImport
.fetchSchemas({ inputPath: filePath, outputPath: firefoxDir })
.then(() => {
schemaImport.importSchemas(firefoxDir, updatesDir, importedDir);
})
.then(() => {
emptyDir(firefoxDir);
fs.rmdirSync(firefoxDir);
})
.catch((error) => {
/* eslint-disable no-console */
console.error(error.toString());
console.error(error.stack);
/* eslint-enable no-console */
process.exit(2);
});