Skip to content

Commit 0c0ad77

Browse files
committed
support rcfile config
1 parent 66a4451 commit 0c0ad77

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

.yalcrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
workspace-resolve=true
2+
sig=false
3+
dev-mod=false

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"fs-extra": "^8.0.1",
3636
"glob": "^7.1.4",
3737
"ignore": "^5.0.4",
38+
"ini": "^2.0.0",
3839
"npm-packlist": "^1.4.1",
3940
"yargs": "^16.1.1"
4041
},

src/rc.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import fs from 'fs'
2+
const ini = require('ini')
3+
4+
const validFlags = [
5+
'sig',
6+
'workspace-resolve',
7+
'dev-mod',
8+
'scripts',
9+
'quiet',
10+
'files',
11+
]
12+
13+
const fileName = '.yalcrc'
14+
15+
const readFile = (): Record<string, string | boolean> | null => {
16+
if (fs.existsSync(fileName)) {
17+
return ini.parse(fs.readFileSync(fileName, 'utf-8'))
18+
}
19+
return null
20+
}
21+
22+
export const readRcConfig = (): Record<string, string | boolean> => {
23+
const rcOptions = readFile()
24+
if (!rcOptions) return {}
25+
26+
const unknown = Object.keys(rcOptions).filter(
27+
(key) => !validFlags.includes(key)
28+
)
29+
30+
if (unknown.length) {
31+
console.warn(`Unknown option in ${fileName}: ${unknown[0]}`)
32+
process.exit()
33+
}
34+
return Object.keys(rcOptions).reduce((prev, flag) => {
35+
return validFlags.includes(flag)
36+
? { ...prev, [flag]: rcOptions[flag] }
37+
: prev
38+
}, {})
39+
}

src/yalc.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env node
22
import yargs from 'yargs'
33
import { join, resolve } from 'path'
4+
import { rcFile } from 'rc-config-loader'
5+
46
import {
57
values,
68
publishPackage,
@@ -16,6 +18,7 @@ import { showInstallations, cleanInstallations } from './installations'
1618
import { checkManifest } from './check'
1719
import { makeConsoleColored, disabledConsoleOutput } from './console'
1820
import { PublishPackageOptions } from './publish'
21+
import { readRcConfig } from './rc'
1922

2023
const updateFlags = ['update', 'upgrade', 'up']
2124

@@ -30,7 +33,9 @@ const getVersionMessage = () => {
3033

3134
makeConsoleColored()
3235

33-
if (process.argv.includes('--quiet')) {
36+
const rcArgs = readRcConfig()
37+
38+
if (process.argv.includes('--quiet') || rcArgs.quiet) {
3439
disabledConsoleOutput()
3540
}
3641

@@ -49,6 +54,8 @@ const getPublishOptions = (
4954
private: argv.private,
5055
scripts: argv.scripts,
5156
update: argv.update || argv.upgrade,
57+
workspaceResolve: argv.workspaceResolve,
58+
devMod: argv.devMod,
5259
...override,
5360
}
5461
}
@@ -87,8 +94,10 @@ yargs
8794
.default('sig', true)
8895
.default('scripts', true)
8996
.default('dev-mod', true)
97+
.default('workspace-resolve', true)
98+
.default(rcArgs)
9099
.alias('script', 'scripts')
91-
.boolean(['push', 'push-safe'].concat(publishFlags))
100+
.boolean(['push'].concat(publishFlags))
92101
},
93102
handler: (argv) => {
94103
return publishPackage(getPublishOptions(argv))
@@ -103,6 +112,9 @@ yargs
103112
.default('force', undefined)
104113
.default('sig', true)
105114
.default('scripts', true)
115+
.default('dev-mod', true)
116+
.default('workspace-resolve', true)
117+
.default(rcArgs)
106118
.alias('script', 'scripts')
107119
.boolean(['safe'].concat(publishFlags))
108120
.option('replace', { describe: 'Force package content replacement' })
@@ -141,6 +153,7 @@ yargs
141153
.alias('D', 'dev')
142154
.boolean('-W')
143155
.alias('save-dev', 'dev')
156+
.default(rcArgs)
144157
.help(true)
145158
},
146159
handler: (argv) => {
@@ -158,7 +171,7 @@ yargs
158171
command: 'link',
159172
describe: 'Link package from yalc repo to the project',
160173
builder: () => {
161-
return yargs.help(true)
174+
return yargs.default(rcArgs).help(true)
162175
},
163176
handler: (argv) => {
164177
return addPackages(argv._.slice(1), {
@@ -172,7 +185,10 @@ yargs
172185
command: 'update',
173186
describe: 'Update packages from yalc repo',
174187
builder: () => {
175-
return yargs.boolean([...updateFlags]).help(true)
188+
return yargs
189+
.boolean([...updateFlags])
190+
.default(rcArgs)
191+
.help(true)
176192
},
177193
handler: (argv) => {
178194
return updatePackages(argv._.slice(1), {
@@ -185,7 +201,7 @@ yargs
185201
command: 'remove',
186202
describe: 'Remove packages from the project',
187203
builder: () => {
188-
return yargs.boolean(['retreat', 'all']).help(true)
204+
return yargs.boolean(['retreat', 'all']).default(rcArgs).help(true)
189205
},
190206
handler: (argv) => {
191207
return removePackages(argv._.slice(1), {

0 commit comments

Comments
 (0)