diff --git a/packages/cli/src/commands/new/new.ts b/packages/cli/src/commands/new/new.ts index 2733b0be9b..7dda08626b 100644 --- a/packages/cli/src/commands/new/new.ts +++ b/packages/cli/src/commands/new/new.ts @@ -23,98 +23,114 @@ import { isMonoRepo } from '@backstage/cli-node'; import { paths } from '../../lib/paths'; import { assertError } from '@backstage/errors'; import { Task } from '../../lib/tasks'; +import defaultTemplates from '../../../templates/alpha/all-default-templates'; -function parseOptions(optionStrings: string[]): Record { - const options: Record = {}; - - for (const str of optionStrings) { - const [key] = str.split('=', 1); - const value = str.slice(key.length + 1); - if (!key || str[key.length] !== '=') { - throw new Error( - `Invalid option '${str}', must be of the format =`, - ); - } - options[key] = value; - } - - return options; +interface TemplateLocation { + id: string; + target: string; } -export default async (opts: OptionValues) => { - const factory = await FactoryRegistry.interactiveSelect(opts.select); - - const providedOptions = parseOptions(opts.option); - const options = await FactoryRegistry.populateOptions( - factory, - providedOptions, - ); - - let defaultVersion = '0.1.0'; - if (opts.baseVersion) { - defaultVersion = opts.baseVersion; - } else { - const lernaVersion = await fs - .readJson(paths.resolveTargetRoot('lerna.json')) - .then(pkg => pkg.version) - .catch(() => undefined); - if (lernaVersion) { - defaultVersion = lernaVersion; - } - } - - const tempDirs = new Array(); - async function createTemporaryDirectory(name: string): Promise { - const dir = await fs.mkdtemp(joinPath(os.tmpdir(), name)); - tempDirs.push(dir); - return dir; - } - - const license = opts.license ?? 'Apache-2.0'; - - let modified = false; - try { - await factory.create(options, { - isMonoRepo: await isMonoRepo(), - defaultVersion, - license, - scope: opts.scope?.replace(/^@/, ''), - npmRegistry: opts.npmRegistry, - private: Boolean(opts.private), - createTemporaryDirectory, - markAsModified() { - modified = true; - }, - }); - - Task.log(); - Task.log(`🎉 Successfully created ${factory.name}`); - Task.log(); - } catch (error) { - assertError(error); - Task.error(error.message); - - if (modified) { - Task.log('It seems that something went wrong in the creation process 🤔'); - Task.log(); - Task.log( - 'We have left the changes that were made intact in case you want to', - ); - Task.log( - 'continue manually, but you can also revert the changes and try again.', - ); - - Task.error(`🔥 Failed to create ${factory.name}!`); - } - } finally { - for (const dir of tempDirs) { - try { - await fs.remove(dir); - } catch (error) { - console.error( - `Failed to remove temporary directory '${dir}', ${error}`, - ); +async function readCliConfig( + cliConfig: + | { + defaults?: boolean; + templates?: TemplateLocation[]; + globals?: Record; } - } + | undefined, +) { + let templates: TemplateLocation[] = []; + const cliTemplates = cliConfig?.templates; + + if (!cliConfig || cliConfig?.defaults) { + templates = defaultTemplates; } + if (cliTemplates?.length) { + cliTemplates.forEach((template: TemplateLocation) => { + templates.push({ + id: template.id, + target: template.target, + }); + }); + } + return { + templates, + globals: { ...cliConfig?.globals }, + }; +} + +export default async () => { + const pkgJson = await fs.readJson(paths.resolveTargetRoot('package.json')); + const cliConfig = pkgJson.backstage?.cli; + + const { templates, globals } = await readCliConfig(cliConfig); + console.log(templates, globals); + + // let defaultVersion = '0.1.0'; + // if (opts.baseVersion) { + // defaultVersion = opts.baseVersion; + // } else { + // const lernaVersion = await fs + // .readJson(paths.resolveTargetRoot('lerna.json')) + // .then(pkg => pkg.version) + // .catch(() => undefined); + // if (lernaVersion) { + // defaultVersion = lernaVersion; + // } + // } + + // const tempDirs = new Array(); + // async function createTemporaryDirectory(name: string): Promise { + // const dir = await fs.mkdtemp(joinPath(os.tmpdir(), name)); + // tempDirs.push(dir); + // return dir; + // } + + // const license = opts.license ?? 'Apache-2.0'; + + // let modified = false; + // try { + // await factory.create(options, { + // isMonoRepo: await isMonoRepo(), + // defaultVersion, + // license, + // scope: opts.scope?.replace(/^@/, ''), + // npmRegistry: opts.npmRegistry, + // private: Boolean(opts.private), + // createTemporaryDirectory, + // markAsModified() { + // modified = true; + // }, + // }); + + // Task.log(); + // Task.log(`🎉 Successfully created ${factory.name}`); + // Task.log(); + // } catch (error) { + // assertError(error); + // Task.error(error.message); + + // if (modified) { + // Task.log('It seems that something went wrong in the creation process 🤔'); + // Task.log(); + // Task.log( + // 'We have left the changes that were made intact in case you want to', + // ); + // Task.log( + // 'continue manually, but you can also revert the changes and try again.', + // ); + + // Task.error(`🔥 Failed to create ${factory.name}!`); + // } + // } finally { + // for (const dir of tempDirs) { + // try { + // await fs.remove(dir); + // } catch (error) { + // console.error( + // `Failed to remove temporary directory '${dir}', ${error}`, + // ); + // } + // } + // } };