From bc94014e03251d85c241649b5e65637d492f391b Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 6 Apr 2020 16:57:53 +0200 Subject: [PATCH] packages/cli: added version helper --- .../cli/src/commands/create-app/createApp.ts | 2 +- .../commands/create-plugin/createPlugin.ts | 6 ++--- packages/cli/src/helpers/version.ts | 26 +++++++++++++++++++ packages/cli/src/index.ts | 6 ++--- 4 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 packages/cli/src/helpers/version.ts diff --git a/packages/cli/src/commands/create-app/createApp.ts b/packages/cli/src/commands/create-app/createApp.ts index ddbbf8e4f2..826f9866d2 100644 --- a/packages/cli/src/commands/create-app/createApp.ts +++ b/packages/cli/src/commands/create-app/createApp.ts @@ -23,6 +23,7 @@ import { resolve as resolvePath } from 'path'; import os from 'os'; import { Task, templatingTask } from 'helpers/tasks'; import { paths } from 'helpers/paths'; +import { version } from 'helpers/version'; const exec = promisify(execCb); async function checkExists(rootDir: string, name: string) { @@ -108,7 +109,6 @@ export default async () => { const templateDir = paths.resolveOwn('templates/default-app'); const tempDir = resolvePath(os.tmpdir(), answers.name); const appDir = resolvePath(paths.targetDir, answers.name); - const version = require(paths.resolveOwn('package.json')).version; Task.log(); Task.log('Creating the app...'); diff --git a/packages/cli/src/commands/create-plugin/createPlugin.ts b/packages/cli/src/commands/create-plugin/createPlugin.ts index 7f05519cd2..7efb29d270 100644 --- a/packages/cli/src/commands/create-plugin/createPlugin.ts +++ b/packages/cli/src/commands/create-plugin/createPlugin.ts @@ -27,6 +27,7 @@ import { getCodeownersFilePath, } from './lib/codeowners'; import { paths } from 'helpers/paths'; +import { version } from 'helpers/version'; import { Task, templatingTask } from 'helpers/tasks'; const exec = promisify(execCb); @@ -86,7 +87,7 @@ const addExportStatement = async ( export async function addPluginDependencyToApp( rootDir: string, pluginName: string, - version: string, + versionStr: string, ) { const pluginPackage = `@backstage/plugin-${pluginName}`; const packageFilePath = 'packages/app/package.json'; @@ -103,7 +104,7 @@ export async function addPluginDependencyToApp( ); } - dependencies[pluginPackage] = `^${version}`; + dependencies[pluginPackage] = `^${versionStr}`; packageFileJson.dependencies = sortObjectByKeys(dependencies); const newContents = `${JSON.stringify(packageFileJson, null, 2)}\n`; @@ -223,7 +224,6 @@ export default async () => { const templateDir = paths.resolveOwn('templates/default-plugin'); const tempDir = resolvePath(os.tmpdir(), answers.id); const pluginDir = paths.resolveTargetRoot('plugins', answers.id); - const version = require(paths.resolveOwn('package.json')).version; const ownerIds = parseOwnerIds(answers.owner); Task.log(); diff --git a/packages/cli/src/helpers/version.ts b/packages/cli/src/helpers/version.ts new file mode 100644 index 0000000000..24734b87cc --- /dev/null +++ b/packages/cli/src/helpers/version.ts @@ -0,0 +1,26 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import fs from 'fs-extra'; +import { paths } from './paths'; + +export function findVersion() { + const pkgContent = fs.readFileSync(paths.resolveOwn('package.json'), 'utf8'); + return JSON.parse(pkgContent).version; +} + +export const version = findVersion(); +export const isDev = fs.pathExistsSync(paths.resolveOwn('src')); diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 4811d34a7a..ca39c1ac3b 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -16,12 +16,10 @@ import program from 'commander'; import chalk from 'chalk'; -import { exitWithError } from './helpers/errors'; -import { paths } from './helpers/paths'; +import { exitWithError } from 'helpers/errors'; +import { version } from 'helpers/version'; const main = (argv: string[]) => { - const version = require(paths.resolveOwn('package.json')).version; - program.name('backstage-cli').version(version); program