From 93bd3e9cf0b1f6084bf191ac07c70a98a0c856e6 Mon Sep 17 00:00:00 2001 From: Quan Nguyen Ba Date: Mon, 11 Nov 2024 14:21:02 +0700 Subject: [PATCH 1/2] create project with the latest template --- packages/cli/src/actions/new.action.ts | 31 +++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/actions/new.action.ts b/packages/cli/src/actions/new.action.ts index 22e3838..c405e3f 100644 --- a/packages/cli/src/actions/new.action.ts +++ b/packages/cli/src/actions/new.action.ts @@ -5,6 +5,7 @@ import { cpSync, mkdirSync, readFileSync, + rmSync, unlinkSync, writeFileSync, } from 'fs' @@ -50,7 +51,7 @@ export default async function newAction( const destDir = path.join(process.cwd(), projectName) console.log('Generating MBC cqrs serverless application in', destDir) mkdirSync(destDir, { recursive: true }) - cpSync(path.join(__dirname, '../../templates'), destDir, { recursive: true }) + useTemplate(destDir) usePackageVersion(destDir, packageVersion, projectName) @@ -75,6 +76,33 @@ export default async function newAction( console.log(logs.toString()) } +function useTemplate(destDir: string) { + if (isLatestCli()) { + cpSync(path.join(__dirname, '../../templates'), destDir, { + recursive: true, + }) + } else { + execSync('npm i @mbc-cqrs-serverless/cli', { cwd: destDir }) + cpSync( + path.join(destDir, 'node_modules/@mbc-cqrs-serverless/cli/templates'), + destDir, + { recursive: true }, + ) + rmSync(path.join(destDir, 'node_modules'), { + recursive: true, + }) + } +} + +function isLatestCli() { + const latestVersion = getPackageVersion('@mbc-cqrs-serverless/cli', true)[0] + const packageJson = JSON.parse( + readFileSync(path.join(__dirname, '../../package.json')).toString(), + ) + const curVersion = packageJson.version + return latestVersion === curVersion +} + function usePackageVersion( destDir: string, packageVersion: string, @@ -114,6 +142,7 @@ function getPackageVersion(packageName: string, isLatest = false): string[] { export let exportsForTesting = { usePackageVersion, getPackageVersion, + isLatestCli, } if (process.env.NODE_ENV !== 'test') { exportsForTesting = undefined From 57441db2f5b17b58c076c639ee3eddbbaa7aa458 Mon Sep 17 00:00:00 2001 From: Quan Nguyen Ba Date: Mon, 11 Nov 2024 14:22:02 +0700 Subject: [PATCH 2/2] test the latest template for cli --- .../new.action.is-latest-version.spec.ts | 46 +++++++++++++++++++ packages/cli/src/actions/new.action.spec.ts | 8 ++-- 2 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 packages/cli/src/actions/new.action.is-latest-version.spec.ts diff --git a/packages/cli/src/actions/new.action.is-latest-version.spec.ts b/packages/cli/src/actions/new.action.is-latest-version.spec.ts new file mode 100644 index 0000000..4af88d4 --- /dev/null +++ b/packages/cli/src/actions/new.action.is-latest-version.spec.ts @@ -0,0 +1,46 @@ +import { execSync } from 'child_process' + +import { exportsForTesting } from './new.action' + +const { isLatestCli } = exportsForTesting + +jest.mock('child_process', () => ({ + execSync: jest.fn(), +})) + +jest.mock('fs', () => ({ + readFileSync: jest.fn(() => JSON.stringify({ version: '1.0.0' })), +})) +jest.mock('path', () => ({ + join: jest.fn(() => '/mocked/path/to/package.json'), +})) + +describe('isLatestCli', () => { + const mockExecSync = execSync as jest.MockedFunction + + afterEach(() => { + jest.clearAllMocks() + }) + + it('should return true if the current version matches the latest version', () => { + const mockLatestVersion = ['1.0.0'] + mockExecSync.mockReturnValue(Buffer.from(`${mockLatestVersion}\n`)) + + // Run the function + const result = isLatestCli() + + // Assert that the result is true + expect(result).toBe(true) + }) + + it('should return false if the current version does not match the latest version', () => { + const mockLatestVersion = ['1.2.3'] + mockExecSync.mockReturnValue(Buffer.from(`${mockLatestVersion}\n`)) + + // Run the function + const result = isLatestCli() + + // Assert that the result is true + expect(result).toBe(false) + }) +}) diff --git a/packages/cli/src/actions/new.action.spec.ts b/packages/cli/src/actions/new.action.spec.ts index 9806fd4..3e08cac 100644 --- a/packages/cli/src/actions/new.action.spec.ts +++ b/packages/cli/src/actions/new.action.spec.ts @@ -16,7 +16,7 @@ jest.mock('fs', () => ({ unlinkSync: jest.fn(), writeFileSync: jest.fn(), readFileSync: jest.fn(() => - JSON.stringify({ dependencies: {}, devDependencies: {} }), + JSON.stringify({ version: '1.2.3', dependencies: {}, devDependencies: {} }), ), })) @@ -32,7 +32,8 @@ describe('newAction', () => { const projectName = 'test-project' const latestVersion = '1.2.3' mockExecSync - .mockReturnValueOnce(Buffer.from(latestVersion)) + .mockReturnValueOnce(Buffer.from(latestVersion)) // latest core + .mockReturnValueOnce(Buffer.from(latestVersion)) // latest cli .mockReturnValue(Buffer.from('')) await newAction(`${projectName}`, {}, mockCommand) @@ -63,7 +64,8 @@ describe('newAction', () => { const version = '1.0.0' const mockVersions = ['1.0.0', '1.1.0', '1.2.0'] mockExecSync - .mockReturnValueOnce(Buffer.from(JSON.stringify(mockVersions))) + .mockReturnValueOnce(Buffer.from(JSON.stringify(mockVersions))) // list version core + .mockReturnValueOnce(Buffer.from('1.2.3')) // latest cli .mockReturnValue(Buffer.from('')) await newAction(`${projectName}@${version}`, {}, mockCommand)