@ts-for-gir/cli 3.0.0-beta.2 → 3.0.0-beta.4

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.
Files changed (39) hide show
  1. package/lib/commands/generate.d.ts +2 -0
  2. package/lib/commands/generate.js +1 -0
  3. package/lib/commands/generate.js.map +1 -1
  4. package/lib/config.d.ts +5 -5
  5. package/lib/config.js +58 -14
  6. package/lib/config.js.map +1 -1
  7. package/lib/conflict-resolver.d.ts +2 -1
  8. package/lib/conflict-resolver.js +4 -2
  9. package/lib/conflict-resolver.js.map +1 -1
  10. package/lib/gir-module.js +1 -1
  11. package/lib/gir-module.js.map +1 -1
  12. package/lib/injection/classes/gjs/GLib-2.0.js +16 -0
  13. package/lib/injection/classes/gjs/GLib-2.0.js.map +1 -1
  14. package/lib/logger.js +8 -4
  15. package/lib/logger.js.map +1 -1
  16. package/lib/module-loader.d.ts +1 -1
  17. package/lib/module-loader.js +1 -1
  18. package/lib/module-loader.js.map +1 -1
  19. package/lib/type-definition-generator.js +59 -0
  20. package/lib/type-definition-generator.js.map +1 -1
  21. package/lib/types/generate-config.d.ts +2 -0
  22. package/lib/types/user-config.d.ts +2 -0
  23. package/lib/utils.d.ts +8 -0
  24. package/lib/utils.js +35 -1
  25. package/lib/utils.js.map +1 -1
  26. package/package.json +13 -13
  27. package/src/commands/generate.ts +1 -0
  28. package/src/config.ts +68 -14
  29. package/src/conflict-resolver.ts +2 -2
  30. package/src/gir-module.ts +1 -1
  31. package/src/injection/classes/gjs/GLib-2.0.ts +16 -0
  32. package/src/logger.ts +4 -4
  33. package/src/module-loader.ts +9 -15
  34. package/src/type-definition-generator.ts +71 -0
  35. package/src/types/generate-config.ts +2 -0
  36. package/src/types/user-config.ts +2 -0
  37. package/src/utils.ts +39 -1
  38. package/templates/Gjs/Gjs.d.ts +6 -6
  39. package/templates/Gjs/index.d.ts +308 -1
@@ -2,7 +2,7 @@
2
2
  * The ModuleLoader is used for reading gir modules from the file system and to solve conflicts (e.g. Gtk-3.0 and Gtk-4.0 would be a conflict)
3
3
  */
4
4
 
5
- import inquirer from 'inquirer'
5
+ import inquirer, { ListQuestion, Answers } from 'inquirer'
6
6
  import glob from 'tiny-glob'
7
7
  import Path from 'path'
8
8
  import fs from 'fs'
@@ -63,7 +63,7 @@ export class ModuleLoader {
63
63
  /**
64
64
  * Sorts out the module the user has not selected via cli prompt
65
65
  * @param girModulesGrouped
66
- * @param keepFullName Users selected module packageName
66
+ * @param selected Users selected module packageName
67
67
  */
68
68
  private sortVersionsByAnswer(
69
69
  girModulesGrouped: GirModulesGrouped,
@@ -99,11 +99,8 @@ export class ModuleLoader {
99
99
  }
100
100
  }
101
101
 
102
- private generateContinueQuestion(
103
- message = `do you want to continue?`,
104
- choices = ['Yes', 'Go back'],
105
- ): inquirer.ListQuestion {
106
- const question: inquirer.ListQuestion = {
102
+ private generateContinueQuestion(message = `do you want to continue?`, choices = ['Yes', 'Go back']): ListQuestion {
103
+ const question: ListQuestion = {
107
104
  name: 'continue',
108
105
  message,
109
106
  type: 'list',
@@ -115,8 +112,8 @@ export class ModuleLoader {
115
112
  private generateIgnoreDepsQuestion(
116
113
  message = `Do you want to ignore them too?`,
117
114
  choices = ['Yes', 'No', 'Go back'],
118
- ): inquirer.ListQuestion {
119
- const question: inquirer.ListQuestion = {
115
+ ): ListQuestion {
116
+ const question: ListQuestion = {
120
117
  name: 'continue',
121
118
  message,
122
119
  type: 'list',
@@ -128,7 +125,7 @@ export class ModuleLoader {
128
125
  private async askIgnoreDepsPrompt(
129
126
  deps: GirModuleResolvedBy[] | Set<GirModuleResolvedBy>,
130
127
  ): Promise<'Yes' | 'No' | 'Go back'> {
131
- let question: inquirer.ListQuestion<inquirer.Answers> | null = null
128
+ let question: ListQuestion<Answers> | null = null
132
129
  const size = (deps as GirModuleResolvedBy[]).length || (deps as Set<GirModuleResolvedBy>).size || 0
133
130
  if (size > 0) {
134
131
  this.log.log(bold('\nThe following modules have the ignored modules as dependencies:'))
@@ -152,13 +149,10 @@ export class ModuleLoader {
152
149
  * @param girModuleGrouped
153
150
  * @param message
154
151
  */
155
- private generateModuleVersionQuestion(
156
- girModuleGrouped: GirModulesGrouped,
157
- message?: string,
158
- ): inquirer.ListQuestion {
152
+ private generateModuleVersionQuestion(girModuleGrouped: GirModulesGrouped, message?: string): ListQuestion {
159
153
  message = message || `Multiple versions of '${girModuleGrouped.namespace}' found, which one do you want to use?`
160
154
  const choices = ['All', ...girModuleGrouped.modules.map((module) => module.packageName)]
161
- const question: inquirer.ListQuestion = {
155
+ const question: ListQuestion = {
162
156
  name: girModuleGrouped.namespace,
163
157
  message,
164
158
  type: 'list',
@@ -1280,6 +1280,77 @@ export default class TypeDefinitionGenerator implements Generator {
1280
1280
  for (const girAlias of girModule.ns.alias)
1281
1281
  if (girModule.packageName !== 'GObject-2.0' || girAlias.$.name !== 'Type')
1282
1282
  out.push(...this.generateAlias(girAlias, girModule.namespace, 1))
1283
+
1284
+ if (this.config.environment === 'gjs') {
1285
+ // Properties added to every GIRepositoryNamespace
1286
+ // https://gitlab.gnome.org/GNOME/gjs/-/blob/master/gi/ns.cpp#L186-190
1287
+ out.push(
1288
+ ...this.generateConstant(
1289
+ {
1290
+ doc: {
1291
+ text: 'Name of the imported GIR library',
1292
+ tags: [
1293
+ {
1294
+ text: 'https://gitlab.gnome.org/GNOME/gjs/-/blob/master/gi/ns.cpp#L188',
1295
+ tagName: 'see',
1296
+ paramName: '',
1297
+ },
1298
+ ],
1299
+ },
1300
+ name: '__name__',
1301
+ type: [
1302
+ {
1303
+ type: 'string',
1304
+ optional: false,
1305
+ nullable: false,
1306
+ callbacks: [],
1307
+ generics: [],
1308
+ isArray: false,
1309
+ isFunction: false,
1310
+ isCallback: false,
1311
+ },
1312
+ ],
1313
+ isInjected: false,
1314
+ tsTypeName: 'constant',
1315
+ girTypeName: 'constant',
1316
+ },
1317
+ girModule.namespace,
1318
+ 0,
1319
+ ),
1320
+ ...this.generateConstant(
1321
+ {
1322
+ doc: {
1323
+ text: 'Version of the imported GIR library',
1324
+ tags: [
1325
+ {
1326
+ text: 'https://gitlab.gnome.org/GNOME/gjs/-/blob/master/gi/ns.cpp#L189',
1327
+ tagName: 'see',
1328
+ paramName: '',
1329
+ },
1330
+ ],
1331
+ },
1332
+ name: '__version__',
1333
+ type: [
1334
+ {
1335
+ type: 'string',
1336
+ optional: false,
1337
+ nullable: false,
1338
+ callbacks: [],
1339
+ generics: [],
1340
+ isArray: false,
1341
+ isFunction: false,
1342
+ isCallback: false,
1343
+ },
1344
+ ],
1345
+ isInjected: false,
1346
+ tsTypeName: 'constant',
1347
+ girTypeName: 'constant',
1348
+ },
1349
+ girModule.namespace,
1350
+ 0,
1351
+ ),
1352
+ )
1353
+ }
1283
1354
  }
1284
1355
  // END Namespace
1285
1356
  if (this.config.useNamespace) {
@@ -28,4 +28,6 @@ export interface GenerateConfig {
28
28
  noCheck: boolean
29
29
  /** Fix Inheritance and implementation type conflicts */
30
30
  fixConflicts: boolean
31
+ /** Disables the generation of types that are in conflict with the DOM types */
32
+ noDOMLib: boolean
31
33
  }
@@ -36,4 +36,6 @@ export interface UserConfig {
36
36
  noCheck: boolean
37
37
  /** Fix Inheritance and implementation type conflicts */
38
38
  fixConflicts: boolean
39
+ /** Disables the generation of types that are in conflict with the DOM types */
40
+ noDOMLib: boolean
39
41
  }
package/src/utils.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  import lodash from 'lodash'
3
3
  import Path from 'path'
4
4
  import fs from 'fs'
5
+ import ts from 'typescript'
5
6
  import { fileURLToPath } from 'url'
6
7
  import { Environment, GirInfoAttrs, TsType } from './types/index.js'
7
8
  import { inspect } from 'util'
@@ -27,7 +28,7 @@ export const splitModuleName = (packageName: string): { packageName: string; nam
27
28
  // There are modules that use multiple hyphens like 'GUPnP-DLNA-1.0'
28
29
  const splits = packageName.split('-')
29
30
  const version = splits.splice(-1, 1)[0]
30
- const namespace = splits.join('-')
31
+ const namespace = splits.join('')
31
32
  return {
32
33
  packageName,
33
34
  namespace,
@@ -215,3 +216,40 @@ export const typeIsOptional = (types: TsType[]) => {
215
216
  }
216
217
  return false
217
218
  }
219
+
220
+ function convertTsJsConfigToObject(path: string) {
221
+ try {
222
+ const config: unknown = ts.parseConfigFileTextToJson(
223
+ Path.basename(path),
224
+ fs.readFileSync(path, 'utf8').trim(),
225
+ ).config
226
+ if (typeof config === 'object' && !Array.isArray(config)) return config as Record<PropertyKey, unknown>
227
+ } catch {
228
+ // ignored
229
+ }
230
+ return null
231
+ }
232
+
233
+ /**
234
+ * Given an directory path search for a tsconfig.json or jsconfig.json file in it or any of its parent directories, then read the file and parse it as json.
235
+ * @see {@link https://github.com/microsoft/TypeScript/blob/5f9c9a6ccf61fa131849797248438e292e7b496a/src/harness/compilerImpl.ts#L11-L35}
236
+ * @see {@link https://github.com/microsoft/TypeScript/blob/3fd8a6e44341f14681aa9d303dc380020ccb2147/src/harness/vfsUtil.ts#L286-L316}
237
+ *
238
+ * @param path - The directory path to search for a tsconfig.json or jsconfig.json file
239
+ */
240
+ export function readTsJsConfig(path: string) {
241
+ let config: null | false | Record<PropertyKey, unknown> = null
242
+ let lastPath = ''
243
+ let currentPath = Path.resolve(path)
244
+ while (!config && currentPath !== lastPath) {
245
+ const tsConfigPath = Path.join(currentPath, 'tsconfig.json')
246
+ const jsConfigPath = Path.join(currentPath, 'jsconfig.json')
247
+ config =
248
+ (fs.existsSync(tsConfigPath) && convertTsJsConfigToObject(tsConfigPath)) ||
249
+ (fs.existsSync(jsConfigPath) && convertTsJsConfigToObject(jsConfigPath))
250
+ lastPath = currentPath
251
+ currentPath = Path.dirname(currentPath)
252
+ }
253
+
254
+ return config === false ? null : config
255
+ }
@@ -120,12 +120,12 @@ export namespace Format {
120
120
 
121
121
  export namespace Mainloop {
122
122
  export function quit(name: string): void
123
- export function idle_source(handler: any, priority: number): any
124
- export function idle_add(handler: any, priority: number): any
125
- export function timeout_source(timeout: any, handler: any, priority: number): any
126
- export function timeout_seconds_source(timeout: any, handler: any, priority: number): any
127
- export function timeout_add(timeout: any, handler: any, priority: number): any
128
- export function timeout_add_seconds(timeout: any, handler: any, priority: number): any
123
+ export function idle_source(handler: any, priority?: number): any
124
+ export function idle_add(handler: any, priority?: number): any
125
+ export function timeout_source(timeout: any, handler: any, priority?: number): any
126
+ export function timeout_seconds_source(timeout: any, handler: any, priority?: number): any
127
+ export function timeout_add(timeout: any, handler: any, priority?: number): any
128
+ export function timeout_add_seconds(timeout: any, handler: any, priority?: number): any
129
129
  export function source_remove(id: any): any
130
130
  export function run(name: string): void
131
131
  }
@@ -199,7 +199,314 @@ declare global {
199
199
  logDomain: string
200
200
  }
201
201
 
202
- const console: Console;
202
+ <% if(noDOMLib){ %>
203
+ const console: Console
204
+ <% } %>
205
+
206
+ // https://gitlab.gnome.org/GNOME/gjs/-/blob/1.73.2/modules/esm/_encoding/encodingMap.js#L7-232
207
+ type TextDecoderEncoding =
208
+ | 'unicode-1-1-utf-8'
209
+ | 'unicode11utf8'
210
+ | 'unicode20utf8'
211
+ | 'utf-8'
212
+ | 'utf8'
213
+ | 'x-unicode20utf8'
214
+ | '866'
215
+ | 'cp866'
216
+ | 'csibm866'
217
+ | 'ibm866'
218
+ | 'csisolatin2'
219
+ | 'iso-8859-2'
220
+ | 'iso-ir-101'
221
+ | 'iso8859-2'
222
+ | 'iso88592'
223
+ | 'iso_8859-2'
224
+ | 'iso_8859-2:1987'
225
+ | 'l2'
226
+ | 'latin2'
227
+ | 'csisolatin3'
228
+ | 'iso-8859-3'
229
+ | 'iso-ir-109'
230
+ | 'iso8859-3'
231
+ | 'iso88593'
232
+ | 'iso_8859-3'
233
+ | 'iso_8859-3:1988'
234
+ | 'l3'
235
+ | 'latin3'
236
+ | 'csisolatin4'
237
+ | 'iso-8859-4'
238
+ | 'iso-ir-110'
239
+ | 'iso8859-4'
240
+ | 'iso88594'
241
+ | 'iso_8859-4'
242
+ | 'iso_8859-4:1988'
243
+ | 'l4'
244
+ | 'latin4'
245
+ | 'csisolatincyrillic'
246
+ | 'cyrillic'
247
+ | 'iso-8859-5'
248
+ | 'iso-ir-144'
249
+ | 'iso8859-5'
250
+ | 'iso88595'
251
+ | 'iso_8859-5'
252
+ | 'iso_8859-5:1988'
253
+ | 'arabic'
254
+ | 'asmo-708'
255
+ | 'csiso88596e'
256
+ | 'csiso88596i'
257
+ | 'csisolatinarabic'
258
+ | 'ecma-114'
259
+ | 'iso-8859-6'
260
+ | 'iso-8859-6-e'
261
+ | 'iso-8859-6-i'
262
+ | 'iso-ir-127'
263
+ | 'iso8859-6'
264
+ | 'iso88596'
265
+ | 'iso_8859-6'
266
+ | 'iso_8859-6:1987'
267
+ | 'csisolatingreek'
268
+ | 'ecma-118'
269
+ | 'elot_928'
270
+ | 'greek'
271
+ | 'greek8'
272
+ | 'iso-8859-7'
273
+ | 'iso-ir-126'
274
+ | 'iso8859-7'
275
+ | 'iso88597'
276
+ | 'iso_8859-7'
277
+ | 'iso_8859-7:1987'
278
+ | 'sun_eu_greek'
279
+ | 'csiso88598e'
280
+ | 'csisolatinhebrew'
281
+ | 'hebrew'
282
+ | 'iso-8859-8'
283
+ | 'iso-8859-8-e'
284
+ | 'iso-ir-138'
285
+ | 'iso8859-8'
286
+ | 'iso88598'
287
+ | 'iso_8859-8'
288
+ | 'iso_8859-8:1988'
289
+ | 'visual'
290
+ | 'csiso88598i'
291
+ | 'iso-8859-8-i'
292
+ | 'logical'
293
+ | 'csisolatin6'
294
+ | 'iso-8859-10'
295
+ | 'iso-ir-157'
296
+ | 'iso8859-10'
297
+ | 'iso885910'
298
+ | 'l6'
299
+ | 'latin6'
300
+ | 'iso-8859-13'
301
+ | 'iso8859-13'
302
+ | 'iso885913'
303
+ | 'iso-8859-14'
304
+ | 'iso8859-14'
305
+ | 'iso885914'
306
+ | 'csisolatin9'
307
+ | 'iso-8859-15'
308
+ | 'iso8859-15'
309
+ | 'iso885915'
310
+ | 'iso_8859-15'
311
+ | 'l9'
312
+ | 'iso-8859-16'
313
+ | 'cskoi8r'
314
+ | 'koi'
315
+ | 'koi8'
316
+ | 'koi8-r'
317
+ | 'koi8_r'
318
+ | 'koi8-ru'
319
+ | 'koi8-u'
320
+ | 'csmacintosh'
321
+ | 'mac'
322
+ | 'macintosh'
323
+ | 'x-mac-roman'
324
+ | 'dos-874'
325
+ | 'iso-8859-11'
326
+ | 'iso8859-11'
327
+ | 'iso885911'
328
+ | 'tis-620'
329
+ | 'windows-874'
330
+ | 'cp1250'
331
+ | 'windows-1250'
332
+ | 'x-cp1250'
333
+ | 'cp1251'
334
+ | 'windows-1251'
335
+ | 'x-cp1251'
336
+ | 'ansi_x3.4-1968'
337
+ | 'ascii'
338
+ | 'cp1252'
339
+ | 'cp819'
340
+ | 'csisolatin1'
341
+ | 'ibm819'
342
+ | 'iso-8859-1'
343
+ | 'iso-ir-100'
344
+ | 'iso8859-1'
345
+ | 'iso88591'
346
+ | 'iso_8859-1'
347
+ | 'iso_8859-1:1987'
348
+ | 'l1'
349
+ | 'latin1'
350
+ | 'us-ascii'
351
+ | 'windows-1252'
352
+ | 'x-cp1252'
353
+ | 'cp1253'
354
+ | 'windows-1253'
355
+ | 'x-cp1253'
356
+ | 'cp1254'
357
+ | 'csisolatin5'
358
+ | 'iso-8859-9'
359
+ | 'iso-ir-148'
360
+ | 'iso8859-9'
361
+ | 'iso88599'
362
+ | 'iso_8859-9'
363
+ | 'iso_8859-9:1989'
364
+ | 'l5'
365
+ | 'latin5'
366
+ | 'windows-1254'
367
+ | 'x-cp1254'
368
+ | 'cp1255'
369
+ | 'windows-1255'
370
+ | 'x-cp1255'
371
+ | 'cp1256'
372
+ | 'windows-1256'
373
+ | 'x-cp1256'
374
+ | 'cp1257'
375
+ | 'windows-1257'
376
+ | 'x-cp1257'
377
+ | 'cp1258'
378
+ | 'windows-1258'
379
+ | 'x-cp1258'
380
+ | 'x-mac-cyrillic'
381
+ | 'x-mac-ukrainian'
382
+ | 'chinese'
383
+ | 'csgb2312'
384
+ | 'csiso58gb231280'
385
+ | 'gb2312'
386
+ | 'gb_2312'
387
+ | 'gb_2312-80'
388
+ | 'gbk'
389
+ | 'iso-ir-58'
390
+ | 'x-gbk'
391
+ | 'gb18030'
392
+ | 'big5'
393
+ | 'cn-big5'
394
+ | 'csbig5'
395
+ | 'x-x-big5'
396
+ | 'cseucpkdfmtjapanese'
397
+ | 'euc-jp'
398
+ | 'x-euc-jp'
399
+ | 'csiso2022jp'
400
+ | 'iso-2022-jp'
401
+ | 'csshiftjis'
402
+ | 'ms932'
403
+ | 'ms_kanji'
404
+ | 'shift-jis'
405
+ | 'shift_jis'
406
+ | 'sjis'
407
+ | 'windows-31j'
408
+ | 'x-sjis'
409
+ | 'cseuckr'
410
+ | 'csksc56011987'
411
+ | 'euc-kr'
412
+ | 'iso-ir-149'
413
+ | 'korean'
414
+ | 'ks_c_5601-1987'
415
+ | 'ks_c_5601-1989'
416
+ | 'ksc5601'
417
+ | 'ksc_5601'
418
+ | 'windows-949'
419
+ | 'unicodefffe'
420
+ | 'utf-16be'
421
+ | 'csunicode'
422
+ | 'iso-10646-ucs-2'
423
+ | 'ucs-2'
424
+ | 'unicode'
425
+ | 'unicodefeff'
426
+ | 'utf-16'
427
+ | 'utf-16le'
428
+
429
+ interface TextDecodeOptions {
430
+ // As of Gjs 1.73.2 stream mode is not supported yet.
431
+ // stream?: boolean
432
+ }
433
+
434
+ interface TextDecoderOptions {
435
+ /** Indicates whether the error mode is fatal. */
436
+ fatal?: boolean
437
+ /** Indicates whether whether the byte order mark is ignored. */
438
+ ignoreBOM?: boolean
439
+ }
440
+
441
+ /**
442
+ * The TextDecoder interface represents a decoder for a specific text encoding.
443
+ * It takes a stream of bytes as input and emits a stream of code points.
444
+ *
445
+ * @version Gjs 1.69.2
446
+ */
447
+ interface TextDecoder {
448
+ /** A string containing the name of the decoder, that is a string describing the method the TextDecoder will use. */
449
+ readonly encoding: TextDecoderEncoding
450
+ /** A Boolean indicating whether the error mode is fatal. */
451
+ readonly fatal: boolean
452
+ /** A Boolean indicating whether the byte order mark is ignored. */
453
+ readonly ignoreBOM: boolean
454
+
455
+ /**
456
+ * Returns a string containing the text decoded with the method of the specific TextDecoder object.
457
+ *
458
+ * If the error mode is "fatal" and the encoder method encounter an error it WILL THROW a TypeError.
459
+ *
460
+ * @param input Buffer containing the text to decode
461
+ * @param options Object defining the decode options
462
+ */
463
+ decode(input?: ArrayBufferView | ArrayBuffer, options?: TextDecodeOptions): string
464
+ }
465
+
466
+ <% if(noDOMLib){ %>
467
+ const TextDecoder: {
468
+ prototype: TextDecoder
469
+ new (label?: TextDecoderEncoding, options?: TextDecoderOptions): TextDecoder
470
+ }
471
+ <% } %>
472
+
473
+ interface TextEncoderEncodeIntoResult {
474
+ read?: number
475
+ written?: number
476
+ }
477
+
478
+ /**
479
+ * TextEncoder takes a stream of code points as input and emits a stream of bytes.
480
+ *
481
+ * @version Gjs 1.69.2
482
+ */
483
+ interface TextEncoder {
484
+ readonly encoding: 'utf-8'
485
+
486
+ /**
487
+ * Takes a string as input, and returns a buffer containing the text given in parameters encoded with the UTF-8 method.
488
+ *
489
+ * @param input Text to encode.
490
+ */
491
+ encode(input?: string): Uint8Array
492
+ /**
493
+ * Takes a string to encode and a destination Uint8Array to put resulting UTF-8 encoded text into,
494
+ * and returns a dictionary object indicating the progress of the encoding.
495
+ *
496
+ * This is potentially more performant than the older encode() method.
497
+ *
498
+ * @param source Text to enconde.
499
+ * @param destination Buffer where to place the resulting UTF-8 encoded text into.
500
+ */
501
+ encodeInto(source: string, destination: Uint8Array): TextEncoderEncodeIntoResult
502
+ }
503
+
504
+ <% if(noDOMLib){ %>
505
+ const TextEncoder: {
506
+ prototype: TextEncoder
507
+ new (): TextEncoder
508
+ }
509
+ <% } %>
203
510
 
204
511
  interface BooleanConstructor {
205
512
  $gtype: GObject20.GType<boolean>