Skip to content
This repository was archived by the owner on Dec 4, 2022. It is now read-only.

Commit 9ae849f

Browse files
amitgilad3GiladShoham
authored andcommitted
Feature/npmlogin-path (#54)
1 parent ab0f81b commit 9ae849f

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

src/registry/exceptions/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @flow
2+
import PathToNpmrcNotExist from './path-to-npmrc-not-exist';
3+
import WriteToNpmrcError from './write-to-npmrc-error';
4+
5+
export {
6+
PathToNpmrcNotExist,
7+
WriteToNpmrcError
8+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/** @flow */
2+
export default class PathsNotExist extends Error {
3+
path: string;
4+
constructor(path: string) {
5+
super();
6+
this.path = path;
7+
this.code = 'PathNotExist';
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/** @flow */
2+
export default class WriteToNpmrcError extends Error {
3+
path: string;
4+
constructor(path: string) {
5+
super();
6+
this.path = path;
7+
this.code = 'WriteError';
8+
}
9+
}

src/registry/registry.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import path from 'path';
44
import iniBuilder from 'ini-builder';
55
import userHome from 'user-home';
66
import { DEFAULT_BINDINGS_PREFIX } from '../constants';
7+
import { PathToNpmrcNotExist, WriteToNpmrcError } from './exceptions';
78

89
function findrc(pathToNpmrc: string) {
910
let userNpmrc = path.join(userHome, '.npmrc');
10-
if (pathToNpmrc && fs.existsSync(pathToNpmrc)) {
11+
if (pathToNpmrc) {
12+
if (!fs.existsSync(pathToNpmrc)) throw new PathToNpmrcNotExist(pathToNpmrc);
1113
const stats = fs.statSync(pathToNpmrc);
1214
if (stats.isFile()) userNpmrc = pathToNpmrc;
1315
else userNpmrc = path.join(pathToNpmrc, '.npmrc');
@@ -38,8 +40,13 @@ function mergeOrCreateConfig(token: string, url: string, config: Array<Object> =
3840
return config;
3941
}
4042

41-
export default function npmLogin(token: string, pathToNpmrc: string, url: string): void {
43+
export default function npmLogin(token: string, pathToNpmrc: string, url: string): string {
4244
const npmrcPath = findrc(pathToNpmrc);
4345
const npmrcConfig = (fs.existsSync(npmrcPath)) ? mergeOrCreateConfig(token, url, iniBuilder.parse(fs.readFileSync(npmrcPath, 'utf-8'))) : mergeOrCreateConfig(token, url);
44-
fs.writeFileSync(npmrcPath, iniBuilder.serialize(npmrcConfig));
46+
try {
47+
fs.writeFileSync(npmrcPath, iniBuilder.serialize(npmrcConfig));
48+
} catch (err) {
49+
throw new WriteToNpmrcError(npmrcPath);
50+
}
51+
return npmrcPath;
4552
}

0 commit comments

Comments
 (0)