-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
135 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
declare module 'inquirer-tree-prompt' | ||
declare module 'inquirer-search-list' | ||
declare module 'envfile' | ||
|
||
declare namespace NodeJS { | ||
interface Process { | ||
GX_CONFIG_PATH: string | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { Module } from '@nestjs/common' | ||
import { Commands } from '@/commands' | ||
import { ConfigModule } from '@/config' | ||
import { LoggerModule } from '@/logger' | ||
import { PackageManagerModule } from '@/pkg-manager' | ||
|
||
@Module({ | ||
imports: [PackageManagerModule, LoggerModule], | ||
imports: [PackageManagerModule, LoggerModule, ConfigModule], | ||
providers: [...Commands] | ||
}) | ||
export class AppModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Global, Module } from '@nestjs/common' | ||
import { ConfigService } from './config.service' | ||
|
||
@Global() | ||
@Module({ | ||
controllers: [], | ||
providers: [ConfigService], | ||
exports: [ConfigService] | ||
}) | ||
export class ConfigModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { z } from 'zod' | ||
|
||
export const ConfigSchema = z.object({ | ||
commandsFile: z.string().default('targets.json'), | ||
preferredResolvingOrder: z | ||
.array(z.union([z.literal('package-scripts'), z.literal('targets')])) | ||
.default(['targets', 'package-scripts']) | ||
}) | ||
|
||
function createConfigValues() { | ||
return class ConfigValues {} as { | ||
new (): z.infer<typeof ConfigSchema> | ||
} | ||
} | ||
|
||
export class ConfigValues extends createConfigValues() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Inject, Injectable } from '@nestjs/common' | ||
import ini from 'ini' | ||
import { existsSync, readFileSync } from 'node:fs' | ||
import { resolve } from 'node:path' | ||
import process from 'node:process' | ||
import { ConfigSchema, ConfigValues } from '@/config/config.schema' | ||
import { LoggerService } from '@/logger' | ||
|
||
@Injectable() | ||
export class ConfigService extends ConfigValues { | ||
constructor(@Inject(LoggerService) private readonly logger: LoggerService) { | ||
super() | ||
Object.assign(this, this.config) | ||
} | ||
|
||
private get config() { | ||
const userConfig = existsSync(this.configPath) | ||
? ini.parse(readFileSync(this.configPath).toString()) | ||
: {} | ||
|
||
try { | ||
return ConfigSchema.parse(userConfig) | ||
} catch (error) { | ||
this.logger.error(error) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
private get configPath() { | ||
const customRcPath = process.env.GX_CONFIG_PATH | ||
|
||
const isWindows = process.platform === 'win32' | ||
const home = isWindows ? process.env.USERPROFILE : process.env.HOME | ||
|
||
const defaultRcPath = resolve(home ?? '~/', '.gxrc') | ||
const projectRcPath = resolve(process.cwd(), '.gxrc') | ||
|
||
const baseRcPath = customRcPath ?? defaultRcPath | ||
|
||
return existsSync(projectRcPath) ? projectRcPath : baseRcPath | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { ConfigModule } from './config.module' | ||
export { ConfigService } from './config.service' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,50 @@ | ||
import { isNull } from '@neodx/std' | ||
import { Injectable } from '@nestjs/common' | ||
import { entries, sortObjectByOrder } from '@neodx/std' | ||
import { Inject, Injectable } from '@nestjs/common' | ||
import { ConfigService } from '@/config' | ||
import { LoggerService } from '@/logger' | ||
import type { PackageJsonResolverService } from '@/resolver/package/package.resolver.service' | ||
import type { ResolvedTargets } from '@/resolver/resolver.types' | ||
import type { ResolvedTargets, TargetType } from '@/resolver/resolver.types' | ||
import type { TargetOptions } from '@/resolver/targets/targets-resolver.schema' | ||
import type { TargetsResolverService } from '@/resolver/targets/targets-resolver.service' | ||
|
||
@Injectable() | ||
export class ResolverService { | ||
constructor( | ||
private readonly targetsResolver: TargetsResolverService, | ||
private readonly pkgResolver: PackageJsonResolverService | ||
private readonly pkgResolver: PackageJsonResolverService, | ||
@Inject(ConfigService) private readonly cfg: ConfigService, | ||
@Inject(LoggerService) private readonly logger: LoggerService | ||
) {} | ||
|
||
public async resolveProjectTargets(cwd: string): Promise<ResolvedTargets> { | ||
const targets = await this.targetsResolver.resolveProjectTargets(cwd) | ||
const sortedResolvers = sortObjectByOrder( | ||
this.resolverMethods, | ||
this.cfg.preferredResolvingOrder | ||
) | ||
|
||
if (isNull(targets)) { | ||
return { | ||
type: 'package-scripts', | ||
targets: await this.pkgResolver.resolvePackageJsonScripts(cwd) | ||
for (const [type, resolveTargets] of entries(sortedResolvers)) { | ||
const targets = await resolveTargets.call(this, cwd) | ||
|
||
if (targets) { | ||
return { type, targets } | ||
} | ||
} | ||
|
||
this.logger.error( | ||
`Error occurred while resolving project targets. | ||
Please check if preferredResolvingOrder is set correctly` | ||
) | ||
process.exit(1) | ||
} | ||
|
||
private get resolverMethods(): Record< | ||
TargetType, | ||
(cwd: string) => Promise<TargetOptions | null> | ||
> { | ||
return { | ||
type: 'targets', | ||
targets | ||
'package-scripts': this.pkgResolver.resolvePackageJsonScripts, | ||
// eslint-disable-next-line prettier/prettier | ||
'targets': this.targetsResolver.resolveProjectTargets | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import type { ConfigValues } from '@/config/config.schema' | ||
import type { TargetOptions } from '@/resolver/targets/targets-resolver.schema' | ||
import type { InferArrayItem } from '@/shared/types' | ||
|
||
export type TargetType = InferArrayItem<ConfigValues['preferredResolvingOrder']> | ||
|
||
export interface ResolvedTargets { | ||
type: 'package-scripts' | 'targets' | ||
type: TargetType | ||
targets: TargetOptions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type InferArrayItem<T extends unknown[]> = T extends (infer S)[] | ||
? S | ||
: never |