diff --git a/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.test.ts b/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.test.ts index bb47f3e19a..ce8a34a6ed 100644 --- a/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.test.ts +++ b/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.test.ts @@ -23,7 +23,7 @@ import type { ActionContext } from '@backstage/plugin-scaffolder-node'; import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; import { JsonObject } from '@backstage/types'; import { join } from 'path'; -import { Logger } from 'winston'; +import { Writable } from 'stream'; import { createFetchCookiecutterAction } from './cookiecutter'; const executeShellCommand = jest.fn(); @@ -168,7 +168,7 @@ describe('fetch:cookiecutter', () => { join(mockTmpDir, 'template'), '--verbose', ], - logger: expect.any(Logger), + logStream: expect.any(Writable), }), ); }); @@ -189,6 +189,7 @@ describe('fetch:cookiecutter', () => { }, workingDir: '/input', envVars: { HOME: '/tmp' }, + logStream: expect.any(Writable), }), ); }); diff --git a/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts b/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts index 5f7f91daab..269630817c 100644 --- a/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts +++ b/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts @@ -30,7 +30,7 @@ import { JsonObject, JsonValue } from '@backstage/types'; import commandExists from 'command-exists'; import fs from 'fs-extra'; import path, { resolve as resolvePath } from 'path'; -import { Logger } from 'winston'; +import { PassThrough, Writable } from 'stream'; import { examples } from './cookiecutter.examples'; export class CookiecutterRunner { @@ -57,14 +57,14 @@ export class CookiecutterRunner { public async run({ workspacePath, values, - logger, + logStream, imageName, templateDir, templateContentsDir, }: { workspacePath: string; values: JsonObject; - logger: Logger; + logStream: Writable; imageName?: string; templateDir: string; templateContentsDir: string; @@ -99,7 +99,7 @@ export class CookiecutterRunner { await executeShellCommand({ command: 'cookiecutter', args: ['--no-input', '-o', intermediateDir, templateDir, '--verbose'], - logger, + logStream, }); } else { if (this.containerRunner === undefined) { @@ -116,6 +116,7 @@ export class CookiecutterRunner { // Set the home directory inside the container as something that applications can // write to, otherwise they will just fail trying to write to / envVars: { HOME: '/tmp' }, + logStream, }); } @@ -246,10 +247,15 @@ export function createFetchCookiecutterAction(options: { _extensions: ctx.input.extensions, }; + const logStream = new PassThrough(); + logStream.on('data', chunk => { + ctx.logger.info(chunk.toString()); + }); + // Will execute the template in ./template and put the result in ./result await cookiecutter.run({ workspacePath: workDir, - logger: ctx.logger, + logStream, values: values, imageName: ctx.input.imageName, templateDir: templateDir, diff --git a/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/index.test.ts b/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/index.test.ts index 3ea3e01bef..c2d3ead6bb 100644 --- a/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/index.test.ts +++ b/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/index.test.ts @@ -35,7 +35,7 @@ import { ScmIntegrations } from '@backstage/integration'; import { fetchContents } from '@backstage/plugin-scaffolder-node'; import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; import { resolve as resolvePath } from 'path'; -import { Logger } from 'winston'; +import { Writable } from 'stream'; import { createFetchRailsAction } from './index'; describe('fetch:rails', () => { @@ -106,7 +106,7 @@ describe('fetch:rails', () => { expect(mockRailsTemplater.run).toHaveBeenCalledWith({ workspacePath: mockContext.workspacePath, - logger: expect.any(Logger), + logStream: expect.any(Writable), values: mockContext.input.values, }); }); @@ -122,7 +122,7 @@ describe('fetch:rails', () => { expect(mockRailsTemplater.run).toHaveBeenCalledWith({ workspacePath: mockContext.workspacePath, - logger: expect.any(Logger), + logStream: expect.any(Writable), values: { ...mockContext.input.values, imageName: 'foo/rails-custom-image', diff --git a/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/index.ts b/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/index.ts index 223aeb19a2..a063ce0a5b 100644 --- a/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/index.ts +++ b/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/index.ts @@ -26,6 +26,7 @@ import fs from 'fs-extra'; import { UrlReaderService } from '@backstage/backend-plugin-api'; import { resolve as resolvePath } from 'path'; +import { PassThrough } from 'stream'; import { examples } from './index.examples'; import { RailsNewRunner } from './railsNewRunner'; @@ -218,10 +219,15 @@ export function createFetchRailsAction(options: { throw new Error(`Image ${imageName} is not allowed`); } + const logStream = new PassThrough(); + logStream.on('data', chunk => { + ctx.logger.info(chunk.toString()); + }); + // Will execute the template in ./template and put the result in ./result await templateRunner.run({ workspacePath: workDir, - logger: ctx.logger, + logStream, values: { ...ctx.input.values, imageName }, }); diff --git a/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/railsNewRunner.test.ts b/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/railsNewRunner.test.ts index 3abbd24ace..c53f368516 100644 --- a/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/railsNewRunner.test.ts +++ b/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/railsNewRunner.test.ts @@ -30,7 +30,7 @@ jest.mock( import { ContainerRunner } from '@backstage/backend-common'; import { createMockDirectory } from '@backstage/backend-test-utils'; import path from 'path'; -import { Logger } from 'winston'; +import { PassThrough } from 'stream'; import { RailsNewRunner } from './railsNewRunner'; describe('Rails Templater', () => { @@ -47,7 +47,7 @@ describe('Rails Templater', () => { describe('when running on docker', () => { it('should run the correct bindings for the volumes', async () => { - const logger = new Logger(); + const logStream = new PassThrough(); const values = { owner: 'angeliski', storePath: 'https://github.com/angeliski/rails-project', @@ -65,7 +65,7 @@ describe('Rails Templater', () => { await templater.run({ workspacePath: mockDir.path, values, - logger, + logStream, }); expect(containerRunner.runContainer).toHaveBeenCalledWith({ @@ -78,11 +78,12 @@ describe('Rails Templater', () => { [path.join(mockDir.path, 'intermediate')]: '/output', }, workingDir: '/input', + logStream: logStream, }); }); it('should use the provided imageName', async () => { - const logger = new Logger(); + const logStream = new PassThrough(); const values = { owner: 'angeliski', storePath: 'https://github.com/angeliski/rails-project', @@ -100,7 +101,7 @@ describe('Rails Templater', () => { await templater.run({ workspacePath: mockDir.path, values, - logger, + logStream, }); expect(containerRunner.runContainer).toHaveBeenCalledWith( @@ -111,7 +112,7 @@ describe('Rails Templater', () => { }); it('should pass through the streamer to the run docker helper', async () => { - const logger = new Logger(); + const stream = new PassThrough(); const values = { owner: 'angeliski', @@ -130,7 +131,7 @@ describe('Rails Templater', () => { await templater.run({ workspacePath: mockDir.path, values, - logger, + logStream: stream, }); expect(containerRunner.runContainer).toHaveBeenCalledWith({ @@ -143,11 +144,12 @@ describe('Rails Templater', () => { [path.join(mockDir.path, 'intermediate')]: '/output', }, workingDir: '/input', + logStream: stream, }); }); it('update the template path to correct location', async () => { - const logger = new Logger(); + const logStream = new PassThrough(); const values = { owner: 'angeliski', storePath: 'https://github.com/angeliski/rails-project', @@ -166,7 +168,7 @@ describe('Rails Templater', () => { await templater.run({ workspacePath: mockDir.path, values, - logger, + logStream, }); expect(containerRunner.runContainer).toHaveBeenCalledWith({ @@ -184,13 +186,14 @@ describe('Rails Templater', () => { [path.join(mockDir.path, 'intermediate')]: '/output', }, workingDir: '/input', + logStream: logStream, }); }); }); describe('when rails is available', () => { it('use the binary', async () => { - const logger = new Logger(); + const stream = new PassThrough(); const values = { owner: 'angeliski', @@ -210,7 +213,7 @@ describe('Rails Templater', () => { await templater.run({ workspacePath: mockDir.path, values, - logger, + logStream: stream, }); expect(executeShellCommand).toHaveBeenCalledWith({ @@ -219,12 +222,12 @@ describe('Rails Templater', () => { 'new', path.join(mockDir.path, 'intermediate', 'rails-project'), ]), - logger, + logStream: stream, }); }); it('update the template path to correct location', async () => { - const logger = new Logger(); + const stream = new PassThrough(); const values = { owner: 'angeliski', @@ -245,7 +248,7 @@ describe('Rails Templater', () => { await templater.run({ workspacePath: mockDir.path, values, - logger, + logStream: stream, }); expect(executeShellCommand).toHaveBeenCalledWith({ @@ -256,14 +259,14 @@ describe('Rails Templater', () => { '--template', path.join(mockDir.path, './something.rb'), ]), - logger, + logStream: stream, }); }); }); describe('when nothing was generated', () => { it('throws an error', async () => { - const logger = new Logger(); + const stream = new PassThrough(); mockDir.setContent({ intermediate: {}, @@ -279,7 +282,7 @@ describe('Rails Templater', () => { name: 'rails-project', imageName: 'foo/rails-custom-image', }, - logger, + logStream: stream, }), ).rejects.toThrow(/No data generated by rails/); }); diff --git a/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/railsNewRunner.ts b/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/railsNewRunner.ts index 906c2e6404..fe2470eef8 100644 --- a/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/railsNewRunner.ts +++ b/plugins/scaffolder-backend-module-rails/src/actions/fetch/rails/railsNewRunner.ts @@ -20,7 +20,7 @@ import { JsonObject } from '@backstage/types'; import commandExists from 'command-exists'; import fs from 'fs-extra'; import path from 'path'; -import { Logger } from 'winston'; +import { Writable } from 'stream'; import { railsArgumentResolver, RailsRunOptions, @@ -36,11 +36,11 @@ export class RailsNewRunner { public async run({ workspacePath, values, - logger, + logStream, }: { workspacePath: string; values: JsonObject; - logger: Logger; + logStream: Writable; }): Promise { const intermediateDir = path.join(workspacePath, 'intermediate'); await fs.ensureDir(intermediateDir); @@ -71,7 +71,7 @@ export class RailsNewRunner { `${intermediateDir}${path.sep}${name}`, ...arrayExtraArguments, ], - logger, + logStream, }); } else { if (!imageName) { @@ -96,6 +96,7 @@ export class RailsNewRunner { // Set the home directory inside the container as something that applications can // write to, otherwise they will just fail trying to write to / envVars: { HOME: '/tmp' }, + logStream, }); }