cli/new: refactor template selection
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -23,13 +23,14 @@ import { paths } from '../paths';
|
||||
import { Task } from '../tasks';
|
||||
import { addCodeownersEntry, getCodeownersFilePath } from '../codeowners';
|
||||
|
||||
import { templateSelector, verifyTemplate } from './templateSelector';
|
||||
import { verifyTemplate } from './templateSelector';
|
||||
import { promptOptions } from './prompts';
|
||||
import { populateOptions, createDirName, resolvePackageName } from './utils';
|
||||
import { runAdditionalActions } from './additionalActions';
|
||||
import { executePluginPackageTemplate } from './executeTemplate';
|
||||
import { TemporaryDirectoryManager } from './TemporaryDirectoryManager';
|
||||
import { loadNewConfig } from './config/loadNewConfig';
|
||||
import { NewTemplateLoader } from './loader/NewTemplateLoader';
|
||||
|
||||
export type CreateNewPackageOptions = {
|
||||
preselectedTemplateId?: string;
|
||||
@@ -47,8 +48,8 @@ export async function createNewPackage(options: CreateNewPackageOptions) {
|
||||
const newConfig = await loadNewConfig();
|
||||
|
||||
const template = verifyTemplate(
|
||||
await templateSelector(
|
||||
newConfig.templatePointers,
|
||||
await NewTemplateLoader.selectTemplateInteractively(
|
||||
newConfig,
|
||||
options.preselectedTemplateId,
|
||||
),
|
||||
);
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import inquirer from 'inquirer';
|
||||
import { NewConfig, NewTemplatePointer } from '../config/types';
|
||||
|
||||
export class NewTemplateLoader {
|
||||
static async selectTemplateInteractively(
|
||||
config: NewConfig,
|
||||
preselectedTemplateId?: string,
|
||||
): Promise<NewTemplatePointer> {
|
||||
let selectedId = preselectedTemplateId;
|
||||
|
||||
if (config.isUsingDefaultTemplates && selectedId === 'plugin') {
|
||||
console.warn(
|
||||
`DEPRECATION WARNING: The 'plugin' template is deprecated, use 'frontend-plugin' instead`,
|
||||
);
|
||||
selectedId = 'frontend-plugin';
|
||||
}
|
||||
|
||||
if (!selectedId) {
|
||||
const answers = await inquirer.prompt<{ id: string }>([
|
||||
{
|
||||
type: 'list',
|
||||
name: 'id',
|
||||
message: 'What do you want to create?',
|
||||
choices: config.templatePointers.map(t => t.id),
|
||||
},
|
||||
]);
|
||||
selectedId = answers.id;
|
||||
}
|
||||
|
||||
const template = config.templatePointers.find(t => t.id === selectedId);
|
||||
if (!template) {
|
||||
throw new Error(`Template '${selectedId}' not found`);
|
||||
}
|
||||
return template;
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import inquirer from 'inquirer';
|
||||
|
||||
import { dirname } from 'path';
|
||||
import { parse } from 'yaml';
|
||||
import fs from 'fs-extra';
|
||||
@@ -21,42 +21,6 @@ import fs from 'fs-extra';
|
||||
import { paths } from '../paths';
|
||||
import { Template, TemplateLocation } from './types';
|
||||
|
||||
import { defaultTemplates } from './defaultTemplates';
|
||||
|
||||
export async function templateSelector(
|
||||
templates: TemplateLocation[],
|
||||
selected?: string,
|
||||
): Promise<TemplateLocation> {
|
||||
if (selected) {
|
||||
let selectedId = selected;
|
||||
if (templates === defaultTemplates && selectedId === 'plugin') {
|
||||
console.warn(
|
||||
`DEPRECATION WARNING: The 'plugin' template is deprecated, use 'frontend-plugin' instead`,
|
||||
);
|
||||
selectedId = 'frontend-plugin';
|
||||
}
|
||||
const template = templates.find(t => t.id === selectedId);
|
||||
if (!template) {
|
||||
throw new Error(`Template '${selected}' not found`);
|
||||
}
|
||||
return template;
|
||||
}
|
||||
const answer = await inquirer.prompt<{ name: TemplateLocation }>([
|
||||
{
|
||||
type: 'list',
|
||||
name: 'name',
|
||||
message: 'What do you want to create?',
|
||||
choices: templates.map(template => {
|
||||
return {
|
||||
name: template.id,
|
||||
value: template,
|
||||
};
|
||||
}),
|
||||
},
|
||||
]);
|
||||
return answer.name;
|
||||
}
|
||||
|
||||
export function verifyTemplate({ id, target }: TemplateLocation): Template {
|
||||
if (target.startsWith('http')) {
|
||||
throw new Error('Remote templates are not supported yet');
|
||||
|
||||
Reference in New Issue
Block a user