@@ -2,7 +2,7 @@ import path from 'node:path';
2
2
import os from 'node:os' ;
3
3
4
4
import { readJson , writeJson } from './file.js' ;
5
- import { existsSync } from 'node:fs' ;
5
+ import { existsSync , mkdtempSync , rmSync } from 'node:fs' ;
6
6
import { spawnSync } from 'node:child_process' ;
7
7
8
8
export const GLOBAL_CONFIG = Symbol ( 'globalConfig' ) ;
@@ -61,13 +61,31 @@ export function getConfigPath(configType, dir) {
61
61
} ;
62
62
63
63
export function writeConfig ( configType , obj , dir ) {
64
- writeJson ( getConfigPath ( configType , dir ) , obj ) ;
64
+ const configPath = getConfigPath ( configType , dir ) ;
65
+ const encryptedConfigPath = configPath + '.gpg' ;
66
+ if ( existsSync ( encryptedConfigPath ) ) {
67
+ const tmpDir = mkdtempSync ( path . join ( os . tmpdir ( ) , 'ncurc-' ) ) ;
68
+ const tmpFile = path . join ( tmpDir , 'config.json' ) ;
69
+ try {
70
+ writeJson ( tmpFile , obj ) ;
71
+ const { status } = spawnSync ( 'gpg' ,
72
+ [ '--default-recipient-self' , '--yes' , '--encrypt' , '--output' , encryptedConfigPath , tmpFile ]
73
+ ) ;
74
+ if ( status !== 0 ) {
75
+ throw new Error ( 'Failed to encrypt config file: ' + encryptedConfigPath ) ;
76
+ }
77
+ } finally {
78
+ rmSync ( tmpDir , { recursive : true , force : true } ) ;
79
+ }
80
+ return encryptedConfigPath ;
81
+ }
82
+ writeJson ( configPath , obj ) ;
83
+ return configPath ;
65
84
} ;
66
85
67
86
export function updateConfig ( configType , obj , dir ) {
68
87
const config = getConfig ( configType , dir ) ;
69
- const configPath = getConfigPath ( configType , dir ) ;
70
- writeJson ( configPath , Object . assign ( config , obj ) ) ;
88
+ writeConfig ( configType , Object . assign ( config , obj ) , dir ) ;
71
89
} ;
72
90
73
91
export function getHomeDir ( home ) {
0 commit comments