From 9530f56a170531b4b05dd485c8b0df68866d5224 Mon Sep 17 00:00:00 2001 From: Paul Robert Lloyd Date: Sat, 15 May 2021 16:03:28 +0100 Subject: [PATCH] refactor: use fileURLToPath directly in place of __dirname --- helpers/get-fixture/index.js | 5 +---- packages/endpoint-media/index.js | 5 +---- packages/endpoint-micropub/index.js | 5 +---- packages/endpoint-share/index.js | 7 ++----- packages/endpoint-syndicate/index.js | 4 ---- packages/indiekit/config/defaults.js | 5 ++--- packages/indiekit/index.js | 4 ---- packages/syndicator-internet-archive/index.js | 3 +-- packages/syndicator-twitter/index.js | 5 +---- 9 files changed, 9 insertions(+), 34 deletions(-) diff --git a/helpers/get-fixture/index.js b/helpers/get-fixture/index.js index 6ed24a76f..96cd06977 100644 --- a/helpers/get-fixture/index.js +++ b/helpers/get-fixture/index.js @@ -1,15 +1,12 @@ import fs from 'node:fs'; -import path from 'node:path'; import {fileURLToPath} from 'node:url'; -const __dirname = path.dirname(fileURLToPath(import.meta.url)); - /** * @param {string} filename Fixture’s file name * @param {string} encoding String encoding * @returns {Promise|object} File contents */ export const getFixture = (filename, encoding = 'utf-8') => { - const file = path.resolve(__dirname, `../../fixtures/${filename}`); + const file = fileURLToPath(new URL(`../../fixtures/${filename}`, import.meta.url)) return fs.readFileSync(file, {encoding}); }; diff --git a/packages/endpoint-media/index.js b/packages/endpoint-media/index.js index 4d6ed04d8..0ac28fde9 100644 --- a/packages/endpoint-media/index.js +++ b/packages/endpoint-media/index.js @@ -1,14 +1,11 @@ import express from 'express'; import multer from 'multer'; import {fileURLToPath} from 'node:url'; -import path from 'node:path'; import {uploadController} from './lib/controllers/upload.js'; import {filesController} from './lib/controllers/files.js'; import {queryController} from './lib/controllers/query.js'; import {locales} from './locales/index.js'; -export const __dirname = path.dirname(fileURLToPath(import.meta.url)); - const defaults = { mountpath: '/media' }; @@ -44,7 +41,7 @@ export const MediaEndpoint = class { routes: () => this.routes(application, publication) }); - indiekitConfig.addView(path.join(__dirname, 'views')); + indiekitConfig.addView(fileURLToPath(new URL('views', import.meta.url))); indiekitConfig.set('publication.mediaEndpoint', this.mountpath); } diff --git a/packages/endpoint-micropub/index.js b/packages/endpoint-micropub/index.js index 30ef575de..431cf9e78 100644 --- a/packages/endpoint-micropub/index.js +++ b/packages/endpoint-micropub/index.js @@ -1,14 +1,11 @@ import express from 'express'; import multer from 'multer'; -import path from 'node:path'; import {fileURLToPath} from 'node:url'; import {actionController} from './lib/controllers/action.js'; import {postsController} from './lib/controllers/posts.js'; import {queryController} from './lib/controllers/query.js'; import {locales} from './locales/index.js'; -export const __dirname = path.dirname(fileURLToPath(import.meta.url)); - const defaults = { mountpath: '/micropub' }; @@ -44,7 +41,7 @@ export const MicropubEndpoint = class { routes: () => this.routes(application, publication) }); - indiekitConfig.addView(path.join(__dirname, 'views')); + indiekitConfig.addView(fileURLToPath(new URL('views', import.meta.url))); indiekitConfig.set('publication.micropubEndpoint', this.mountpath); } diff --git a/packages/endpoint-share/index.js b/packages/endpoint-share/index.js index 36d623c1e..71a1acabb 100644 --- a/packages/endpoint-share/index.js +++ b/packages/endpoint-share/index.js @@ -1,12 +1,9 @@ import express from 'express'; -import path from 'node:path'; import {fileURLToPath} from 'node:url'; import {locales} from './locales/index.js'; import {shareController} from './lib/controllers/share.js'; import {validate} from './lib/middleware/validation.js'; -export const __dirname = path.dirname(fileURLToPath(import.meta.url)); - const defaults = { mountpath: '/share' }; @@ -41,8 +38,8 @@ export const ShareEndpoint = class { }); indiekitConfig.addView([ - path.join(__dirname, 'includes'), - path.join(__dirname, 'views') + fileURLToPath(new URL('includes', import.meta.url)), + fileURLToPath(new URL('views', import.meta.url)) ]); indiekitConfig.set('application.shareEndpoint', this.mountpath); diff --git a/packages/endpoint-syndicate/index.js b/packages/endpoint-syndicate/index.js index a72aa16c2..9f63fc30e 100644 --- a/packages/endpoint-syndicate/index.js +++ b/packages/endpoint-syndicate/index.js @@ -1,10 +1,6 @@ import express from 'express'; -import path from 'node:path'; -import {fileURLToPath} from 'node:url'; import {syndicateController} from './lib/controllers/syndicate.js'; -export const __dirname = path.dirname(fileURLToPath(import.meta.url)); - const defaults = { mountpath: '/syndicate' }; diff --git a/packages/indiekit/config/defaults.js b/packages/indiekit/config/defaults.js index ebff67627..c2e92be66 100644 --- a/packages/indiekit/config/defaults.js +++ b/packages/indiekit/config/defaults.js @@ -9,8 +9,7 @@ import {authenticate} from '../lib/middleware/authentication.js'; import {indieauth} from '../lib/middleware/indieauth.js'; import {locales} from '../locales/index.js'; -const __dirname = path.dirname(fileURLToPath(import.meta.url)); -const packagePath = path.join(__dirname, '..', 'package.json'); +const packagePath = fileURLToPath(new URL('../package.json', import.meta.url)); const package_ = JSON.parse(fs.readFileSync(packagePath, 'utf8')); const mediaEndpoint = new MediaEndpoint(); @@ -40,7 +39,7 @@ const application = { themeColorScheme: 'automatic', version: package_.version, views: [ - path.join(__dirname, '..', 'views') + fileURLToPath(new URL('../views', import.meta.url)) ] }; diff --git a/packages/indiekit/index.js b/packages/indiekit/index.js index 6070dceaf..8b1d9d75c 100644 --- a/packages/indiekit/index.js +++ b/packages/indiekit/index.js @@ -1,6 +1,4 @@ import _ from 'lodash'; -import path from 'node:path'; -import {fileURLToPath} from 'node:url'; import {defaultConfig} from './config/defaults.js'; import {mongodbConfig} from './config/mongodb.js'; import {serverConfig} from './config/server.js'; @@ -11,8 +9,6 @@ import { getPostTypes } from './lib/publication.js'; -export const __dirname = path.dirname(fileURLToPath(import.meta.url)); - export const Indiekit = class { constructor(config) { this._config = config || defaultConfig; diff --git a/packages/syndicator-internet-archive/index.js b/packages/syndicator-internet-archive/index.js index 70771c797..33d59d6d0 100644 --- a/packages/syndicator-internet-archive/index.js +++ b/packages/syndicator-internet-archive/index.js @@ -4,7 +4,6 @@ import path from 'node:path'; import {internetArchive} from './lib/internet-archive.js'; export const debug = new Debug('indiekit:syndicator-internet-archive'); -export const __dirname = path.dirname(fileURLToPath(import.meta.url)); const defaults = { checked: false, @@ -20,7 +19,7 @@ export const InternetArchiveSyndicator = class { } get assetsPath() { - return path.join(__dirname, 'assets'); + return fileURLToPath(new URL('assets', import.meta.url)); } get info() { diff --git a/packages/syndicator-twitter/index.js b/packages/syndicator-twitter/index.js index 82c907b47..cbe83434f 100644 --- a/packages/syndicator-twitter/index.js +++ b/packages/syndicator-twitter/index.js @@ -1,9 +1,6 @@ import {fileURLToPath} from 'node:url'; -import path from 'node:path'; import {twitter} from './lib/twitter.js'; -export const __dirname = path.dirname(fileURLToPath(import.meta.url)); - const defaults = { checked: false }; @@ -16,7 +13,7 @@ export const TwitterSyndicator = class { } get assetsPath() { - return path.join(__dirname, 'assets'); + return fileURLToPath(new URL('assets', import.meta.url)); } get info() {