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

Cleanup and lint after ES Module refactoring #1947

Merged
merged 11 commits into from
May 25, 2021
24 changes: 14 additions & 10 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ const enabledRuleParameters = {
'no-multi-spaces': [],
'no-new-object': [],
'no-prototype-builtins': [],
'no-restricted-imports': [{
name: `fs`,
message: `Please use 'fs/promises' instead.`,
}],
'no-return-assign': [],
'no-return-await': [],
'no-shadow': [{ builtinGlobals: false }],
Expand Down Expand Up @@ -80,11 +84,20 @@ const enabledRuleParameters = {

// eslint-plugin-import
'import/extensions': [`ignorePackages`],
'import/first': [],
'import/newline-after-import': [],
'import/no-commonjs': [{ allowConditionalRequire: false }],
'import/no-dynamic-require': [],
'import/no-unresolved': [{
ignore: [`^fs/promises$`],
}],
'import/order': [{
groups: [`builtin`, `external`, `internal`, `parent`, `sibling`],
alphabetize: {
order: `asc`,
caseInsensitive: true,
},
}],

// eslint-plugin-jsdoc
'jsdoc/check-alignment': [],
Expand Down Expand Up @@ -128,15 +141,7 @@ const enabledRuleParameters = {
// eslint-plugin-unicorn
'unicorn/import-style': [{
styles: {
fs: {
unassigned: false,
default: false,
namespace: false,
named: false,
},
'fs/promises': {
named: true,
},
'fs/promises': { named: true },
},
}],
'unicorn/prevent-abbreviations': [{
Expand Down Expand Up @@ -234,7 +239,6 @@ const warnRules = new Set([

const disabledRules = [
`no-console`,
`import/no-duplicates`,
`jsdoc/newline-after-description`,
`jsdoc/no-undefined-types`,
`jsdoc/require-description`,
Expand Down
2 changes: 1 addition & 1 deletion cli/build-plugin-data.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

import { readdir, writeFile } from 'fs/promises';
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';

Expand Down
4 changes: 2 additions & 2 deletions cli/build-register.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env node

import { fileURLToPath } from 'url';
import { readdir, writeFile } from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import chalk from 'chalk';

import { Register } from '../lib/register.js';
import importJson from '../lib/import-json.js';
import { Register } from '../lib/register.js';

let register;
let manufacturers;
Expand Down
4 changes: 2 additions & 2 deletions cli/build-test-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
* keeping the set as small as possible) and updates tests/test-fixtures.json and tests/test-fixtures.md.
*/

import { fileURLToPath } from 'url';
import { readdir, writeFile } from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import chalk from 'chalk';

import { fixtureFromRepository } from '../lib/model.js';
import importJson from '../lib/import-json.js';
import { fixtureFromRepository } from '../lib/model.js';

const fixtureFeaturesDirectoryUrl = new URL(`../lib/fixture-features/`, import.meta.url);
const jsonPath = fileURLToPath(new URL(`../tests/test-fixtures.json`, import.meta.url));
Expand Down
2 changes: 1 addition & 1 deletion cli/diff-plugin-outputs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

import minimist from 'minimist';
import chalk from 'chalk';
import minimist from 'minimist';

import diffPluginOutputs from '../lib/diff-plugin-outputs.js';
import importJson from '../lib/import-json.js';
Expand Down
7 changes: 4 additions & 3 deletions cli/export-fixture.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/usr/bin/env node
import { fileURLToPath } from 'url';
import { mkdir, writeFile } from 'fs/promises';
import path from 'path';
import minimist from 'minimist';
import { fileURLToPath } from 'url';

import chalk from 'chalk';
import minimist from 'minimist';

import { fixtureFromRepository } from '../lib/model.js';
import importJson from '../lib/import-json.js';
import { fixtureFromRepository } from '../lib/model.js';

(async () => {
try {
Expand Down
6 changes: 3 additions & 3 deletions cli/import-fixture.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env node

import minimist from 'minimist';
import { readFile } from 'fs/promises';
import minimist from 'minimist';

import { checkFixture } from '../tests/fixture-valid.js';
import fixtureJsonStringify from '../lib/fixture-json-stringify.js';
import createPullRequest from '../lib/create-github-pr.js';
import fixtureJsonStringify from '../lib/fixture-json-stringify.js';
import importJson from '../lib/import-json.js';
import { checkFixture } from '../tests/fixture-valid.js';

/** @typedef {import('../lib/types.js').FixtureCreateResult} FixtureCreateResult */

Expand Down
6 changes: 3 additions & 3 deletions cli/run-export-test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env node
import path from 'path';
import minimist from 'minimist';
import chalk from 'chalk';
import { fileURLToPath } from 'url';
import chalk from 'chalk';
import minimist from 'minimist';

import { fixtureFromFile, fixtureFromRepository } from '../lib/model.js';
import importJson from '../lib/import-json.js';
import { fixtureFromFile, fixtureFromRepository } from '../lib/model.js';

(async () => {
try {
Expand Down
2 changes: 1 addition & 1 deletion docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ MY_ENV_VARIABLE=hello
NODE_ENV=production
```

Every script that uses environment variables must import [`lib/load-env-file.js`](../lib/load-env-file.js) first (usually placed right after all the other `import` calls).
Every script that uses environment variables must import [`lib/load-env-file.js`](../lib/load-env-file.js) as the very first statement.

Please update these docs and [`cli/debug-env-variables.js`](../cli/debug-env-variables.js) after introducing new variables.

Expand Down
2 changes: 0 additions & 2 deletions docs/fixture-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ if (panFine.coarseChannel.hasHighlightValue) {
}
```

If you want to use a model class directly, also import it via `model.js` (like this: `import { Meta } from './lib/model.js';`).

Model properties are always implemented using getters and setters. To store data, we use backing fields (an internal property prefixed with underscore, e.g. `_jsonObject`) to hold the data. The backing field should never be accessed directly, but only through its getter and setter functions (without underscore).

Avoid returning `undefined` by returning smart default values if necessary. If the default value is not `null`, also provide a `hasXY` boolean getter function. Properties that need further computation or create other objects should be cached in an internal `_cache` object.
Expand Down
6 changes: 3 additions & 3 deletions lib/create-github-pr.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fixtureJsonStringify from './fixture-json-stringify.js';
import { getObjectSortedByKeys } from './register.js';

import './load-env-file.js';

import { Octokit } from '@octokit/rest';

import fixtureJsonStringify from './fixture-json-stringify.js';
import { getObjectSortedByKeys } from './register.js';

/** @typedef {import('./types.js').FixtureCreateResult} FixtureCreateResult */

/**
Expand Down
11 changes: 6 additions & 5 deletions lib/diff-plugin-outputs.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import childProcess from 'child_process';
import { mkdir, readFile, rm, writeFile } from 'fs/promises';
import path from 'path';
import childProcess from 'child_process';
import JSZip from 'jszip';
import chalk from 'chalk';
import disparity from 'disparity';
import directoryCompare from 'dir-compare';
import { fileURLToPath } from 'url';
import { promisify } from 'util';

import chalk from 'chalk';
import directoryCompare from 'dir-compare';
import disparity from 'disparity';
import JSZip from 'jszip';

/**
* @typedef {Object} PluginDiffOutput
* @property {Array.<String>} removedFiles Removed outputted files' paths.
Expand Down
5 changes: 2 additions & 3 deletions lib/fixture-features/capability-types.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/** @typedef {import('../model/Fixture.js').default} Fixture */

import schemaProperties from '../../lib/schema-properties.js';
const capabilityTypes = Object.keys(schemaProperties.capabilityTypes);
import { capabilityTypes } from '../../lib/schema-properties.js';

export default capabilityTypes.map(type => ({
export default Object.keys(capabilityTypes).map(type => ({
id: `capability-type-${type}`,
name: `Capability type ${type}`,
description: `Whether the fixture has at least one capability of type '${type}'`,
Expand Down
2 changes: 1 addition & 1 deletion lib/fixture-features/fine-positions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FineChannel } from '../model.js';
import FineChannel from '../model/FineChannel.js';
/** @typedef {import('../model/Fixture.js').default} Fixture */

export default [
Expand Down
3 changes: 2 additions & 1 deletion lib/fixture-features/matrices.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FineChannel, SwitchingChannel } from '../model.js';
import FineChannel from '../model/FineChannel.js';
import SwitchingChannel from '../model/SwitchingChannel.js';
/** @typedef {import('../model/Fixture.js').default} Fixture */

export default [
Expand Down
4 changes: 2 additions & 2 deletions lib/fixture-features/redirect-reasons.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/** @typedef {import('../model/Fixture.js').default} Fixture */

import { fixtureRedirectProperties } from '../../lib/schema-properties.js';
import importJson from '../import-json.js';
import schemaProperties from '../../lib/schema-properties.js';

const registerPromise = importJson(`../../fixtures/register.json`, import.meta.url);

/** @type {Array.<String>} */
const redirectReasons = schemaProperties.fixtureRedirect.reason.enum;
const redirectReasons = fixtureRedirectProperties.reason.enum;

export default redirectReasons.map(reason => ({
id: `redirect-reason-${reason}`,
Expand Down
2 changes: 1 addition & 1 deletion lib/fixture-features/switching-channels.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FineChannel } from '../model.js';
import FineChannel from '../model/FineChannel.js';
/** @typedef {import('../model/Fixture.js').default} Fixture */

export default [
Expand Down
8 changes: 4 additions & 4 deletions lib/fixture-features/wheels.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/** @typedef {import('../model/Fixture.js').default} Fixture */

import schemaProperties from '../../lib/schema-properties.js';
const wheelSlotTypes = Object.keys(schemaProperties.wheelSlotTypes);
const wheelTypes = wheelSlotTypes.filter(type => !type.startsWith(`AnimationGobo`)).concat(`AnimationGobo`);
import { wheelSlotTypes } from '../../lib/schema-properties.js';

const wheelTypes = Object.keys(wheelSlotTypes).filter(type => !type.startsWith(`AnimationGobo`)).concat(`AnimationGobo`);

const wheelTypeFeatures = wheelTypes.map(type => ({
id: `wheel-type-${type}`,
Expand All @@ -18,7 +18,7 @@ const wheelTypeFeatures = wheelTypes.map(type => ({
),
}));

const wheelSlotTypeFeatures = wheelSlotTypes.map(type => ({
const wheelSlotTypeFeatures = Object.keys(wheelSlotTypes).map(type => ({
id: `wheel-slot-type-${type}`,
name: `Wheel slot type ${type}`,
description: `Whether the fixture has at least one wheel slot of type '${type}'`,
Expand Down
2 changes: 1 addition & 1 deletion lib/load-env-file.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import env from 'node-env-file';
import { fileURLToPath } from 'url';
import env from 'node-env-file';

const envFilePath = fileURLToPath(new URL(`../.env`, import.meta.url));
env(envFilePath, { raise: false });
53 changes: 6 additions & 47 deletions lib/model.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
import path from 'path';
import { readFile } from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';

import importJson from './import-json.js';

import AbstractChannel from './model/AbstractChannel.js';
import Capability from './model/Capability.js';
import CoarseChannel from './model/CoarseChannel.js';
import Entity from './model/Entity.js';
import FineChannel from './model/FineChannel.js';
import Fixture from './model/Fixture.js';
import Manufacturer from './model/Manufacturer.js';
import Matrix from './model/Matrix.js';
import Meta from './model/Meta.js';
import Mode from './model/Mode.js';
import NullChannel from './model/NullChannel.js';
import Physical from './model/Physical.js';
import Range from './model/Range.js';
import SwitchingChannel from './model/SwitchingChannel.js';
import TemplateChannel from './model/TemplateChannel.js';
import Wheel from './model/Wheel.js';
import WheelSlot from './model/WheelSlot.js';

/**
* Look up the fixture definition in the directory structure and create a Fixture instance.
* @param {String} absolutePath The fixture file absolute path, including filename.
* @returns {Promise.<Fixture, Error>} A Promise that resolves to the created Fixture instance or is rejected with a `MODULE_NOT_FOUND` error if the given fixture file does not exist.
*/
async function fixtureFromFile(absolutePath) {
export async function fixtureFromFile(absolutePath) {
let manufacturerKey = path.basename(path.dirname(absolutePath));
let fixtureKey = path.basename(absolutePath, path.extname(absolutePath));
let fixtureJson = await importJson(absolutePath);
Expand All @@ -50,7 +35,7 @@ async function fixtureFromFile(absolutePath) {
* @param {String} fixtureKey The fixture's key (filename without .json)
* @returns {Promise.<Fixture, Error>} A Promise that resolves to the created Fixture instance or is rejected with a `MODULE_NOT_FOUND` error if the given fixture file does not exist.
*/
async function fixtureFromRepository(manufacturerKey, fixtureKey) {
export async function fixtureFromRepository(manufacturerKey, fixtureKey) {
let fixturePath = `../fixtures/${manufacturerKey}/${fixtureKey}.json`;
let fixtureJson = await importJson(fixturePath, import.meta.url);

Expand All @@ -70,15 +55,15 @@ async function fixtureFromRepository(manufacturerKey, fixtureKey) {
* @param {String} manufacturerKey The manufacturer's key (directory name).
* @returns {Promise.<Manufacturer>} A Promise that resolves to the created Manufacturer instance.
*/
async function manufacturerFromRepository(manufacturerKey) {
export async function manufacturerFromRepository(manufacturerKey) {
const manufacturers = await importJson(`../fixtures/manufacturers.json`, import.meta.url);
return new Manufacturer(manufacturerKey, manufacturers[manufacturerKey]);
}

/**
* @param {Object} fixtureJson The fixture JSON to embed resoures into.
*/
async function embedResourcesIntoFixtureJson(fixtureJson) {
export async function embedResourcesIntoFixtureJson(fixtureJson) {
if (`wheels` in fixtureJson) {
for (const wheel of Object.values(fixtureJson.wheels)) {
for (const slot of wheel.slots) {
Expand All @@ -94,7 +79,7 @@ async function embedResourcesIntoFixtureJson(fixtureJson) {
* @param {String} resourceName The resource name, as specified in a fixture.
* @returns {Promise.<Object>} A Promise that resolves to the resource object to be embedded into the fixture.
*/
async function getResourceFromString(resourceName) {
export async function getResourceFromString(resourceName) {
const { type, key, alias } = await resolveResourceName(resourceName);

const resourceBaseUrl = new URL(`../resources/${type}/`, import.meta.url);
Expand Down Expand Up @@ -220,29 +205,3 @@ async function getImageForResource(type, baseUrl, key) {

return undefined;
}

export {
AbstractChannel,
Capability,
CoarseChannel,
Entity,
FineChannel,
Fixture,
Manufacturer,
Matrix,
Meta,
Mode,
NullChannel,
Physical,
Range,
SwitchingChannel,
TemplateChannel,
Wheel,
WheelSlot,

fixtureFromFile,
fixtureFromRepository,
manufacturerFromRepository,
embedResourcesIntoFixtureJson,
getResourceFromString,
};
4 changes: 2 additions & 2 deletions lib/model/Capability.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { scaleDmxRange } from '../scale-dmx-values.js';

/** @ignore @typedef {import('./CoarseChannel.js').default} CoarseChannel */
import Entity from './Entity.js';
import Range from './Range.js';
/** @ignore @typedef {import('./Wheel.js').default} Wheel */
/** @ignore @typedef {import('./WheelSlot.js').default} WheelSlot */

import { scaleDmxRange } from '../scale-dmx-values.js';

const START_END_ENTITIES = [`speed`, `duration`, `time`, `brightness`, `slotNumber`, `angle`, `horizontalAngle`, `verticalAngle`, `colorTemperature`, `soundSensitivity`, `shakeAngle`, `shakeSpeed`, `distance`, `openPercent`, `frostIntensity`, `insertion`, `fogOutput`, `parameter`];

const namePerType = {
Expand Down
Loading