-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from mbc-net/feat/cli-version-flag
[CLI]: specify framework version for project creation
- Loading branch information
Showing
7 changed files
with
274 additions
and
52 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/cli/src/actions/new.action.get-package-version.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { execSync } from 'child_process' | ||
|
||
import { exportsForTesting } from './new.action' | ||
|
||
const { getPackageVersion } = exportsForTesting | ||
|
||
jest.mock('child_process', () => ({ | ||
execSync: jest.fn(), | ||
})) | ||
|
||
describe('getPackageVersion', () => { | ||
const mockExecSync = execSync as jest.MockedFunction<typeof execSync> | ||
const packageName = '@mbc-cqrs-serverless/core' | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('should return the latest version when isLatest is true', () => { | ||
const mockLatestVersion = '1.2.3' | ||
mockExecSync.mockReturnValue(Buffer.from(`${mockLatestVersion}\n`)) | ||
|
||
const result = getPackageVersion(packageName, true) | ||
|
||
expect(mockExecSync).toHaveBeenCalledWith( | ||
`npm view ${packageName} dist-tags.latest`, | ||
) | ||
expect(result).toEqual([mockLatestVersion]) | ||
}) | ||
|
||
it('should return all versions when isLatest is false', () => { | ||
const mockVersions = ['1.0.0', '1.1.0', '1.2.0'] | ||
mockExecSync.mockReturnValue(Buffer.from(JSON.stringify(mockVersions))) | ||
|
||
const result = getPackageVersion(packageName, false) | ||
|
||
expect(mockExecSync).toHaveBeenCalledWith( | ||
`npm view ${packageName} versions --json`, | ||
) | ||
expect(result).toEqual(mockVersions) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,111 @@ | ||
import { faker } from '@faker-js/faker' | ||
import { copyFileSync, readFileSync, unlinkSync } from 'fs' | ||
import { execSync } from 'child_process' | ||
import { Command } from 'commander' | ||
import { copyFileSync, cpSync, mkdirSync } from 'fs' | ||
import path from 'path' | ||
|
||
import { exportsForTesting } from './new.action' | ||
import newAction, { exportsForTesting } from './new.action' | ||
|
||
const { useLatestPackageVersion } = exportsForTesting | ||
jest.mock('child_process', () => ({ | ||
execSync: jest.fn(), | ||
})) | ||
|
||
// create testcase for useLatestPackageVersion function in new.action.ts file | ||
describe('useLatestPackageVersion', () => { | ||
const fname = path.join(__dirname, 'package.json') | ||
const packageJson = JSON.parse( | ||
readFileSync(path.join(__dirname, '../../package.json')).toString(), | ||
) | ||
jest.mock('fs', () => ({ | ||
copyFileSync: jest.fn(), | ||
cpSync: jest.fn(), | ||
mkdirSync: jest.fn(), | ||
unlinkSync: jest.fn(), | ||
writeFileSync: jest.fn(), | ||
readFileSync: jest.fn(() => | ||
JSON.stringify({ dependencies: {}, devDependencies: {} }), | ||
), | ||
})) | ||
|
||
describe('newAction', () => { | ||
const mockExecSync = execSync as jest.MockedFunction<typeof execSync> | ||
const mockCommand = new Command().name('new') // Mock command with name 'new' | ||
|
||
beforeEach(() => { | ||
copyFileSync(path.join(__dirname, '../../templates/package.json'), fname) | ||
jest.clearAllMocks() | ||
}) | ||
|
||
afterEach(() => { | ||
unlinkSync(fname) | ||
}) | ||
it('should generate a project with the latest version when version is not specified', async () => { | ||
const projectName = 'test-project' | ||
const latestVersion = '1.2.3' | ||
mockExecSync | ||
.mockReturnValueOnce(Buffer.from(latestVersion)) | ||
.mockReturnValue(Buffer.from('')) | ||
|
||
it('it should update deps', () => { | ||
useLatestPackageVersion(__dirname) | ||
const tplPackageJson = JSON.parse(readFileSync(fname).toString()) | ||
await newAction(`${projectName}`, {}, mockCommand) | ||
|
||
expect(packageJson.devDependencies['@mbc-cqrs-serverless/core']).toBe( | ||
tplPackageJson.dependencies['@mbc-cqrs-serverless/core'], | ||
expect(execSync).toHaveBeenCalledWith( | ||
'npm view @mbc-cqrs-serverless/core dist-tags.latest', | ||
) | ||
expect(mkdirSync).toHaveBeenCalledWith( | ||
path.join(process.cwd(), projectName), | ||
{ recursive: true }, | ||
) | ||
expect(packageJson.version).toBe( | ||
tplPackageJson.devDependencies['@mbc-cqrs-serverless/cli'], | ||
expect(cpSync).toHaveBeenCalledWith( | ||
path.join(__dirname, '../../templates'), | ||
path.join(process.cwd(), projectName), | ||
{ recursive: true }, | ||
) | ||
expect(copyFileSync).toHaveBeenCalledTimes(2) // For .gitignore and .env.local | ||
expect(mockExecSync).toHaveBeenCalledWith('git init', { | ||
cwd: path.join(process.cwd(), projectName), | ||
}) | ||
expect(mockExecSync).toHaveBeenCalledWith('npm i', { | ||
cwd: path.join(process.cwd(), projectName), | ||
}) | ||
}) | ||
|
||
it('it should not update name', () => { | ||
const { name } = JSON.parse(readFileSync(fname).toString()) | ||
it('should use a specific version if specified', async () => { | ||
const projectName = 'test-project' | ||
const version = '1.0.0' | ||
const mockVersions = ['1.0.0', '1.1.0', '1.2.0'] | ||
mockExecSync | ||
.mockReturnValueOnce(Buffer.from(JSON.stringify(mockVersions))) | ||
.mockReturnValue(Buffer.from('')) | ||
|
||
useLatestPackageVersion(__dirname) | ||
const tplPackageJson = JSON.parse(readFileSync(fname).toString()) | ||
await newAction(`${projectName}@${version}`, {}, mockCommand) | ||
|
||
expect(name).toBe(tplPackageJson.name) | ||
expect(execSync).toHaveBeenCalledWith( | ||
'npm view @mbc-cqrs-serverless/core versions --json', | ||
) | ||
expect(mkdirSync).toHaveBeenCalledWith( | ||
path.join(process.cwd(), projectName), | ||
{ recursive: true }, | ||
) | ||
expect(cpSync).toHaveBeenCalledWith( | ||
path.join(__dirname, '../../templates'), | ||
path.join(process.cwd(), projectName), | ||
{ recursive: true }, | ||
) | ||
expect(copyFileSync).toHaveBeenCalledTimes(2) // For .gitignore and .env.local | ||
expect(mockExecSync).toHaveBeenCalledWith('git init', { | ||
cwd: path.join(process.cwd(), projectName), | ||
}) | ||
expect(mockExecSync).toHaveBeenCalledWith('npm i', { | ||
cwd: path.join(process.cwd(), projectName), | ||
}) | ||
}) | ||
|
||
it('it should not update name with empty name', () => { | ||
const { name } = JSON.parse(readFileSync(fname).toString()) | ||
|
||
useLatestPackageVersion(__dirname, '') | ||
const tplPackageJson = JSON.parse(readFileSync(fname).toString()) | ||
it('should throw an error for an invalid version', async () => { | ||
const projectName = 'test-project' | ||
const invalidVersion = '2.0.0' | ||
const mockVersions = ['1.0.0', '1.1.0', '1.2.0'] | ||
mockExecSync.mockReturnValueOnce(Buffer.from(JSON.stringify(mockVersions))) | ||
|
||
expect(name).toBe(tplPackageJson.name) | ||
}) | ||
const consoleSpy = jest.spyOn(console, 'log').mockImplementation() | ||
|
||
it('it should update name', () => { | ||
const name = faker.word.sample() | ||
useLatestPackageVersion(__dirname, name) | ||
const tplPackageJson = JSON.parse(readFileSync(fname).toString()) | ||
await newAction(`${projectName}@${invalidVersion}`, {}, mockCommand) | ||
|
||
expect(name).toBe(tplPackageJson.name) | ||
expect(execSync).toHaveBeenCalledWith( | ||
'npm view @mbc-cqrs-serverless/core versions --json', | ||
) | ||
expect(consoleSpy).toHaveBeenCalledWith( | ||
'The specified package version does not exist. Please chose a valid version!\n', | ||
mockVersions, | ||
) | ||
consoleSpy.mockRestore() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.