Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Esm support v3 #241

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "MIT",
"private": true,
"scripts": {
"predocs:start": "ts-node scripts/docs",
"predocs:start": "tsx scripts/docs.ts",
"docs:start": "cd website && yarn start",
"postinstall": "yarn monorepo:fix && yarn monorepo:check",
"build": "yarn build:pkgs && yarn build:community",
Expand All @@ -25,14 +25,14 @@
"types:check": "tsc --noEmit --skipLibCheck",
"monorepo:check": "manypkg check",
"monorepo:fix": "manypkg fix",
"cli:start": "ts-node packages/cli/bin/hypermod-cli.js",
"cli:validate": "ts-node packages/cli/bin/hypermod-cli.js validate",
"cli:init": "ts-node packages/cli/bin/hypermod-cli.js init",
"cli:list": "ts-node packages/cli/bin/hypermod-cli.js list",
"community:init": "ts-node scripts/initialize.ts",
"community:init:preset": "ts-node scripts/initialize-preset.ts",
"community:validate": "ts-node scripts/validate.ts ./community",
"worker:sync": "ts-node scripts/sync.ts",
"cli:start": "tsx packages/cli/bin/hypermod-cli.js",
"cli:validate": "tsx packages/cli/bin/hypermod-cli.js validate",
"cli:init": "tsx packages/cli/bin/hypermod-cli.js init",
"cli:list": "tsx packages/cli/bin/hypermod-cli.js list",
"community:init": "tsx scripts/initialize.ts",
"community:init:preset": "tsx scripts/initialize-preset.ts",
"community:validate": "tsx scripts/validate.ts ./community",
"worker:sync": "tsx scripts/sync.ts",
"prerelease": "yarn build && yarn validate && yarn test",
"release": "yarn changeset publish"
},
Expand Down Expand Up @@ -71,8 +71,8 @@
"ts-jest": "^29.2.5",
"ts-jest-mock-import-meta": "^1.2.0",
"ts-loader": "^6.2.1",
"ts-node": "^10.9.1",
"tslib": "^2.2.0",
"tsx": "^4.19.1",
"typescript": "^5.2.2"
},
"resolutions": {
Expand Down
4 changes: 1 addition & 3 deletions packages/cli-alias/bin/codeshift-cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
#!/usr/bin/env node
const path = require('path');

require(path.join('..', 'dist', 'index.js'));
export * from '@hypermod/cli';
9 changes: 6 additions & 3 deletions packages/cli-alias/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@codeshift/cli",
"type": "module",
"version": "0.24.0",
"source": "src/index.ts",
"main": "dist/index.js",
Expand All @@ -12,11 +13,13 @@
},
"scripts": {
"start": "./bin/codeshift-cli.js",
"start:dev": "ts-node src/index.ts"
"start:dev": "tsx src/index.ts"
},
"dependencies": {
"@hypermod/cli": "^0.24.0",
"ts-node": "^10.9.1"
"@hypermod/cli": "^0.24.0"
},
"devDependencies": {
"tsx": "^4.19.1"
},
"engines": {
"node": ">=20.17"
Expand Down
4 changes: 1 addition & 3 deletions packages/cli/bin/hypermod-cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
#!/usr/bin/env node
const path = require('path');

require(path.join('..', 'dist', 'index.js'));
export * from '../dist/index.js';
5 changes: 3 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@hypermod/cli",
"type": "module",
"version": "0.24.0",
"source": "src/index.ts",
"main": "dist/index.js",
Expand All @@ -14,7 +15,7 @@
},
"scripts": {
"start": "./bin/hypermod-cli.js",
"start:dev": "ts-node src/index.ts"
"start:dev": "tsx src/index.ts"
},
"dependencies": {
"@antfu/install-pkg": "^0.1.1",
Expand All @@ -33,7 +34,7 @@
"lodash": "^4.17.21",
"ora": "^5.4.1",
"semver": "^7.3.5",
"ts-node": "^10.9.1"
"tsx": "^4.19.1"
},
"engines": {
"node": ">=20.17"
Expand Down
14 changes: 9 additions & 5 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import path from 'path';
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import chalk from 'chalk';
import { Command, Option, CommanderError } from 'commander';

import main from './main';
import list from './list';
import init from './init';
import validate from './validate';
import { InvalidUserInputError, InvalidConfigError } from './errors';
import main from './main.js';
import list from './list.js';
import init from './init.js';
import validate from './validate.js';
import { InvalidUserInputError, InvalidConfigError } from './errors.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const packageJson = readFileSync(
path.join(__dirname, '..', 'package.json'),
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ jest.mock('live-plugin-manager');
import chalk from 'chalk';
import { PluginManager } from 'live-plugin-manager';

import list from './list';
import list from './list.js';

describe('list', () => {
beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/list.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import chalk from 'chalk';
import { PluginManager } from 'live-plugin-manager';

import { fetchPackages } from './utils/fetch-package';
import { getHypermodPackageName } from './utils/package-names';
import { fetchPackages } from './utils/fetch-package.js';
import { getHypermodPackageName } from './utils/package-names.js';

export default async function list(packages: string[]) {
const packageManager = new PluginManager() as any;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import globby from 'globby';

import * as core from '@hypermod/core';

import main from './main';
import main from './main.js';

const mockPath = 'src/pages/home-page/';

Expand Down
21 changes: 14 additions & 7 deletions packages/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import semver from 'semver';
import chalk from 'chalk';
import findUp from 'find-up';
import inquirer from 'inquirer';
import { fileURLToPath } from 'url';
import { PluginManager, PluginManagerOptions } from 'live-plugin-manager';

import * as core from '@hypermod/core';
Expand All @@ -11,12 +12,18 @@ import {
fetchConfigAtPath,
} from '@hypermod/fetcher';

import { InvalidUserInputError } from './errors';
import { fetchPackages } from './utils/fetch-package';
import { mergeConfigs } from './utils/merge-configs';
import { fetchConfigsForWorkspaces, getPackageJson } from './utils/file-system';
import ModuleLoader from './utils/module-loader';
import { getConfigPrompt, getMultiConfigPrompt } from './prompt';
import { InvalidUserInputError } from './errors.js';
import { fetchPackages } from './utils/fetch-package.js';
import { mergeConfigs } from './utils/merge-configs.js';
import {
fetchConfigsForWorkspaces,
getPackageJson,
} from './utils/file-system.js';
import ModuleLoader from './utils/module-loader.js';
import { getConfigPrompt, getMultiConfigPrompt } from './prompt.js';

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);

export default async function main(
paths: string[],
Expand All @@ -29,7 +36,7 @@ export default async function main(
}

const pluginManagerConfig: Partial<PluginManagerOptions> = {
pluginsPath: path.join(__dirname, '..', 'node_modules'),
pluginsPath: path.join(dirname, '..', 'node_modules'),
};

// If a registry is provided in the CLI flags, use it for the pluginManagers configuration.
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/utils/fetch-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@hypermod/fetcher';
import { isValidConfig } from '@hypermod/validator';

import { getHypermodPackageName } from './package-names';
import { getHypermodPackageName } from './package-names.js';

export async function fetchPackages(
packageName: string,
Expand Down
28 changes: 21 additions & 7 deletions packages/cli/src/utils/module-loader.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import path from 'path';
import fs from 'fs-extra';
import { fileURLToPath } from 'url';
import { installPackage } from '@antfu/install-pkg';

import { ModuleLoader } from '@hypermod/fetcher';

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);

/**
* Register the TSX plugin to allow require TS(X) files.
*/
import { register } from 'tsx/esm/api';
register();

const ModuleLoader = (config: {
npmRegistryUrl?: string;
authToken?: string;
}): ModuleLoader => {
const getInfo = (packageName: string) => {
const entryPath = require.resolve(packageName);
const location = entryPath.split(packageName)[0] + packageName;
const getInfo = async (packageName: string) => {
// @ts-expect-error - TS doesn't know about import.meta
const entryPath = await import.meta.resolve(packageName);
const location = (entryPath.split(packageName)[0] + packageName).replace(
'file://',
'',
);
const pkgJsonRaw = fs.readFileSync(
path.join(location, 'package.json'),
'utf8',
Expand All @@ -26,7 +40,7 @@ const ModuleLoader = (config: {

const install = async (packageName: string) => {
await installPackage(packageName, {
cwd: __dirname,
cwd: dirname,
packageManager: 'npm',
additionalArgs: [
'--force',
Expand All @@ -38,7 +52,7 @@ const ModuleLoader = (config: {
],
});

const { pkgJson } = getInfo(packageName);
const { pkgJson } = await getInfo(packageName);

// Install whitelisted devDependencies
if (pkgJson?.hypermod?.dependencies) {
Expand All @@ -47,7 +61,7 @@ const ModuleLoader = (config: {
const version = pkgJson.devDependencies[dep];
if (!version) return;
return installPackage(`${dep}@${version}`, {
cwd: __dirname,
cwd: dirname,
packageManager: 'npm',
additionalArgs: ['--force'],
});
Expand All @@ -59,7 +73,7 @@ const ModuleLoader = (config: {
return {
install,
getInfo,
require: (packageName: string) => require(packageName),
require: async (packageName: string) => await import(packageName),
};
};

Expand Down
Loading
Loading