Skip to content

Commit

Permalink
refactor: use fileURLToPath directly in place of __dirname
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed May 15, 2021
1 parent b1b0692 commit 9530f56
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 34 deletions.
5 changes: 1 addition & 4 deletions helpers/get-fixture/index.js
Original file line number Diff line number Diff line change
@@ -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});
};
5 changes: 1 addition & 4 deletions packages/endpoint-media/index.js
Original file line number Diff line number Diff line change
@@ -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'
};
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 1 addition & 4 deletions packages/endpoint-micropub/index.js
Original file line number Diff line number Diff line change
@@ -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'
};
Expand Down Expand Up @@ -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);
}
Expand Down
7 changes: 2 additions & 5 deletions packages/endpoint-share/index.js
Original file line number Diff line number Diff line change
@@ -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'
};
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions packages/endpoint-syndicate/index.js
Original file line number Diff line number Diff line change
@@ -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'
};
Expand Down
5 changes: 2 additions & 3 deletions packages/indiekit/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -40,7 +39,7 @@ const application = {
themeColorScheme: 'automatic',
version: package_.version,
views: [
path.join(__dirname, '..', 'views')
fileURLToPath(new URL('../views', import.meta.url))
]
};

Expand Down
4 changes: 0 additions & 4 deletions packages/indiekit/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions packages/syndicator-internet-archive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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() {
Expand Down
5 changes: 1 addition & 4 deletions packages/syndicator-twitter/index.js
Original file line number Diff line number Diff line change
@@ -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
};
Expand All @@ -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() {
Expand Down

0 comments on commit 9530f56

Please sign in to comment.