Skip to content

Commit

Permalink
Provide type declarations for consumers of the package
Browse files Browse the repository at this point in the history
Explicitly export userscript metadata types instead of implicitly having
ambient declarations which are visible only inside the local package.
  • Loading branch information
kellnerd committed Apr 15, 2023
1 parent e7e46cc commit 495a757
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
17 changes: 17 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Keep the following exports in sync with index.js

export * from './src/buildUserscripts.js';

export * from './src/buildBookmarklets.js';

export * from './src/build.js';

export * from './src/extractDocumentation.js';

export * from './src/github.js';

export * from './src/userscriptMetadata.js';

// Additional type exports

export * from './src/types/UserscriptMetadata';
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
export { buildUserscript, buildUserscripts } from './src/buildUserscripts.js';
// Keep the following exports in sync with index.d.ts

export { buildBookmarklet, buildBookmarklets } from './src/buildBookmarklets.js';
export * from './src/buildUserscripts.js';

export { build } from './src/build.js';
export * from './src/buildBookmarklets.js';

export { extractDocumentation } from './src/extractDocumentation.js';
export * from './src/build.js';

export { GITHUB, sourceAndInstallButton } from './src/github.js';
export * from './src/extractDocumentation.js';

export * from './src/github.js';

export * from './src/userscriptMetadata.js';
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
"files": [
"src/",
"utils/",
"index.js",
"index.d.ts",
"bookmarkletify.js"
],
"main": "index.js",
"types": "index.d.ts",
"dependencies": {
"@rollup/plugin-image": "^3.0.2",
"@rollup/plugin-node-resolve": "^15.0.1",
Expand Down
8 changes: 4 additions & 4 deletions src/types/UserscriptMetadata.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type UserscriptSpecificMetadata = {
export type UserscriptSpecificMetadata = {
name: string;
version?: string;
description: string;
Expand All @@ -14,7 +14,7 @@ type UserscriptSpecificMetadata = {
exclude?: MaybeArray<string | RegExp>;
}

type UserscriptDefaultMetadata = {
export type UserscriptDefaultMetadata = {
author: string;
namespace: string | URL;
homepageURL: string | URL;
Expand All @@ -23,9 +23,9 @@ type UserscriptDefaultMetadata = {
supportURL: string | URL;
}

type UserscriptMetadata = UserscriptSpecificMetadata & Partial<UserscriptDefaultMetadata>;
export type UserscriptMetadata = UserscriptSpecificMetadata & Partial<UserscriptDefaultMetadata>;

type EnhancedUserscriptMetadata = UserscriptMetadata & {
export type EnhancedUserscriptMetadata = UserscriptMetadata & {
/** List of features which will be shown in the README, items may contain inline Markdown. */
features?: string[];
};
8 changes: 4 additions & 4 deletions src/userscriptMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { pathToFileURL } from 'url';
import { GITHUB } from './github.js';
import { preferArray } from '../utils/array/scalar.js';

/** @type {Array<keyof UserscriptMetadata>} */
/** @type {Array<keyof import('./types/UserscriptMetadata.js').UserscriptMetadata>} */
const metadataOrder = [
'name',
'version',
Expand Down Expand Up @@ -36,7 +36,7 @@ export async function generateMetadataBlock(userscriptPath) {
const date = new Date(); // current date will be used as version identifier
const maxKeyLength = Math.max(...metadataOrder.map((key) => key.length));

/** @type {UserscriptDefaultMetadata} */
/** @type {import('./types/UserscriptMetadata.js').UserscriptDefaultMetadata} */
const defaultMetadata = {
author: GITHUB.owner,
namespace: GITHUB.repoUrl,
Expand All @@ -46,7 +46,7 @@ export async function generateMetadataBlock(userscriptPath) {
supportURL: GITHUB.supportUrl,
};

/** @type {UserscriptMetadata} */
/** @type {import('./types/UserscriptMetadata.js').UserscriptMetadata} */
const metadata = {
...defaultMetadata,
version: [date.getFullYear(), date.getMonth() + 1, date.getDate()].join('.'),
Expand All @@ -69,7 +69,7 @@ export async function generateMetadataBlock(userscriptPath) {
/**
* Loads the metadata for the given userscript from the .meta.js ES module of the same name.
* @param {string} userscriptPath
* @returns {Promise<EnhancedUserscriptMetadata>}
* @returns {Promise<import('./types/UserscriptMetadata.js').EnhancedUserscriptMetadata>}
*/
export async function loadMetadata(userscriptPath) {
const metadataPath = userscriptPath.replace(/\.user\.js$/, '.meta.js');
Expand Down

0 comments on commit 495a757

Please sign in to comment.