@rbleattler/omp-ts-typegen 0.2025.1 → 0.2025.68

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.
@@ -1,184 +1,184 @@
1
- import fs from 'fs/promises';
2
- import path from 'path';
3
- import axios from 'axios';
4
- import {
5
- quicktype,
6
- InputData,
7
- jsonInputForTargetLanguage,
8
- JSONSchemaInput,
9
- FetchingJSONSchemaStore
10
- } from "quicktype-core";
11
-
12
- const SCHEMA_URL = 'https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/refs/heads/main/themes/schema.json';
13
- const OUTPUT_DIR = path.join(process.cwd(), 'src', 'types');
14
- const OUTPUT_FILE = path.join(OUTPUT_DIR, 'omp.ts');
15
- const SCHEMA_CACHE_FILE = path.join(process.cwd(), 'cache', 'schema.json');
16
-
17
- async function main() {
18
- // Dynamically import chalk (ESM module)
19
- const { default: chalk } = await import('chalk');
20
-
21
- console.log(chalk.blue('Starting TypeScript type generation...'));
22
-
23
- try {
24
- // Ensure output directories exist
25
- await fs.mkdir(path.join(process.cwd(), 'cache'), { recursive: true });
26
- await fs.mkdir(OUTPUT_DIR, { recursive: true });
27
-
28
- // Fetch the latest schema from the Oh My Posh repository
29
- console.log(chalk.yellow('Fetching schema from Oh My Posh repository...'));
30
- const response = await axios.get(SCHEMA_URL);
31
- const schema = response.data;
32
-
33
- // This is a dumb workaround but maybe someday I can get the schema file to not have this title...
34
- // Remove the title property from definitions.segment to avoid dumb naming
35
- //TODO: Create an issue on the Oh My Posh repository to remove the title property from the segment definition in the schema file... or fix it myself with a PR
36
- // PR pending: [fix: update segment title in schema.json #6244](https://github.com/JanDeDobbeleer/oh-my-posh/pull/6244)
37
- if (schema.definitions && schema.definitions.segment) {
38
- delete schema.definitions.segment.title;
39
- }
40
-
41
- // Cache the schema for future comparisons
42
- await fs.writeFile(SCHEMA_CACHE_FILE, JSON.stringify(schema, null, 2), { flag: 'w' });
43
-
44
-
45
- console.log(chalk.green('Schema fetched and cached successfully.'));
46
-
47
- // Generate TypeScript types using quicktype with the provided options
48
- console.log(chalk.yellow('Generating TypeScript types...'));
49
-
50
-
51
-
52
- // Set up quicktype options based on the available options in quicktype-core
53
- const quicktypeOptions = {
54
- // if debug add debugPrintGraph: true
55
- // This will print the type graph to the console at every processing step
56
- debugPrintGraph: process.features.debug ? true : false,
57
-
58
- // Check Provenance:
59
- // Check that we're propagating all type attributes (unless we actually can't)
60
- //! Experimenting with this
61
- checkProvenance: true,
62
-
63
- inferMaps: true,
64
- inferEnums: true,
65
- inferDateTimes: true,
66
- inferIntegerStrings: true,
67
- inferBooleanStrings: true,
68
- inferUUIDStrings: true, // Renamed from inferUuids to match quicktype's API
69
- combineClasses: true,
70
- allPropertiesOptional: false,
71
- alphabetizeProperties: true, // Sort properties for better readability
72
- rendererOptions: {
73
- "explicit-unions": true,
74
- "runtime-typecheck": true,
75
- "acronym-style": "original",
76
- "converters": "top-level",
77
- "raw-type": "json",
78
- "prefer-unions": true,
79
- "prefer-types": true
80
- },
81
- // Additional options from Options interface
82
- fixedTopLevels: true,
83
- leadingComments: undefined,
84
- density: "normal" as const, // Available options: 'normal', 'dense'
85
- useStructuralFeatures: true
86
- };
87
-
88
- // Generate the types using quicktype API
89
- const { lines } = await quicktypeJSONSchema("typescript", "OhMyPosh", JSON.stringify(schema), quicktypeOptions);
90
-
91
- // Add a header to the generated file
92
- const header = `/**
93
- * Oh My Posh TypeScript definitions
94
- *
95
- * Generated from schema: ${SCHEMA_URL}
96
- * Generated on: ${new Date().toISOString()}
97
- *
98
- * @see https://ohmyposh.dev/docs/
99
- */
100
-
101
- /* eslint-disable */
102
- /* tslint:disable */
103
-
104
- `;
105
-
106
- let typesContent = header + lines.join('\n');
107
-
108
- // Ensure the OhMyPosh type is properly exported
109
- // This adds an export statement if one isn't already present
110
- if (!typesContent.includes('export interface OhMyPosh')) {
111
- typesContent = typesContent.replace(
112
- 'interface OhMyPosh',
113
- 'export interface OhMyPosh'
114
- );
115
- }
116
-
117
- // Write the generated TypeScript types to the output file
118
- await fs.writeFile(OUTPUT_FILE, typesContent, { flag: 'w' });
119
- console.log(chalk.green(`TypeScript types generated successfully at ${OUTPUT_FILE}`));
120
-
121
- } catch (error) {
122
- // Dynamically import chalk again to ensure it's available in the catch block
123
- const { default: chalk } = await import('chalk');
124
- console.error(chalk.red('Error generating TypeScript types:'), error);
125
- process.exit(1);
126
- }
127
- }
128
-
129
-
130
- // This function is not currently used. But it can be used to generate types from JSON input.
131
- // We'll keep it here for reference / future use.
132
- async function quicktypeJSON(
133
- targetLanguage: string,
134
- typeName: string,
135
- jsonString: string
136
- ) {
137
- const jsonInput = jsonInputForTargetLanguage(targetLanguage);
138
-
139
- // We could add multiple samples for the same desired
140
- // type, or many sources for other types. Here we're
141
- // just making one type from one piece of sample JSON.
142
- await jsonInput.addSource({
143
- name: typeName,
144
- samples: [jsonString]
145
- });
146
-
147
- const inputData = new InputData();
148
- inputData.addInput(jsonInput);
149
-
150
- return await quicktype({
151
- inputData,
152
- lang: targetLanguage
153
- });
154
- }
155
-
156
- async function quicktypeJSONSchema(
157
- targetLanguage: string,
158
- typeName: string,
159
- jsonSchemaString: string,
160
- options: any = {}
161
- ) {
162
- const schemaInput = new JSONSchemaInput(new FetchingJSONSchemaStore());
163
-
164
- // We could add multiple schemas for multiple types,
165
- // but here we're just making one type from JSON schema.
166
- await schemaInput.addSource({ name: typeName, schema: jsonSchemaString });
167
-
168
- const inputData = new InputData();
169
- inputData.addInput(schemaInput);
170
-
171
- // Pass all options to quicktype along with the required inputData and lang
172
- return await quicktype({
173
- ...options,
174
- inputData,
175
- lang: targetLanguage
176
- });
177
- }
178
-
179
- main().catch(async error => {
180
- // Dynamically import chalk in catch block
181
- const { default: chalk } = await import('chalk');
182
- console.error(chalk.red('Unhandled error:'), error);
183
- process.exit(1);
184
- });
1
+ import fs from 'fs/promises';
2
+ import path from 'path';
3
+ import axios from 'axios';
4
+ import {
5
+ quicktype,
6
+ InputData,
7
+ jsonInputForTargetLanguage,
8
+ JSONSchemaInput,
9
+ FetchingJSONSchemaStore
10
+ } from "quicktype-core";
11
+
12
+ const SCHEMA_URL = 'https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/refs/heads/main/themes/schema.json';
13
+ const OUTPUT_DIR = path.join(process.cwd(), 'src', 'types');
14
+ const OUTPUT_FILE = path.join(OUTPUT_DIR, 'omp.ts');
15
+ const SCHEMA_CACHE_FILE = path.join(process.cwd(), 'cache', 'schema.json');
16
+
17
+ async function main() {
18
+ // Dynamically import chalk (ESM module)
19
+ const { default: chalk } = await import('chalk');
20
+
21
+ console.log(chalk.blue('Starting TypeScript type generation...'));
22
+
23
+ try {
24
+ // Ensure output directories exist
25
+ await fs.mkdir(path.join(process.cwd(), 'cache'), { recursive: true });
26
+ await fs.mkdir(OUTPUT_DIR, { recursive: true });
27
+
28
+ // Fetch the latest schema from the Oh My Posh repository
29
+ console.log(chalk.yellow('Fetching schema from Oh My Posh repository...'));
30
+ const response = await axios.get(SCHEMA_URL);
31
+ const schema = response.data;
32
+
33
+ // This is a dumb workaround but maybe someday I can get the schema file to not have this title...
34
+ // Remove the title property from definitions.segment to avoid dumb naming
35
+ //TODO: Create an issue on the Oh My Posh repository to remove the title property from the segment definition in the schema file... or fix it myself with a PR
36
+ // PR pending: [fix: update segment title in schema.json #6244](https://github.com/JanDeDobbeleer/oh-my-posh/pull/6244)
37
+ if (schema.definitions && schema.definitions.segment) {
38
+ delete schema.definitions.segment.title;
39
+ }
40
+
41
+ // Cache the schema for future comparisons
42
+ await fs.writeFile(SCHEMA_CACHE_FILE, JSON.stringify(schema, null, 2), { flag: 'w' });
43
+
44
+
45
+ console.log(chalk.green('Schema fetched and cached successfully.'));
46
+
47
+ // Generate TypeScript types using quicktype with the provided options
48
+ console.log(chalk.yellow('Generating TypeScript types...'));
49
+
50
+
51
+
52
+ // Set up quicktype options based on the available options in quicktype-core
53
+ const quicktypeOptions = {
54
+ // if debug add debugPrintGraph: true
55
+ // This will print the type graph to the console at every processing step
56
+ debugPrintGraph: process.features.debug ? true : false,
57
+
58
+ // Check Provenance:
59
+ // Check that we're propagating all type attributes (unless we actually can't)
60
+ //! Experimenting with this
61
+ checkProvenance: true,
62
+
63
+ inferMaps: true,
64
+ inferEnums: true,
65
+ inferDateTimes: true,
66
+ inferIntegerStrings: true,
67
+ inferBooleanStrings: true,
68
+ inferUUIDStrings: true, // Renamed from inferUuids to match quicktype's API
69
+ combineClasses: true,
70
+ allPropertiesOptional: false,
71
+ alphabetizeProperties: true, // Sort properties for better readability
72
+ rendererOptions: {
73
+ "explicit-unions": true,
74
+ "runtime-typecheck": true,
75
+ "acronym-style": "original",
76
+ "converters": "top-level",
77
+ "raw-type": "json",
78
+ "prefer-unions": true,
79
+ "prefer-types": true
80
+ },
81
+ // Additional options from Options interface
82
+ fixedTopLevels: true,
83
+ leadingComments: undefined,
84
+ density: "normal" as const, // Available options: 'normal', 'dense'
85
+ useStructuralFeatures: true
86
+ };
87
+
88
+ // Generate the types using quicktype API
89
+ const { lines } = await quicktypeJSONSchema("typescript", "OhMyPosh", JSON.stringify(schema), quicktypeOptions);
90
+
91
+ // Add a header to the generated file
92
+ const header = `/**
93
+ * Oh My Posh TypeScript definitions
94
+ *
95
+ * Generated from schema: ${SCHEMA_URL}
96
+ * Generated on: ${new Date().toISOString()}
97
+ *
98
+ * @see https://ohmyposh.dev/docs/
99
+ */
100
+
101
+ /* eslint-disable */
102
+ /* tslint:disable */
103
+
104
+ `;
105
+
106
+ let typesContent = header + lines.join('\n');
107
+
108
+ // Ensure the OhMyPosh type is properly exported
109
+ // This adds an export statement if one isn't already present
110
+ if (!typesContent.includes('export interface OhMyPosh')) {
111
+ typesContent = typesContent.replace(
112
+ 'interface OhMyPosh',
113
+ 'export interface OhMyPosh'
114
+ );
115
+ }
116
+
117
+ // Write the generated TypeScript types to the output file
118
+ await fs.writeFile(OUTPUT_FILE, typesContent, { flag: 'w' });
119
+ console.log(chalk.green(`TypeScript types generated successfully at ${OUTPUT_FILE}`));
120
+
121
+ } catch (error) {
122
+ // Dynamically import chalk again to ensure it's available in the catch block
123
+ const { default: chalk } = await import('chalk');
124
+ console.error(chalk.red('Error generating TypeScript types:'), error);
125
+ process.exit(1);
126
+ }
127
+ }
128
+
129
+
130
+ // This function is not currently used. But it can be used to generate types from JSON input.
131
+ // We'll keep it here for reference / future use.
132
+ async function quicktypeJSON(
133
+ targetLanguage: string,
134
+ typeName: string,
135
+ jsonString: string
136
+ ) {
137
+ const jsonInput = jsonInputForTargetLanguage(targetLanguage);
138
+
139
+ // We could add multiple samples for the same desired
140
+ // type, or many sources for other types. Here we're
141
+ // just making one type from one piece of sample JSON.
142
+ await jsonInput.addSource({
143
+ name: typeName,
144
+ samples: [jsonString]
145
+ });
146
+
147
+ const inputData = new InputData();
148
+ inputData.addInput(jsonInput);
149
+
150
+ return await quicktype({
151
+ inputData,
152
+ lang: targetLanguage
153
+ });
154
+ }
155
+
156
+ async function quicktypeJSONSchema(
157
+ targetLanguage: string,
158
+ typeName: string,
159
+ jsonSchemaString: string,
160
+ options: any = {}
161
+ ) {
162
+ const schemaInput = new JSONSchemaInput(new FetchingJSONSchemaStore());
163
+
164
+ // We could add multiple schemas for multiple types,
165
+ // but here we're just making one type from JSON schema.
166
+ await schemaInput.addSource({ name: typeName, schema: jsonSchemaString });
167
+
168
+ const inputData = new InputData();
169
+ inputData.addInput(schemaInput);
170
+
171
+ // Pass all options to quicktype along with the required inputData and lang
172
+ return await quicktype({
173
+ ...options,
174
+ inputData,
175
+ lang: targetLanguage
176
+ });
177
+ }
178
+
179
+ main().catch(async error => {
180
+ // Dynamically import chalk in catch block
181
+ const { default: chalk } = await import('chalk');
182
+ console.error(chalk.red('Unhandled error:'), error);
183
+ process.exit(1);
184
+ });