11import fs from "node:fs/promises" ;
2+ import fsSync from "node:fs" ;
23import os from "node:os" ;
34import path from "node:path" ;
45import process from "node:process" ;
@@ -8,18 +9,29 @@ interface Options {
89 credentialDir ?: string ;
910 profile ?: string ;
1011 itemsRootDir ?: string ;
12+ userConfigDir ?: string ;
1113}
1214
15+ type UserConfig = {
16+ includePrivate : boolean ;
17+ } ;
18+
1319class Config {
1420 private credentialDir ?: string ;
1521 private itemsRootDir ?: string ;
22+ private userConfigFilePath ?: string ;
23+ private userConfigDir ?: string ;
1624 private credential ?: Credential ;
1725
1826 constructor ( ) { }
1927
2028 load ( options : Options ) {
2129 this . credentialDir = this . resolveConfigDir ( options . credentialDir ) ;
2230 this . itemsRootDir = this . resolveItemsRootDir ( options . itemsRootDir ) ;
31+ this . userConfigDir = this . resolveUserConfigDirPath ( options . userConfigDir ) ;
32+ this . userConfigFilePath = this . resolveUserConfigFilePath (
33+ options . userConfigDir
34+ ) ;
2335 this . credential = new Credential ( {
2436 credentialDir : this . credentialDir ,
2537 profile : options . profile ,
@@ -30,6 +42,7 @@ class Config {
3042 JSON . stringify ( {
3143 credentialDir : this . credentialDir ,
3244 itemsRootDir : this . itemsRootDir ,
45+ userConfigFilePath : this . userConfigFilePath ,
3346 } )
3447 ) ;
3548 }
@@ -49,6 +62,20 @@ class Config {
4962 return this . itemsRootDir ;
5063 }
5164
65+ getUserConfigDir ( ) {
66+ if ( ! this . userConfigDir ) {
67+ throw new Error ( "userConfigDir is undefined" ) ;
68+ }
69+ return this . userConfigDir ;
70+ }
71+
72+ getUserConfigFilePath ( ) {
73+ if ( ! this . userConfigFilePath ) {
74+ throw new Error ( "userConfigFilePath is undefined" ) ;
75+ }
76+ return this . userConfigFilePath ;
77+ }
78+
5279 getCredential ( ) {
5380 if ( ! this . credential ) {
5481 throw new Error ( "credential is undefined" ) ;
@@ -63,6 +90,24 @@ class Config {
6390 return this . credential . setCredential ( credential ) ;
6491 }
6592
93+ async getUserConfig ( ) {
94+ const defaultConfig = {
95+ includePrivate : false ,
96+ } as UserConfig ;
97+
98+ if ( fsSync . existsSync ( this . getUserConfigFilePath ( ) ) ) {
99+ const userConfigFileData = await fs . readFile (
100+ this . userConfigFilePath as string
101+ ) ;
102+ const userConfig = JSON . parse (
103+ userConfigFileData . toString ( )
104+ ) as UserConfig ;
105+ return { ...defaultConfig , ...userConfig } ;
106+ }
107+
108+ return defaultConfig ;
109+ }
110+
66111 private resolveConfigDir ( credentialDirPath ?: string ) {
67112 const packageName = "qiita-cli" ;
68113
@@ -89,6 +134,22 @@ class Config {
89134 return this . resolveFullPath ( dirPath ) ;
90135 }
91136
137+ private resolveUserConfigDirPath ( dirPath ?: string ) {
138+ if ( process . env . QIITA_CLI_USER_CONFIG_DIR ) {
139+ return process . env . QIITA_CLI_USER_CONFIG_DIR ;
140+ }
141+ if ( ! dirPath ) {
142+ return process . cwd ( ) ;
143+ }
144+
145+ return this . resolveFullPath ( dirPath ) ;
146+ }
147+
148+ private resolveUserConfigFilePath ( dirPath ?: string ) {
149+ const filename = "qiita.config.json" ;
150+ return path . join ( this . resolveUserConfigDirPath ( dirPath ) , filename ) ;
151+ }
152+
92153 private resolveFullPath ( filePath : string ) {
93154 if ( path . isAbsolute ( filePath ) ) {
94155 return filePath ;
0 commit comments