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 Jul 8, 2024
1 parent 0bb61ce commit e68717f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
12 changes: 5 additions & 7 deletions lib/classes/eik-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const SingleDestMultipleSourcesError = require('./single-dest-multiple-source-er
const FileMapping = require('./file-mapping');
const RemoteFileLocation = require('./remote-file-location');
const schemas = require('../schemas');
const { removeTrailingSlash } = require('../helpers/path-slashes');
const { removeTrailingSlash, ensurePosix } = require('../helpers/path-slashes');
const typeSlug = require('../helpers/type-slug');
const resolveFiles = require('../helpers/resolve-files');

Expand Down Expand Up @@ -147,12 +147,10 @@ module.exports = class EikConfig {
const shouldMapFilename = extname(destination);
const relativePathname = shouldMapFilename
? destination
: join(destination, localFile.relative).replace(/\\/g, '/');
const packagePathname = join(
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 @@ -4,6 +4,7 @@
const assert = require('assert');
const { join, extname, isAbsolute } = require('path');
const mime = require('mime-types');
const { ensurePosix } = require('../helpers/path-slashes');

/**
* Class containing information about a local file
Expand Down Expand Up @@ -35,7 +36,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
8 changes: 8 additions & 0 deletions lib/helpers/path-slashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,16 @@ const removeLeadingSlash = (val) =>
*/
const ensureOsSep = (val) => val.replace(/\/\\/g, sep);

/**
* Replaces any backslash with a forward slash.
* @param {string} val
* @returns {string}
*/
const ensurePosix = (val) => val.replace(/\\/g, '/');

module.exports = {
ensureOsSep,
ensurePosix,
addTrailingSlash,
removeTrailingSlash,
addLeadingSlash,
Expand Down
5 changes: 3 additions & 2 deletions lib/helpers/resolve-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
addLeadingSlash,
removeLeadingSlash,
ensureOsSep,
ensurePosix,
} = require('./path-slashes');
const ResolvedFiles = require('../classes/resolved-files');

Expand Down Expand Up @@ -71,8 +72,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 @@ -4,6 +4,7 @@ const FileMapping = require('../../../lib/classes/file-mapping');
const LocalFileLocation = require('../../../lib/classes/local-file-location');
const RemoteFileLocation = require('../../../lib/classes/remote-file-location');
const EikConfig = require('../../../lib/classes/eik-config');
const { ensurePosix } = require('../../../lib/helpers/path-slashes');

const validEikConfig = {
name: 'pizza',
Expand Down Expand Up @@ -305,7 +306,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 @@ -492,7 +493,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 @@ -3,6 +3,7 @@ const os = require('os');
const { join, basename } = require('path');
const tap = require('tap');
const resolveFiles = require('../../lib/helpers/resolve-files');
const { ensurePosix } = require('../../lib/helpers/path-slashes');

// then integrate the resolve-files function into eik-config.

Expand Down Expand Up @@ -48,7 +49,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 @@ -198,7 +199,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 e68717f

Please sign in to comment.