Skip to content

Commit

Permalink
fix: trim values when returning, closes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Dec 22, 2023
1 parent 2f87969 commit ca311e2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .as-a.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ MY_VARIABLE=80
; section name with empty aliases
[alias1,, , alias2]
MY_VARIABLE=100

[section-with-trailing-spaces]
MY_VAR_WITH_SPACES=Hello
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"version": "0.0.0-development",
"scripts": {
"test": "ava test",
"test:watch": "ava test --watch",
"demo:dot": "node ./bin/as-a . node ./demo",
"demo": "node ./bin/as-a demo node ./demo",
"commit": "commit-wizard",
Expand Down
17 changes: 13 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ function getSettings(name) {
}

const ini = loadUserIni()
return getSettingsFrom(name, ini)
}

function getSettingsFrom(name, ini) {
la(is.unemptyString(name), 'expected env name', name)
la(ini, 'expected loaded ini object', ini)

const envNames = name.split(',').map(_.trim).filter(is.unemptyString)
debug('loading sections', envNames)
Expand All @@ -33,14 +39,17 @@ function getSettings(name) {
process.exit(-1)
}
const settings = ini[envName]
// Trim the values of each setting
const trimmedSettings = _.mapValues(settings, _.trim)

la(
is.object(settings),
is.object(trimmedSettings),
'expected settings for section',
envName,
'not',
settings,
trimmedSettings,
)
return settings
return trimmedSettings
})

const combined = _.assign.apply(null, [{}].concat(settings))
Expand All @@ -59,4 +68,4 @@ function asA(name, command) {
})
}

module.exports = { asA, getSettings }
module.exports = { asA, getSettings, getSettingsFrom }
29 changes: 18 additions & 11 deletions src/load-user-ini.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const userHome = require('user-home')
const PROFILE_FOLDER = '.as-a'
const PROFILE_FILENAME = '.as-a.ini'

function readFiles (first, second) {
function readFiles(first, second) {
const firstSource = read(first, 'utf-8')
if (!second) {
return firstSource
Expand All @@ -20,11 +20,11 @@ function readFiles (first, second) {
return secondSource + '\n' + firstSource
}

function hasAliases (name) {
function hasAliases(name) {
return name.includes(',')
}

function loadIniFiles (filename1, filename2) {
function loadIniFiles(filename1, filename2) {
const readIni = readFiles.bind(null, filename1, filename2)
const ini = new SimpleIni(readIni, { throwOnDuplicate: false })
if (filename1) {
Expand All @@ -39,7 +39,10 @@ function loadIniFiles (filename1, filename2) {
Object.keys(ini).forEach(function (sectionName) {
if (hasAliases(sectionName)) {
debug('splitting section "%s" into multiple sections', sectionName)
const aliases = sectionName.split(',').filter(Boolean).map(s => s.trim())
const aliases = sectionName
.split(',')
.filter(Boolean)
.map((s) => s.trim())
aliases.forEach(function (alias) {
ini[alias] = ini[sectionName]
})
Expand All @@ -50,18 +53,22 @@ function loadIniFiles (filename1, filename2) {
return ini
}

function loadUserIni () {
function loadUserIni(useCurrentFolderOnly = false) {
const localFile = join(process.cwd(), PROFILE_FILENAME)
const localIniFilename = exists(localFile) ? localFile : undefined

const fullProfileFilename = join(userHome, PROFILE_FILENAME)
if (exists(fullProfileFilename)) {
return loadIniFiles(fullProfileFilename, localIniFilename)
if (!useCurrentFolderOnly) {
const fullProfileFilename = join(userHome, PROFILE_FILENAME)
if (exists(fullProfileFilename)) {
return loadIniFiles(fullProfileFilename, localIniFilename)
}
}

const profileInFolder = join(userHome, PROFILE_FOLDER, PROFILE_FILENAME)
if (exists(profileInFolder)) {
return loadIniFiles(profileInFolder, localIniFilename)
if (!useCurrentFolderOnly) {
const profileInFolder = join(userHome, PROFILE_FOLDER, PROFILE_FILENAME)
if (exists(profileInFolder)) {
return loadIniFiles(profileInFolder, localIniFilename)
}
}

if (localIniFilename) {
Expand Down
31 changes: 27 additions & 4 deletions test/load-user-ini-test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
const test = require('ava')
const loadUserIni = require('../src/load-user-ini')
const { getSettingsFrom } = require('../src/index')

test('has ini sections', t => {
test('has ini sections', (t) => {
const ini = loadUserIni()
t.true(ini.hasSection('demo'), 'has demo section')
t.true(ini.hasSection('as-a'), 'has as-a section')
t.false(ini.hasSection('does not exist'), 'does not have unknown section')
})

test('has section under an alias', t => {
test('has section under an alias', (t) => {
const ini = loadUserIni()
t.true(ini.hasSection('name-one'), 'has section name-one')
t.true(ini.hasSection('name-two'), 'has section name-two')
t.true(ini.hasSection('name-three'), 'has section name-three')
t.false(ini.hasSection('name-one, name-two, name-three'), 'initial section was removed')
t.false(
ini.hasSection('name-one, name-two, name-three'),
'initial section was removed',
)
})

test('ignores empty aliases', t => {
test('ignores empty aliases', (t) => {
const ini = loadUserIni()
t.true(ini.hasSection('alias1'), 'has section alias1')
t.true(ini.hasSection('alias2'), 'has section alias2')
})

test('trims trailing spaces', (t) => {
const ini = loadUserIni(true)
t.true(
ini.hasSection('section-with-trailing-spaces'),
'has section with trailing spaces',
)
const settings = ini['section-with-trailing-spaces']
// the loaded ini has the trailing spaces
t.deepEqual(settings, {
MY_VAR_WITH_SPACES: 'Hello ',
})

const section = getSettingsFrom('section-with-trailing-spaces', ini)
// the setting should have its trailing spaces removed
t.deepEqual(section, {
MY_VAR_WITH_SPACES: 'Hello',
})
})

0 comments on commit ca311e2

Please sign in to comment.