Skip to content

Commit

Permalink
fix(plugin-dependencies): update version only in dependencies and dev…
Browse files Browse the repository at this point in the history
…Dependencies
  • Loading branch information
Simon Mollweide committed Oct 31, 2019
1 parent 241fd77 commit 13b4368
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
12 changes: 10 additions & 2 deletions plugins/plugin-dependencies/src/server/index.ts
Expand Up @@ -19,6 +19,7 @@ import {
import { isVersionUpToDate } from './is-version-up-to-date';
import { getLatestVersion } from './latest-version';
import { detailed as getDetailedLernaPackages } from './lerna';
import { updateDependencyInPackage } from './update-dependency-in-package';
import { asyncForEach } from './utils';

export interface IOptions {
Expand Down Expand Up @@ -142,13 +143,20 @@ export class PluginDependencies

const version = await getLatestVersion(dependencyName, this.getCurrentVersion(dependencyName));

if (!version) {
await this.getAllDependencies();
this.send('data', this._foundDependencies);
this._isProcessing = false;
return;
}

await asyncForEach(this._packages, async (item) => {
const pkgPath = path.join(this.getPkgBasePath(item.path), 'package.json');
const pkg = await fs.readFile(pkgPath, 'utf8');
const pkg = JSON.parse(await fs.readFile(pkgPath, 'utf8'));

await fs.writeFile(
pkgPath,
pkg.replace(new RegExp(`("${dependencyName}" ?: ?")(~|\^)?([^"]*)(")`, 'g'), `$1$2${version}$4`)
JSON.stringify(updateDependencyInPackage(pkg, dependencyName, version), null, 2)
);
});

Expand Down
@@ -0,0 +1,31 @@
/// <reference types="@types/jest" />
import { updateDependencyInPackage } from './update-dependency-in-package';

const mockPackage = {
dependencies: {
'@dash4/server': '0.5.2',
},
devDependencies: {
'cross-env': '6.0.0',
},
};

describe('updateVersionInPackage', () => {
test('exists', () => {
expect(typeof updateDependencyInPackage).toBe('function');
});
test('update dependency', () => {
const result = updateDependencyInPackage(mockPackage, '@dash4/server', '0.5.3');
if (!result || !result.dependencies) {
return;
}
expect(result.dependencies['@dash4/server']).toBe('0.5.3');
});
test('update devDependency', () => {
const result = updateDependencyInPackage(mockPackage, 'cross-env', '6.0.1');
if (!result || !result.devDependency) {
return;
}
expect(result.devDependency['cross-env']).toBe('6.0.1');
});
});
@@ -0,0 +1,13 @@
import { IPackageJson } from '../shared-types';

export function updateDependencyInPackage(packageData: IPackageJson, dependencyName: string, newVersion: string) {
if (packageData.dependencies && packageData.dependencies[dependencyName]) {
packageData.dependencies[dependencyName] = newVersion;
}

if (packageData.devDependencies && packageData.devDependencies[dependencyName]) {
packageData.devDependencies[dependencyName] = newVersion;
}

return packageData;
}

0 comments on commit 13b4368

Please sign in to comment.