Skip to content

Commit

Permalink
Supports more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanis committed May 19, 2019
1 parent 5c6c036 commit 4d02b86
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
45 changes: 34 additions & 11 deletions packages/berry-parsers/sources/syml.ts
@@ -1,7 +1,7 @@
// @ts-ignore
import {load} from 'js-yaml';
import {safeLoad} from 'js-yaml';

import {parse} from './grammars/syml';
import {parse} from './grammars/syml';

const simpleStringPattern = /^(?![-?:,\][{}#&*!|>'"%@` \t\r\n]).([ \t]*(?![,\][{}:# \t\r\n]).)*$/;

Expand Down Expand Up @@ -84,19 +84,42 @@ function stringifyValue(value: any, indentLevel: number): string {
}

export function stringifySyml(value: any) {
return stringifyValue(value, 0);
}

export function parseSyml(source: string) {
try {
try {
return load(source) as {[key: string]: any};
} catch (error) {
return parse(source.endsWith(`\n`) ? source : `${source}\n`);
}
return stringifyValue(value, 0);
} catch (error) {
if (error.location)
error.message = error.message.replace(/(\.)?$/, ` (line ${error.location.start.line}, column ${error.location.start.column})$1`);
throw error;
}
}

function parseViaPeg(source: string) {
if (!source.endsWith(`\n`))
source += `\n`;

return parse(source);
}

function parseViaJsYaml(source: string) {
let value;

try {
value = safeLoad(source);
} catch (error) {
return parseViaPeg(source);
}

// Empty files are parsed as `null` instead of an empty object
if (value === null)
return {} as {[key: string]: string};

// Files that contain single invalid line are treated as a raw string
if (typeof value === `string`)
return parseViaPeg(source);

return value as {[key: string]: string};
}

export function parseSyml(source: string) {
return parseViaJsYaml(source);
}
3 changes: 1 addition & 2 deletions packages/plugin-npm-cli/sources/commands/npm/whoami.ts
Expand Up @@ -34,9 +34,8 @@ export default (clipanion: Clipanion, pluginConfiguration: PluginConfiguration)
const report = await StreamReport.start({configuration, stdout}, async report => {
let ident: Ident | null = null;

if (scope) {
if (scope)
ident = structUtils.makeIdent(scope, ``);
}

try {
const response = await npmHttpUtils.get(`/-/whoami`, { configuration, ident, authType: npmHttpUtils.AuthType.ALWAYS_AUTH, json: true });
Expand Down

0 comments on commit 4d02b86

Please sign in to comment.