Skip to content

Commit

Permalink
fix: normalize paths to remove trailing slashes (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneslumpe committed Jun 11, 2019
1 parent 6ca3170 commit bbf65a2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/cli/commands/applications/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function removeApplicationCommand(
const { config, groupname, applicationpaths } = argv;
const updatedConfig = await removeApplication({
config: config.contents,
configPath: config.path,
groupName: groupname,
relativePaths: applicationpaths,
});
Expand Down
15 changes: 10 additions & 5 deletions src/core/applications/add.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import path from 'path';
import fs from 'fs';

import { emphasize, getGroupConfig, isNodeJSError } from '../utils';
import {
emphasize,
getGroupConfig,
isNodeJSError,
normalizePaths,
} from '../utils';
import { VersionGuardConfig } from '../config';
import { VersionGuardError } from '../errors';

Expand Down Expand Up @@ -44,20 +49,20 @@ export async function addApplications({
config,
}: AddApplicationOptions): Promise<VersionGuardConfig> {
const groupConfig = getGroupConfig(groupName, config);
relativePaths.forEach(relativePath => {
const normalizedPaths = normalizePaths({ configPath, relativePaths });
normalizedPaths.forEach(relativePath => {
if (groupConfig.applications.includes(relativePath)) {
throw VersionGuardError.from(
emphasize`Group ${groupName} already includes application with path ${relativePath}`,
);
}
});
await ensurePackageJsonsExist(path.dirname(configPath), relativePaths);

await ensurePackageJsonsExist(path.dirname(configPath), normalizedPaths);
return {
...config,
[groupName]: {
...groupConfig,
applications: groupConfig.applications.concat(relativePaths),
applications: groupConfig.applications.concat(normalizedPaths),
},
};
}
9 changes: 6 additions & 3 deletions src/core/applications/remove.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { emphasize, getGroupConfig } from '../utils';
import { emphasize, getGroupConfig, normalizePaths } from '../utils';
import { VersionGuardConfig } from '../config';
import { VersionGuardError } from '../errors';

interface RemoveApplicationOptions {
relativePaths: string[];
groupName: string;
config: VersionGuardConfig;
configPath: string;
}

export function removeApplication({
relativePaths,
config,
configPath,
groupName,
}: RemoveApplicationOptions): VersionGuardConfig {
const groupConfig = getGroupConfig(groupName, config);
relativePaths.forEach(relativePath => {
const normalizedPaths = normalizePaths({ configPath, relativePaths });
normalizedPaths.forEach(relativePath => {
if (!groupConfig.applications.includes(relativePath)) {
throw VersionGuardError.from(
emphasize`Group does not include application with path ${relativePath}`,
Expand All @@ -27,7 +30,7 @@ export function removeApplication({
[groupName]: {
...groupConfig,
applications: groupConfig.applications.filter(
path => !relativePaths.includes(path),
path => !normalizedPaths.includes(path),
),
},
};
Expand Down
19 changes: 18 additions & 1 deletion src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import chalk from 'chalk';
import semver, { SemVer } from 'semver';
import semver from 'semver';
import path from 'path';

import { VersionGuardConfig } from './config';
import { GroupConfig } from './groups';
Expand Down Expand Up @@ -76,3 +77,19 @@ export function getMinSemverVersionOrThrow(
}
return minVersion;
}

export function normalizePaths({
configPath,
relativePaths,
}: {
configPath: string;
relativePaths: string[];
}): string[] {
const configBase = path.dirname(configPath);
const paths = relativePaths.map(p =>
// combining `path.resolve` with `path.relative` in this way
// gets rid of any trailing slashes that may exist on `p`
path.relative(configBase, path.resolve(configBase, p)),
);
return paths;
}

0 comments on commit bbf65a2

Please sign in to comment.