From 664e76da46194d4546e8709cc39a437b9983a5dd Mon Sep 17 00:00:00 2001 From: Min Kim Date: Thu, 19 Dec 2024 17:22:50 -0500 Subject: [PATCH] Relocate templatingTask into executeTemplate Signed-off-by: Min Kim --- packages/cli/src/commands/new/new.ts | 3 - .../cli/src/lib/new/executeTemplate.test.ts | 63 ++++++++++++--- packages/cli/src/lib/new/executeTemplate.ts | 76 ++++++++++++++++++- packages/cli/src/lib/new/types.ts | 10 --- packages/cli/src/lib/tasks.test.ts | 63 --------------- packages/cli/src/lib/tasks.ts | 64 ---------------- 6 files changed, 126 insertions(+), 153 deletions(-) delete mode 100644 packages/cli/src/lib/tasks.test.ts diff --git a/packages/cli/src/commands/new/new.ts b/packages/cli/src/commands/new/new.ts index 9760a0ff84..230e528186 100644 --- a/packages/cli/src/commands/new/new.ts +++ b/packages/cli/src/commands/new/new.ts @@ -87,9 +87,6 @@ export default async () => { try { await executePluginPackageTemplate( { - private: options.private, - defaultVersion: options.baseVersion, - license: options.license, isMonoRepo: await isMonoRepo(), createTemporaryDirectory, markAsModified() { diff --git a/packages/cli/src/lib/new/executeTemplate.test.ts b/packages/cli/src/lib/new/executeTemplate.test.ts index f1affc5b24..e9879f8c23 100644 --- a/packages/cli/src/lib/new/executeTemplate.test.ts +++ b/packages/cli/src/lib/new/executeTemplate.test.ts @@ -22,17 +22,18 @@ import { mockPaths, } from './testUtils'; import { CreateContext } from './types'; -import { executePluginPackageTemplate } from './executeTemplate'; +import { + executePluginPackageTemplate, + templatingTask, +} from './executeTemplate'; import { createMockDirectory } from '@backstage/backend-test-utils'; -const mockDir = createMockDirectory(); - -mockPaths({ - ownDir: mockDir.resolve('own'), - targetRoot: mockDir.resolve('root'), -}); - describe('executePluginPackageTemplate', () => { + const mockDir = createMockDirectory(); + mockPaths({ + ownDir: mockDir.resolve('own'), + targetRoot: mockDir.resolve('root'), + }); afterEach(() => { jest.resetAllMocks(); }); @@ -82,7 +83,7 @@ some-package@^1.1.0: }, } as CreateContext, { - templateName: 'test-template', + templateDir: 'test-template', targetDir: mockDir.resolve('target'), values: { id: 'testing', @@ -122,3 +123,47 @@ some-package@^1.1.0: ).resolves.toBe('Hello {{id}}!'); }); }); + +describe('templatingTask', () => { + const mockDir = createMockDirectory(); + + it('should template a directory with mix of regular files and templates', async () => { + // Testing template directory + const tmplDir = 'test-tmpl'; + + // Temporary dest dir to write the template to + const destDir = 'test-dest'; + + // Files content + const testFileContent = 'testing'; + const testVersionFileContent = + "version: {{pluginVersion}} {{versionQuery 'mock-pkg'}}"; + + mockDir.setContent({ + [tmplDir]: { + sub: { + 'version.txt.hbs': testVersionFileContent, + }, + 'test.txt': testFileContent, + }, + [destDir]: {}, + }); + + await templatingTask( + mockDir.resolve(tmplDir), + mockDir.resolve(destDir), + { + pluginVersion: '0.0.0', + }, + () => '^0.1.2', + true, + ); + + await expect( + fs.readFile(mockDir.resolve(destDir, 'test.txt'), 'utf8'), + ).resolves.toBe(testFileContent); + await expect( + fs.readFile(mockDir.resolve(destDir, 'sub/version.txt'), 'utf8'), + ).resolves.toBe('version: 0.0.0 ^0.1.2'); + }); +}); diff --git a/packages/cli/src/lib/new/executeTemplate.ts b/packages/cli/src/lib/new/executeTemplate.ts index fa43ccd45e..131d6344c2 100644 --- a/packages/cli/src/lib/new/executeTemplate.ts +++ b/packages/cli/src/lib/new/executeTemplate.ts @@ -16,9 +16,17 @@ import fs from 'fs-extra'; import chalk from 'chalk'; -import { resolve as resolvePath, relative as relativePath } from 'path'; +import handlebars from 'handlebars'; +import recursive from 'recursive-readdir'; +import { + basename, + dirname, + resolve as resolvePath, + relative as relativePath, +} from 'path'; + import { paths } from '../paths'; -import { Task, templatingTask } from '../tasks'; +import { Task } from '../tasks'; import { Lockfile } from '../versioning'; import { createPackageVersionProvider } from '../version'; import { CreateContext } from './types'; @@ -31,7 +39,7 @@ export async function executePluginPackageTemplate( values: Record; }, ) { - const { targetDir, templateDir } = options; + const { targetDir, templateDir, values } = options; let lockfile: Lockfile | undefined; try { @@ -60,7 +68,7 @@ export async function executePluginPackageTemplate( await templatingTask( templateDir, tempDir, - options.values, + values, createPackageVersionProvider(lockfile), ctx.isMonoRepo, ); @@ -83,3 +91,63 @@ export async function executePluginPackageTemplate( ctx.markAsModified(); } + +export async function templatingTask( + templateDir: string, + destinationDir: string, + context: any, + versionProvider: (name: string, versionHint?: string) => string, + isMonoRepo: boolean, +) { + const files = await recursive(templateDir).catch(error => { + throw new Error(`Failed to read template directory: ${error.message}`); + }); + + for (const file of files) { + const destinationFile = file.replace(templateDir, destinationDir); + await fs.ensureDir(dirname(destinationFile)); + + if (file.endsWith('.hbs')) { + await Task.forItem('templating', basename(file), async () => { + const destination = destinationFile.replace(/\.hbs$/, ''); + + const template = await fs.readFile(file); + const compiled = handlebars.compile(template.toString(), { + strict: true, + }); + const contents = compiled( + { name: basename(destination), ...context }, + { + helpers: { + versionQuery(name: string, versionHint: string | unknown) { + return versionProvider( + name, + typeof versionHint === 'string' ? versionHint : undefined, + ); + }, + }, + }, + ); + + await fs.writeFile(destination, contents).catch(error => { + throw new Error( + `Failed to create file: ${destination}: ${error.message}`, + ); + }); + }); + } else { + if (isMonoRepo && file.match('tsconfig.json')) { + continue; + } + + await Task.forItem('copying', basename(file), async () => { + await fs.copyFile(file, destinationFile).catch(error => { + const destination = destinationFile; + throw new Error( + `Failed to copy file to ${destination} : ${error.message}`, + ); + }); + }); + } + } +} diff --git a/packages/cli/src/lib/new/types.ts b/packages/cli/src/lib/new/types.ts index bd11702c4f..82a13a8b2d 100644 --- a/packages/cli/src/lib/new/types.ts +++ b/packages/cli/src/lib/new/types.ts @@ -17,18 +17,8 @@ import { Answers, DistinctQuestion } from 'inquirer'; export interface CreateContext { - /** The package scope to use for new packages */ - scope?: string; - /** The NPM registry to use for new packages */ - npmRegistry?: string; - /** Whether new packages should be marked as private */ - private: boolean; /** Whether we are creating something in a monorepo or not */ isMonoRepo: boolean; - /** The default version to use for new packages */ - defaultVersion: string; - /** License to use for new packages */ - license: string; /** Creates a temporary directory. This will always be deleted after creation is done. */ createTemporaryDirectory(name: string): Promise; diff --git a/packages/cli/src/lib/tasks.test.ts b/packages/cli/src/lib/tasks.test.ts deleted file mode 100644 index 1119adb7f7..0000000000 --- a/packages/cli/src/lib/tasks.test.ts +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2020 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 fs from 'fs-extra'; -import { templatingTask } from './tasks'; -import { createMockDirectory } from '@backstage/backend-test-utils'; - -describe('templatingTask', () => { - const mockDir = createMockDirectory(); - - it('should template a directory with mix of regular files and templates', async () => { - // Testing template directory - const tmplDir = 'test-tmpl'; - - // Temporary dest dir to write the template to - const destDir = 'test-dest'; - - // Files content - const testFileContent = 'testing'; - const testVersionFileContent = - "version: {{pluginVersion}} {{versionQuery 'mock-pkg'}}"; - - mockDir.setContent({ - [tmplDir]: { - sub: { - 'version.txt.hbs': testVersionFileContent, - }, - 'test.txt': testFileContent, - }, - [destDir]: {}, - }); - - await templatingTask( - mockDir.resolve(tmplDir), - mockDir.resolve(destDir), - { - pluginVersion: '0.0.0', - }, - () => '^0.1.2', - true, - ); - - await expect( - fs.readFile(mockDir.resolve(destDir, 'test.txt'), 'utf8'), - ).resolves.toBe(testFileContent); - await expect( - fs.readFile(mockDir.resolve(destDir, 'sub/version.txt'), 'utf8'), - ).resolves.toBe('version: 0.0.0 ^0.1.2'); - }); -}); diff --git a/packages/cli/src/lib/tasks.ts b/packages/cli/src/lib/tasks.ts index 47a2a98af0..2922f34f35 100644 --- a/packages/cli/src/lib/tasks.ts +++ b/packages/cli/src/lib/tasks.ts @@ -15,12 +15,8 @@ */ import chalk from 'chalk'; -import fs from 'fs-extra'; -import handlebars from 'handlebars'; import ora from 'ora'; import { promisify } from 'util'; -import { basename, dirname } from 'path'; -import recursive from 'recursive-readdir'; import { exec as execCb } from 'child_process'; import { assertError } from '@backstage/errors'; @@ -95,63 +91,3 @@ export class Task { } } } - -export async function templatingTask( - templateDir: string, - destinationDir: string, - context: any, - versionProvider: (name: string, versionHint?: string) => string, - isMonoRepo: boolean, -) { - const files = await recursive(templateDir).catch(error => { - throw new Error(`Failed to read template directory: ${error.message}`); - }); - - for (const file of files) { - const destinationFile = file.replace(templateDir, destinationDir); - await fs.ensureDir(dirname(destinationFile)); - - if (file.endsWith('.hbs')) { - await Task.forItem('templating', basename(file), async () => { - const destination = destinationFile.replace(/\.hbs$/, ''); - - const template = await fs.readFile(file); - const compiled = handlebars.compile(template.toString(), { - strict: true, - }); - const contents = compiled( - { name: basename(destination), ...context }, - { - helpers: { - versionQuery(name: string, versionHint: string | unknown) { - return versionProvider( - name, - typeof versionHint === 'string' ? versionHint : undefined, - ); - }, - }, - }, - ); - - await fs.writeFile(destination, contents).catch(error => { - throw new Error( - `Failed to create file: ${destination}: ${error.message}`, - ); - }); - }); - } else { - if (isMonoRepo && file.match('tsconfig.json')) { - continue; - } - - await Task.forItem('copying', basename(file), async () => { - await fs.copyFile(file, destinationFile).catch(error => { - const destination = destinationFile; - throw new Error( - `Failed to copy file to ${destination} : ${error.message}`, - ); - }); - }); - } - } -}