cli/new: rename types to use PortableTemplate prefix

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2025-02-07 16:10:49 +01:00
parent e4a1a2d8db
commit c08a9ab25f
11 changed files with 45 additions and 40 deletions
@@ -15,7 +15,7 @@
*/
import inquirer from 'inquirer';
import { NewConfig } from '../types';
import { PortableTemplateConfig } from '../types';
import { collectTemplateParams } from './collectTemplateParams';
describe('collectTemplateParams', () => {
@@ -24,7 +24,7 @@ describe('collectTemplateParams', () => {
isUsingDefaultTemplates: false,
templatePointers: [],
globals: {},
} satisfies NewConfig,
} satisfies PortableTemplateConfig,
template: {
id: 'test',
templatePath: '/test',
@@ -17,13 +17,13 @@
import inquirer from 'inquirer';
import { getCodeownersFilePath } from '../../codeowners';
import { paths } from '../../paths';
import { NewConfig } from '../types';
import { PortableTemplateConfig } from '../types';
import { customPrompt, getPromptsForRole, ownerPrompt } from './prompts';
import { NewTemplate } from '../types';
import { PortableTemplate } from '../types';
type CollectTemplateParamsOptions = {
config: NewConfig;
template: NewTemplate;
config: PortableTemplateConfig;
template: PortableTemplate;
prefilledParams: Record<string, string | number | boolean>;
};
@@ -15,7 +15,7 @@
*/
import { DistinctQuestion } from 'inquirer';
import { NewTemplatePrompt, TemplateRole } from '../types';
import { PortableTemplatePrompt, PortableTemplateRole } from '../types';
import { parseOwnerIds } from '../../codeowners';
export function namePrompt(): DistinctQuestion {
@@ -66,7 +66,9 @@ export function moduleIdIdPrompt(): DistinctQuestion {
};
}
export function getPromptsForRole(role: TemplateRole): Array<DistinctQuestion> {
export function getPromptsForRole(
role: PortableTemplateRole,
): Array<DistinctQuestion> {
switch (role) {
case 'web-library':
case 'node-library':
@@ -106,7 +108,7 @@ export function ownerPrompt(): DistinctQuestion {
};
}
export function customPrompt(prompt: NewTemplatePrompt): DistinctQuestion {
export function customPrompt(prompt: PortableTemplatePrompt): DistinctQuestion {
return {
type: 'input',
name: prompt.id,
@@ -27,12 +27,12 @@ import { createDirName, resolvePackageName } from './utils';
import { runAdditionalActions } from './additionalActions';
import { executePluginPackageTemplate } from './executePluginPackageTemplate';
import { TemporaryDirectoryManager } from './TemporaryDirectoryManager';
import { NewConfig } from '../types';
import { NewTemplate } from '../types';
import { PortableTemplateConfig } from '../types';
import { PortableTemplate } from '../types';
type ExecuteNewTemplateOptions = {
config: NewConfig;
template: NewTemplate;
config: PortableTemplateConfig;
template: PortableTemplate;
params: Record<string, string | number | boolean>;
};
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { resolvePackageName, createDirName, Options } from './utils';
import { NewTemplate } from '../types';
import { PortableTemplate } 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 NewTemplate,
{ backendModulePrefix: true } as PortableTemplate,
{
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 NewTemplate,
{ backendModulePrefix: true } as PortableTemplate,
{
id: 'foo',
moduleId: '',
@@ -105,13 +105,16 @@ describe('createDirName', () => {
it('should append the suffix value if one is provided', () => {
expect(
createDirName({ suffix: 'foo' } as NewTemplate, { id: 'bar' } as Options),
createDirName(
{ suffix: 'foo' } as PortableTemplate,
{ id: 'bar' } as Options,
),
).toEqual('bar-foo');
});
it('should return id if neither backendModulePrefix nor suffix is specified', () => {
expect(createDirName({} as NewTemplate, { id: 'foo' } as Options)).toEqual(
'foo',
);
expect(
createDirName({} as PortableTemplate, { id: 'foo' } as Options),
).toEqual('foo');
});
});
+2 -2
View File
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { NewTemplate } from '../types';
import { PortableTemplate } from '../types';
export const resolvePackageName = (options: {
baseName: string;
@@ -38,7 +38,7 @@ export const resolvePackageName = (options: {
return plugin ? `backstage-plugin-${baseName}` : baseName;
};
export function createDirName(template: NewTemplate, options: Options) {
export function createDirName(template: PortableTemplate, options: Options) {
if (!options.id) {
throw new Error(`id prompt is mandatory for all cli templates`);
}
@@ -17,7 +17,7 @@
import fs from 'fs-extra';
import { paths } from '../../paths';
import { defaultTemplates } from '../defaultTemplates';
import { NewConfig } from '../types';
import { PortableTemplateConfig } from '../types';
import { z } from 'zod';
import { fromZodError } from 'zod-validation-error';
import { ForwardedError } from '@backstage/errors';
@@ -60,7 +60,7 @@ type LoadConfigOptions = {
export async function loadConfig(
options: LoadConfigOptions = {},
): Promise<NewConfig> {
): Promise<PortableTemplateConfig> {
const pkgPath =
options.packagePath ?? paths.resolveTargetRoot('package.json');
const pkgJson = await fs.readJson(pkgPath);
@@ -20,8 +20,8 @@ import { resolve as resolvePath } from 'path';
import { dirname } from 'node:path';
import { parse as parseYaml } from 'yaml';
import { paths } from '../../paths';
import { NewTemplatePointer, TEMPLATE_ROLES } from '../types';
import { NewTemplate } from '../types';
import { PortableTemplatePointer, TEMPLATE_ROLES } from '../types';
import { PortableTemplate } from '../types';
import { ForwardedError } from '@backstage/errors';
import { fromZodError } from 'zod-validation-error';
@@ -48,7 +48,7 @@ const templateDefinitionSchema = z
export async function loadTemplate({
id,
target,
}: NewTemplatePointer): Promise<NewTemplate> {
}: PortableTemplatePointer): Promise<PortableTemplate> {
if (target.match(/https?:\/\//)) {
throw new Error('Remote templates are not supported yet');
}
@@ -14,13 +14,13 @@
* limitations under the License.
*/
import { NewConfig } from '../types';
import { PortableTemplateConfig } from '../types';
import inquirer from 'inquirer';
import { withLogCollector } from '@backstage/test-utils';
import { selectTemplateInteractively } from './selectTemplateInteractively';
describe('selectTemplateInteractively', () => {
const mockConfig: NewConfig = {
const mockConfig: PortableTemplateConfig = {
isUsingDefaultTemplates: false,
templatePointers: [
{ id: 'template1', target: 'path/to/template1' },
@@ -15,12 +15,12 @@
*/
import inquirer from 'inquirer';
import { NewConfig, NewTemplatePointer } from '../types';
import { PortableTemplateConfig, PortableTemplatePointer } from '../types';
export async function selectTemplateInteractively(
config: NewConfig,
config: PortableTemplateConfig,
preselectedTemplateId?: string,
): Promise<NewTemplatePointer> {
): Promise<PortableTemplatePointer> {
let selectedId = preselectedTemplateId;
if (config.isUsingDefaultTemplates && selectedId === 'plugin') {
+8 -8
View File
@@ -14,11 +14,11 @@
* limitations under the License.
*/
export type NewConfig = {
export type PortableTemplateConfig = {
/**
* The pointers to templates that can be used.
*/
templatePointers: NewTemplatePointer[];
templatePointers: PortableTemplatePointer[];
/**
* Whether the default set of templates are being used or not.
@@ -33,12 +33,12 @@ export type NewConfig = {
};
};
export type NewTemplatePointer = {
export type PortableTemplatePointer = {
id: string;
target: string;
};
export type NewTemplatePrompt = {
export type PortableTemplatePrompt = {
id: string;
prompt: string;
validate?: string;
@@ -58,14 +58,14 @@ export const TEMPLATE_ROLES = [
'backend-plugin-module',
] as const;
export type TemplateRole = (typeof TEMPLATE_ROLES)[number];
export type PortableTemplateRole = (typeof TEMPLATE_ROLES)[number];
export interface NewTemplate {
export interface PortableTemplate {
id: string;
description?: string;
templatePath: string;
targetPath: string;
role: TemplateRole;
prompts?: NewTemplatePrompt[];
role: PortableTemplateRole;
prompts?: PortableTemplatePrompt[];
additionalActions?: string[];
}