Use import.meta.dirname & import.meta.filename

This commit is contained in:
LitoMore 2024-04-27 04:57:55 +08:00
parent 32e8072d52
commit 30ef9680cd
No known key found for this signature in database
GPG key ID: B8653F9344667340
13 changed files with 27 additions and 79 deletions

View file

@ -47,7 +47,7 @@
"svglint.config.mjs",
"svgo.config.mjs"
],
"nodeVersion": ">=18"
"nodeVersion": ">=20.11"
}
]
}

View file

@ -7,10 +7,8 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import {getDirnameFromImportMeta} from '../../sdk.mjs';
const __dirname = getDirnameFromImportMeta(import.meta.url);
const rootDirectory = path.resolve(__dirname, '..', '..');
const rootDirectory = path.resolve(import.meta.dirname, '..', '..');
const files = ['index.js', 'index.mjs', 'index.d.ts', 'sdk.js'];
const fileExists = (fpath) =>

View file

@ -10,7 +10,6 @@ import util from 'node:util';
import {transform as esbuildTransform} from 'esbuild';
import {
collator,
getDirnameFromImportMeta,
getIconSlug,
getIconsData,
slugToVariableName,
@ -18,11 +17,9 @@ import {
titleToHtmlFriendly,
} from '../../sdk.mjs';
const __dirname = getDirnameFromImportMeta(import.meta.url);
const UTF8 = 'utf8';
const rootDirectory = path.resolve(__dirname, '..', '..');
const rootDirectory = path.resolve(import.meta.dirname, '..', '..');
const iconsDirectory = path.resolve(rootDirectory, 'icons');
const indexJsFile = path.resolve(rootDirectory, 'index.js');
const indexMjsFile = path.resolve(rootDirectory, 'index.mjs');
@ -30,7 +27,7 @@ const sdkJsFile = path.resolve(rootDirectory, 'sdk.js');
const sdkMjsFile = path.resolve(rootDirectory, 'sdk.mjs');
const indexDtsFile = path.resolve(rootDirectory, 'index.d.ts');
const templatesDirectory = path.resolve(__dirname, 'templates');
const templatesDirectory = path.resolve(import.meta.dirname, 'templates');
const iconObjectTemplateFile = path.resolve(
templatesDirectory,
'icon-object.js.template',

View file

@ -7,13 +7,10 @@
import {readFile, writeFile} from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import {getDirnameFromImportMeta} from '../../sdk.mjs';
const LINKS_BRANCH = process.argv[2] || 'develop';
const __dirname = getDirnameFromImportMeta(import.meta.url);
const rootDirectory = path.resolve(__dirname, '..', '..');
const rootDirectory = path.resolve(import.meta.dirname, '..', '..');
const readmeFile = path.resolve(rootDirectory, 'README.md');
const disclaimerFile = path.resolve(rootDirectory, 'DISCLAIMER.md');

View file

@ -8,11 +8,8 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import {getDirnameFromImportMeta} from '../../sdk.mjs';
const __dirname = getDirnameFromImportMeta(import.meta.url);
const rootDirectory = path.resolve(__dirname, '..', '..');
const rootDirectory = path.resolve(import.meta.dirname, '..', '..');
const packageJsonFile = path.resolve(rootDirectory, 'package.json');
const readmeFile = path.resolve(rootDirectory, 'README.md');

View file

@ -9,10 +9,8 @@ import {execSync} from 'node:child_process';
import fs from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import {getDirnameFromImportMeta} from '../../sdk.mjs';
const __dirname = getDirnameFromImportMeta(import.meta.url);
const rootDirectory = path.resolve(__dirname, '..', '..');
const rootDirectory = path.resolve(import.meta.dirname, '..', '..');
const sdkTs = path.resolve(rootDirectory, 'sdk.d.ts');
const sdkMts = path.resolve(rootDirectory, 'sdk.d.mts');

View file

@ -6,18 +6,14 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {getIconSlug, getIconsData} from '../../sdk.mjs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const rootDirectory = path.resolve(__dirname, '..', '..');
const rootDirectory = path.resolve(import.meta.dirname, '..', '..');
const slugsFile = path.resolve(rootDirectory, 'slugs.md');
let content = `<!--
This file is automatically generated. If you want to change something, please
update the script at '${path.relative(rootDirectory, __filename)}'.
update the script at '${path.relative(rootDirectory, import.meta.filename)}'.
-->
# Simple Icons slugs

View file

@ -9,13 +9,12 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import {getDirnameFromImportMeta, getIconsData} from '../../sdk.mjs';
import {getIconsData} from '../../sdk.mjs';
const regexMatcher = /Over\s(\d+)\s/;
const updateRange = 100;
const __dirname = getDirnameFromImportMeta(import.meta.url);
const rootDirectory = path.resolve(__dirname, '..', '..');
const rootDirectory = path.resolve(import.meta.dirname, '..', '..');
const readmeFile = path.resolve(rootDirectory, 'README.md');
const readmeContent = await fs.readFile(readmeFile, 'utf8');

View file

@ -1,15 +1,13 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import {getDirnameFromImportMeta, getIconDataPath} from '../sdk.mjs';
const __dirname = getDirnameFromImportMeta(import.meta.url);
import {getIconDataPath} from '../sdk.mjs';
/**
* Get JSON schema data.
* @param {String} rootDirectory Path to the root directory of the project.
*/
export const getJsonSchemaData = async (
rootDirectory = path.resolve(__dirname, '..'),
rootDirectory = path.resolve(import.meta.dirname, '..'),
) => {
const jsonSchemaPath = path.resolve(rootDirectory, '.jsonschema.json');
const jsonSchemaString = await fs.readFile(jsonSchemaPath, 'utf8');
@ -23,7 +21,7 @@ export const getJsonSchemaData = async (
*/
export const writeIconsData = async (
iconsData,
rootDirectory = path.resolve(__dirname, '..'),
rootDirectory = path.resolve(import.meta.dirname, '..'),
) => {
await fs.writeFile(
getIconDataPath(rootDirectory),

1
sdk.d.ts vendored
View file

@ -65,7 +65,6 @@ export type IconData = {
export const URL_REGEX: RegExp;
export const SVG_PATH_REGEX: RegExp;
export function getDirnameFromImportMeta(importMetaUrl: string): string;
export function getIconSlug(icon: IconData): string;
export function svgToPath(svg: string): string;
export function titleToSlug(title: string): string;

27
sdk.mjs
View file

@ -5,7 +5,6 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
/**
* @typedef {import("./sdk").ThirdPartyExtension} ThirdPartyExtension
@ -43,15 +42,6 @@ export const URL_REGEX = /^https:\/\/[^\s"']+$/;
*/
export const SVG_PATH_REGEX = /^m[-mzlhvcsqtae\d,. ]+$/i;
/**
* Get the directory name where this file is located from `import.meta.url`,
* equivalent to the `__dirname` global variable in CommonJS.
* @param {String} importMetaUrl import.meta.url
* @returns {String} Directory name in which this file is located
*/
export const getDirnameFromImportMeta = (importMetaUrl) =>
path.dirname(fileURLToPath(importMetaUrl));
/**
* Get the slug/filename for an icon.
* @param {IconData} icon The icon data as it appears in *_data/simple-icons.json*
@ -129,9 +119,7 @@ export const htmlFriendlyToTitle = (htmlFriendlyTitle) =>
* @param {String} rootDirectory Path to the root directory of the project
* @returns {String} Path of *_data/simple-icons.json*
*/
export const getIconDataPath = (
rootDirectory = getDirnameFromImportMeta(import.meta.url),
) => {
export const getIconDataPath = (rootDirectory = import.meta.dirname) => {
return path.resolve(rootDirectory, '_data', 'simple-icons.json');
};
@ -140,9 +128,7 @@ export const getIconDataPath = (
* @param {String} rootDirectory Path to the root directory of the project
* @returns {String} Content of *_data/simple-icons.json*
*/
export const getIconsDataString = (
rootDirectory = getDirnameFromImportMeta(import.meta.url),
) => {
export const getIconsDataString = (rootDirectory = import.meta.dirname) => {
return fs.readFile(getIconDataPath(rootDirectory), 'utf8');
};
@ -151,9 +137,7 @@ export const getIconsDataString = (
* @param {String} rootDirectory Path to the root directory of the project
* @returns {IconData[]} Icons data as array from *_data/simple-icons.json*
*/
export const getIconsData = async (
rootDirectory = getDirnameFromImportMeta(import.meta.url),
) => {
export const getIconsData = async (rootDirectory = import.meta.dirname) => {
const fileContents = await getIconsDataString(rootDirectory);
return JSON.parse(fileContents).icons;
};
@ -190,10 +174,7 @@ export const normalizeColor = (text) => {
* @returns {Promise<ThirdPartyExtension[]>} Information about third party extensions
*/
export const getThirdPartyExtensions = async (
readmePath = path.join(
getDirnameFromImportMeta(import.meta.url),
'README.md',
),
readmePath = path.join(import.meta.dirname, 'README.md'),
) =>
normalizeNewlines(await fs.readFile(readmePath, 'utf8'))
.split('## Third-Party Extensions\n\n')[1]

View file

@ -5,22 +5,19 @@ import process from 'node:process';
import svgPathBbox from 'svg-path-bbox';
import parsePath from 'svg-path-segments';
import svgpath from 'svgpath';
import {
SVG_PATH_REGEX,
collator,
getDirnameFromImportMeta,
htmlFriendlyToTitle,
} from './sdk.mjs';
import {SVG_PATH_REGEX, collator, htmlFriendlyToTitle} from './sdk.mjs';
const __dirname = getDirnameFromImportMeta(import.meta.url);
const dataFile = path.join(__dirname, '_data', 'simple-icons.json');
const dataFile = path.join(import.meta.dirname, '_data', 'simple-icons.json');
const htmlNamedEntitiesFile = path.join(
__dirname,
import.meta.dirname,
'node_modules',
'named-html-entities-json',
'index.json',
);
const svglintIgnoredFile = path.join(__dirname, '.svglint-ignored.json');
const svglintIgnoredFile = path.join(
import.meta.dirname,
'.svglint-ignored.json',
);
const data = JSON.parse(await fs.readFile(dataFile, 'utf8'));
const htmlNamedEntities = JSON.parse(

View file

@ -2,18 +2,9 @@ import {strict as assert} from 'node:assert';
import fs from 'node:fs/promises';
import path from 'node:path';
import {describe, it} from 'mocha';
import {
SVG_PATH_REGEX,
URL_REGEX,
getDirnameFromImportMeta,
titleToSlug,
} from '../sdk.mjs';
import {SVG_PATH_REGEX, URL_REGEX, titleToSlug} from '../sdk.mjs';
const iconsDirectory = path.resolve(
getDirnameFromImportMeta(import.meta.url),
'..',
'icons',
);
const iconsDirectory = path.resolve(import.meta.dirname, '..', 'icons');
/**
* @typedef {import('..').SimpleIcon} SimpleIcon