Skip to content

Commit

Permalink
refactor: extract magix regex to a utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Aug 9, 2024
1 parent 0ef53ad commit f99b007
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
13 changes: 5 additions & 8 deletions lib/classes/eik-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import SingleDestMultipleSourcesError from './single-dest-multiple-source-error.
import FileMapping from './file-mapping.js';
import RemoteFileLocation from './remote-file-location.js';
import schemas from '../schemas/index.js';
import { removeTrailingSlash } from '../helpers/path-slashes.js';
import { removeTrailingSlash, ensurePosix } from '../helpers/path-slashes.js';
import typeSlug from '../helpers/type-slug.js';
import resolveFiles from '../helpers/resolve-files.js';

Expand Down Expand Up @@ -152,13 +152,10 @@ export default class EikConfig {
const shouldMapFilename = extname(destination);
const relativePathname = shouldMapFilename
? destination
: join(destination, localFile.relative).replace(/\\/g, '/');
const packagePathname = join(
// @ts-ignore
typeSlug(this.type),
this.name,
this.version,
).replace(/\\/g, '/');
: ensurePosix(join(destination, localFile.relative));
const packagePathname = ensurePosix(
join(typeSlug(this.type), this.name, this.version),
);

const remoteDestination = new RemoteFileLocation(
relativePathname,
Expand Down
3 changes: 2 additions & 1 deletion lib/classes/local-file-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import assert from 'node:assert';
import { join, extname, isAbsolute } from 'node:path';
// @ts-ignore
import mime from 'mime-types';
import { ensurePosix } from '../helpers/path-slashes.js';

/**
* Class containing information about a local file
Expand Down Expand Up @@ -36,7 +37,7 @@ class LocalFileLocation {
* @type {string} absolute path to file on disk,
* this is a concatentation of this.basePath and this.relative
*/
this.absolute = join(basePath, path).replace(/\\/g, '/');
this.absolute = ensurePosix(join(basePath, path));

/**
* @type {string} file extension with "." character included. (eg. ".json")
Expand Down
27 changes: 13 additions & 14 deletions lib/helpers/path-slashes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { sep } = require('path');
const { platform } = require('os');
import { sep } from 'path';
import { platform } from 'os';

/**
* Add a trailing slash to a path if necessary
Expand All @@ -8,7 +8,7 @@ const { platform } = require('os');
*
* @returns {string}
*/
const addTrailingSlash = (val) => (val.endsWith('/') ? val : `${val}/`);
export const addTrailingSlash = (val) => (val.endsWith('/') ? val : `${val}/`);

/**
* Remove a trailing slash from a path if necessary
Expand All @@ -17,7 +17,7 @@ const addTrailingSlash = (val) => (val.endsWith('/') ? val : `${val}/`);
*
* @returns {string}
*/
const removeTrailingSlash = (val) =>
export const removeTrailingSlash = (val) =>
// this is also used to trim from config files, which may now always use the OS's sep value. Look for both.
val.endsWith('/') || val.endsWith('\\')
? val.substr(0, val.length - 1)
Expand All @@ -30,7 +30,7 @@ const removeTrailingSlash = (val) =>
*
* @returns {string}
*/
const addLeadingSlash = (val) =>
export const addLeadingSlash = (val) =>
val.startsWith('/') || platform() === 'win32' ? val : `/${val}`;

/**
Expand All @@ -40,20 +40,19 @@ const addLeadingSlash = (val) =>
*
* @returns {string}
*/
const removeLeadingSlash = (val) =>
export const removeLeadingSlash = (val) =>
val.startsWith('/') || val.startsWith('\\') ? val.substr(1) : val;

/**
* Replaces a path string's separators (/ or \) with the current OS's sep value from node:path.
* @param {string} val
* @returns {string}
*/
const ensureOsSep = (val) => val.replace(/\/\\/g, sep);
export const ensureOsSep = (val) => val.replace(/\/\\/g, sep);

export {
ensureOsSep,
addTrailingSlash,
removeTrailingSlash,
addLeadingSlash,
removeLeadingSlash,
};
/**
* Replaces any backslash with a forward slash.
* @param {string} val
* @returns {string}
*/
export const ensurePosix = (val) => val.replace(/\\/g, '/');
5 changes: 3 additions & 2 deletions lib/helpers/resolve-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
addLeadingSlash,
removeLeadingSlash,
ensureOsSep,
ensurePosix,
} from './path-slashes.js';
import ResolvedFiles from '../classes/resolved-files.js';

Expand Down Expand Up @@ -68,8 +69,8 @@ const resolveFiles = async (files, cwd) =>

// convert glob pattern to forward slash separators
// https://www.npmjs.com/package/glob#windows
basePath = basePath.replace(/\\/g, '/');
pattern = pattern.replace(/\\/g, '/');
basePath = ensurePosix(basePath);
pattern = ensurePosix(pattern);

// process glob pattern into a list of existing files
const resolvedFiles = await glob(pattern, {
Expand Down
5 changes: 3 additions & 2 deletions test/classes/eik-config/mappings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import FileMapping from '../../../lib/classes/file-mapping.js';
import LocalFileLocation from '../../../lib/classes/local-file-location.js';
import RemoteFileLocation from '../../../lib/classes/remote-file-location.js';
import EikConfig from '../../../lib/classes/eik-config.js';
import { ensurePosix } from '../../../lib/helpers/path-slashes.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Expand Down Expand Up @@ -309,7 +310,7 @@ test('mappings - files is an object - mapped to folder - absolute path to folder
{
...validEikConfig,
files: {
folder: join(baseDir, 'folder').replace(/\\/g, '/'),
folder: ensurePosix(join(baseDir, 'folder')),
},
},
null,
Expand Down Expand Up @@ -496,7 +497,7 @@ test('mappings - files is an object - mapped to folder - absolute path to folder
{
...validEikConfig,
files: {
folder: join(baseDir, 'folder').replace(/\\/g, '/'),
folder: ensurePosix(join(baseDir, 'folder')),
},
},
null,
Expand Down
5 changes: 3 additions & 2 deletions test/helpers/resolve-files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fileURLToPath } from 'node:url';
import { join, basename, dirname } from 'node:path';
import tap from 'tap';
import resolveFiles from '../../lib/helpers/resolve-files.js';
import { ensurePosix } from '../../lib/helpers/path-slashes.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Expand Down Expand Up @@ -51,7 +52,7 @@ tap.test(
);
t.equal(
absolute,
join(basePath, relative).replace(/\\/g, '/'),
ensurePosix(join(basePath, relative)),
'.absolute should include .basePath and .relative',
);
t.match(
Expand Down Expand Up @@ -201,7 +202,7 @@ tap.test(
);
t.equal(
absolute,
join(basePath, relative).replace(/\\/g, '/'),
ensurePosix(join(basePath, relative)),
'.absolute should include .basePath and .relative',
);
t.match(
Expand Down

0 comments on commit f99b007

Please sign in to comment.