Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor everything to ES Modules #1929

Merged
merged 14 commits into from
May 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const pluginPresets = {
'array-func': `all`,
import: `recommended`,
jsdoc: `recommended`,
markdown: `recommended`,
nuxt: `recommended`,
Expand Down Expand Up @@ -77,6 +78,14 @@ const enabledRuleParameters = {
'spaced-comment': [`always`],
'template-curly-spacing': [],

// eslint-plugin-import
'import/extensions': [`ignorePackages`],
'import/no-commonjs': [{ allowConditionalRequire: false }],
'import/no-dynamic-require': [],
'import/no-unresolved': [{
ignore: [`^fs/promises$`],
}],

// eslint-plugin-jsdoc
'jsdoc/check-alignment': [],
'jsdoc/check-indentation': [],
Expand Down Expand Up @@ -225,6 +234,7 @@ const warnRules = new Set([

const disabledRules = [
`no-console`,
`import/no-duplicates`,
`jsdoc/newline-after-description`,
`jsdoc/no-undefined-types`,
`jsdoc/require-description`,
Expand All @@ -241,7 +251,6 @@ const disabledRules = [
`unicorn/no-array-reduce`,
`unicorn/no-useless-undefined`,
`unicorn/prefer-node-protocol`,
`unicorn/prefer-module`,
`unicorn/prefer-spread`,
`vue/multiline-html-element-content-newline`,
`vue/singleline-html-element-content-newline`,
Expand Down Expand Up @@ -308,6 +317,16 @@ module.exports = {
'jsdoc/require-jsdoc': `off`,
},
},
{
files: [`**/*.cjs`, `server/**.js`],
parserOptions: {
sourceType: `script`,
},
rules: {
'import/no-commonjs': `off`,
'unicorn/prefer-module': `off`,
},
},
{
files: [`**/*.vue`],
},
Expand Down
46 changes: 19 additions & 27 deletions cli/build-plugin-data.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#!/usr/bin/env node

const path = require(`path`);
const {
readdir,
stat,
writeFile,
} = require(`fs/promises`);
const chalk = require(`chalk`);
const importJson = require(`../lib/import-json.js`);
import path from 'path';
import { fileURLToPath } from 'url';
import { readdir, writeFile } from 'fs/promises';
import chalk from 'chalk';
import importJson from '../lib/import-json.js';

const plugins = {
importPlugins: [],
Expand All @@ -17,10 +14,10 @@ const plugins = {

const allPreviousVersions = {};

const pluginDirectory = path.join(__dirname, `../plugins/`);
const pluginDirectoryUrl = new URL(`../plugins/`, import.meta.url);

(async () => {
const directoryEntries = await readdir(pluginDirectory, { withFileTypes: true });
const directoryEntries = await readdir(pluginDirectoryUrl, { withFileTypes: true });
const pluginKeys = directoryEntries.filter(entry => entry.isDirectory()).map(entry => entry.name);

for (const pluginKey of pluginKeys) {
Expand All @@ -46,11 +43,11 @@ const pluginDirectory = path.join(__dirname, `../plugins/`);
Object.keys(plugins.data).sort().map(key => [key, plugins.data[key]]),
);

const filename = path.join(pluginDirectory, `plugins.json`);
const filePath = fileURLToPath(new URL(`plugins.json`, pluginDirectoryUrl));

try {
await writeFile(filename, `${JSON.stringify(plugins, null, 2)}\n`, `utf8`);
console.log(chalk.green(`[Success]`), `Updated plugin data file`, filename);
await writeFile(filePath, `${JSON.stringify(plugins, null, 2)}\n`, `utf8`);
console.log(chalk.green(`[Success]`), `Updated plugin data file`, filePath);
process.exit(0);
}
catch (error) {
Expand All @@ -66,7 +63,7 @@ const pluginDirectory = path.join(__dirname, `../plugins/`);
*/
async function readPluginJson(pluginKey) {
try {
const pluginJson = await importJson(`${pluginKey}/plugin.json`, pluginDirectory);
const pluginJson = await importJson(`${pluginKey}/plugin.json`, pluginDirectoryUrl);
plugins.data[pluginKey].name = pluginJson.name;

if (pluginJson.previousVersions) {
Expand All @@ -90,20 +87,18 @@ async function readPluginJson(pluginKey) {
* @param {String} pluginKey The plugin key.
*/
async function readPluginImport(pluginKey) {
const importPath = path.join(pluginDirectory, pluginKey, `import.js`);
try {
await stat(importPath);
const importPlugin = require(importPath);
const importPlugin = await import(new URL(`${pluginKey}/import.js`, pluginDirectoryUrl));
plugins.importPlugins.push(pluginKey);
plugins.data[pluginKey].importPluginVersion = importPlugin.version;
}
catch (error) {
if (error.code === `ENOENT`) {
if (error.code === `ERR_MODULE_NOT_FOUND`) {
// ignore non-existing file
return;
}

console.error(error.message);
console.error(`Import plugin ${pluginKey} could not be parsed:`, error.message);
process.exit(1);
}
}
Expand All @@ -113,21 +108,19 @@ async function readPluginImport(pluginKey) {
* @param {String} pluginKey The plugin key.
*/
async function readPluginExport(pluginKey) {
const exportPath = path.join(pluginDirectory, pluginKey, `export.js`);
try {
await stat(exportPath);
const exportPlugin = require(exportPath);
const exportPlugin = await import(new URL(`${pluginKey}/export.js`, pluginDirectoryUrl));
plugins.exportPlugins.push(pluginKey);
plugins.data[pluginKey].exportPluginVersion = exportPlugin.version;
plugins.data[pluginKey].exportTests = [];
}
catch (error) {
if (error.code === `ENOENT`) {
if (error.code === `ERR_MODULE_NOT_FOUND`) {
// ignore non-existing file
return;
}

console.error(error.message);
console.error(`Export plugin ${pluginKey} could not be parsed:`, error.message);
process.exit(1);
}
}
Expand All @@ -137,9 +130,8 @@ async function readPluginExport(pluginKey) {
* @param {String} pluginKey The plugin key.
*/
async function readPluginExportTests(pluginKey) {
const exportTestsPath = path.join(pluginDirectory, pluginKey, `exportTests`);
try {
const exportTestFiles = await readdir(exportTestsPath);
const exportTestFiles = await readdir(new URL(`${pluginKey}/exportTests/`, pluginDirectoryUrl));
for (const test of exportTestFiles) {
const testKey = path.basename(test, path.extname(test));
plugins.data[pluginKey].exportTests.push(testKey);
Expand All @@ -151,7 +143,7 @@ async function readPluginExportTests(pluginKey) {
return;
}

console.error(error.message);
console.error(`Export tests for plugin ${pluginKey} could not be read:`, error.message);
process.exit(1);
}
}
15 changes: 8 additions & 7 deletions cli/build-register.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
#!/usr/bin/env node

const { readdir, writeFile } = require(`fs/promises`);
const path = require(`path`);
const chalk = require(`chalk`);
import { fileURLToPath } from 'url';
import { readdir, writeFile } from 'fs/promises';
import path from 'path';
import chalk from 'chalk';

const { Register } = require(`../lib/register.js`);
const importJson = require(`../lib/import-json.js`);
import { Register } from '../lib/register.js';
import importJson from '../lib/import-json.js';

let register;
let manufacturers;

const fixturesPath = path.join(__dirname, `../fixtures/`);
const fixturesPath = fileURLToPath(new URL(`../fixtures/`, import.meta.url));

(async () => {
try {
manufacturers = await importJson(`../fixtures/manufacturers.json`, __dirname);
manufacturers = await importJson(`../fixtures/manufacturers.json`, import.meta.url);
register = new Register(manufacturers);

await addFixturesToRegister();
Expand Down
38 changes: 20 additions & 18 deletions cli/build-test-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
* keeping the set as small as possible) and updates tests/test-fixtures.json and tests/test-fixtures.md.
*/

const { readdir, writeFile } = require(`fs/promises`);
const path = require(`path`);
const chalk = require(`chalk`);
import { fileURLToPath } from 'url';
import { readdir, writeFile } from 'fs/promises';
import path from 'path';
import chalk from 'chalk';

const { fixtureFromRepository } = require(`../lib/model.js`);
const importJson = require(`../lib/import-json.js`);
import { fixtureFromRepository } from '../lib/model.js';
import importJson from '../lib/import-json.js';

const fixtureFeaturesDirectory = path.join(__dirname, `../lib/fixture-features`);
const jsonFile = path.join(__dirname, `../tests/test-fixtures.json`);
const markdownFile = path.join(__dirname, `../tests/test-fixtures.md`);
const fixtureFeaturesDirectoryUrl = new URL(`../lib/fixture-features/`, import.meta.url);
const jsonPath = fileURLToPath(new URL(`../tests/test-fixtures.json`, import.meta.url));
const markdownPath = fileURLToPath(new URL(`../tests/test-fixtures.md`, import.meta.url));

/**
* @typedef {Object} FixtureFeature
Expand All @@ -33,7 +34,7 @@ const markdownFile = path.join(__dirname, `../tests/test-fixtures.md`);
*/

(async () => {
const register = await importJson(`../fixtures/register.json`, __dirname);
const register = await importJson(`../fixtures/register.json`, import.meta.url);

const fixtureFeatures = await getFixtureFeatures();
const featuresUsed = Object.fromEntries(fixtureFeatures.map(feature => [feature.id, 0]));// check which features each fixture supports
Expand Down Expand Up @@ -99,11 +100,11 @@ const markdownFile = path.join(__dirname, `../tests/test-fixtures.md`);
}

try {
await writeFile(jsonFile, `${JSON.stringify(fixtures, null, 2)}\n`, `utf8`);
console.log(chalk.green(`[Success]`), `Updated ${jsonFile}`);
await writeFile(jsonPath, `${JSON.stringify(fixtures, null, 2)}\n`, `utf8`);
console.log(chalk.green(`[Success]`), `Updated ${jsonPath}`);

await writeFile(markdownFile, await getMarkdownCode(fixtures, fixtureFeatures), `utf8`);
console.log(chalk.green(`[Success]`), `Updated ${markdownFile}`);
await writeFile(markdownPath, await getMarkdownCode(fixtures, fixtureFeatures), `utf8`);
console.log(chalk.green(`[Success]`), `Updated ${markdownPath}`);
}
catch (error) {
console.error(chalk.red(`[Fail]`), `Could not write test fixtures file:`, error);
Expand All @@ -117,18 +118,19 @@ const markdownFile = path.join(__dirname, `../tests/test-fixtures.md`);
async function getFixtureFeatures() {
const fixtureFeatures = [];

for (const featureFile of await readdir(fixtureFeaturesDirectory)) {
if (path.extname(featureFile) !== `.js`) {
for (const fileName of await readdir(fixtureFeaturesDirectoryUrl)) {
if (path.extname(fileName) !== `.js`) {
continue;
}

// module exports array of fix features
const fixtureFeatureFile = require(path.join(fixtureFeaturesDirectory, featureFile));
const fixtureFeatureFileUrl = new URL(fileName, fixtureFeaturesDirectoryUrl);
const { default: fixtureFeatureFile } = await import(fixtureFeatureFileUrl);

for (const [index, fixtureFeature] of fixtureFeatureFile.entries()) {
// default id
if (!(`id` in fixtureFeature)) {
fixtureFeature.id = path.basename(featureFile, `.js`);
fixtureFeature.id = path.basename(fileName, `.js`);
if (fixtureFeatureFile.length > 1) {
fixtureFeature.id += `-${index}`;
}
Expand All @@ -155,7 +157,7 @@ async function getFixtureFeatures() {
* @returns {Promise.<String>} A Promise that resolves to the markdown code to be used in a markdown file.
*/
async function getMarkdownCode(fixtures, fixtureFeatures) {
const manufacturers = await importJson(`../fixtures/manufacturers.json`, __dirname);
const manufacturers = await importJson(`../fixtures/manufacturers.json`, import.meta.url);

const mdLines = [
`# Test fixtures`,
Expand Down
12 changes: 7 additions & 5 deletions cli/debug-env-variables.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

const chalk = require(`chalk`);
import chalk from 'chalk';

const usedVariables = [
`ALLOW_SEARCH_INDEXING`,
Expand All @@ -23,10 +23,12 @@ console.log(`Process environment variables:`);
printVariables();
console.log();

require(`../lib/load-env-file`);

console.log(`Environment variables after reading .env:`);
printVariables();
import(`../lib/load-env-file.js`).then(() => {
console.log(`Environment variables after reading .env:`);
printVariables();
}).catch(error => {
throw error;
});

/**
* Prints all used environment variables and their values / unset
Expand Down
17 changes: 9 additions & 8 deletions cli/diff-plugin-outputs.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/usr/bin/env node

const path = require(`path`);
const minimist = require(`minimist`);
const chalk = require(`chalk`);
import minimist from 'minimist';
import chalk from 'chalk';

const diffPluginOutputs = require(`../lib/diff-plugin-outputs.js`);
const importJson = require(`../lib/import-json.js`);
import diffPluginOutputs from '../lib/diff-plugin-outputs.js';
import importJson from '../lib/import-json.js';

(async () => {
const plugins = await importJson(`../plugins/plugins.json`, __dirname);
const testFixtures = (await importJson(`../tests/test-fixtures.json`, __dirname)).map(
const plugins = await importJson(`../plugins/plugins.json`, import.meta.url);
const testFixtures = (await importJson(`../tests/test-fixtures.json`, import.meta.url)).map(
fixture => `${fixture.man}/${fixture.key}`,
);

Expand All @@ -23,11 +22,13 @@ const importJson = require(`../lib/import-json.js`);
cliArguments.testFix = cliArguments[`test-fix`];
cliArguments.fixtures = cliArguments._;

const scriptName = import.meta.url.split(`/`).slice(-2).join(`/`);

const helpMessage = [
`This script exports the given fixtures with the current version of the given plugin and diffs the results`,
`against the files exported with the comparePlugin at the state of the given Git reference.`,
`Fixtures have to be declared with the path to its file in the fixtures/ directory.`,
`Usage: node ${path.relative(process.cwd(), __filename)} -p <plugin-key> [-c <compare-plugin-key>] [-r <git-ref>] [ -t | <fixture> [<more fixtures>] ]`,
`Usage: node ${scriptName} -p <plugin-key> [-c <compare-plugin-key>] [-r <git-ref>] [ -t | <fixture> [<more fixtures>] ]`,
`Options:`,
` --plugin, -p: Which plugin should be used to output fixtures. Allowed values:`,
` ${plugins.exportPlugins.join(`, `)}`,
Expand Down
Loading