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

Publish v32.0.3 #66

Merged
merged 11 commits into from
Jul 15, 2024
1 change: 1 addition & 0 deletions packages/cli/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './index.cjs';
31 changes: 28 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@ag-grid-devtools/cli",
"version": "32.0.2",
"version": "32.0.3",
"license": "MIT",
"description": "AG Grid developer toolkit",
"author": "AG Grid <[email protected]>",
Expand Down Expand Up @@ -33,7 +33,31 @@
},
"pkg": {
"main": "./index.cjs",
"bin": "./index.cjs"
"bin": "./index.cjs",
"exports": {
".": {
"import": "./index.mjs",
"require": "./index.cjs",
"default": "./index.mjs"
},
"./index": {
"import": "./index.mjs",
"require": "./index.cjs",
"default": "./index.mjs"
},
"./index.js": "./index.mjs",
"./index.mjs": "./index.mjs",
"./index.cjs": "./index.cjs",
"./user-config": {
"import": "./user-config.mjs",
"require": "./user-config.cjs",
"default": "./user-config.mjs"
},
"./user-config.js": "./user-config.mjs",
"./user-config.mjs": "./user-config.mjs",
"./user-config.cjs": "./user-config.cjs",
"./package.json": "./package.json"
}
},
"types": "./index.d.ts",
"bundleDependencies": [
Expand All @@ -58,7 +82,8 @@
"graceful-fs": "4.2.11",
"ignore": "5.3.1",
"semver": "7.6.2",
"vite-plugin-dts": "3.9.1"
"vite-plugin-dts": "3.9.1",
"vite-plugin-static-copy": "1.0.6"
},
"peerDependencies": {
"eslint": "^8",
Expand Down
7 changes: 1 addition & 6 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,7 @@ export async function cli(args: Array<string>, cli: CliOptions): Promise<void> {
throw null;
}

// Add typescript support by loading tsx
try {
dynamicRequire.require('tsx/cjs', import.meta);
} catch {
// ignore error if tsx could not be loaded
}
dynamicRequire.initialize();

const task = match(options.command, {
Migrate: ({ args }) => migrate(args, cli),
Expand Down
22 changes: 11 additions & 11 deletions packages/cli/src/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,29 +446,29 @@ async function migrate(
// Create a worker pool to run the codemods in parallel
let scriptPath: string | URL = dynamicRequire.resolve(WORKER_PATH, import.meta);

const resolvedCodemodPaths = codemodPaths.map((codemodPath) =>
dynamicRequire.resolve(codemodPath, import.meta),
);
// This will be true if we are trying to run from the codemod repo source code using tsx or vitest
const isTsSourceCodeWorker = scriptPath.endsWith('.ts');

const config: WorkerOptions = {
// Pass the list of codemod paths to the worker via workerData
workerData: {
codemodPaths: resolvedCodemodPaths,
codemodPaths: isTsSourceCodeWorker
? codemodPaths.map((codemodPath) => dynamicRequire.resolve(codemodPath, import.meta))
: codemodPaths,
userConfigPath,
},
env: process.env,
argv: [scriptPath],
eval: true,
eval: isTsSourceCodeWorker,
};

const workerCodeOrPath = isTsSourceCodeWorker
? `try { require("tsx/cjs"); } catch (_) {} require(${JSON.stringify(scriptPath)});`
: scriptPath;

const workers = Array.from(
{ length: numWorkers },
() =>
new Worker(
// Try to add typescript support by loading tsx and load the worker script
`try { require("tsx/cjs"); } catch (_) {} require(${JSON.stringify(scriptPath)});`,
config,
),
() => new Worker(workerCodeOrPath, config),
);
const workerPool = new WorkerTaskQueue<CodemodTaskInput, CodemodTaskWorkerResult>(workers);
return executeCodemodMultiThreaded(workerPool, inputFilePaths, {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/user-config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './user-config.cjs';
7 changes: 7 additions & 0 deletions packages/cli/vite.config.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { resolve } from 'path';
import { defineConfig, mergeConfig } from 'vite';
import dts from 'vite-plugin-dts';
import { viteStaticCopy } from 'vite-plugin-static-copy';

import base from '../build-config/templates/vite/cli.vite.config';

Expand Down Expand Up @@ -29,6 +30,12 @@ export default mergeConfig(
bundledPackages: ['@ag-grid-devtools/types'],
exclude: ['node_modules/**', '*.config.mts', '**/*.test.ts', 'package.json', 'index.ts'],
}),
viteStaticCopy({
targets: [
{ src: 'index.mjs', dest: '.' },
{ src: 'user-config.mjs', dest: '.' },
],
}),
],
}),
);
4 changes: 3 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
"@ag-grid-devtools/types": "workspace:*"
},
"devDependencies": {
"@ag-grid-devtools/build-config": "workspace:*"
"@ag-grid-devtools/build-config": "workspace:*",
"@types/app-module-path": "^2.2.2",
"app-module-path": "2.2.0"
},
"peerDependencies": {
"eslint": "^8",
Expand Down
80 changes: 69 additions & 11 deletions packages/utils/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,68 @@
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import { createRequire, Module } from 'node:module';
import { existsSync } from 'node:fs';
import { addPath as addNodeModulePath } from 'app-module-path';
import { dirname, resolve as pathResolve, join as pathJoin } from 'node:path';

let initialized = false;

const thisDir = pathResolve(
import.meta.url
? dirname(fileURLToPath(import.meta.url))
: typeof __dirname !== 'undefined'
? __dirname
: process.cwd(),
);

export const dynamicRequire = {
resolve(path: string, meta: ImportMeta): string {
if (meta.url === undefined && typeof require !== undefined) {
// import.meta not available, maybe running a ts file with tsx? use default cjs require
return require.resolve(path);
initialize() {
if (initialized) {
return;
}
initialized = true;

// Register node_modules paths

let currentDir = thisDir;
while (currentDir) {
if (currentDir.endsWith('node_modules')) {
tryAddNodeModulePath(currentDir);
break;
}
tryAddNodeModulePath(pathJoin(currentDir, 'node_modules'));
let parentDir = dirname(currentDir);
if (parentDir === currentDir) {
break;
}
currentDir = parentDir;
}

// Add typescript support by loading tsx
try {
dynamicRequire.require('tsx/cjs', import.meta);
} catch {
// ignore error if tsx could not be loaded
}

// Register .cjs and .cts extensions

const exts = (Module as any)._extensions;
if (exts && !('.cjs' in exts)) {
exts['.cjs'] = exts['.js'];
}
if (exts && !('.cts' in exts) && '.ts' in exts) {
exts['.cts'] = exts['.ts'];
}
return createRequire(meta.url).resolve(path);
},

resolve(path: string, meta: ImportMeta): string {
dynamicRequire.initialize();
return createRequire(meta.url || pathResolve(thisDir, 'index.js')).resolve(path);
},

require<T = unknown>(path: string, meta: ImportMeta): T {
if (meta.url === undefined && typeof require !== undefined) {
// import.meta not available, maybe running a ts file with tsx? use default cjs require
return require(path);
}
return createRequire(meta.url)(path);
dynamicRequire.initialize();
return createRequire(meta.url || pathResolve(thisDir, 'index.js'))(path);
},

/** Like require, but supports modules with a default export transpiled to cjs */
Expand All @@ -32,3 +80,13 @@ export const dynamicRequire = {
return required;
},
};

function tryAddNodeModulePath(nodeModulesPath: string) {
try {
if (existsSync(nodeModulesPath)) {
addNodeModulePath(nodeModulesPath);
}
} catch {
// ignore error
}
}
27 changes: 27 additions & 0 deletions pnpm-lock.yaml

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

Loading