Skip to content
Merged
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
7 changes: 4 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,20 @@
},
"types": "./index.d.ts",
"dependencies": {
"@inquirer/prompts": "^7.1.0",
"@types/jscodeshift": "0.12.0",
"jscodeshift": "17.1.1",
"@typescript-eslint/parser": "7.18.0",
"eslint": "8.57.0",
"jscodeshift": "17.1.1",
"tsx": "4.19.1"
},
"devDependencies": {
"@ag-grid-devtools/ast": "workspace:*",
"@ag-grid-devtools/build-config": "workspace:*",
"@ag-grid-devtools/build-tools": "workspace:*",
"@ag-grid-devtools/codemod-task-utils": "workspace:*",
"@ag-grid-devtools/codemod-utils": "workspace:*",
"@ag-grid-devtools/test-utils": "workspace:*",
"@ag-grid-devtools/build-config": "workspace:*",
"@ag-grid-devtools/codemod-task-utils": "workspace:*",
"@ag-grid-devtools/types": "workspace:*",
"@ag-grid-devtools/utils": "workspace:*",
"@ag-grid-devtools/worker-utils": "workspace:*",
Expand Down
35 changes: 0 additions & 35 deletions packages/cli/src/codemods/lib.test.ts

This file was deleted.

62 changes: 61 additions & 1 deletion packages/cli/src/codemods/versions/33.0.0/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,77 @@ import transformGridApiMethodsV33_0 from '../../transforms/transform-grid-api-me
import transformSparklinesOptionsV33_0 from '../../transforms/transform-sparklines-options-v33-0/manifest.ts';

import transformModulesToPackagesV33 from '../../transforms/transform-modules-to-packages-v33/manifest.ts';
import { select } from '@inquirer/prompts';

const transforms: Array<TransformManifest> = [
transformGridApiMethodsV33_0,
transformSparklinesOptionsV33_0,
transformModulesToPackagesV33,
];

const manifest: VersionManifest = {
type Choices33 = {
usingCharts: boolean;
chartType: string;
};

const manifest: VersionManifest<Choices33> = {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea is to have each version able to ask the user questions for input. These can then be used to set global variables that can be used within the transforms themselves to tweak behaviour.

version: '33.0.0',
codemodPath: 'versions/33.0.0',
transforms,
choices: {
usingCharts: () => {
return select({
message:
'Are you using chart based features in any of your grids, i.e Sparklines / Integrated Charts?',
default: false,
choices: [
{
value: true,
name: 'Yes',
description: 'Some grid has Sparklines in cells or is using Integrated Charts.',
},
{
value: false,
name: 'No',
description: 'No charting based features used in any of the grids.',
},
],
});
},
chartType: () => {
if (process.env.AG_IS_USING_CHARTS == 'false') {
// If they are using community packages, they are not using AG Charts
return Promise.resolve('none');
}

return select({
message: 'Are you using AG Charts Community or Enterprise?',
default: 'community',
choices: [
{
value: 'community',
name: 'Community',
description:
'Using the Community version of AG Charts via one of: ag-grid-enterprise / @ag-grid-enterprise/charts',
},
{
value: 'enterprise',
name: 'Enterprise',
description:
'Using the Enterprise version of AG Charts via one of: ag-grid-charts-enterprise / @ag-grid-enterprise/charts-enterprise',
},
],
});
},
},
setAnswers: {
usingCharts: (answer) => {
process.env.AG_IS_USING_CHARTS = answer;
},
chartType: (answers) => {
process.env.AG_USING_CHARTS = answers;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches the existing env value that was previously set by the command line.

},
},
};

export default manifest;
80 changes: 46 additions & 34 deletions packages/cli/src/commands/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import codemods from '../codemods/lib';
import {
composeCodemods,
createCodemodTask,
Expand All @@ -11,24 +10,26 @@ import {
TaskRunnerEnvironment,
type VersionManifest,
} from '@ag-grid-devtools/types';
import { createFsHelpers } from '@ag-grid-devtools/worker-utils';
import codemods from '../codemods/lib';

import { nonNull } from '@ag-grid-devtools/utils';
import { createFsHelpers } from '@ag-grid-devtools/worker-utils';
import { createTwoFilesPatch } from 'diff';
import { dirname, join, resolve as pathResolve } from 'node:path';
import { cpus } from 'node:os';
import { dirname, join, resolve as pathResolve } from 'node:path';
import semver from 'semver';

import { dynamicRequire } from '@ag-grid-devtools/utils';
import { relative, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { type CliEnv, type CliOptions } from '../types/cli';
import { type WritableStream } from '../types/io';
import { CliArgsError, CliError } from '../utils/cli';
import { findSourceFiles, findGitRoot } from '../utils/fs';
import { findGitRoot, findSourceFiles } from '../utils/fs';
import { findInGitRepository, getUncommittedGitFiles } from '../utils/git';
import { resolve, relative } from 'node:path';
import { getCliCommand, getCliPackageVersion } from '../utils/pkg';
import { green, indentErrorMessage, log } from '../utils/stdio';
import { Worker, WorkerTaskQueue, type WorkerOptions } from '../utils/worker';
import { dynamicRequire } from '@ag-grid-devtools/utils';
import { fileURLToPath } from 'node:url';

const { versions } = codemods;

Expand Down Expand Up @@ -90,10 +91,11 @@ export interface MigrateCommandArgs {
* The path of the user config to load
*/
userConfigPath?: string;

/**
* Hint about which AG Chart features / package is required.
* INTERNAL: Disable interactive prompts for testing
*/
usingCharts?: 'community' | 'enterprise' | 'none';
nonInteractive?: boolean;
}

function usage(env: CliEnv): string {
Expand All @@ -118,9 +120,6 @@ Options:
--config=<file.cjs> Loads a .cjs or .cts configuration file to customize the codemod behavior.
See https://ag-grid.com/javascript-data-grid/codemods/#configuration-file

Version Specific Options:
--using-charts=<value> v33 Which AG Charts bundle to used if it cannot be inferred automatically. One of: ['community' | 'enterprise' | 'none']

Additional arguments:
[<file>...<dir>...] List of input files and directories to operate on.
Defaults to all source files in the current working directory excluding patterns in .gitignore
Expand All @@ -143,7 +142,7 @@ export function parseArgs(args: string[], env: CliEnv): MigrateCommandArgs {
help: false,
input: [],
userConfigPath: undefined,
usingCharts: 'community',
nonInteractive: false,
};
let arg;
while ((arg = args.shift())) {
Expand All @@ -165,22 +164,6 @@ export function parseArgs(args: string[], env: CliEnv): MigrateCommandArgs {
case '-d':
options.allowDirty = true;
break;
case '--using-charts':
{
let value = args.shift();
if (!value || value.startsWith('-')) {
throw new CliArgsError(`Missing value for ${arg}`, usage(env));
}
const validValues = ['community', 'enterprise', 'none'];
if (!validValues.includes(value)) {
throw new CliArgsError(
`Invalid value for ${arg}: ${value} (Pick one of: ${validValues.join()})`,
usage(env),
);
}
options.usingCharts = value as 'community' | 'enterprise' | 'none';
}
break;
case '--no-allow-dirty':
options.allowDirty = false;
break;
Expand Down Expand Up @@ -270,6 +253,14 @@ export function parseArgs(args: string[], env: CliEnv): MigrateCommandArgs {
}
break;
}
case '--non-interactive': {
// private option to disable interactive prompts for testing
options.nonInteractive = true;
console.warn(
'The --non-interactive option is deprecated and will be removed in a future release',
);
break;
}

case '--help':
case '-h':
Expand Down Expand Up @@ -328,7 +319,7 @@ async function migrate(
verbose,
userConfigPath,
input,
usingCharts,
nonInteractive,
} = args;
let { cwd, env, stdio } = options;
const { stdout, stderr } = stdio;
Expand All @@ -353,10 +344,6 @@ async function migrate(
skipFiles.add(userConfigPath);
}

if (usingCharts) {
process.env.AG_USING_CHARTS = usingCharts;
}

const inputFilePaths = await findSourceFiles(
cwd,
input.length > 0 ? input : [cwd],
Expand Down Expand Up @@ -456,6 +443,31 @@ async function migrate(
// Process the tasks either in-process or via a worker pool
const isSingleThreaded = numThreads === 0;

if (!nonInteractive) {
// See if any of the codemods have choices that need to be made by the user
for (let i = 0; i < codemodVersions.length; i++) {
const codemod = codemodVersions[i];
const choices = codemod.choices;
if (!choices) continue;

log(
stderr,
['', `Codemod for version ${green(codemod.version, env)} requires user input:`, ''].join(
'\n',
),
);

for (let key of Object.keys(choices)) {
const choice = choices[key];
if (choice) {
const answer = await choice();
const setAnswers = codemod.setAnswers?.[key] ?? (() => {});
setAnswers(answer);
}
}
}
}

const codemodPaths = codemodVersions.map(({ codemodPath }) =>
join(CODEMODS_FOLDER, codemodPath, 'codemod'),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test(

await env.writeTempSrc('untracked.js', '// untracked');

await cli(['migrate', '--from=30.0.0'], env.cliOptions);
await cli(['migrate', '--non-interactive', '--from=30.0.0'], env.cliOptions);
} catch (e) {
error = e as Error;
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test(
async () => {
try {
await env.init({ gitInit: true });
await cli(['migrate', '--from=30.0.0'], env.cliOptions);
await cli(['migrate', '--non-interactive', '--from=30.0.0'], env.cliOptions);
} finally {
await env.removeGitFolder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ test(
'cli e2e - gitignore simple',
async () => {
await env.init();
await cli(['migrate', '--allow-untracked', '--from=30.0.0'], env.cliOptions);
await cli(
['migrate', '--allow-untracked', '--from=30.0.0', '--to=33', '--non-interactive'],
env.cliOptions,
);

// changed files

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test(
await env.writeTempSrc('uncommitted.js', `// uncommitted`);
await env.addGitFile('uncommitted.js');

await cli(['migrate', '--from=30.0.0'], env.cliOptions);
await cli(['migrate', '--non-interactive', '--from=30.0.0'], env.cliOptions);
} catch (e) {
error = e as Error;
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ test(
async () => {
await env.init();
await cli(
['migrate', '--num-threads=3', '--allow-untracked', '--from=30.0.0', 'file1.js', 'dir'],
[
'migrate',
'--non-interactive',
'--num-threads=3',
'--allow-untracked',
'--from=30.0.0',
'file1.js',
'dir',
],
env.cliOptions,
);
expect(await env.loadExpectedSrc('file1.js')).toEqual(await env.loadTempSrc('file1.js'));
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/test/e2e/multi-thread/multi-thread.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ test(
'cli e2e - multi thread',
async () => {
await env.init();
await cli(['migrate', '--num-threads=3', '--allow-untracked', '--from=30.0.0'], env.cliOptions);
await cli(
['migrate', '--non-interactive', '--num-threads=3', '--allow-untracked', '--from=30.0.0'],
env.cliOptions,
);
expect(await env.loadExpectedSrc('file1.js')).toEqual(await env.loadTempSrc('file1.js'));
expect(await env.loadExpectedSrc('file2.js')).toEqual(await env.loadTempSrc('file2.js'));
expect(await env.loadExpectedSrc('file3.js')).toEqual(await env.loadTempSrc('file3.js'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ test(
'cli e2e - single thread',
async () => {
await env.init();
await cli(['migrate', '--num-threads=0', '--allow-untracked', '--from=30.0.0'], env.cliOptions);
await cli(
['migrate', '--non-interactive', '--num-threads=0', '--allow-untracked', '--from=30.0.0'],
env.cliOptions,
);
expect(await env.loadExpectedSrc('file.js')).toEqual(await env.loadTempSrc('file.js'));
},
env.TIMEOUT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test(
await cli(
[
'migrate',
'--non-interactive',
'--num-threads=4',
'--allow-untracked',
'--from=30.0.0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test(
await cli(
[
'migrate',
'--non-interactive',
'--num-threads=0',
'--allow-untracked',
'--from=30.0.0',
Expand Down
Loading
Loading