Skip to content

Commit

Permalink
Merge pull request #9 from jotoeri/enh/config-file
Browse files Browse the repository at this point in the history
Move to separate config file
  • Loading branch information
jotoeri authored Dec 27, 2024
2 parents d098ae3 + 878527c commit aa1f551
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
nc_changelog.md
nc_changelog.md
config.json
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ CLI Options
- `version` - Tag-Name of upcoming release.
- `previousVersion` - Tag-Name of the previous release. Defaults to latest Github release.

All these options are possible to store a default value in npm-config as `ncc_${option}`. Such they dont have to be given on each execution.
All these options are possible to store a default value in a file named `config.json` in the repository root. Such they dont have to be given on each execution.
24 changes: 17 additions & 7 deletions utils/createRuntimeConfig.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
import { readFileSync } from 'fs'

function createRuntimeConfig() {
const runtimeConfig = {}
const processArgs = readProcessArgs()
let configFile = {}

// Read config file
try {
configFile = JSON.parse(readFileSync('./config.json'))
} catch (err) {
console.info('No config file found.')
}

// Read github token
runtimeConfig.ghtoken = processArgs.ghtoken ? processArgs.ghtoken : process.env.npm_config_ncc_ghtoken
runtimeConfig.ghtoken = processArgs.ghtoken ? processArgs.ghtoken : configFile.ghtoken

// Read repository
runtimeConfig.repository = processArgs.repository ? processArgs.repository : process.env.npm_config_ncc_repository
runtimeConfig.repository = processArgs.repository ? processArgs.repository : configFile.repository

// Read base branch
runtimeConfig.baseBranch = processArgs.base ? processArgs.base :
(process.env.npm_config_ncc_base ? process.env.npm_config_ncc_base : 'main')
(configFile.base ? configFile.base : 'main')

// Read version
runtimeConfig.version = processArgs.version ? processArgs.version :
(process.env.npm_config_ncc_version ? process.env.npm_config_ncc_version : 'Unreleased')
(configFile.version ? configFile.version : 'Unreleased')

// Read previous version
runtimeConfig.previousVersion = processArgs.previousVersion ? processArgs.previousVersion : process.env.npm_config_ncc_previousVersion
runtimeConfig.previousVersion = processArgs.previousVersion ? processArgs.previousVersion : configFile.previousVersion

// Read release date
runtimeConfig.releaseDate = processArgs.releaseDate ? processArgs.releaseDate : process.env.npm_config_ncc_releaseDate
runtimeConfig.releaseDate = processArgs.releaseDate ? processArgs.releaseDate : configFile.releaseDate
if(!runtimeConfig.releaseDate) {
// If no date set, take current time.
// Using swedish format for date, but iso formatted date-string.
Expand All @@ -30,7 +40,7 @@ function createRuntimeConfig() {

// Read outFile
runtimeConfig.outFile = processArgs.out ? processArgs.out :
(process.env.npm_config_ncc_out ? process.env.npm_config_ncc_out : 'nc_changelog.md')
(configFile.out ? configFile.out : 'nc_changelog.md')

console.debug(runtimeConfig)
return runtimeConfig
Expand Down

0 comments on commit aa1f551

Please sign in to comment.