Skip to content

Commit

Permalink
Add XO linter (#10643)
Browse files Browse the repository at this point in the history
  • Loading branch information
LitoMore committed Mar 24, 2024
1 parent d66bdb1 commit bf69b6d
Show file tree
Hide file tree
Showing 30 changed files with 630 additions and 490 deletions.
2 changes: 1 addition & 1 deletion .github/renovate.json5
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
separateMajorMinor: false,

// We manually update digest dependencies (eg. hashes in Github actions)
digest: { enabled: false },
digest: {enabled: false},
}
8 changes: 4 additions & 4 deletions .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ jobs:
- name: Install dependencies
run: npm i --ignore-scripts --no-audit --no-fund
- name: Update major version in CDN URLs
run: node ./scripts/release/update-cdn-urls.js
run: ./scripts/release/update-cdn-urls.js
- name: Update SVGs count milestone
run: node ./scripts/release/update-svgs-count.js
run: ./scripts/release/update-svgs-count.js
- name: Update slugs table
run: node ./scripts/release/update-slugs-table.js
run: ./scripts/release/update-slugs-table.js
- name: Update SDK Typescript definitions
run: node ./scripts/release/update-sdk-ts-defs.js
run: ./scripts/release/update-sdk-ts-defs.js
- name: Commit version bump
uses: stefanzweifel/git-auto-commit-action@v5
with:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ jobs:
- name: Install dependencies
run: npm i --ignore-scripts --no-audit --no-fund
- name: Reformat to regular markdown
run: node ./scripts/release/reformat-markdown.js "${{ steps.get-version.outputs.version }}"
run: ./scripts/release/reformat-markdown.js "${{ steps.get-version.outputs.version }}"
- name: Update SDK Typescript definitions
run: node ./scripts/release/update-sdk-ts-defs.js
run: ./scripts/release/update-sdk-ts-defs.js
- name: Build NodeJS package
run: npm run build
- name: Deploy to NPM
Expand All @@ -65,7 +65,7 @@ jobs:
- id: get-version
uses: ./.github/actions/get-version
- name: Reformat to regular markdown
run: node ./scripts/release/reformat-markdown.js "${{ steps.get-version.outputs.version }}"
run: ./scripts/release/reformat-markdown.js "${{ steps.get-version.outputs.version }}"
- name: Configure GIT credentials
run: |
git config user.name "${GITHUB_ACTOR}"
Expand Down
8 changes: 4 additions & 4 deletions .jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
"aka": {
"description": "The brand is also known as (e.g. full length name or abbreviation)",
"type": "array",
"items": { "type": "string" }
"items": {"type": "string"}
},
"dup": {
"description": "Different brands that use the exact same icon",
"type": "array",
"items": { "$ref": "#/definitions/duplicate" }
"items": {"$ref": "#/definitions/duplicate"}
},
"loc": {
"description": "Localized names of the brand",
Expand All @@ -46,7 +46,7 @@
"old": {
"description": "Old names, for backwards compatibility",
"type": "array",
"items": { "type": "string" }
"items": {"type": "string"}
}
},
"minProperties": 1,
Expand Down Expand Up @@ -702,7 +702,7 @@
"icons": {
"description": "A list of brands",
"type": "array",
"items": { "$ref": "#/definitions/brand" }
"items": {"$ref": "#/definitions/brand"}
}
},
"additionalProperties": false,
Expand Down
9 changes: 0 additions & 9 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,3 @@

# We use our own formatting for the data files.
_data/simple-icons.json

# JavaScript templates are invalid JavaScript so cannot be formatted.
scripts/build/templates/*.js

# Generated JavaScript files don't need to be formatted
index.js
index.mjs
index.d.ts
sdk.js
3 changes: 2 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"singleQuote": true
"singleQuote": true,
"bracketSpacing": false
}
49 changes: 49 additions & 0 deletions .xo-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"prettier": true,
"space": 2,
"plugins": ["import"],
"rules": {
"n/no-unsupported-features": "off",
"n/no-unsupported-features/node-builtins": "off",
"n/file-extension-in-import": "off",
"sort-imports": [
"error",
{
"ignoreCase": false,
"ignoreDeclarationSort": true,
"ignoreMemberSort": false,
"memberSyntaxSortOrder": ["none", "all", "multiple", "single"],
"allowSeparatedGroups": false
}
],
"import/no-named-as-default": "off",
"import/extensions": "off",
"import/order": [
"error",
{
"groups": ["builtin", "external", "parent", "sibling", "index"],
"alphabetize": {
"order": "asc",
"caseInsensitive": true
},
"warnOnUnassignedImports": true,
"newlines-between": "never"
}
]
},
"overrides": [
{
"files": ["sdk.mjs", "sdk.d.ts"],
"nodeVersion": ">=14"
},
{
"files": [
"scripts/**/*",
"tests/**/*",
"svglint.config.mjs",
"svgo.config.mjs"
],
"nodeVersion": ">=18"
}
]
}
26 changes: 14 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"chalk": "5.3.0",
"editorconfig-checker": "5.1.5",
"esbuild": "0.19.4",
"eslint-plugin-import": "2.29.1",
"fake-diff": "1.0.0",
"fast-fuzzy": "1.12.0",
"get-relative-luminance": "1.0.0",
Expand All @@ -97,32 +98,33 @@
"markdown-link-check": "3.11.2",
"mocha": "10.2.0",
"named-html-entities-json": "1.0.0",
"prettier": "3.0.3",
"svg-path-bbox": "1.2.5",
"svg-path-segments": "1.0.0",
"svglint": "2.4.0",
"svgo": "3.0.2",
"svgpath": "2.6.0",
"typescript": "5.2.2"
"typescript": "5.2.2",
"xo": "0.58.0"
},
"scripts": {
"build": "node scripts/build/package.js",
"clean": "node scripts/build/clean.js",
"format": "prettier --cache --write .",
"lint": "npm run ourlint && npm run jslint && npm run jsonlint && npm run svglint && npm run wslint",
"ourlint": "node scripts/lint/ourlint.js",
"jslint": "prettier --cache --check .",
"jsonlint": "node scripts/lint/jsonlint.js",
"svglint": "svglint --ci $npm_config_icons",
"build": "./scripts/build/package.js",
"clean": "./scripts/build/clean.js",
"format": "prettier --cache --write --ignore-unknown '**/*.!(js|jsx|mjs|cjs|ts|tsx|mts|cts|svg)' && xo --fix",
"lint": "npm run ourlint && npm run prettierlint && npm run jslint && npm run jsonlint && npm run svglint && npm run wslint",
"ourlint": "./scripts/lint/ourlint.js",
"prettierlint": "prettier --cache --check --ignore-unknown '**/*.!(js|jsx|mjs|cjs|ts|tsx|mts|cts|svg)'",
"jslint": "xo",
"jsonlint": "./scripts/lint/jsonlint.js",
"svglint": "svglint --ci $npm_config_icons --config svglint.config.mjs",
"wslint": "editorconfig-checker",
"prepare": "husky",
"prepublishOnly": "npm run build",
"postpublish": "npm run clean",
"test": "mocha tests --reporter tests/min-reporter.cjs --inline-diffs",
"pretest": "npm run prepublishOnly",
"posttest": "npm run postpublish",
"get-filename": "node scripts/get-filename.js",
"add-icon-data": "node scripts/add-icon-data.js"
"get-filename": "./scripts/get-filename.js",
"add-icon-data": "./scripts/add-icon-data.js"
},
"engines": {
"node": ">=0.12.18"
Expand Down
29 changes: 16 additions & 13 deletions scripts/add-icon-data.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
#!/usr/bin/env node
import process from 'node:process';
import {ExitPromptError, checkbox, confirm, input} from '@inquirer/prompts';
import chalk from 'chalk';
import { input, confirm, checkbox, ExitPromptError } from '@inquirer/prompts';
import autocomplete from 'inquirer-autocomplete-standalone';
import {search} from 'fast-fuzzy';
import getRelativeLuminance from 'get-relative-luminance';
import { search } from 'fast-fuzzy';
import autocomplete from 'inquirer-autocomplete-standalone';
import {
URL_REGEX,
collator,
getIconsDataString,
titleToSlug,
normalizeColor,
titleToSlug,
} from '../sdk.mjs';
import { getJsonSchemaData, writeIconsData } from './utils.js';
import {getJsonSchemaData, writeIconsData} from './utils.js';

const iconsData = JSON.parse(await getIconsDataString());
const jsonSchema = await getJsonSchemaData();

const HEX_REGEX = /^#?[a-f0-9]{3,8}$/i;
const HEX_REGEX = /^#?[a-f\d]{3,8}$/i;

const aliasTypes = ['aka', 'old'].map((key) => ({
name: `${key} (${jsonSchema.definitions.brand.properties.aliases.properties[key].description})`,
Expand All @@ -25,7 +26,7 @@ const aliasTypes = ['aka', 'old'].map((key) => ({

const licenseTypes =
jsonSchema.definitions.brand.properties.license.oneOf[0].properties.type.enum.map(
(license) => ({ name: license, value: license }),
(license) => ({name: license, value: license}),
);

const isValidURL = (input) =>
Expand All @@ -35,7 +36,7 @@ const isValidHexColor = (input) =>
HEX_REGEX.test(input) || 'Must be a valid hex code.';

const isNewIcon = (input) =>
!iconsData.icons.find(
!iconsData.icons.some(
(icon) =>
icon.title === input || titleToSlug(icon.title) === titleToSlug(input),
) || 'This icon title or slug already exists.';
Expand Down Expand Up @@ -83,10 +84,10 @@ try {
? {
type: await autocomplete({
message: "What is the icon's license?",
source: async (input) => {
async source(input) {
input = (input || '').trim();
return input
? search(input, licenseTypes, { keySelector: (x) => x.value })
? search(input, licenseTypes, {keySelector: (x) => x.value})
: licenseTypes;
},
}),
Expand All @@ -107,12 +108,14 @@ try {
}).then(async (aliases) => {
const result = {};
for (const alias of aliases) {
// eslint-disable-next-line no-await-in-loop
result[alias] = await input({
message: `What ${alias} aliases would you like to add? (separate with commas)`,
}).then((aliases) =>
aliases.split(',').map((alias) => alias.trim()),
);
}

return result;
})
: undefined,
Expand All @@ -136,11 +139,11 @@ try {
console.log(chalk.red('\nAborted.'));
process.exit(1);
}
} catch (err) {
if (err instanceof ExitPromptError) {
} catch (error) {
if (error instanceof ExitPromptError) {
console.log(chalk.red('\nAborted.'));
process.exit(1);
}

throw err;
throw error;
}
38 changes: 23 additions & 15 deletions scripts/build/clean.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
#!/usr/bin/env node
/**
* @fileoverview
* Clean files built by the build process.
*/

import fs from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import { getDirnameFromImportMeta } from '../../sdk.mjs';
import process from 'node:process';
import {getDirnameFromImportMeta} from '../../sdk.mjs';

const __dirname = getDirnameFromImportMeta(import.meta.url);
const rootDirectory = path.resolve(__dirname, '..', '..');
const files = ['index.js', 'index.mjs', 'index.d.ts', 'sdk.js'];

const fileExists = (fpath) =>
new Promise((r) => fs.access(fpath, fs.constants.F_OK, (e) => r(!e)));
fs
.access(fpath, fs.constants.F_OK)
.then(() => true)
.catch(() => false);

Promise.all(
files.map(async (file) => {
const filepath = path.join(rootDirectory, file);
if (!(await fileExists(filepath))) {
console.error(`File ${file} does not exist, skipping...`);
return;
}
return fs.promises.unlink(filepath);
}),
).catch((error) => {
console.error(`Error cleaning files: ${error.message}`);
try {
Promise.all(
files.map(async (file) => {
const filepath = path.join(rootDirectory, file);
if (!(await fileExists(filepath))) {
console.error(`File ${file} does not exist, skipping...`);
return;
}

return fs.unlink(filepath);
}),
);
} catch (error) {
console.error('Error cleaning files:', error);
process.exit(1);
});
}

0 comments on commit bf69b6d

Please sign in to comment.