Skip to content

Commit

Permalink
Support ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
gimenete committed May 18, 2022
1 parent 2f0de48 commit 939b5f7
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 7 deletions.
68 changes: 68 additions & 0 deletions cli/bin/mokey-patch-oclif.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Oclif mostly supports ESM, but it doesn't properly support commands
* in "index.js" files. It always thinks they are not ESM files.
*
* This file monkey-patches Oclif with the code
* from this PR https://github.com/oclif/core/pull/417 to support them.
*/
import { tsPath } from '@oclif/core';
import ModuleLoader from '@oclif/core/lib/module-loader.js';
import fs from 'fs';
import path from 'path';

ModuleLoader.default.resolvePath = (config, modulePath) => {
let isESM;
let filePath;

try {
// eslint-disable-next-line no-undef
filePath = require.resolve(modulePath);
isESM = ModuleLoader.default.isPathModule(filePath);
} catch {
filePath = tsPath(config.root, modulePath);

let fileExists = false;
let isDirectory = false;
if (fs.existsSync(filePath)) {
fileExists = true;
try {
if (fs.lstatSync(filePath)?.isDirectory?.()) {
fileExists = false;
isDirectory = true;
}
} catch {
// ignore
}
}

if (!fileExists) {
// Try all supported extensions.
let foundPath = findFile(filePath);
if (!foundPath && isDirectory) {
// Since filePath is a directory, try looking for index.js file.
foundPath = findFile(path.join(filePath, 'index'));
}

if (foundPath) {
filePath = foundPath;
}
}

isESM = ModuleLoader.default.isPathModule(filePath);
}

return { isESM, filePath };
};

function findFile(filePath) {
// eslint-disable-next-line camelcase
for (const extension of ['.js', '.cjs']) {
const testPath = `${filePath}${extension}`;

if (fs.existsSync(testPath)) {
return testPath;
}
}

return null;
}
5 changes: 0 additions & 5 deletions cli/bin/run

This file was deleted.

7 changes: 7 additions & 0 deletions cli/bin/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env node
import { Errors, flush, run } from '@oclif/core';
import './mokey-patch-oclif.js';

run(void 0, import.meta.url)
.then(flush)
.catch(Errors.handle);
4 changes: 3 additions & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"homepage": "https://github.com/xataio/client-ts",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/index.js",
"type": "module",
"repository": "xataio/client-ts",
"files": [
"/bin",
Expand All @@ -17,7 +19,7 @@
"/oclif.manifest.json"
],
"dependencies": {
"@oclif/core": "^1",
"@oclif/core": "^1.8.1",
"@oclif/plugin-help": "^5",
"@oclif/plugin-plugins": "^2.0.1"
},
Expand Down
3 changes: 2 additions & 1 deletion cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"compilerOptions": {
"declaration": true,
"importHelpers": true,
"module": "commonjs",
"module": "es2020",
"moduleResolution": "node",
"outDir": "dist",
"rootDir": "src",
"strict": true,
Expand Down

0 comments on commit 939b5f7

Please sign in to comment.