Skip to content

Commit

Permalink
Merge pull request #16 from Comandeer/t/13
Browse files Browse the repository at this point in the history
  • Loading branch information
Comandeer authored Jul 16, 2023
2 parents a6aa3b8 + 4de8b4b commit 882f09a
Show file tree
Hide file tree
Showing 27 changed files with 628 additions and 544 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

---

## [0.4.0]
### Changed
* [#13]: **BREAKING CHANGE**: added the `pathe` dependency to replace `node:path` usage in all path-related tasks.
* [#18]: **BREAKING CHANGE**: changed logic for detecting and loading the loader file.

### Fixed
* [#17]: incorrect error handling in the `resolveConfigFile()` utility.

## [0.3.0] – 2023-07-13
### Added
* [#8]: Official support for Node 20.
Expand Down Expand Up @@ -36,6 +44,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
[#6]: https://github.com/Comandeer/esm-loader-manager/issues/6
[#8]: https://github.com/Comandeer/esm-loader-manager/issues/8
[#9]: https://github.com/Comandeer/esm-loader-manager/issues/9
[#13]: https://github.com/Comandeer/esm-loader-manager/issues/13
[#17]: https://github.com/Comandeer/esm-loader-manager/issues/17
[#18]: https://github.com/Comandeer/esm-loader-manager/issues/18

[0.4.0]: https://github.com/Comandeer/esm-loader-manager/compare/v0.3.0...v0.4.0
[0.3.0]: https://github.com/Comandeer/esm-loader-manager/compare/v0.2.0...v0.3.0
[0.2.0]: https://github.com/Comandeer/esm-loader-manager/compare/v0.1.0...v0.2.0
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"mock-fs": "^5.2.0"
},
"dependencies": {
"execa": "^7.1.1"
"execa": "^7.1.1",
"pathe": "^1.1.1"
}
}
4 changes: 2 additions & 2 deletions src/__bin__/esmlm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import { argv } from 'node:process';
import { cwd as processCWD } from 'node:process';
import { exit } from 'node:process';
import { dirname } from 'node:path';
import { resolve as resolvePath } from 'node:path';
import { dirname } from 'pathe';
import { resolve as resolvePath } from 'pathe';
import { fileURLToPath } from 'node:url';
import { pathToFileURL } from 'node:url';
import { execa } from 'execa';
Expand Down
24 changes: 13 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/* eslint-disable no-console */

import { existsSync as fileExists } from 'node:fs';
import { resolve as resolvePath } from 'node:path';
import { access } from 'node:fs/promises';
import { cwd as processCWD } from 'node:process';
import { env as processEnv } from 'node:process';
import { pathToFileURL } from 'node:url';
import { isBuiltInModule } from './utilities.js';
import { isInsideDir } from './utilities.js';
import { isInsideNodeModules } from './utilities.js';
import { resolveConfigFile } from './utilities.js';
import { resolveProjectRoot } from './utilities.js';
import { loadURL } from './utilities.js';
import { resolve as resolvePath } from 'pathe';
import isBuiltInModule from './utilities/isBuiltInModule.js';
import isInsideDir from './utilities/isInsideDir.js';
import isInsideNodeModules from './utilities/isInsideNodeModules.js';
import resolveConfigFile from './utilities/resolveConfigFile.js';
import resolveProjectRoot from './utilities/resolveProjectRoot.js';
import loadURL from './utilities/loadURL.js';

const cwd = processCWD();
const resolvedProjectRoot = await resolveProjectRoot( cwd );
Expand All @@ -25,13 +25,15 @@ const loaderFileName = 'ESMLM_CONFIG' in processEnv ? processEnv.ESMLM_CONFIG :
const loaderPath = loaderFileName ? resolvePath( cwd, loaderFileName ) : null;
let loaders = [];

if ( loaderPath && fileExists( loaderPath ) ) {
try {
await access( loaderPath );

const loaderURL = pathToFileURL( loaderPath );
const { default: config } = await import( loaderURL );

loaders = config.loaders;
} else {
console.warn( 'ESMLM: The file with loaders\' definition was not found.' );
} catch {
console.warn( 'ESMLM: The file with loaders\' definition was not found or cannot be accessed.' );
}

async function resolve( specifier, context, defaultResolve ) {
Expand Down
91 changes: 0 additions & 91 deletions src/utilities.js

This file was deleted.

9 changes: 9 additions & 0 deletions src/utilities/isBuiltInModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function isBuiltInModule( { url, format } ) {
if ( format ) {
return format === 'builtin';
}

return url.startsWith( 'node:' );
}

export default isBuiltInModule;
16 changes: 16 additions & 0 deletions src/utilities/isInsideDir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { isAbsolute } from 'pathe';
import { relative as getRelativePath } from 'pathe';
import { fileURLToPath } from 'node:url';

function isInsideDir( dir, pathOrURL ) {
const filePath = pathOrURL.startsWith( 'file://' ) ? fileURLToPath( pathOrURL ) : pathOrURL;
const relativePath = getRelativePath( dir, filePath );
const isNotEmptyPath = relativePath.length > 0;
const isNotOutsideDir = !relativePath.startsWith( '..' );
const isNotAbsolutePath = !isAbsolute( relativePath );

// https://stackoverflow.com/a/45242825/9025529
return isNotEmptyPath && isNotOutsideDir && isNotAbsolutePath;
}

export default isInsideDir;
7 changes: 7 additions & 0 deletions src/utilities/isInsideNodeModules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function isInsideNodeModules( pathOrURL ) {
const npmModulesPathRegex = /[/\\]node_modules[/\\]/gi;

return npmModulesPathRegex.test( pathOrURL );
}

export default isInsideNodeModules;
10 changes: 10 additions & 0 deletions src/utilities/loadURL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';

async function loadURL( url ) {
const path = fileURLToPath( url );

return readFile( path );
}

export default loadURL;
37 changes: 37 additions & 0 deletions src/utilities/resolveConfigFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { readdir } from 'node:fs/promises';
import { resolve as resolvePath } from 'pathe';

const configFileName = '.esmlmrc';
const configFileExtensions = [
'.js',
'.mjs'
];

async function resolveConfigFile( startDir, projectRoot ) {
try {
const files = await readdir( startDir );

for ( const extension of configFileExtensions ) {
const configFileFullName = `${ configFileName }${ extension }`;

if ( files.includes( configFileFullName ) ) {
const resolvedConfigFilePath = resolvePath( startDir, configFileFullName );

return resolvedConfigFilePath;
}
}

// Do not go outside of the project root.
if ( startDir === projectRoot ) {
return null;
}

const dirUp = resolvePath( startDir, '..' );

return resolveConfigFile( dirUp );
} catch {
return null;
}
}

export default resolveConfigFile;
26 changes: 26 additions & 0 deletions src/utilities/resolveProjectRoot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { readdir } from 'node:fs/promises';
import { resolve as resolvePath } from 'pathe';

async function resolveProjectRoot( startDir ) {
try {
const files = await readdir( startDir );

if ( files.includes( 'package.json' ) ) {
return startDir;
}

const dirUp = resolvePath( startDir, '..' );

// If directory one level up is the same as the current on,
// we're at / and there's nowhere to go up.
if ( dirUp === startDir ) {
return null;
}

return resolveProjectRoot( dirUp );
} catch {
return null;
}
}

export default resolveProjectRoot;
2 changes: 1 addition & 1 deletion tests/__fixtures__/builtinModules/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'node:path';
import { resolve } from 'pathe';
import { readFile } from 'fs';
import { readdir } from 'fs/promises';

Expand Down
6 changes: 2 additions & 4 deletions tests/__helpers__/createAbsolutePath.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { platform } from 'node:os';
import { sep as separator } from 'node:path';

/**
* @param {string} posixPath
* @returns {string} Path compatible with the operating system.
*/
function createAbsolutePath( posixPath ) {
if ( platform !== 'win32' ) {
if ( platform() !== 'win32' ) {
return posixPath;
}

const pathWithReplacedSeparators = posixPath.replaceAll( '/', separator );
const windowsPath = `C:\\${ pathWithReplacedSeparators }`;
const windowsPath = `/C:/${ posixPath }`;

return windowsPath;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/__helpers__/fixtureDirPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { dirname } from 'pathe';
import { resolve as resolvePath } from 'pathe';
import { fileURLToPath } from 'node:url';

const __dirname = dirname( fileURLToPath( import.meta.url ) );
const fixtureDirPath = resolvePath( __dirname, '..', '__fixtures__' );

export default fixtureDirPath;
4 changes: 2 additions & 2 deletions tests/__helpers__/macros/testEsmlm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dirname } from 'node:path';
import { resolve as resolvePath } from 'node:path';
import { dirname } from 'pathe';
import { resolve as resolvePath } from 'pathe';
import { fileURLToPath } from 'node:url';
import test from 'ava';
import testCmd from '../testCmd.js';
Expand Down
4 changes: 2 additions & 2 deletions tests/__helpers__/macros/testLoader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dirname } from 'node:path';
import { resolve as resolvePath } from 'node:path';
import { dirname } from 'pathe';
import { resolve as resolvePath } from 'pathe';
import { fileURLToPath, pathToFileURL } from 'node:url';
import test from 'ava';
import testCmd from '../testCmd.js';
Expand Down
7 changes: 2 additions & 5 deletions tests/esmlm.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { dirname } from 'node:path';
import { resolve as resolvePath } from 'node:path';
import { fileURLToPath } from 'node:url';
import { resolve as resolvePath } from 'pathe';
import test from 'ava';
import fixtureDirPath from './__helpers__/fixtureDirPath.js';
import testEsmlm from './__helpers__/macros/testEsmlm.js';

const __dirname = dirname( fileURLToPath( import.meta.url ) );
const fixtureDirPath = resolvePath( __dirname, '__fixtures__' );
const esmlmFixturePath = resolvePath( fixtureDirPath, 'esmlm' );
const esmlmFixtureEntryPointPath = resolvePath( esmlmFixturePath, 'index.js' );
const esmlmArgsFixturePath = resolvePath( fixtureDirPath, 'esmlmArgs' );
Expand Down
Loading

0 comments on commit 882f09a

Please sign in to comment.