From 067a2011c03b1e3e9d164844d594e4f332982f12 Mon Sep 17 00:00:00 2001 From: Karl-Philipp Wulfert Date: Fri, 5 Jul 2019 13:21:50 +0200 Subject: [PATCH] feat: add function and task to get version of used dependency Fixes #20 --- src/cli.ts | 15 ++++++++- src/tasks/get-used-version.ts | 61 +++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/tasks/get-used-version.ts diff --git a/src/cli.ts b/src/cli.ts index 411d1931..f74005ee 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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 [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); diff --git a/src/tasks/get-used-version.ts b/src/tasks/get-used-version.ts new file mode 100644 index 00000000..5a6af62f --- /dev/null +++ b/src/tasks/get-used-version.ts @@ -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 . + */ +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 { + 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 { + 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]; +}