Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLI]: create project with the latest template #42

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/cli/src/actions/new.action.is-latest-version.spec.ts
Original file line number Diff line number Diff line change
@@ -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<typeof execSync>

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)
})
})
8 changes: 5 additions & 3 deletions packages/cli/src/actions/new.action.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {} }),
),
}))

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 30 additions & 1 deletion packages/cli/src/actions/new.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
cpSync,
mkdirSync,
readFileSync,
rmSync,
unlinkSync,
writeFileSync,
} from 'fs'
Expand Down Expand Up @@ -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)

Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down