@ts-for-gir/cli 4.0.0-beta.24 → 4.0.0-beta.26
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 +84 -2
- package/bin/ts-for-gir.js +43 -0
- package/package.json +30 -36
- package/src/commands/analyze.ts +344 -0
- package/src/commands/command-builder.ts +30 -0
- package/src/commands/copy.ts +71 -76
- package/src/commands/doc.ts +58 -46
- package/src/commands/generate.ts +97 -77
- package/src/commands/index.ts +6 -4
- package/src/commands/json.ts +104 -0
- package/src/commands/list.ts +81 -90
- package/src/config/config-loader.ts +203 -0
- package/src/config/config-writer.ts +52 -0
- package/src/config/defaults.ts +61 -0
- package/src/config/index.ts +8 -0
- package/src/config/options.ts +292 -0
- package/src/config.ts +3 -456
- package/src/formatters/typescript-formatter.ts +24 -0
- package/src/generation-handler.ts +122 -67
- package/src/index.ts +4 -4
- package/src/module-loader/dependency-resolver.ts +100 -0
- package/src/module-loader/file-finder.ts +56 -0
- package/src/module-loader/index.ts +8 -0
- package/src/module-loader/module-grouper.ts +77 -0
- package/src/module-loader/prompt-handler.ts +111 -0
- package/src/module-loader.ts +280 -580
- package/src/start.ts +18 -14
- package/src/types/command-args.ts +110 -0
- package/src/types/command-definition.ts +15 -0
- package/src/types/commands.ts +35 -0
- package/src/types/index.ts +15 -0
- package/src/types/report-types.ts +34 -0
- package/lib/commands/copy.d.ts +0 -12
- package/lib/commands/copy.js +0 -78
- package/lib/commands/copy.js.map +0 -1
- package/lib/commands/doc.d.ts +0 -12
- package/lib/commands/doc.js +0 -38
- package/lib/commands/doc.js.map +0 -1
- package/lib/commands/generate.d.ts +0 -12
- package/lib/commands/generate.js +0 -70
- package/lib/commands/generate.js.map +0 -1
- package/lib/commands/index.d.ts +0 -4
- package/lib/commands/index.js +0 -5
- package/lib/commands/index.js.map +0 -1
- package/lib/commands/list.d.ts +0 -12
- package/lib/commands/list.js +0 -79
- package/lib/commands/list.js.map +0 -1
- package/lib/config.d.ts +0 -108
- package/lib/config.js +0 -409
- package/lib/config.js.map +0 -1
- package/lib/generation-handler.d.ts +0 -10
- package/lib/generation-handler.js +0 -48
- package/lib/generation-handler.js.map +0 -1
- package/lib/index.d.ts +0 -4
- package/lib/index.js +0 -5
- package/lib/index.js.map +0 -1
- package/lib/module-loader.d.ts +0 -154
- package/lib/module-loader.js +0 -465
- package/lib/module-loader.js.map +0 -1
- package/lib/start.d.ts +0 -2
- package/lib/start.js +0 -16
- package/lib/start.js.map +0 -1
package/src/commands/copy.ts
CHANGED
|
@@ -2,93 +2,88 @@
|
|
|
2
2
|
* Everything you need for the `ts-for-gir copy` command is located here
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
5
|
+
import { copyFile, mkdir } from "node:fs/promises";
|
|
6
|
+
import { basename, join } from "node:path";
|
|
7
|
+
import type { ConfigFlags, GirModuleResolvedBy, UserConfig } from "@ts-for-gir/lib";
|
|
8
|
+
import { APP_NAME, ERROR_NO_MODULES_FOUND, Logger, NSRegistry } from "@ts-for-gir/lib";
|
|
9
|
+
import { copyOptions, getOptionsGeneration, load } from "../config.ts";
|
|
10
|
+
import { ModuleLoader } from "../module-loader.ts";
|
|
11
|
+
import type { CopyCommandArgs } from "../types/index.ts";
|
|
12
|
+
import { createBuilder } from "./command-builder.ts";
|
|
11
13
|
|
|
12
|
-
|
|
14
|
+
const command = "copy [modules..]";
|
|
13
15
|
|
|
14
|
-
const
|
|
16
|
+
const description = "Scan for *.gir files and copy them to a new directory";
|
|
15
17
|
|
|
16
|
-
const
|
|
18
|
+
const logger = new Logger(false, "CopyCommand");
|
|
17
19
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
const examples: ReadonlyArray<[string, string?]> = [
|
|
21
|
+
[`${APP_NAME} copy -o ./gir`, "Copy found *.gir files to ./gir"],
|
|
22
|
+
[
|
|
23
|
+
`${APP_NAME} copy -g /usr/share/gir-1.0 --ignore=Gtk-3.0 xrandr-1.3 -o ./gir`,
|
|
24
|
+
"Copy all found *.gir files in /usr/share/gir-1.0 excluding Gtk-3.0 and xrandr-1.3 to ./gir",
|
|
25
|
+
],
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
const builder = createBuilder<CopyCommandArgs>(copyOptions, examples);
|
|
26
29
|
|
|
27
30
|
const copyGirFile = async (config: UserConfig, depModule: GirModuleResolvedBy) => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
31
|
+
if (!depModule.path) {
|
|
32
|
+
logger.danger(`- ${depModule.packageName} not found`);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (!config.outdir) {
|
|
36
|
+
logger.error("outdir not found");
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const filename = basename(depModule.path);
|
|
40
|
+
const dest = join(config.outdir, filename);
|
|
41
|
+
if (depModule.path === dest) {
|
|
42
|
+
logger.yellow(`Skip ${depModule.path}`);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
logger.success(`Copy ${depModule.path}`);
|
|
46
|
+
await copyFile(depModule.path, dest);
|
|
47
|
+
};
|
|
45
48
|
|
|
46
|
-
|
|
47
49
|
const handler = async (args: ConfigFlags) => {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
50
|
+
const config = await load(args);
|
|
51
|
+
const generateConfig = getOptionsGeneration(config);
|
|
52
|
+
const registry = new NSRegistry(); // TODO: Use singleton
|
|
53
|
+
const moduleLoader = new ModuleLoader(generateConfig, registry);
|
|
54
|
+
const { grouped, failed } = await moduleLoader.getModules(config.modules, config.ignore);
|
|
55
|
+
const moduleGroups = Object.values(grouped);
|
|
56
|
+
if (Object.keys(grouped).length === 0) {
|
|
57
|
+
return logger.error(ERROR_NO_MODULES_FOUND(config.girDirectories));
|
|
58
|
+
}
|
|
56
59
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
60
|
+
if (!config.outdir) {
|
|
61
|
+
logger.error("outdir not found");
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
61
64
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
+
await mkdir(config.outdir, { recursive: true }).catch((err) => {
|
|
66
|
+
logger.error(`Failed to copy gir files to ${config.outdir}: ${err}`);
|
|
67
|
+
});
|
|
65
68
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
for (const module of moduleGroups) {
|
|
70
|
+
for (const mod of module.modules) {
|
|
71
|
+
await copyGirFile(config, mod);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
71
74
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const examples: ReadonlyArray<[string, string?]> = [
|
|
81
|
-
[`${Config.appName} copy -o ./gir`, `Copy found *.gir files to ./gir`],
|
|
82
|
-
[
|
|
83
|
-
`${Config.appName} copy -g /usr/share/gir-1.0 --ignore=Gtk-3.0 xrandr-1.3 -o ./gir`,
|
|
84
|
-
'Copy all found *.gir files in /usr/share/gir-1.0 excluding Gtk-3.0 and xrandr-1.3 to ./gir',
|
|
85
|
-
],
|
|
86
|
-
]
|
|
75
|
+
if (failed.length > 0) {
|
|
76
|
+
logger.danger("\nDependencies not found:");
|
|
77
|
+
for (const fail of failed) {
|
|
78
|
+
logger.white(`- ${fail}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
87
82
|
|
|
88
83
|
export const copy = {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
84
|
+
command,
|
|
85
|
+
description,
|
|
86
|
+
builder,
|
|
87
|
+
handler,
|
|
88
|
+
examples,
|
|
89
|
+
};
|
package/src/commands/doc.ts
CHANGED
|
@@ -2,57 +2,69 @@
|
|
|
2
2
|
* Everything you need for the `ts-for-gir generate` command is located here
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
import { GeneratorType } from "@ts-for-gir/generator-base";
|
|
6
|
+
import type { ConfigFlags } from "@ts-for-gir/lib";
|
|
7
|
+
import {
|
|
8
|
+
configureConflictsReporter,
|
|
9
|
+
ERROR_NO_MODULES_FOUND,
|
|
10
|
+
type GirModule,
|
|
11
|
+
Logger,
|
|
12
|
+
NSRegistry,
|
|
13
|
+
TypeIdentifier,
|
|
14
|
+
} from "@ts-for-gir/lib";
|
|
15
|
+
import type { Argv, BuilderCallback } from "yargs";
|
|
16
|
+
import { docOptions, getOptionsGeneration, load } from "../config.ts";
|
|
17
|
+
import { GenerationHandler } from "../generation-handler.ts";
|
|
18
|
+
import { ModuleLoader } from "../module-loader.ts";
|
|
19
|
+
import type { DocCommandArgs } from "../types/index.ts";
|
|
11
20
|
|
|
12
|
-
|
|
21
|
+
const command = "doc [modules..]";
|
|
13
22
|
|
|
14
|
-
const
|
|
23
|
+
const description = "The HTML documentation generator is not yet implemented, but feel free to implement it 🤗";
|
|
15
24
|
|
|
16
|
-
const
|
|
25
|
+
const logger = new Logger(false, "DocCommand");
|
|
17
26
|
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
27
|
+
const builder: BuilderCallback<DocCommandArgs, ConfigFlags> = (yargs: Argv<DocCommandArgs>) => {
|
|
28
|
+
const optionNames = Object.keys(docOptions);
|
|
29
|
+
for (const optionName of optionNames) {
|
|
30
|
+
yargs = yargs.option(optionName, docOptions[optionName]);
|
|
31
|
+
}
|
|
32
|
+
return yargs.example(examples) as Argv<ConfigFlags>;
|
|
33
|
+
};
|
|
26
34
|
|
|
27
|
-
|
|
28
35
|
const handler = async (args: ConfigFlags) => {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const
|
|
36
|
+
const config = await load(args);
|
|
37
|
+
|
|
38
|
+
const generateConfig = getOptionsGeneration(config);
|
|
39
|
+
const registry = new NSRegistry(); // TODO: Use singleton
|
|
40
|
+
|
|
41
|
+
// Configure reporters BEFORE parsing to capture all problems
|
|
42
|
+
if (generateConfig.reporter) {
|
|
43
|
+
TypeIdentifier.configureReporter(generateConfig.reporter, generateConfig.reporterOutput);
|
|
44
|
+
configureConflictsReporter(generateConfig.reporter, generateConfig.reporterOutput);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const moduleLoader = new ModuleLoader(generateConfig, registry);
|
|
48
|
+
const { keep } = await moduleLoader.getModulesResolved(
|
|
49
|
+
config.modules,
|
|
50
|
+
config.ignore || [],
|
|
51
|
+
config.ignoreVersionConflicts,
|
|
52
|
+
);
|
|
53
|
+
if (keep.length === 0) {
|
|
54
|
+
logger.error(ERROR_NO_MODULES_FOUND(config.girDirectories));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const tsForGir = new GenerationHandler(generateConfig, GeneratorType.HTML_DOC, registry);
|
|
58
|
+
|
|
59
|
+
await tsForGir.start(Array.from(keep).map((girModuleResolvedBy) => girModuleResolvedBy.module as GirModule));
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const examples: ReadonlyArray<[string, string?]> = [];
|
|
51
63
|
|
|
52
64
|
export const doc = {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
65
|
+
command,
|
|
66
|
+
description,
|
|
67
|
+
builder,
|
|
68
|
+
handler,
|
|
69
|
+
examples,
|
|
70
|
+
};
|
package/src/commands/generate.ts
CHANGED
|
@@ -2,90 +2,110 @@
|
|
|
2
2
|
* Everything you need for the `ts-for-gir generate` command is located here
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
5
|
+
import { GeneratorType } from "@ts-for-gir/generator-base";
|
|
6
|
+
import type { ConfigFlags } from "@ts-for-gir/lib";
|
|
7
|
+
import {
|
|
8
|
+
APP_NAME,
|
|
9
|
+
configureConflictsReporter,
|
|
10
|
+
ERROR_NO_MODULES_FOUND,
|
|
11
|
+
type GirModule,
|
|
12
|
+
Logger,
|
|
13
|
+
NSRegistry,
|
|
14
|
+
ReporterService,
|
|
15
|
+
TypeIdentifier,
|
|
16
|
+
} from "@ts-for-gir/lib";
|
|
17
|
+
import { generateOptions, getOptionsGeneration, load } from "../config.ts";
|
|
18
|
+
import { TypeScriptFormatter } from "../formatters/typescript-formatter.ts";
|
|
19
|
+
import { GenerationHandler } from "../generation-handler.ts";
|
|
20
|
+
import { ModuleLoader } from "../module-loader.ts";
|
|
21
|
+
import type { GenerateCommandArgs } from "../types/index.ts";
|
|
22
|
+
import { createBuilder } from "./command-builder.ts";
|
|
23
|
+
|
|
24
|
+
const command = "generate [modules..]";
|
|
25
|
+
|
|
26
|
+
const description = "Generates Typescript type definition .d.ts files from GIR for GJS";
|
|
27
|
+
|
|
28
|
+
const logger = new Logger(false, "GenerateCommand");
|
|
29
|
+
|
|
30
|
+
const examples: ReadonlyArray<[string, string?]> = [
|
|
31
|
+
[
|
|
32
|
+
`${APP_NAME} generate`,
|
|
33
|
+
`Run '${APP_NAME} generate' in your gjs project to generate typings for your project, pass the gir modules you need for your project`,
|
|
34
|
+
],
|
|
35
|
+
[`${APP_NAME} generate Gtk*`, "You can also use wild cards"],
|
|
36
|
+
[`${APP_NAME} generate '*'`, "If you want to parse all of your locally installed gir modules run"],
|
|
37
|
+
[`${APP_NAME} generate --configName='.ts-for-gir.gtk4.rc.js`, "Use a special config file"],
|
|
38
|
+
[`${APP_NAME} generate --ignore=Gtk-4.0 xrandr-1.3`, "Generate .d.ts. files but not for Gtk-4.0 and xrandr-1.3"],
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
const builder = createBuilder<GenerateCommandArgs>(generateOptions, examples);
|
|
42
|
+
|
|
30
43
|
const handler = async (args: ConfigFlags) => {
|
|
31
|
-
|
|
44
|
+
const config = await load(args);
|
|
32
45
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const { keep } = await moduleLoader.getModulesResolved(
|
|
36
|
-
config.modules,
|
|
37
|
-
config.ignore || [],
|
|
38
|
-
config.ignoreVersionConflicts,
|
|
39
|
-
)
|
|
46
|
+
const generateConfig = getOptionsGeneration(config);
|
|
47
|
+
const registry = new NSRegistry(); // TODO: Use singleton
|
|
40
48
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
49
|
+
// Register TypeScript formatter for .d.ts files
|
|
50
|
+
registry.registerFormatter("dts", new TypeScriptFormatter());
|
|
44
51
|
|
|
45
|
-
|
|
52
|
+
const moduleLoader = new ModuleLoader(generateConfig, registry);
|
|
46
53
|
|
|
47
|
-
|
|
54
|
+
// Configure reporters BEFORE parsing to capture all problems
|
|
55
|
+
if (generateConfig.reporter) {
|
|
56
|
+
TypeIdentifier.configureReporter(generateConfig.reporter, generateConfig.reporterOutput);
|
|
57
|
+
configureConflictsReporter(generateConfig.reporter, generateConfig.reporterOutput);
|
|
58
|
+
}
|
|
48
59
|
|
|
49
|
-
|
|
60
|
+
let tsForGir: GenerationHandler | null = null;
|
|
50
61
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
62
|
+
try {
|
|
63
|
+
const { keep } = await moduleLoader.getModulesResolved(
|
|
64
|
+
config.modules,
|
|
65
|
+
config.ignore || [],
|
|
66
|
+
config.ignoreVersionConflicts,
|
|
67
|
+
);
|
|
54
68
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
69
|
+
if (keep.length === 0) {
|
|
70
|
+
logger.error(ERROR_NO_MODULES_FOUND(config.girDirectories));
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
moduleLoader.parse(keep);
|
|
75
|
+
|
|
76
|
+
tsForGir = new GenerationHandler(generateConfig, GeneratorType.TYPES, registry);
|
|
77
|
+
|
|
78
|
+
const girModules = Array.from(keep).map((girModuleResolvedBy) => girModuleResolvedBy.module as GirModule);
|
|
79
|
+
|
|
80
|
+
await tsForGir.start(girModules);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
// If reporter is enabled and tsForGir was created, make sure the report is generated
|
|
83
|
+
if (generateConfig.reporter && tsForGir) {
|
|
84
|
+
const service = ReporterService.getInstance();
|
|
85
|
+
|
|
86
|
+
// Log the error to the reporter
|
|
87
|
+
if (tsForGir.log) {
|
|
88
|
+
tsForGir.log.reportGenerationFailure(
|
|
89
|
+
"Main",
|
|
90
|
+
error instanceof Error ? error : new Error(String(error)),
|
|
91
|
+
"Generation failed",
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Generate and save the report
|
|
96
|
+
await service.printComprehensiveSummary();
|
|
97
|
+
await service.saveComprehensiveReport();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Re-throw the error to maintain existing behavior
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
84
104
|
|
|
85
105
|
export const generate = {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
106
|
+
command,
|
|
107
|
+
description,
|
|
108
|
+
builder,
|
|
109
|
+
handler,
|
|
110
|
+
examples,
|
|
111
|
+
};
|
package/src/commands/index.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
1
|
+
export * from "./analyze.ts";
|
|
2
|
+
export * from "./copy.ts";
|
|
3
|
+
export * from "./doc.ts";
|
|
4
|
+
export * from "./generate.ts";
|
|
5
|
+
export * from "./json.ts";
|
|
6
|
+
export * from "./list.ts";
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Everything you need for the `ts-for-gir json` command is located here
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { GeneratorType } from "@ts-for-gir/generator-base";
|
|
6
|
+
import type { ConfigFlags } from "@ts-for-gir/lib";
|
|
7
|
+
import {
|
|
8
|
+
APP_NAME,
|
|
9
|
+
configureConflictsReporter,
|
|
10
|
+
ERROR_NO_MODULES_FOUND,
|
|
11
|
+
type GirModule,
|
|
12
|
+
Logger,
|
|
13
|
+
NSRegistry,
|
|
14
|
+
ReporterService,
|
|
15
|
+
TypeIdentifier,
|
|
16
|
+
} from "@ts-for-gir/lib";
|
|
17
|
+
import { generateOptions, getOptionsGeneration, load } from "../config.ts";
|
|
18
|
+
import { GenerationHandler } from "../generation-handler.ts";
|
|
19
|
+
import { ModuleLoader } from "../module-loader.ts";
|
|
20
|
+
import type { GenerateCommandArgs } from "../types/index.ts";
|
|
21
|
+
import { createBuilder } from "./command-builder.ts";
|
|
22
|
+
|
|
23
|
+
const command = "json [modules..]";
|
|
24
|
+
|
|
25
|
+
const description = "Generates JSON representation from GIR files for analysis and tooling";
|
|
26
|
+
|
|
27
|
+
const logger = new Logger(false, "JsonCommand");
|
|
28
|
+
|
|
29
|
+
const examples: ReadonlyArray<[string, string?]> = [
|
|
30
|
+
[`${APP_NAME} json`, `Run '${APP_NAME} json' in your gjs project to generate JSON files for your project`],
|
|
31
|
+
[`${APP_NAME} json Gtk*`, "You can also use wild cards"],
|
|
32
|
+
[`${APP_NAME} json '*'`, "If you want to parse all of your locally installed gir modules run"],
|
|
33
|
+
[`${APP_NAME} json --configName='.ts-for-gir.gtk4.rc.js`, "Use a special config file"],
|
|
34
|
+
[`${APP_NAME} json --ignore=Gtk-4.0 xrandr-1.3`, "Generate JSON files but not for Gtk-4.0 and xrandr-1.3"],
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
const builder = createBuilder<GenerateCommandArgs>(generateOptions, examples);
|
|
38
|
+
|
|
39
|
+
const handler = async (args: ConfigFlags) => {
|
|
40
|
+
const config = await load(args);
|
|
41
|
+
|
|
42
|
+
const generateConfig = getOptionsGeneration(config);
|
|
43
|
+
const registry = new NSRegistry(); // TODO: Use singleton
|
|
44
|
+
|
|
45
|
+
const moduleLoader = new ModuleLoader(generateConfig, registry);
|
|
46
|
+
|
|
47
|
+
// Configure reporters BEFORE parsing to capture all problems
|
|
48
|
+
if (generateConfig.reporter) {
|
|
49
|
+
TypeIdentifier.configureReporter(generateConfig.reporter, generateConfig.reporterOutput);
|
|
50
|
+
configureConflictsReporter(generateConfig.reporter, generateConfig.reporterOutput);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let tsForGir: GenerationHandler | null = null;
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const { keep } = await moduleLoader.getModulesResolved(
|
|
57
|
+
config.modules,
|
|
58
|
+
config.ignore || [],
|
|
59
|
+
config.ignoreVersionConflicts,
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
if (keep.length === 0) {
|
|
63
|
+
logger.error(ERROR_NO_MODULES_FOUND(config.girDirectories));
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
moduleLoader.parse(keep);
|
|
68
|
+
|
|
69
|
+
tsForGir = new GenerationHandler(generateConfig, GeneratorType.JSON, registry);
|
|
70
|
+
|
|
71
|
+
const girModules = Array.from(keep).map((girModuleResolvedBy) => girModuleResolvedBy.module as GirModule);
|
|
72
|
+
|
|
73
|
+
await tsForGir.start(girModules);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
// If reporter is enabled and tsForGir was created, make sure the report is generated
|
|
76
|
+
if (generateConfig.reporter && tsForGir) {
|
|
77
|
+
const service = ReporterService.getInstance();
|
|
78
|
+
|
|
79
|
+
// Log the error to the reporter
|
|
80
|
+
if (tsForGir.log) {
|
|
81
|
+
tsForGir.log.reportGenerationFailure(
|
|
82
|
+
"Main",
|
|
83
|
+
error instanceof Error ? error : new Error(String(error)),
|
|
84
|
+
"JSON generation failed",
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Generate and save the report
|
|
89
|
+
await service.printComprehensiveSummary();
|
|
90
|
+
await service.saveComprehensiveReport();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Re-throw the error to maintain existing behavior
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export const json = {
|
|
99
|
+
command,
|
|
100
|
+
description,
|
|
101
|
+
builder,
|
|
102
|
+
handler,
|
|
103
|
+
examples,
|
|
104
|
+
};
|