Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define package properties as defined as soon as package.init was called #226

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions examples/pkg-tsc/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
// Before initialization - TypeScript knows these are possibly undefined
console.log('Before initialization:');
console.log('name:', imports.package.name);
console.log('version:', imports.package.version);
console.log('datadir:', imports.package.datadir);

// Initialize the package
imports.package.init({
name: "test",
version: "1.0.0",
prefix: "test",
libdir: "lib"
})
console.log("name", imports.package.name, pkg.name);
console.log("version", imports.package.version, pkg.version);
name: 'org.example.App',
version: '1.0.0',
prefix: '/usr/local',
libdir: '/usr/local/lib'
});

// After initialization - TypeScript knows these are definitely defined
console.log('\nAfter initialization:');
console.log('name:', imports.package.name);
console.log('version:', imports.package.version);
console.log('datadir:', imports.package.datadir);

// TypeScript type check demonstration
function requiresInitializedPackage(pkgName: string) {
return pkgName === imports.package.name;
}

const isMatchingPackage = requiresInitializedPackage('org.example.App');
console.log('\nPackage name matches:', isMatchingPackage);
68 changes: 57 additions & 11 deletions packages/generator-typescript/templates/gjs/gjs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,52 @@ import cairo from 'cairo';

// https://gitlab.gnome.org/GNOME/gjs/-/blob/1.72.0/modules/script/package.js
declare namespace package {


// Defines the state before initialization
export interface UninitializedPackage {
/** The base name of the entry point (eg. org.foo.Bar.App) */
name: undefined;
/** The version of the package */
version: undefined;
/** The prefix of the package */
prefix: undefined;
/** The final datadir when installed; usually, these would be prefix + '/share' */
datadir: undefined;
/** The final libdir when installed; usually, these would be prefix + '/lib' (or '/lib64') */
libdir: undefined;
/** The final pkgdatadir when installed; usually, this would be prefix + '/share' */
pkgdatadir: undefined;
/** The final pkglibdir when installed; usually, this would be prefix + '/lib' (or '/lib64') */
pkglibdir: undefined;
/** The final moduledir when installed; usually, this would be prefix + '/lib' (or '/lib64') */
moduledir: undefined;
/** The directory containing gettext translation files; this will be datadir + '/locale' when installed and './po' in the source tree */
localedir: undefined;
}

// Defines the state after initialization
export interface InitializedPackage {
/** The base name of the entry point (eg. org.foo.Bar.App) */
name: string;
/** The version of the package */
version: string;
/** The prefix of the package */
prefix: string;
/** The final datadir when installed; usually, these would be prefix + '/share' */
datadir: string;
/** The final libdir when installed; usually, these would be prefix + '/lib' (or '/lib64') */
libdir: string;
/** The final pkgdatadir when installed; usually, this would be prefix + '/share' */
pkgdatadir: string;
/** The final pkglibdir when installed; usually, this would be prefix + '/lib' (or '/lib64') */
pkglibdir: string;
/** The final moduledir when installed; usually, this would be prefix + '/lib' (or '/lib64') */
moduledir: string;
/** The directory containing gettext translation files; this will be datadir + '/locale' when installed and './po' in the source tree */
localedir: string;
}

/**
* Although there are references in the documentation of more properties that
* this object should accepts, only the following are actually used in the init code,
Expand Down Expand Up @@ -61,23 +107,23 @@ declare namespace package {
}

/** The base name of the entry point (eg. org.foo.Bar.App) */
export const name: string | undefined
export const name: InitializedPackage['name'] | UninitializedPackage['name'];
/** The version of the package */
export const version: string | undefined
export const version: InitializedPackage['version'] | UninitializedPackage['version']
/** The prefix of the package */
export const prefix: string | undefined
export const prefix: InitializedPackage['prefix'] | UninitializedPackage['prefix']
/** The final datadir when installed; usually, these would be prefix + '/share' */
export const datadir: string | undefined
export const datadir: InitializedPackage['datadir'] | UninitializedPackage['datadir']
/** The final libdir when installed; usually, these would be prefix + '/lib' (or '/lib64') */
export const libdir: string | undefined
export const libdir: InitializedPackage['libdir'] | UninitializedPackage['libdir']
/** The final pkgdatadir when installed; usually, this would be prefix + '/share' */
export const pkgdatadir: string | undefined
export const pkgdatadir: InitializedPackage['pkgdatadir'] | UninitializedPackage['pkgdatadir']
/** The final pkglibdir when installed; usually, this would be prefix + '/lib' (or '/lib64') */
export const pkglibdir: string | undefined
export const pkglibdir: InitializedPackage['pkglibdir'] | UninitializedPackage['pkglibdir']
/** The final moduledir when installed; usually, this would be prefix + '/lib' (or '/lib64') */
export const moduledir: string | undefined
export const moduledir: InitializedPackage['moduledir'] | UninitializedPackage['moduledir']
/** The directory containing gettext translation files; this will be datadir + '/locale' when installed and './po' in the source tree */
export const localedir: string | undefined
export const localedir: InitializedPackage['localedir'] | UninitializedPackage['localedir']

/**
* Initialize directories and global variables. Must be called
Expand Down Expand Up @@ -121,7 +167,7 @@ declare namespace package {
*
* @param {object} params package parameters
*/
export function init(params: PackageInitParams): void;
export function init(params: PackageInitParams): asserts this is InitializedPackage;
/**
* This is the function to use if you want to have multiple
* entry points in one package.
Expand Down Expand Up @@ -606,7 +652,7 @@ declare global {
function logError(exception: object, message?: any): void
function logError(message?: any): void

const pkg: typeof package
const pkg: undefined | typeof package & package.InitializedPackage

interface BooleanConstructor {
$gtype: GObject.GType<boolean>
Expand Down
Loading