Auto-generate the src/schema/imported/index.js file (#1347)

This commit is contained in:
Mark Striemer 2017-06-13 14:33:58 -05:00 коммит произвёл GitHub
Родитель 1aa0ab93c6
Коммит 9843edb0ad
3 изменённых файлов: 102 добавлений и 53 удалений

Просмотреть файл

@ -48,7 +48,8 @@ try {
}
// Remove the old schema files.
emptyDir(importedDir, /.*.json$/);
emptyDir(importedDir, /.*\.json$/);
emptyDir(importedDir, /index\.js$/);
schemaImport.fetchSchemas(
{ inputPath: filePath, outputPath: firefoxDir, version })

Просмотреть файл

@ -466,11 +466,26 @@ function schemaFiles(basePath) {
}
function writeSchemasToFile(basePath, importedPath, loadedSchemas) {
const ids = Object.keys(loadedSchemas);
// Write out the schemas.
Object.keys(loadedSchemas).forEach((id) => {
ids.forEach((id) => {
const { file, schema } = loadedSchemas[id];
writeSchema(importedPath, file, schema);
});
// Write out the index.js to easily import all schemas.
const imports = ids.map((id) => {
const { file } = loadedSchemas[id];
const basename = path.basename(file);
return `import ${id} from './${basename}'`;
}).join(';\n');
const fileContents = `// This file is generated by the schema import script.
${imports};
export default [
${ids.join(',\n ')},
];
`;
fs.writeFileSync(path.join(importedPath, 'index.js'), fileContents);
}
function loadSchemasFromFile(basePath) {
@ -567,7 +582,9 @@ export function fetchSchemas({ inputPath, outputPath, version }) {
reject(error);
})
.on('end', () => {
fs.unlinkSync(tarballPath);
if (version && !inputPath) {
fs.unlinkSync(tarballPath);
}
resolve();
});
})

Просмотреть файл

@ -1,51 +1,82 @@
/*
This is a hack to workaround our tests running with Jest.
Jest does not provide `require.context` because dynamic imports don't work
with it's --watch feature.
// This file is generated by the schema import script.
So we're working around that and implementing `require.context` ourselves.
*/
/* istanbul ignore next */
if (typeof require.context === 'undefined') {
const fs = require('fs');
const path = require('path');
require.context = function(
base = '.', scanSubDirectories = false, regularExpression = /\.js$/) {
const files = {};
function readDirectory(directory) {
fs.readdirSync(directory).forEach((file) => {
const fullPath = path.resolve(directory, file);
if (fs.statSync(fullPath).isDirectory()) {
if (scanSubDirectories) {
readDirectory(fullPath);
}
return;
}
if (!regularExpression.test(fullPath)) {
return;
}
files[fullPath] = true;
});
}
readDirectory(path.resolve(__dirname, base));
function Module(file) {
return require(file);
}
Module.keys = () => Object.keys(files);
return Module;
};
}
const req = require.context('./', false, /\.json$/);
export default req.keys().map((key) => req(key));
import alarms from './alarms.json';
import bookmarks from './bookmarks.json';
import browserAction from './browser_action.json';
import browsingData from './browsing_data.json';
import commands from './commands.json';
import contextMenus from './context_menus.json';
import contextMenusInternal from './context_menus_internal.json';
import contextualIdentities from './contextual_identities.json';
import cookies from './cookies.json';
import devtools from './devtools.json';
import downloads from './downloads.json';
import events from './events.json';
import experiments from './experiments.json';
import extension from './extension.json';
import extension_protocol_handlers from './extension_protocol_handlers.json';
import extensionTypes from './extension_types.json';
import history from './history.json';
import i18n from './i18n.json';
import identity from './identity.json';
import idle from './idle.json';
import management from './management.json';
import manifest from './manifest.json';
import notifications from './notifications.json';
import omnibox from './omnibox.json';
import pageAction from './page_action.json';
import privacy from './privacy.json';
import runtime from './runtime.json';
import sessions from './sessions.json';
import sidebarAction from './sidebar_action.json';
import storage from './storage.json';
import tabs from './tabs.json';
import test from './test.json';
import theme from './theme.json';
import topSites from './top_sites.json';
import types from './types.json';
import url_overrides from './url_overrides.json';
import webNavigation from './web_navigation.json';
import webRequest from './web_request.json';
import windows from './windows.json';
export default [
alarms,
bookmarks,
browserAction,
browsingData,
commands,
contextMenus,
contextMenusInternal,
contextualIdentities,
cookies,
devtools,
downloads,
events,
experiments,
extension,
extension_protocol_handlers,
extensionTypes,
history,
i18n,
identity,
idle,
management,
manifest,
notifications,
omnibox,
pageAction,
privacy,
runtime,
sessions,
sidebarAction,
storage,
tabs,
test,
theme,
topSites,
types,
url_overrides,
webNavigation,
webRequest,
windows,
];