diff --git a/README.md b/README.md index 3b70b12..47acc25 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -The command line tool to read, manipulate and update .env files. +The command line tool and library to read, manipulate and update .env files. ## Features diff --git a/src/parser.test.ts b/src/parser.test.ts index c008824..aaec7f7 100644 --- a/src/parser.test.ts +++ b/src/parser.test.ts @@ -9,7 +9,11 @@ describe('MultiLine format parser', () => { expect(parseMultiLine('VARNAME=value \t ')).toEqual([vtoken('VARNAME', 'value', '', '', '', ' \t ')]); expect(parseMultiLine('VARNAME=A:\\DYNA')).toEqual([vtoken('VARNAME', 'A:\\DYNA')]); expect(parseMultiLine('VARNAME=quote\'is"ok')).toEqual([vtoken('VARNAME', 'quote\'is"ok')]); - expect(() => parseMultiLine('VARNAME=value not escaped')).toThrow('PARSING FAILED'); + expect(parseMultiLine('VARNAME=value not escaped')).toEqual([vtoken('VARNAME', 'value not escaped')]); + expect(parseMultiLine('VARNAME= value not escaped')).toEqual([vtoken('VARNAME', 'value not escaped', '', '', '', '', '= ')]); + expect(parseMultiLine('VARNAME=value not escaped ')).toEqual([vtoken('VARNAME', 'value not escaped', '', '', '', ' ')]); + expect(parseMultiLine('VARNAME=this# is# value')).toEqual([vtoken('VARNAME', 'this# is# value')]); + expect(parseMultiLine('VARNAME= this# is# value # and comment')).toEqual([vtoken('VARNAME', 'this# is# value', '', ' # and comment', '', '', '= ')]); }); it('prop have defined names', () => { diff --git a/src/parser.ts b/src/parser.ts index 3352216..72626a0 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -13,15 +13,14 @@ const escapedStrSQ = nonEscapedValuePartSQ.or(escapedChar.map((v: string) => v.r const nonEscapedValuePartDQ = Parsimmon.regexp(/[^"\\]+/); const escapedStrDQ = nonEscapedValuePartDQ.or(escapedChar.map((v: string) => v.replace('\\"', '"').replace('\\\\', '\\'))).many(); -// to alow spaces in simple values: [^"'\s][^\s]*([^\S\r\n]+\S+)* -- but that wont work in BASH so we do not allowed unescaped spaces! -const simpleValueWithoutSpacesAndQuotes = Parsimmon.regexp(/[^"'\s][^\s]*/); +const simpleValueNotQuoted = Parsimmon.regexp(/[^"'\s][^\s]*([^\S\r\n]+[^\s#][^\s]*)*/); const valueWithSpacesDoubleQuoted = Parsimmon.seqObj(['quote', doubleQuote], ['value', escapedStrDQ.map((v) => v.join(''))], doubleQuote); const valueWithSpacesSingleQuoted = Parsimmon.seqObj(['quote', singleQuote], ['value', escapedStrSQ.map((v) => v.join(''))], singleQuote); const value = Parsimmon.alt( valueWithSpacesSingleQuoted, valueWithSpacesDoubleQuoted, - simpleValueWithoutSpacesAndQuotes.map((v) => ({ quote: '', value: v })), + simpleValueNotQuoted.map((v) => ({ quote: '', value: v })), ).desc('property value'); const commentOpt = Parsimmon.regexp(/[^\S\r\n]+#[^\r\n]*/); const comment = Parsimmon.regexp(/[\n\r]*[^\S\r\n]*#[^\r\n]*/);