diff --git a/src/cli/commands/applications/remove.ts b/src/cli/commands/applications/remove.ts index 0f5f389..9f39237 100644 --- a/src/cli/commands/applications/remove.ts +++ b/src/cli/commands/applications/remove.ts @@ -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, }); diff --git a/src/core/applications/add.ts b/src/core/applications/add.ts index 9340bbb..fe0e073 100644 --- a/src/core/applications/add.ts +++ b/src/core/applications/add.ts @@ -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'; @@ -44,20 +49,20 @@ export async function addApplications({ config, }: AddApplicationOptions): Promise { 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), }, }; } diff --git a/src/core/applications/remove.ts b/src/core/applications/remove.ts index 17ed791..9068690 100644 --- a/src/core/applications/remove.ts +++ b/src/core/applications/remove.ts @@ -1,4 +1,4 @@ -import { emphasize, getGroupConfig } from '../utils'; +import { emphasize, getGroupConfig, normalizePaths } from '../utils'; import { VersionGuardConfig } from '../config'; import { VersionGuardError } from '../errors'; @@ -6,15 +6,18 @@ 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}`, @@ -27,7 +30,7 @@ export function removeApplication({ [groupName]: { ...groupConfig, applications: groupConfig.applications.filter( - path => !relativePaths.includes(path), + path => !normalizedPaths.includes(path), ), }, }; diff --git a/src/core/utils.ts b/src/core/utils.ts index 7298b41..f6d82ef 100644 --- a/src/core/utils.ts +++ b/src/core/utils.ts @@ -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'; @@ -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; +}