diff --git a/packages/generator-typescript/src/module-generator.ts b/packages/generator-typescript/src/module-generator.ts index 0ce98b26..64c7bd3b 100644 --- a/packages/generator-typescript/src/module-generator.ts +++ b/packages/generator-typescript/src/module-generator.ts @@ -60,7 +60,6 @@ import { import { writeFile, mkdir } from 'fs/promises' import { dirname } from 'path' -import { wrapIntoAmbientModule } from './utils.js' import { TemplateProcessor } from './template-processor.js' import { PackageDataParser } from './package-data-parser.js' import { NpmPackage } from './npm-package.js' @@ -1604,71 +1603,16 @@ export class ModuleGenerator extends FormatGenerator { } } - /** - * Used to only generate ambient types for a module. - * Used if package.json support is disabled - */ - protected async exportModuleAmbientOnly(): Promise { - const { namespace: girModule } = this - const namespaceContent = await this.generateNamespace(girModule) - - if (!namespaceContent) { - this.log.error('Failed to generate namespace') - return - } - - // Ambient module with version - const output = wrapIntoAmbientModule(girModule.namespace, girModule.version, namespaceContent, undefined, { - protocol: 'gi', - }) - - // Ambient module without version - if (!this.config.onlyVersionPrefix) { - const reexport = [ - `import ${girModule.importNamespace} from 'gi://${girModule.namespace}?version=${girModule.version}';`, - `export default ${girModule.importNamespace};`, - ] - wrapIntoAmbientModule(girModule.namespace, null, reexport, output, { protocol: 'gi' }) - } - - const target = `${girModule.importName}.d.ts` - - const formatter = this.dependencyManager.getFormatter('dts') - - let contents!: string - try { - contents = this.config.noPrettyPrint ? output.join('\n') : await formatter.format(output.join('\n')) - } catch (error) { - this.log.error('Failed to format output...', error) - contents = output.join('\n') - } - - if (this.config.outdir) { - const outputPath = this.moduleTemplateProcessor.getOutputPath(this.config.outdir, target) - - if (this.config.verbose) { - this.log.debug(`Outputting ${target} to ${outputPath}`) - } - - // write template result file - await mkdir(dirname(outputPath), { recursive: true }) - await writeFile(outputPath, contents, { encoding: 'utf8', flag: 'w' }) - } else { - this.log.log(contents) - } - } - async exportModuleTS(): Promise { const { namespace: girModule } = this const output = await this.generateNamespace(girModule) + const target = `${girModule.importName}.d.ts` if (!output) { this.log.error('Failed to generate namespace') return } - const target = `${girModule.importName}.d.ts` - const formatter = this.dependencyManager.getFormatter('dts') let contents!: string @@ -1843,7 +1787,6 @@ export class ModuleGenerator extends FormatGenerator { await this.exportModuleIndexTS() await this.exportModuleIndexJS() - await this.exportModuleTS() await this.exportModuleJS(girModule) await this.exportModuleAmbientTS(girModule) @@ -1855,7 +1798,7 @@ export class ModuleGenerator extends FormatGenerator { const pkg = new NpmPackage(this.config, this.dependencyManager, girModule, girModule.transitiveDependencies) await pkg.exportNPMPackage() } else { - await this.exportModuleAmbientOnly() + await this.exportModuleTS() } } } diff --git a/packages/generator-typescript/src/type-definition-generator.ts b/packages/generator-typescript/src/type-definition-generator.ts index 0eda8259..d7242ca5 100644 --- a/packages/generator-typescript/src/type-definition-generator.ts +++ b/packages/generator-typescript/src/type-definition-generator.ts @@ -1,7 +1,6 @@ import { Generator } from '@ts-for-gir/generator-base' import { Logger, DependencyManager, NSRegistry, OptionsGeneration, GirModule } from '@ts-for-gir/lib' -import { wrapIntoAmbientModule } from './utils.js' import { TemplateProcessor } from './template-processor.js' import { PackageDataParser } from './package-data-parser.js' import { NpmPackage } from './npm-package.js' @@ -76,25 +75,25 @@ export class TypeDefinitionGenerator implements Generator { await templateProcessor.write(gjsContent.prepend + '\n' + gjsContent.append, config.outdir, 'gjs.d.ts') const gettextContent = await templateProcessor.load('gjs/gettext.d.ts') - const gettextContentAmbient = wrapIntoAmbientModule('gettext', null, [ - gettextContent.prepend, - gettextContent.append, - ]) - await templateProcessor.write(gettextContentAmbient.join('\n'), config.outdir, 'gettext.d.ts') + await templateProcessor.write( + gettextContent.prepend + '\n' + gettextContent.append, + config.outdir, + 'gettext.d.ts', + ) const systemContent = await templateProcessor.load('gjs/system.d.ts') - const systemContentAmbient = wrapIntoAmbientModule('system', null, [ - systemContent.prepend, - systemContent.append, - ]) - await templateProcessor.write(systemContentAmbient.join('\n'), config.outdir, 'system.d.ts') + await templateProcessor.write( + systemContent.prepend + '\n' + systemContent.append, + config.outdir, + 'system.d.ts', + ) const cairoContent = await templateProcessor.load('gjs/cairo.d.ts') - const cairoContentAmbient = wrapIntoAmbientModule('cairo', null, [ - cairoContent.prepend, - cairoContent.append, - ]) - await templateProcessor.write(cairoContentAmbient.join('\n'), config.outdir, 'cairo.d.ts') + await templateProcessor.write( + cairoContent.prepend + '\n' + cairoContent.append, + config.outdir, + 'cairo.d.ts', + ) // Additional DOM types supported by GJS const domContent = await templateProcessor.load('gjs/dom.d.ts') diff --git a/packages/generator-typescript/src/utils.ts b/packages/generator-typescript/src/utils.ts index 399867b4..e92b1d26 100644 --- a/packages/generator-typescript/src/utils.ts +++ b/packages/generator-typescript/src/utils.ts @@ -5,25 +5,3 @@ import { dirname, resolve } from 'path' const __filename = fileURLToPath(import.meta.url) // Get __dirname on ESM, resolve to the root directory of this package export const __dirname = resolve(dirname(__filename), '..') - -export const mergeLargeStringArrays = (target: string[], source: string[], chunkSize: number = 1000): string[] => { - for (let i = 0; i < source.length; i += chunkSize) { - target.push(...source.slice(i, i + chunkSize)) - } - return target -} - -export const wrapIntoAmbientModule = ( - namespace: string, - version: string | null, - content: string[], - target: string[] = [], - options: { protocol?: string } = {}, -): string[] => { - target.push( - `declare module '${options.protocol ? `${options.protocol}://` : ''}${namespace}${version ? `?version=${version}` : ''}' {`, - ) - mergeLargeStringArrays(target, content) - target.push('}') - return target -} diff --git a/packages/generator-typescript/templates/gjs/cairo.d.ts b/packages/generator-typescript/templates/gjs/cairo.d.ts index 073bf609..d2f3cfc6 100644 --- a/packages/generator-typescript/templates/gjs/cairo.d.ts +++ b/packages/generator-typescript/templates/gjs/cairo.d.ts @@ -1,4 +1,12 @@ +<%_ if(!package){ -%> +declare module 'cairo' { +<% } -%> + // TODO: See ./cairo-1.0.d.ts <%- package ? 'declare' : '' %> const Cairo: any; export default Cairo; + +<%_ if(!package){ -%> +} +<% } -%> \ No newline at end of file diff --git a/packages/generator-typescript/templates/gjs/gettext.d.ts b/packages/generator-typescript/templates/gjs/gettext.d.ts index f34878b1..8ffd4fcc 100644 --- a/packages/generator-typescript/templates/gjs/gettext.d.ts +++ b/packages/generator-typescript/templates/gjs/gettext.d.ts @@ -1,3 +1,6 @@ +<%_ if(!package){ -%> +declare module 'gettext' { +<% } -%> export enum LocaleCategory { ALL, COLLATE, @@ -38,4 +41,8 @@ export function domain(domainName: string): { domain: typeof domain, } -export default Gettext \ No newline at end of file +export default Gettext + +<%_ if(!package){ -%> +} +<% } -%> \ No newline at end of file diff --git a/packages/generator-typescript/templates/gjs/gjs.d.ts b/packages/generator-typescript/templates/gjs/gjs.d.ts index 0565b7cc..ff82b36f 100644 --- a/packages/generator-typescript/templates/gjs/gjs.d.ts +++ b/packages/generator-typescript/templates/gjs/gjs.d.ts @@ -1,12 +1,12 @@ <%_ const GObject = await dep.get('GObject', '2.0') _%> <%_ const GLib = await dep.get('GLib', '2.0') _%> <%_ if(!package){ -%> - <%_ if(GObject){ -%> +<%_ if(GObject){ -%> /// - <% } -%> - <%_ if(GLib){ -%> +<% } -%> +<%_ if(GLib){ -%> /// - <% } -%> +<% } -%> /// /// /// diff --git a/packages/generator-typescript/templates/gjs/system.d.ts b/packages/generator-typescript/templates/gjs/system.d.ts index ea40f29d..1976be8e 100644 --- a/packages/generator-typescript/templates/gjs/system.d.ts +++ b/packages/generator-typescript/templates/gjs/system.d.ts @@ -3,6 +3,10 @@ /// <%_ } -%> +<%_ if(!package){ -%> +declare module 'system' { +<% } -%> + <%- GObject ? GObject.importDef : '' %> /** @@ -176,4 +180,8 @@ export function exit(code: number): void exit: typeof exit, } -export default System \ No newline at end of file +export default System + +<%_ if(!package){ -%> +} +<% } -%> \ No newline at end of file diff --git a/packages/generator-typescript/templates/module.append.d.ts b/packages/generator-typescript/templates/module.append.d.ts index fb1bc88f..3e2f55c5 100644 --- a/packages/generator-typescript/templates/module.append.d.ts +++ b/packages/generator-typescript/templates/module.append.d.ts @@ -1 +1,11 @@ +<%_ if(!package){ -%> +} + +<%_ if(!onlyVersionPrefix){ -%> +declare module 'gi://<%- girModule.namespace %>' { + import <%- girModule.importNamespace %> from 'gi://<%- girModule.namespace %>?version=<%- girModule.version %>'; + export default <%- girModule.importNamespace %>; +} +<% } -%> +<% } -%> // END \ No newline at end of file diff --git a/packages/generator-typescript/templates/module.d.ts b/packages/generator-typescript/templates/module.d.ts index f241b6ca..3fcdfd41 100644 --- a/packages/generator-typescript/templates/module.d.ts +++ b/packages/generator-typescript/templates/module.d.ts @@ -5,3 +5,7 @@ * These type definitions are automatically generated, do not edit them by hand. * If you found a bug fix it in `<%= APP_NAME %>` or create a bug report on <%= APP_SOURCE %> */ + +<%_ if(!package){ -%> + declare module 'gi://<%- girModule.namespace %>?version=<%- girModule.version %>' { +<% } -%> \ No newline at end of file diff --git a/packages/lib/src/utils/strings.ts b/packages/lib/src/utils/strings.ts index bdb7fd7f..784e1585 100644 --- a/packages/lib/src/utils/strings.ts +++ b/packages/lib/src/utils/strings.ts @@ -118,3 +118,17 @@ export const underscores = (str: string): string => { export const generateIndent = (indents = 1, spaceForIndent = 4): string => { return ' '.repeat(indents * spaceForIndent) } + +/** + * Merge a large array of strings by using a smaller chunk size + * @param target The target array to merge into + * @param source The source array to merge from + * @param chunkSize The size of the chunks to merge + * @returns The merged array + */ +export const mergeLargeStringArray = (target: string[], source: string[], chunkSize: number = 1000): string[] => { + for (let i = 0; i < source.length; i += chunkSize) { + target.push(...source.slice(i, i + chunkSize)) + } + return target +}