From ac04e0482eaa16348bff9698d388b9f27ec6c290 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 7 Feb 2025 14:28:28 +0100 Subject: [PATCH] cli/new: refactor types Signed-off-by: Patrik Oldsberg --- .../collection/collectTemplateParams.test.ts | 2 +- .../new/collection/collectTemplateParams.ts | 6 +- .../cli/src/lib/new/collection/prompts.ts | 12 ++-- .../cli/src/lib/new/config/loadNewConfig.ts | 2 +- packages/cli/src/lib/new/config/types.ts | 39 ------------- .../execution/executePluginPackageTemplate.ts | 12 +++- .../src/lib/new/execution/executeTemplate.ts | 6 +- .../cli/src/lib/new/execution/utils.test.ts | 10 ++-- packages/cli/src/lib/new/execution/utils.ts | 4 +- .../src/lib/new/loader/NewTemplateLoader.ts | 6 +- packages/cli/src/lib/new/types.ts | 55 ++++++++----------- 11 files changed, 60 insertions(+), 94 deletions(-) delete mode 100644 packages/cli/src/lib/new/config/types.ts diff --git a/packages/cli/src/lib/new/collection/collectTemplateParams.test.ts b/packages/cli/src/lib/new/collection/collectTemplateParams.test.ts index 5668273ff6..a4ca4f412d 100644 --- a/packages/cli/src/lib/new/collection/collectTemplateParams.test.ts +++ b/packages/cli/src/lib/new/collection/collectTemplateParams.test.ts @@ -15,7 +15,7 @@ */ import inquirer from 'inquirer'; -import { NewConfig } from '../config/types'; +import { NewConfig } from '../types'; import { collectTemplateParams } from './collectTemplateParams'; describe('collectTemplateParams', () => { diff --git a/packages/cli/src/lib/new/collection/collectTemplateParams.ts b/packages/cli/src/lib/new/collection/collectTemplateParams.ts index 6af95d511d..a9c7c92f3c 100644 --- a/packages/cli/src/lib/new/collection/collectTemplateParams.ts +++ b/packages/cli/src/lib/new/collection/collectTemplateParams.ts @@ -16,14 +16,14 @@ import { getCodeownersFilePath } from '../../codeowners'; import { paths } from '../../paths'; -import { NewConfig } from '../config/types'; +import { NewConfig } from '../types'; import { promptOptions } from './prompts'; -import { Template } from '../types'; +import { NewTemplate } from '../types'; import { Options } from '../execution/utils'; type CollectTemplateParamsOptions = { config: NewConfig; - template: Template; + template: NewTemplate; globals: Record; prefilledParams: Record; }; diff --git a/packages/cli/src/lib/new/collection/prompts.ts b/packages/cli/src/lib/new/collection/prompts.ts index 246c899a28..842bffb458 100644 --- a/packages/cli/src/lib/new/collection/prompts.ts +++ b/packages/cli/src/lib/new/collection/prompts.ts @@ -14,10 +14,14 @@ * limitations under the License. */ -import inquirer from 'inquirer'; -import { Prompt, ConfigurablePrompt } from '../types'; +import inquirer, { Answers, DistinctQuestion } from 'inquirer'; +import { NewTemplatePrompt } from '../types'; import { parseOwnerIds } from '../../codeowners'; +export type Prompt = DistinctQuestion & { + name: string; +}; + export function pluginIdPrompt(): Prompt<{ id: string }> { return { type: 'input', @@ -94,12 +98,12 @@ export async function promptOptions({ globals, codeOwnersFilePath, }: { - prompts: ConfigurablePrompt[]; + prompts: NewTemplatePrompt[]; globals: { [name in string]?: string | boolean | number }; codeOwnersFilePath: string | undefined; }): Promise> { const answers = await inquirer.prompt( - prompts.map((prompt: ConfigurablePrompt) => { + prompts.map((prompt: NewTemplatePrompt) => { if (typeof prompt === 'string') { switch (prompt) { case 'id': diff --git a/packages/cli/src/lib/new/config/loadNewConfig.ts b/packages/cli/src/lib/new/config/loadNewConfig.ts index 1e9f54e13e..1387f5b45c 100644 --- a/packages/cli/src/lib/new/config/loadNewConfig.ts +++ b/packages/cli/src/lib/new/config/loadNewConfig.ts @@ -17,7 +17,7 @@ import fs from 'fs-extra'; import { paths } from '../../paths'; import { defaultTemplates } from '../defaultTemplates'; -import { NewConfig } from './types'; +import { NewConfig } from '../types'; import { z } from 'zod'; import { fromZodError } from 'zod-validation-error'; import { ForwardedError } from '@backstage/errors'; diff --git a/packages/cli/src/lib/new/config/types.ts b/packages/cli/src/lib/new/config/types.ts deleted file mode 100644 index b4949b59a1..0000000000 --- a/packages/cli/src/lib/new/config/types.ts +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2025 The Backstage Authors - * - * 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. - */ - -export type NewConfig = { - /** - * The pointers to templates that can be used. - */ - templatePointers: NewTemplatePointer[]; - - /** - * Whether the default set of templates are being used or not. - */ - isUsingDefaultTemplates: boolean; - - /** - * Templating globals that should apply to all templates. - */ - globals: { - [KName in string]?: number | string | boolean; - }; -}; - -export type NewTemplatePointer = { - id: string; - target: string; -}; diff --git a/packages/cli/src/lib/new/execution/executePluginPackageTemplate.ts b/packages/cli/src/lib/new/execution/executePluginPackageTemplate.ts index 259d1ae928..6f7bf6b997 100644 --- a/packages/cli/src/lib/new/execution/executePluginPackageTemplate.ts +++ b/packages/cli/src/lib/new/execution/executePluginPackageTemplate.ts @@ -29,7 +29,17 @@ import { paths } from '../../paths'; import { Task } from '../../tasks'; import { Lockfile } from '../../versioning'; import { createPackageVersionProvider } from '../../version'; -import { CreateContext } from '../types'; + +export interface CreateContext { + /** Whether we are creating something in a monorepo or not */ + isMonoRepo: boolean; + + /** Creates a temporary directory. This will always be deleted after creation is done. */ + createTemporaryDirectory(name: string): Promise; + + /** Signal that the creation process got to a point where permanent modifications were made */ + markAsModified(): void; +} export async function executePluginPackageTemplate( ctx: CreateContext, diff --git a/packages/cli/src/lib/new/execution/executeTemplate.ts b/packages/cli/src/lib/new/execution/executeTemplate.ts index 319a74350a..5549a354cb 100644 --- a/packages/cli/src/lib/new/execution/executeTemplate.ts +++ b/packages/cli/src/lib/new/execution/executeTemplate.ts @@ -27,13 +27,13 @@ import { createDirName, resolvePackageName } from './utils'; import { runAdditionalActions } from './additionalActions'; import { executePluginPackageTemplate } from './executePluginPackageTemplate'; import { TemporaryDirectoryManager } from './TemporaryDirectoryManager'; -import { NewConfig } from '../config/types'; -import { Template } from '../types'; +import { NewConfig } from '../types'; +import { NewTemplate } from '../types'; import { Options } from './utils'; type ExecuteNewTemplateOptions = { config: NewConfig; - template: Template; + template: NewTemplate; params: Options; }; diff --git a/packages/cli/src/lib/new/execution/utils.test.ts b/packages/cli/src/lib/new/execution/utils.test.ts index a6fc7abdb5..8147e9ebbb 100644 --- a/packages/cli/src/lib/new/execution/utils.test.ts +++ b/packages/cli/src/lib/new/execution/utils.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ import { resolvePackageName, createDirName, Options } from './utils'; -import { Template } from '../types'; +import { NewTemplate } from '../types'; describe('resolvePackageName', () => { it('should generate correct name without scope', () => { @@ -82,7 +82,7 @@ describe('createDirName', () => { it('should return name in the backend-module format if backendModulePrefix is set to true', () => { expect( createDirName( - { backendModulePrefix: true } as Template, + { backendModulePrefix: true } as NewTemplate, { id: 'foo', moduleId: 'bar', @@ -94,7 +94,7 @@ describe('createDirName', () => { it('should throw an error if backendModulePrefix is configured as true but is missing moduleId', () => { expect(() => createDirName( - { backendModulePrefix: true } as Template, + { backendModulePrefix: true } as NewTemplate, { id: 'foo', moduleId: '', @@ -105,12 +105,12 @@ describe('createDirName', () => { it('should append the suffix value if one is provided', () => { expect( - createDirName({ suffix: 'foo' } as Template, { id: 'bar' } as Options), + createDirName({ suffix: 'foo' } as NewTemplate, { id: 'bar' } as Options), ).toEqual('bar-foo'); }); it('should return id if neither backendModulePrefix nor suffix is specified', () => { - expect(createDirName({} as Template, { id: 'foo' } as Options)).toEqual( + expect(createDirName({} as NewTemplate, { id: 'foo' } as Options)).toEqual( 'foo', ); }); diff --git a/packages/cli/src/lib/new/execution/utils.ts b/packages/cli/src/lib/new/execution/utils.ts index c9c1e08be8..3fb2a97047 100644 --- a/packages/cli/src/lib/new/execution/utils.ts +++ b/packages/cli/src/lib/new/execution/utils.ts @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { Template } from '../types'; +import { NewTemplate } from '../types'; export interface Options extends Record { id: string; @@ -49,7 +49,7 @@ export const resolvePackageName = (options: { return plugin ? `backstage-plugin-${baseName}` : baseName; }; -export function createDirName(template: Template, options: Options) { +export function createDirName(template: NewTemplate, options: Options) { if (!options.id) { throw new Error(`id prompt is mandatory for all cli templates`); } diff --git a/packages/cli/src/lib/new/loader/NewTemplateLoader.ts b/packages/cli/src/lib/new/loader/NewTemplateLoader.ts index 35adfff2fd..c7deb9114f 100644 --- a/packages/cli/src/lib/new/loader/NewTemplateLoader.ts +++ b/packages/cli/src/lib/new/loader/NewTemplateLoader.ts @@ -21,8 +21,8 @@ import { resolve as resolvePath } from 'path'; import { dirname } from 'node:path'; import { parse as parseYaml } from 'yaml'; import { paths } from '../../paths'; -import { NewConfig, NewTemplatePointer } from '../config/types'; -import { Template } from '../types'; +import { NewConfig, NewTemplatePointer } from '../types'; +import { NewTemplate } from '../types'; import { ForwardedError } from '@backstage/errors'; import { fromZodError } from 'zod-validation-error'; @@ -87,7 +87,7 @@ export class NewTemplateLoader { static async loadTemplate({ id, target, - }: NewTemplatePointer): Promise