Skip to content

Commit

Permalink
refactor: move interfaces out of types.ts
Browse files Browse the repository at this point in the history
This makes jump-to-definition much more useful.
  • Loading branch information
lishaduck committed Jul 18, 2024
1 parent 7baedc9 commit 9f5ab1d
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 164 deletions.
13 changes: 7 additions & 6 deletions src/__tests__/type-syncer.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import type { IConfigService } from '../config-service'
import type { IGlobber } from '../globber'
import type { IPackageJSONService } from '../package-json-file-service'
import type { IPackageSource, IPackageInfo } from '../package-source'
import { createTypeSyncer } from '../type-syncer'
import {
type IConfigService,
IDependencySection,
type IPackageFile,
type IPackageInfo,
type IPackageJSONService,
type IPackageSource,
type IPackageTypingDescriptor,
type IWorkspaceResolverService,
type IWorkspacesArray,
} from '../types'
import type {
IWorkspacesArray,
IWorkspaceResolverService,
} from '../workspace-resolver'

const descriptors: IPackageTypingDescriptor[] = [
{
Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/versioning.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { IPackageVersionInfo } from '../types'
import { getClosestMatchingVersion } from '../versioning'
import {
getClosestMatchingVersion,
type IPackageVersionInfo,
} from '../versioning'

describe('getClosestMatchingVersion', () => {
it('returns the closest matching version', () => {
Expand Down
14 changes: 13 additions & 1 deletion src/config-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ import * as path from 'node:path'
import { cosmiconfig } from 'cosmiconfig'
import {
type ICLIArguments,
type IConfigService,
IDependencySection,
type ISyncOptions,
} from './types'
import { shrinkObject } from './util'

/**
* Config Service.
*/
export interface IConfigService {
/**
* Get typesync config.
*/
readConfig(
filePath: string,
flags: ICLIArguments['flags'],
): Promise<ISyncOptions>
}

const explorer = cosmiconfig('typesync')

export function createConfigService(): IConfigService {
Expand Down
16 changes: 15 additions & 1 deletion src/package-json-file-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import * as fsp from 'node:fs/promises'
import detectIndent from 'detect-indent'
import { readFileContents } from './fs-utils'
import { IPackageJSONService, IPackageFile } from './types'
import { IPackageFile } from './types'

/**
* File service.
*/
export interface IPackageJSONService {
/**
* Reads and parses JSON from the specified file. Path is relative to the current working directory.
*/
readPackageFile(filePath: string): Promise<IPackageFile>
/**
* Writes the JSON to the specified file.
*/
writePackageFile(filePath: string, fileContents: IPackageFile): Promise<void>
}

export function createPackageJSONFileService(): IPackageJSONService {
return {
Expand Down
24 changes: 23 additions & 1 deletion src/package-source.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import fetch from 'npm-registry-fetch'
import { compare } from 'semver'
import type { IPackageSource, IPackageVersionInfo } from './types'
import type { IPackageVersionInfo } from './versioning'

/**
* Fetches info about a package.
*/
export interface IPackageSource {
/**
* Fetches package info from an external source.
*
* @param packageName
*/
fetch(packageName: string): Promise<IPackageInfo | null>
}

/**
* Interface for the Package Info structure.
*/
export interface IPackageInfo {
name: string
latestVersion: string
deprecated: boolean
versions: Array<IPackageVersionInfo>
}

/**
* Creates a package source.
Expand Down
8 changes: 4 additions & 4 deletions src/type-syncer.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import * as path from 'node:path'
import type { IConfigService } from './config-service'
import type { IGlobber } from './globber'
import type { IPackageJSONService } from './package-json-file-service'
import type { IPackageSource } from './package-source'
import {
type ICLIArguments,
type IConfigService,
type IDependenciesSection,
IDependencySection,
type IPackageFile,
type IPackageJSONService,
type IPackageSource,
type IPackageTypingDescriptor,
type IPackageVersion,
type ISyncOptions,
type ISyncResult,
type ISyncedFile,
type ITypeSyncer,
type IWorkspaceResolverService,
} from './types'
import {
filterMap,
Expand All @@ -25,6 +24,7 @@ import {
uniq,
} from './util'
import { getClosestMatchingVersion } from './versioning'
import type { IWorkspaceResolverService } from './workspace-resolver'

/**
* Creates a type syncer.
Expand Down
141 changes: 1 addition & 140 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IGlobber } from './globber'
import type { IWorkspacesSection } from './workspace-resolver'

/**
* The guts of the program.
Expand All @@ -21,63 +21,6 @@ export interface ISyncOptions {
ignorePackages?: string[]
}

/**
* Config Service.
*/
export interface IConfigService {
/**
* Get typesync config.
*/
readConfig(
filePath: string,
flags: ICLIArguments['flags'],
): Promise<ISyncOptions>
}

/**
* Fetches info about a package.
*/
export interface IPackageSource {
/**
* Fetches package info from an external source.
*
* @param packageName
*/
fetch(packageName: string): Promise<IPackageInfo | null>
}

/**
* File service.
*/
export interface IPackageJSONService {
/**
* Reads and parses JSON from the specified file. Path is relative to the current working directory.
*/
readPackageFile(filePath: string): Promise<IPackageFile>
/**
* Writes the JSON to the specified file.
*/
writePackageFile(filePath: string, fileContents: IPackageFile): Promise<void>
}

/**
* Interface for the Package Info structure.
*/
export interface IPackageInfo {
name: string
latestVersion: string
deprecated: boolean
versions: Array<IPackageVersionInfo>
}

/**
* Version descriptor for versions returned in remote package info.
*/
export interface IPackageVersionInfo {
version: string
containsInternalTypings: boolean
}

/**
* Package.json file.
*/
Expand All @@ -98,88 +41,6 @@ export interface IDependenciesSection {
[packageName: string]: string
}

/**
* @example
* ```json
* "workspaces": [
* "packages/*",
* ]
* ```
*/
export type IWorkspacesArray = Array<string>

/**
* @example
* ```yaml
* projects:
* - 'packages/*'
* ```
*/
export type IWorkspacesObject = {
packages: IWorkspacesArray
}

/**
* @see {@link IWorkspacesArray}
*/
type NpmWorkspacesConfig = IWorkspacesArray

/**
* Yarn is a special snowflake.
*
* @example
* ```json
* "workspaces": {
* "packages": [
* "packages/*",
* ],
* "nohoist": []
* }
* ```
*/
type YarnWorkspacesConfig =
| IWorkspacesArray
| (IWorkspacesObject & { nohoist?: string[] })

/**
* The contents of a `pnpm-workspace.yaml` file.
*
* @example
* ```yaml
* packages:
* - 'packages/*'
* ```
*/
export type PnpmWorkspacesConfig = IWorkspacesObject

/**
* @see {@link IWorkspacesArray}
*/
type BunWorkspacesConfig = IWorkspacesArray

/**
* Section in `package.json` representing workspaces.
*/
export type IWorkspacesSection =
| NpmWorkspacesConfig
| YarnWorkspacesConfig
| BunWorkspacesConfig

/**
* Files service.
*/
export interface IWorkspaceResolverService {
/**
* Reads, parses, and normalizes a workspaces configuration from the following files, in this order:
* - `package.json` `workspaces` field, as an array of globs.
* - `package.json` `workspaces` field, as an object with a `projects` field, which is an array of globs.
* - `pnpm-workspace.yaml` `packages` field, as an array of globs.
*
* Path is relative to the current working directory.
*/
getWorkspaces(root: string, globber: IGlobber): Promise<IWorkspacesArray>
}

/**
* Package + version record, collected from the {"package": "^1.2.3"} sections.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IWorkspacesSection, IWorkspacesArray } from './types'
import type { IWorkspacesArray, IWorkspacesSection } from './workspace-resolver'

/**
* Returns unique items.
Expand Down
9 changes: 8 additions & 1 deletion src/versioning.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { parse } from 'semver'
import type { IPackageVersionInfo } from './types'

/**
* Version descriptor for versions returned in remote package info.
*/
export interface IPackageVersionInfo {
version: string
containsInternalTypings: boolean
}

/**
* Gets the closest matching package version info.
Expand Down
Loading

0 comments on commit 9f5ab1d

Please sign in to comment.