feat: add function and task to get version of used dependency

Fixes #20
This commit is contained in:
Karl-Philipp Wulfert
2019-07-05 13:21:50 +02:00
parent 9d3dd97830
commit 067a2011c0
2 changed files with 75 additions and 1 deletions

View File

@@ -16,8 +16,10 @@ import {Api} from '@openstapps/gitlab-api';
import {Logger} from '@openstapps/logger';
import * as commander from 'commander';
import {existsSync, readFileSync} from 'fs';
import {join} from 'path';
import {join, resolve} from 'path';
import {cwd, stdout} from 'process';
import {GITLAB_API_URL} from './configuration';
import {getUsedVersionMajorMinor} from './tasks/get-used-version';
import {remind} from './tasks/remind';
import {tidy} from './tasks/tidy';
import {unlabel} from './tasks/unlabel';
@@ -63,6 +65,17 @@ commander
Logger.ok('Done!');
});
commander
.command('get-used-version <dependency> [path]')
.action(async (dependency, path) => {
let fallbackPath = cwd();
if (typeof path === 'string' && path.length > 0) {
fallbackPath = resolve(path);
}
stdout.write(await getUsedVersionMajorMinor(fallbackPath, dependency), process.exit);
});
commander
.parse(process.argv);

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {existsSync, PathLike} from 'fs';
import {join} from 'path';
import {readFilePromisified} from '../common';
/**
* Get used version of a dependency of a project referenced by a path
*
* @param path Path to a Node.js project directory
* @param dependency Dependency to get used version of
*/
export async function getUsedVersion(path: PathLike, dependency: string): Promise<string> {
if (!existsSync(join(path.toString(), 'package.json'))) {
throw new Error(`'package.json' does not exist in '${path}'. Not a Node.js project?`);
}
const buffer = await readFilePromisified(join(path.toString(), 'package.json'));
const content = buffer.toString();
const pkgJson = JSON.parse(content);
if (typeof pkgJson.dependencies !== 'object') {
throw new Error(`Project in '${path}' has no dependencies!`);
}
if (typeof pkgJson.dependencies[dependency] !== 'string') {
throw new Error(`Project in '${path}' does not depend on '${dependency}'.`);
}
return pkgJson.dependencies[dependency];
}
/**
* Get 'MAJOR.MINOR' part of a used version
*
* See [[getUsedVersion]].
*
* @param path see [[getUsedVersion]]
* @param dependency see [[getUsedVersion]]
*/
export async function getUsedVersionMajorMinor(path: PathLike, dependency: string): Promise<string> {
const versionMatch = (await getUsedVersion(path, dependency)).match(/([0-9]+\.[0-9]+)\.[0-9]+/);
if (versionMatch === null) {
throw new Error(`Used version of '${dependency}' of project in '${path}' could not be determined.`);
}
return versionMatch[1];
}