ts-codemod-lib 2.2.4 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -54
- package/dist/functions/ast-transformers/convert-to-readonly.d.mts.map +1 -1
- package/dist/functions/ast-transformers/convert-to-readonly.mjs +2 -3
- package/dist/functions/ast-transformers/convert-to-readonly.mjs.map +1 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/readonly-context.d.mts +3 -3
- package/dist/functions/ast-transformers/readonly-transformer-helpers/readonly-context.d.mts.map +1 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/readonly-context.mjs.map +1 -1
- package/dist/functions/ast-transformers/transform-source-code.mjs +1 -1
- package/dist/functions/ast-transformers/transform-source-code.mjs.map +1 -1
- package/dist/functions/functions/is-readonly-node.d.mts +5 -5
- package/dist/functions/functions/is-readonly-node.d.mts.map +1 -1
- package/dist/functions/functions/is-readonly-node.mjs.map +1 -1
- package/dist/functions/functions/should-avoid-parentheses-for-readonly.d.mts.map +1 -1
- package/dist/functions/functions/should-avoid-parentheses-for-readonly.mjs +1 -4
- package/dist/functions/functions/should-avoid-parentheses-for-readonly.mjs.map +1 -1
- package/package.json +69 -115
- package/src/functions/ast-transformers/convert-to-readonly.mts +20 -17
- package/src/functions/ast-transformers/readonly-transformer-helpers/readonly-context.mts +3 -3
- package/src/functions/ast-transformers/transform-source-code.mts +1 -1
- package/src/functions/functions/is-readonly-node.mts +5 -5
- package/src/functions/functions/should-avoid-parentheses-for-readonly.mts +1 -5
- package/dist/cmd/append-as-const.d.mts +0 -3
- package/dist/cmd/append-as-const.d.mts.map +0 -1
- package/dist/cmd/append-as-const.mjs +0 -63
- package/dist/cmd/append-as-const.mjs.map +0 -1
- package/dist/cmd/convert-interface-to-type.d.mts +0 -3
- package/dist/cmd/convert-interface-to-type.d.mts.map +0 -1
- package/dist/cmd/convert-interface-to-type.mjs +0 -63
- package/dist/cmd/convert-interface-to-type.mjs.map +0 -1
- package/dist/cmd/convert-to-readonly.d.mts +0 -3
- package/dist/cmd/convert-to-readonly.d.mts.map +0 -1
- package/dist/cmd/convert-to-readonly.mjs +0 -63
- package/dist/cmd/convert-to-readonly.mjs.map +0 -1
- package/dist/cmd/replace-any-with-unknown.d.mts +0 -3
- package/dist/cmd/replace-any-with-unknown.d.mts.map +0 -1
- package/dist/cmd/replace-any-with-unknown.mjs +0 -63
- package/dist/cmd/replace-any-with-unknown.mjs.map +0 -1
- package/dist/cmd/replace-record-with-unknown-record.d.mts +0 -3
- package/dist/cmd/replace-record-with-unknown-record.d.mts.map +0 -1
- package/dist/cmd/replace-record-with-unknown-record.mjs +0 -63
- package/dist/cmd/replace-record-with-unknown-record.mjs.map +0 -1
- package/dist/cmd/run-transformer-cli.d.mts +0 -13
- package/dist/cmd/run-transformer-cli.d.mts.map +0 -1
- package/dist/cmd/run-transformer-cli.mjs +0 -176
- package/dist/cmd/run-transformer-cli.mjs.map +0 -1
- package/src/cmd/append-as-const.mts +0 -67
- package/src/cmd/convert-interface-to-type.mts +0 -67
- package/src/cmd/convert-to-readonly.mts +0 -67
- package/src/cmd/replace-any-with-unknown.mts +0 -67
- package/src/cmd/replace-record-with-unknown-record.mts +0 -67
- package/src/cmd/run-transformer-cli.mts +0 -307
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import * as cmd from 'cmd-ts';
|
|
4
|
-
import { Arr, Result } from 'ts-data-forge';
|
|
5
|
-
import { appendAsConstTransformer } from '../functions/index.mjs';
|
|
6
|
-
import { runTransformerCLI } from './run-transformer-cli.mjs';
|
|
7
|
-
|
|
8
|
-
const transformer = appendAsConstTransformer();
|
|
9
|
-
|
|
10
|
-
const cmdDef = cmd.command({
|
|
11
|
-
name: transformer.name,
|
|
12
|
-
version: '2.2.4',
|
|
13
|
-
args: {
|
|
14
|
-
baseDir: cmd.positional({
|
|
15
|
-
type: cmd.string,
|
|
16
|
-
displayName: 'baseDir',
|
|
17
|
-
description: 'The base directory in which to perform the conversion',
|
|
18
|
-
}),
|
|
19
|
-
exclude: cmd.multioption({
|
|
20
|
-
long: 'exclude',
|
|
21
|
-
type: cmd.optional(cmd.array(cmd.string)),
|
|
22
|
-
description:
|
|
23
|
-
'Glob patterns of files to exclude from the base directory (e.g., "src/generated/**/*.mts")',
|
|
24
|
-
}),
|
|
25
|
-
uncommitted: cmd.flag({
|
|
26
|
-
long: 'uncommitted',
|
|
27
|
-
type: cmd.optional(cmd.boolean),
|
|
28
|
-
description:
|
|
29
|
-
'If true, transforms only uncommitted files (untracked, modified, and staged files)',
|
|
30
|
-
}),
|
|
31
|
-
diffFrom: cmd.option({
|
|
32
|
-
long: 'diff-from',
|
|
33
|
-
type: cmd.optional(cmd.string),
|
|
34
|
-
description:
|
|
35
|
-
'If provided, transforms only files that differ from the given base branch or commit hash',
|
|
36
|
-
}),
|
|
37
|
-
silent: cmd.flag({
|
|
38
|
-
long: 'silent',
|
|
39
|
-
type: cmd.optional(cmd.boolean),
|
|
40
|
-
description: 'If true, suppresses output messages (default: false)',
|
|
41
|
-
}),
|
|
42
|
-
},
|
|
43
|
-
handler: (args) => {
|
|
44
|
-
(async (): Promise<void> => {
|
|
45
|
-
const result = await runTransformerCLI(
|
|
46
|
-
{
|
|
47
|
-
baseDir: args.baseDir,
|
|
48
|
-
exclude: args.exclude ?? [],
|
|
49
|
-
uncommitted: args.uncommitted ?? false,
|
|
50
|
-
diffFrom: args.diffFrom,
|
|
51
|
-
silent: args.silent ?? false,
|
|
52
|
-
},
|
|
53
|
-
[transformer],
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
if (Result.isErr(result)) {
|
|
57
|
-
process.exit(1);
|
|
58
|
-
}
|
|
59
|
-
})().catch((error: unknown) => {
|
|
60
|
-
console.error('An error occurred:', error);
|
|
61
|
-
|
|
62
|
-
process.exit(1);
|
|
63
|
-
});
|
|
64
|
-
},
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
await cmd.run(cmdDef, Arr.skip(process.argv, 2));
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import * as cmd from 'cmd-ts';
|
|
4
|
-
import { Arr, Result } from 'ts-data-forge';
|
|
5
|
-
import { convertInterfaceToTypeTransformer } from '../functions/index.mjs';
|
|
6
|
-
import { runTransformerCLI } from './run-transformer-cli.mjs';
|
|
7
|
-
|
|
8
|
-
const transformer = convertInterfaceToTypeTransformer();
|
|
9
|
-
|
|
10
|
-
const cmdDef = cmd.command({
|
|
11
|
-
name: transformer.name,
|
|
12
|
-
version: '2.2.4',
|
|
13
|
-
args: {
|
|
14
|
-
baseDir: cmd.positional({
|
|
15
|
-
type: cmd.string,
|
|
16
|
-
displayName: 'baseDir',
|
|
17
|
-
description: 'The base directory in which to perform the conversion',
|
|
18
|
-
}),
|
|
19
|
-
exclude: cmd.multioption({
|
|
20
|
-
long: 'exclude',
|
|
21
|
-
type: cmd.optional(cmd.array(cmd.string)),
|
|
22
|
-
description:
|
|
23
|
-
'Glob patterns of files to exclude from the base directory (e.g., "src/generated/**/*.mts")',
|
|
24
|
-
}),
|
|
25
|
-
uncommitted: cmd.flag({
|
|
26
|
-
long: 'uncommitted',
|
|
27
|
-
type: cmd.optional(cmd.boolean),
|
|
28
|
-
description:
|
|
29
|
-
'If true, transforms only uncommitted files (untracked, modified, and staged files)',
|
|
30
|
-
}),
|
|
31
|
-
diffFrom: cmd.option({
|
|
32
|
-
long: 'diff-from',
|
|
33
|
-
type: cmd.optional(cmd.string),
|
|
34
|
-
description:
|
|
35
|
-
'If provided, transforms only files that differ from the given base branch or commit hash',
|
|
36
|
-
}),
|
|
37
|
-
silent: cmd.flag({
|
|
38
|
-
long: 'silent',
|
|
39
|
-
type: cmd.optional(cmd.boolean),
|
|
40
|
-
description: 'If true, suppresses output messages (default: false)',
|
|
41
|
-
}),
|
|
42
|
-
},
|
|
43
|
-
handler: (args) => {
|
|
44
|
-
(async (): Promise<void> => {
|
|
45
|
-
const result = await runTransformerCLI(
|
|
46
|
-
{
|
|
47
|
-
baseDir: args.baseDir,
|
|
48
|
-
exclude: args.exclude ?? [],
|
|
49
|
-
uncommitted: args.uncommitted ?? false,
|
|
50
|
-
diffFrom: args.diffFrom,
|
|
51
|
-
silent: args.silent ?? false,
|
|
52
|
-
},
|
|
53
|
-
[transformer],
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
if (Result.isErr(result)) {
|
|
57
|
-
process.exit(1);
|
|
58
|
-
}
|
|
59
|
-
})().catch((error: unknown) => {
|
|
60
|
-
console.error('An error occurred:', error);
|
|
61
|
-
|
|
62
|
-
process.exit(1);
|
|
63
|
-
});
|
|
64
|
-
},
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
await cmd.run(cmdDef, Arr.skip(process.argv, 2));
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import * as cmd from 'cmd-ts';
|
|
4
|
-
import { Arr, Result } from 'ts-data-forge';
|
|
5
|
-
import { convertToReadonlyTransformer } from '../functions/index.mjs';
|
|
6
|
-
import { runTransformerCLI } from './run-transformer-cli.mjs';
|
|
7
|
-
|
|
8
|
-
const transformer = convertToReadonlyTransformer();
|
|
9
|
-
|
|
10
|
-
const cmdDef = cmd.command({
|
|
11
|
-
name: transformer.name,
|
|
12
|
-
version: '2.2.4',
|
|
13
|
-
args: {
|
|
14
|
-
baseDir: cmd.positional({
|
|
15
|
-
type: cmd.string,
|
|
16
|
-
displayName: 'baseDir',
|
|
17
|
-
description: 'The base directory in which to perform the conversion',
|
|
18
|
-
}),
|
|
19
|
-
exclude: cmd.multioption({
|
|
20
|
-
long: 'exclude',
|
|
21
|
-
type: cmd.optional(cmd.array(cmd.string)),
|
|
22
|
-
description:
|
|
23
|
-
'Glob patterns of files to exclude from the base directory (e.g., "src/generated/**/*.mts")',
|
|
24
|
-
}),
|
|
25
|
-
uncommitted: cmd.flag({
|
|
26
|
-
long: 'uncommitted',
|
|
27
|
-
type: cmd.optional(cmd.boolean),
|
|
28
|
-
description:
|
|
29
|
-
'If true, transforms only uncommitted files (untracked, modified, and staged files)',
|
|
30
|
-
}),
|
|
31
|
-
diffFrom: cmd.option({
|
|
32
|
-
long: 'diff-from',
|
|
33
|
-
type: cmd.optional(cmd.string),
|
|
34
|
-
description:
|
|
35
|
-
'If provided, transforms only files that differ from the given base branch or commit hash',
|
|
36
|
-
}),
|
|
37
|
-
silent: cmd.flag({
|
|
38
|
-
long: 'silent',
|
|
39
|
-
type: cmd.optional(cmd.boolean),
|
|
40
|
-
description: 'If true, suppresses output messages (default: false)',
|
|
41
|
-
}),
|
|
42
|
-
},
|
|
43
|
-
handler: (args) => {
|
|
44
|
-
(async (): Promise<void> => {
|
|
45
|
-
const result = await runTransformerCLI(
|
|
46
|
-
{
|
|
47
|
-
baseDir: args.baseDir,
|
|
48
|
-
exclude: args.exclude ?? [],
|
|
49
|
-
uncommitted: args.uncommitted ?? false,
|
|
50
|
-
diffFrom: args.diffFrom,
|
|
51
|
-
silent: args.silent ?? false,
|
|
52
|
-
},
|
|
53
|
-
[transformer],
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
if (Result.isErr(result)) {
|
|
57
|
-
process.exit(1);
|
|
58
|
-
}
|
|
59
|
-
})().catch((error: unknown) => {
|
|
60
|
-
console.error('An error occurred:', error);
|
|
61
|
-
|
|
62
|
-
process.exit(1);
|
|
63
|
-
});
|
|
64
|
-
},
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
await cmd.run(cmdDef, Arr.skip(process.argv, 2));
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import * as cmd from 'cmd-ts';
|
|
4
|
-
import { Arr, Result } from 'ts-data-forge';
|
|
5
|
-
import { replaceAnyWithUnknownTransformer } from '../functions/index.mjs';
|
|
6
|
-
import { runTransformerCLI } from './run-transformer-cli.mjs';
|
|
7
|
-
|
|
8
|
-
const transformer = replaceAnyWithUnknownTransformer();
|
|
9
|
-
|
|
10
|
-
const cmdDef = cmd.command({
|
|
11
|
-
name: transformer.name,
|
|
12
|
-
version: '2.2.4',
|
|
13
|
-
args: {
|
|
14
|
-
baseDir: cmd.positional({
|
|
15
|
-
type: cmd.string,
|
|
16
|
-
displayName: 'baseDir',
|
|
17
|
-
description: 'The base directory in which to perform the conversion',
|
|
18
|
-
}),
|
|
19
|
-
exclude: cmd.multioption({
|
|
20
|
-
long: 'exclude',
|
|
21
|
-
type: cmd.optional(cmd.array(cmd.string)),
|
|
22
|
-
description:
|
|
23
|
-
'Glob patterns of files to exclude from the base directory (e.g., "src/generated/**/*.mts")',
|
|
24
|
-
}),
|
|
25
|
-
uncommitted: cmd.flag({
|
|
26
|
-
long: 'uncommitted',
|
|
27
|
-
type: cmd.optional(cmd.boolean),
|
|
28
|
-
description:
|
|
29
|
-
'If true, transforms only uncommitted files (untracked, modified, and staged files)',
|
|
30
|
-
}),
|
|
31
|
-
diffFrom: cmd.option({
|
|
32
|
-
long: 'diff-from',
|
|
33
|
-
type: cmd.optional(cmd.string),
|
|
34
|
-
description:
|
|
35
|
-
'If provided, transforms only files that differ from the given base branch or commit hash',
|
|
36
|
-
}),
|
|
37
|
-
silent: cmd.flag({
|
|
38
|
-
long: 'silent',
|
|
39
|
-
type: cmd.optional(cmd.boolean),
|
|
40
|
-
description: 'If true, suppresses output messages (default: false)',
|
|
41
|
-
}),
|
|
42
|
-
},
|
|
43
|
-
handler: (args) => {
|
|
44
|
-
(async (): Promise<void> => {
|
|
45
|
-
const result = await runTransformerCLI(
|
|
46
|
-
{
|
|
47
|
-
baseDir: args.baseDir,
|
|
48
|
-
exclude: args.exclude ?? [],
|
|
49
|
-
uncommitted: args.uncommitted ?? false,
|
|
50
|
-
diffFrom: args.diffFrom,
|
|
51
|
-
silent: args.silent ?? false,
|
|
52
|
-
},
|
|
53
|
-
[transformer],
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
if (Result.isErr(result)) {
|
|
57
|
-
process.exit(1);
|
|
58
|
-
}
|
|
59
|
-
})().catch((error: unknown) => {
|
|
60
|
-
console.error('An error occurred:', error);
|
|
61
|
-
|
|
62
|
-
process.exit(1);
|
|
63
|
-
});
|
|
64
|
-
},
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
await cmd.run(cmdDef, Arr.skip(process.argv, 2));
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import * as cmd from 'cmd-ts';
|
|
4
|
-
import { Arr, Result } from 'ts-data-forge';
|
|
5
|
-
import { replaceRecordWithUnknownRecordTransformer } from '../functions/index.mjs';
|
|
6
|
-
import { runTransformerCLI } from './run-transformer-cli.mjs';
|
|
7
|
-
|
|
8
|
-
const transformer = replaceRecordWithUnknownRecordTransformer();
|
|
9
|
-
|
|
10
|
-
const cmdDef = cmd.command({
|
|
11
|
-
name: transformer.name,
|
|
12
|
-
version: '2.2.4',
|
|
13
|
-
args: {
|
|
14
|
-
baseDir: cmd.positional({
|
|
15
|
-
type: cmd.string,
|
|
16
|
-
displayName: 'baseDir',
|
|
17
|
-
description: 'The base directory in which to perform the conversion',
|
|
18
|
-
}),
|
|
19
|
-
exclude: cmd.multioption({
|
|
20
|
-
long: 'exclude',
|
|
21
|
-
type: cmd.optional(cmd.array(cmd.string)),
|
|
22
|
-
description:
|
|
23
|
-
'Glob patterns of files to exclude from the base directory (e.g., "src/generated/**/*.mts")',
|
|
24
|
-
}),
|
|
25
|
-
uncommitted: cmd.flag({
|
|
26
|
-
long: 'uncommitted',
|
|
27
|
-
type: cmd.optional(cmd.boolean),
|
|
28
|
-
description:
|
|
29
|
-
'If true, transforms only uncommitted files (untracked, modified, and staged files)',
|
|
30
|
-
}),
|
|
31
|
-
diffFrom: cmd.option({
|
|
32
|
-
long: 'diff-from',
|
|
33
|
-
type: cmd.optional(cmd.string),
|
|
34
|
-
description:
|
|
35
|
-
'If provided, transforms only files that differ from the given base branch or commit hash',
|
|
36
|
-
}),
|
|
37
|
-
silent: cmd.flag({
|
|
38
|
-
long: 'silent',
|
|
39
|
-
type: cmd.optional(cmd.boolean),
|
|
40
|
-
description: 'If true, suppresses output messages (default: false)',
|
|
41
|
-
}),
|
|
42
|
-
},
|
|
43
|
-
handler: (args) => {
|
|
44
|
-
(async (): Promise<void> => {
|
|
45
|
-
const result = await runTransformerCLI(
|
|
46
|
-
{
|
|
47
|
-
baseDir: args.baseDir,
|
|
48
|
-
exclude: args.exclude ?? [],
|
|
49
|
-
uncommitted: args.uncommitted ?? false,
|
|
50
|
-
diffFrom: args.diffFrom,
|
|
51
|
-
silent: args.silent ?? false,
|
|
52
|
-
},
|
|
53
|
-
[transformer],
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
if (Result.isErr(result)) {
|
|
57
|
-
process.exit(1);
|
|
58
|
-
}
|
|
59
|
-
})().catch((error: unknown) => {
|
|
60
|
-
console.error('An error occurred:', error);
|
|
61
|
-
|
|
62
|
-
process.exit(1);
|
|
63
|
-
});
|
|
64
|
-
},
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
await cmd.run(cmdDef, Arr.skip(process.argv, 2));
|
|
@@ -1,307 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-await-in-loop */
|
|
2
|
-
|
|
3
|
-
import dedent from 'dedent';
|
|
4
|
-
import * as fs from 'node:fs/promises';
|
|
5
|
-
import * as path from 'node:path';
|
|
6
|
-
import { Arr, expectType, Result, unknownToString } from 'ts-data-forge';
|
|
7
|
-
import {
|
|
8
|
-
getDiffFrom,
|
|
9
|
-
getModifiedFiles,
|
|
10
|
-
getStagedFiles,
|
|
11
|
-
getUntrackedFiles,
|
|
12
|
-
glob,
|
|
13
|
-
} from 'ts-repo-utils';
|
|
14
|
-
import {
|
|
15
|
-
transformSourceCode,
|
|
16
|
-
type TsMorphTransformer,
|
|
17
|
-
} from '../functions/index.mjs';
|
|
18
|
-
|
|
19
|
-
type GetFilesOptions = Readonly<{
|
|
20
|
-
baseDir: string;
|
|
21
|
-
exclude: readonly string[];
|
|
22
|
-
uncommitted: boolean;
|
|
23
|
-
diffFrom: string | undefined;
|
|
24
|
-
silent: boolean;
|
|
25
|
-
}>;
|
|
26
|
-
|
|
27
|
-
type TransformerCLIOptions = GetFilesOptions;
|
|
28
|
-
|
|
29
|
-
export const runTransformerCLI = async (
|
|
30
|
-
options: TransformerCLIOptions,
|
|
31
|
-
transformers: readonly TsMorphTransformer[],
|
|
32
|
-
): Promise<Result<undefined, undefined>> => {
|
|
33
|
-
const echoIfNotSilent = options.silent ? () => {} : console.log;
|
|
34
|
-
|
|
35
|
-
const errorIfNotSilent = options.silent ? () => {} : console.error;
|
|
36
|
-
|
|
37
|
-
const filesResult = await getFilesForTransformation(options);
|
|
38
|
-
|
|
39
|
-
if (Result.isErr(filesResult)) {
|
|
40
|
-
errorIfNotSilent(
|
|
41
|
-
options.diffFrom !== undefined
|
|
42
|
-
? `Error getting diff files from "${options.diffFrom}":`
|
|
43
|
-
: options.uncommitted
|
|
44
|
-
? 'Error getting uncommitted files:'
|
|
45
|
-
: 'Error finding files matching pattern:',
|
|
46
|
-
filesResult.value,
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
return Result.err(undefined);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const files = filesResult.value;
|
|
53
|
-
|
|
54
|
-
if (Arr.isEmpty(files)) {
|
|
55
|
-
echoIfNotSilent(
|
|
56
|
-
options.diffFrom !== undefined
|
|
57
|
-
? `No files found in diff from "${options.diffFrom}" matching pattern: ${options.baseDir}`
|
|
58
|
-
: options.uncommitted
|
|
59
|
-
? 'No uncommitted files found'
|
|
60
|
-
: `No files found matching pattern: ${options.baseDir}`,
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
return Result.ok(undefined);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const { errorFiles, transformedCount, unchangedCount } = await transformFiles(
|
|
67
|
-
files,
|
|
68
|
-
transformers,
|
|
69
|
-
options.silent,
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
const hr = '='.repeat(50);
|
|
73
|
-
|
|
74
|
-
echoIfNotSilent(dedent`
|
|
75
|
-
${hr}
|
|
76
|
-
Summary:
|
|
77
|
-
✅ Transformed: ${transformedCount}
|
|
78
|
-
⏭️ Unchanged: ${unchangedCount}
|
|
79
|
-
❌ Errors: ${errorFiles.length}
|
|
80
|
-
📊 Total: ${files.length}
|
|
81
|
-
`);
|
|
82
|
-
|
|
83
|
-
if (Arr.isNonEmpty(errorFiles)) {
|
|
84
|
-
echoIfNotSilent('\nFiles with errors:');
|
|
85
|
-
|
|
86
|
-
for (const fileName of errorFiles) {
|
|
87
|
-
echoIfNotSilent(` - ${fileName}`);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
echoIfNotSilent(hr);
|
|
92
|
-
|
|
93
|
-
if (Arr.isNonEmpty(errorFiles)) {
|
|
94
|
-
return Result.err(undefined);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return Result.ok(undefined);
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const getFilesForTransformation = async (
|
|
101
|
-
options: GetFilesOptions,
|
|
102
|
-
): Promise<
|
|
103
|
-
Result<
|
|
104
|
-
readonly string[],
|
|
105
|
-
| readonly unknown[]
|
|
106
|
-
| Readonly<{
|
|
107
|
-
message: string;
|
|
108
|
-
}>
|
|
109
|
-
>
|
|
110
|
-
> => {
|
|
111
|
-
const filesFromGlob = await getFilesFromGlob(
|
|
112
|
-
options.baseDir,
|
|
113
|
-
options.exclude,
|
|
114
|
-
options.silent,
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
if (Result.isErr(filesFromGlob)) {
|
|
118
|
-
return filesFromGlob;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
let mut_files = filesFromGlob.value;
|
|
122
|
-
|
|
123
|
-
if (options.uncommitted) {
|
|
124
|
-
const uncommittedFiles = await getUncommittedFiles(options.silent);
|
|
125
|
-
|
|
126
|
-
if (Result.isErr(uncommittedFiles)) {
|
|
127
|
-
return uncommittedFiles;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
mut_files = Arr.setIntersection(mut_files, uncommittedFiles.value);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (options.diffFrom !== undefined) {
|
|
134
|
-
const diffFiles = await getDiffFrom(options.diffFrom, {
|
|
135
|
-
silent: options.silent,
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
if (Result.isErr(diffFiles)) {
|
|
139
|
-
return diffFiles;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
mut_files = Arr.setIntersection(mut_files, diffFiles.value);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return Result.ok(mut_files);
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
const getUncommittedFiles = async (
|
|
149
|
-
silent: boolean,
|
|
150
|
-
): Promise<
|
|
151
|
-
Result<
|
|
152
|
-
readonly string[],
|
|
153
|
-
| readonly unknown[]
|
|
154
|
-
| Readonly<{
|
|
155
|
-
message: string;
|
|
156
|
-
}>
|
|
157
|
-
>
|
|
158
|
-
> => {
|
|
159
|
-
const mut_files: string[] = [];
|
|
160
|
-
|
|
161
|
-
const untrackedFilesResult = await getUntrackedFiles({ silent });
|
|
162
|
-
|
|
163
|
-
if (Result.isErr(untrackedFilesResult)) {
|
|
164
|
-
return untrackedFilesResult;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
mut_files.push(...untrackedFilesResult.value);
|
|
168
|
-
|
|
169
|
-
const modifiedFilesResult = await getModifiedFiles({ silent });
|
|
170
|
-
|
|
171
|
-
if (Result.isErr(modifiedFilesResult)) {
|
|
172
|
-
return modifiedFilesResult;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
mut_files.push(...modifiedFilesResult.value);
|
|
176
|
-
|
|
177
|
-
const stagedFilesResult = await getStagedFiles({ silent });
|
|
178
|
-
|
|
179
|
-
if (Result.isErr(stagedFilesResult)) {
|
|
180
|
-
return stagedFilesResult;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
mut_files.push(...stagedFilesResult.value);
|
|
184
|
-
|
|
185
|
-
return Result.ok(Arr.uniq(mut_files));
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
const getFilesFromGlob = async (
|
|
189
|
-
baseDir: string,
|
|
190
|
-
exclude: readonly string[],
|
|
191
|
-
silent: boolean,
|
|
192
|
-
): Promise<
|
|
193
|
-
Result<
|
|
194
|
-
readonly string[],
|
|
195
|
-
| readonly unknown[]
|
|
196
|
-
| Readonly<{
|
|
197
|
-
message: string;
|
|
198
|
-
}>
|
|
199
|
-
>
|
|
200
|
-
> => {
|
|
201
|
-
const globResult = await glob(baseDir, {
|
|
202
|
-
// Never transform files inside dependencies. With pnpm,
|
|
203
|
-
// `packages/*/node_modules/<dep>` are symlinks whose `src/` directory would
|
|
204
|
-
// otherwise be matched by patterns such as
|
|
205
|
-
// `packages/**/{src,scripts,samples,test}/**`, so the codemod would rewrite
|
|
206
|
-
// dependency source files (and could hit pathological types there).
|
|
207
|
-
ignore: Arr.toPushed(exclude, '**/node_modules/**'),
|
|
208
|
-
absolute: true,
|
|
209
|
-
// Do not escape the project tree by following symlinks (e.g. the pnpm
|
|
210
|
-
// `node_modules` symlinks above, or workspace-package links).
|
|
211
|
-
followSymbolicLinks: false,
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
if (Result.isErr(globResult)) {
|
|
215
|
-
if (!silent) {
|
|
216
|
-
console.error('Error finding files matching pattern:', globResult.value);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return Result.err([globResult.value]);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
return Result.ok(globResult.value);
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
const transformFiles = async (
|
|
226
|
-
filePaths: readonly string[],
|
|
227
|
-
transformers: readonly TsMorphTransformer[],
|
|
228
|
-
silent: boolean,
|
|
229
|
-
): Promise<
|
|
230
|
-
Readonly<{
|
|
231
|
-
transformedCount: number;
|
|
232
|
-
unchangedCount: number;
|
|
233
|
-
errorFiles: readonly string[];
|
|
234
|
-
}>
|
|
235
|
-
> => {
|
|
236
|
-
let mut_transformedCount: number = 0;
|
|
237
|
-
|
|
238
|
-
let mut_unchangedCount: number = 0;
|
|
239
|
-
|
|
240
|
-
const mut_errorFiles: string[] = [];
|
|
241
|
-
|
|
242
|
-
for (const filePath of filePaths) {
|
|
243
|
-
const result = await transformOneFile(filePath, transformers, silent);
|
|
244
|
-
|
|
245
|
-
if (Result.isOk(result)) {
|
|
246
|
-
expectType<typeof result.value, 'unchanged' | 'transformed'>('=');
|
|
247
|
-
|
|
248
|
-
if (result.value === 'transformed') {
|
|
249
|
-
mut_transformedCount += 1;
|
|
250
|
-
} else {
|
|
251
|
-
mut_unchangedCount += 1;
|
|
252
|
-
}
|
|
253
|
-
} else {
|
|
254
|
-
mut_errorFiles.push(path.basename(filePath));
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
return {
|
|
259
|
-
transformedCount: mut_transformedCount,
|
|
260
|
-
unchangedCount: mut_unchangedCount,
|
|
261
|
-
errorFiles: mut_errorFiles,
|
|
262
|
-
};
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
const transformOneFile = async (
|
|
266
|
-
filePath: string,
|
|
267
|
-
transformers: readonly TsMorphTransformer[],
|
|
268
|
-
silent: boolean,
|
|
269
|
-
): Promise<Result<'unchanged' | 'transformed', string>> => {
|
|
270
|
-
const echoIfNotSilent = silent ? () => {} : console.log;
|
|
271
|
-
|
|
272
|
-
const errorIfNotSilent = silent ? () => {} : console.error;
|
|
273
|
-
|
|
274
|
-
const fileName = path.basename(filePath);
|
|
275
|
-
|
|
276
|
-
const isTsx = fileName.endsWith('.tsx') || fileName.endsWith('.jsx');
|
|
277
|
-
|
|
278
|
-
try {
|
|
279
|
-
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
|
280
|
-
const originalCode = await fs.readFile(filePath, 'utf8');
|
|
281
|
-
|
|
282
|
-
const transformedCode = transformSourceCode(
|
|
283
|
-
originalCode,
|
|
284
|
-
isTsx,
|
|
285
|
-
transformers,
|
|
286
|
-
);
|
|
287
|
-
|
|
288
|
-
if (transformedCode === originalCode) {
|
|
289
|
-
echoIfNotSilent(`⏭️ ${fileName} - no changes needed`);
|
|
290
|
-
|
|
291
|
-
return Result.ok('unchanged');
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
|
295
|
-
await fs.writeFile(filePath, transformedCode, 'utf8');
|
|
296
|
-
|
|
297
|
-
echoIfNotSilent(`✅ ${fileName} - transformed`);
|
|
298
|
-
|
|
299
|
-
return Result.ok('transformed');
|
|
300
|
-
} catch (error) {
|
|
301
|
-
const errStr = unknownToString(error);
|
|
302
|
-
|
|
303
|
-
errorIfNotSilent(`❌ ${fileName} - error: ${errStr}`);
|
|
304
|
-
|
|
305
|
-
return Result.err(errStr);
|
|
306
|
-
}
|
|
307
|
-
};
|