Skip to content

Commit

Permalink
Fix ESM issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tobeno committed Dec 1, 2023
1 parent b6172cb commit d5f57f3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
2 changes: 1 addition & 1 deletion bin/sol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { loadSol } from '../src/sol/sol-setup';
);
}

loadSol();
await loadSol();

const args = process.argv.slice(2);
if (args.length > 0 && args[0] !== '--') {
Expand Down
24 changes: 7 additions & 17 deletions src/mutations/global.mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ import { Text } from '../wrappers/text.wrapper';
import { TmpDirectory } from '../wrappers/tmp-directory.wrapper';
import { TmpFile } from '../wrappers/tmp-file.wrapper';
import { Url } from '../wrappers/url.wrapper';
import * as day from '../utils/day.utils';
import { morphProject } from '../utils/morph.utils';
import { Xml } from '../wrappers/xml.wrapper';
import module from 'node:module';
import { jwt } from '../utils/jwt.utils';

const require = module.createRequire(import.meta.url);

Expand Down Expand Up @@ -83,21 +86,11 @@ export const globals = {
day: withHelp(
{
get() {
return require('../utils/day.utils').day;
return day;
},
},
'See https://day.js.org/docs/en/installation/installation',
),
debug: withHelp(
{
get() {
return {
...require('../utils/debug.utils'),
};
},
},
'Debug utils',
),
dir: withHelp(
{
value: Directory.create,
Expand All @@ -121,8 +114,6 @@ export const globals = {
exec: withHelp(
{
get() {
const { Shell } =
require('../wrappers/shell.wrapper') as typeof import('../wrappers/shell.wrapper');
const shell = Shell.create();

return shell.exec.bind(shell);
Expand Down Expand Up @@ -191,7 +182,7 @@ export const globals = {
jwt: withHelp(
{
get() {
return require('../utils/jwt.utils').jwt;
return jwt;
},
},
'See https://github.com/auth0/node-jsonwebtoken#readme',
Expand Down Expand Up @@ -219,7 +210,7 @@ export const globals = {
morphProject: withHelp(
{
get() {
return require('../utils/morph.utils').morphProject;
return morphProject;
},
},
'Returns ts-morph project for the current working directory',
Expand Down Expand Up @@ -281,7 +272,7 @@ export const globals = {
sol: withHelp(
{
get() {
return require('../index');
return import('../index');
},
},
'Returns the extension for the given name or path',
Expand Down Expand Up @@ -386,7 +377,6 @@ declare global {
const cwd: Globals['cwd'];
const data: Globals['data'];
const day: Globals['day'];
const debug: Globals['debug'];
const dir: Globals['dir'];
const dirs: Globals['dirs'];
const env: Globals['env'];
Expand Down
7 changes: 2 additions & 5 deletions src/sol/sol-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import { Directory } from '../wrappers/directory.wrapper';
import { File } from '../wrappers/file.wrapper';
import { getSolPackage } from './sol-package';
import { getCurrentSolWorkspace, getSolUserWorkspace } from './sol-workspace';
import module from 'node:module';

const require = module.createRequire(import.meta.url);

/**
* Class for loading a Sol extension.
Expand Down Expand Up @@ -104,7 +101,7 @@ mutateGlobals(definePropertiesMutation(globals));
/**
* Loads the Sol extension using its setup file.
*/
load(): void {
async load(): Promise<void> {
if (this.loaded) {
return;
}
Expand All @@ -116,7 +113,7 @@ mutateGlobals(definePropertiesMutation(globals));
this.prepare();

try {
require(this.setupFile.path);
await import(this.setupFile.path);
logDebug(`Loaded setup file at ${this.setupFile.path}`);
} catch (e) {
this.setupFile.delete();
Expand Down
6 changes: 3 additions & 3 deletions src/sol/sol-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { getCurrentSolWorkspace, getSolUserWorkspace } from './sol-workspace';
/**
* Initializes Sol globals and extensions.
*/
export function loadSol(): void {
export async function loadSol(): Promise<void> {
logDebug('Loading Sol...');

const workspace = getCurrentSolWorkspace();
const userWorkspace = getSolUserWorkspace();

userWorkspace.load();
workspace.load();
await userWorkspace.load();
await workspace.load();

// Update context files again
userWorkspace.updateContextFile();
Expand Down
25 changes: 20 additions & 5 deletions src/sol/sol-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import { Directory } from '../wrappers/directory.wrapper';
import { File } from '../wrappers/file.wrapper';
import { getLoadedSolExtensions } from './sol-extension';
import { getSolPackage } from './sol-package';
import module from 'node:module';

const require = module.createRequire(import.meta.url);

/**
* Class for interacting with a Sol workspace directors.
Expand Down Expand Up @@ -50,6 +47,13 @@ export class SolWorkspace {
return this.dir.file('setup.ts');
}

/**
* Setup file of the workspace.
*/
get packageFile(): File {
return this.dir.file('package.json');
}

/**
* .env file of the workspace.
*/
Expand Down Expand Up @@ -117,14 +121,25 @@ import { solExtension } from '${solPackage.dir.relativePathFrom(
// ToDo: Register your first extension
// solExtension('your-extension', __dirname).load();
`.trimStart();
}

const packageFile = this.packageFile;

if (!packageFile.exists || force) {
packageFile.create();
packageFile.text = `
{
"type": "module"
}
`.trimStart();
}
}

/**
* Loads the workspace as well as its setup.ts and .env file.
*/
load(): void {
async load(): Promise<void> {
if (this.loaded) {
return;
}
Expand All @@ -136,7 +151,7 @@ import { solExtension } from '${solPackage.dir.relativePathFrom(
this.prepare();

try {
require(this.setupFile.pathWithoutExt);
await import(this.setupFile.pathWithoutExt);
logDebug(`Loaded setup file at ${this.setupFile.path}`);
} catch (e) {
this.setupFile.delete();
Expand Down

0 comments on commit d5f57f3

Please sign in to comment.