-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c078fb8
commit 196ced5
Showing
4 changed files
with
160 additions
and
75 deletions.
There are no files selected for viewing
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
92 changes: 92 additions & 0 deletions
92
packages/strapi/lib/commands/__tests__/export-template.test.js
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,92 @@ | ||
'use strict'; | ||
jest.mock('fs-extra', () => ({ | ||
ensureDir: jest.fn(() => Promise.resolve()), | ||
copy: jest.fn(() => Promise.resolve()), | ||
pathExists: jest.fn(() => Promise.resolve()), | ||
})); | ||
|
||
const { resolve, join } = require('path'); | ||
const fse = require('fs-extra'); | ||
const inquirer = require('inquirer'); | ||
|
||
const exportTemplate = require('../export-template'); | ||
|
||
describe('export:template command', () => { | ||
beforeEach(() => { | ||
jest.spyOn(console, 'log').mockImplementation(() => {}); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('creates a new template directory', async () => { | ||
fse.pathExists.mockReturnValue(false); | ||
const directory = '../test-dir'; | ||
const templatePath = resolve(directory); | ||
const contentPath = join(templatePath, 'template'); | ||
|
||
await exportTemplate(directory); | ||
|
||
expect(fse.pathExists).toHaveBeenCalledWith(contentPath); | ||
expect(fse.ensureDir).toHaveBeenCalledWith(contentPath); | ||
}); | ||
|
||
it.each(['api', 'components', 'config/functions/bootstrap.js', 'data'])( | ||
'copies folder %s', | ||
async item => { | ||
const directory = '../test-dir'; | ||
const templatePath = resolve(directory); | ||
const contentPath = join(templatePath, 'template'); | ||
|
||
await exportTemplate(directory); | ||
|
||
expect(fse.copy).toHaveBeenCalledWith(join(process.cwd(), item), join(contentPath, item)); | ||
} | ||
); | ||
|
||
describe('handles prompt input', () => { | ||
it('updates directory if confirmed', async () => { | ||
fse.pathExists.mockReturnValue(true); | ||
const mockInquiry = jest | ||
.spyOn(inquirer, 'prompt') | ||
.mockImplementationOnce(() => ({ confirm: true })); | ||
const directory = '../test-dir'; | ||
const templatePath = resolve(directory); | ||
const contentPath = join(templatePath, 'template'); | ||
|
||
await exportTemplate(directory); | ||
|
||
expect(fse.pathExists).toHaveBeenCalledWith(contentPath); | ||
expect(mockInquiry).toHaveBeenLastCalledWith( | ||
expect.objectContaining({ message: expect.any(String), name: 'confirm', type: 'confirm' }) | ||
); | ||
expect(fse.ensureDir).toHaveBeenCalled(); | ||
expect(fse.copy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('exits if not confirmed', async () => { | ||
fse.pathExists.mockReturnValue(true); | ||
jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
const mockInquiry = jest | ||
.spyOn(inquirer, 'prompt') | ||
.mockImplementationOnce(() => ({ confirm: false })); | ||
|
||
const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => { | ||
throw new Error('exit'); | ||
}); | ||
const directory = '../test-dir'; | ||
const templatePath = resolve(directory); | ||
const contentPath = join(templatePath, 'template'); | ||
|
||
await exportTemplate(directory).catch(err => { | ||
expect(err).toEqual(new Error('exit')); | ||
}); | ||
|
||
expect(fse.pathExists).toHaveBeenCalledWith(contentPath); | ||
expect(mockInquiry).toHaveBeenLastCalledWith( | ||
expect.objectContaining({ message: expect.any(String), name: 'confirm', type: 'confirm' }) | ||
); | ||
expect(mockExit).toHaveBeenCalledWith(0); | ||
expect(fse.ensureDir).not.toHaveBeenCalled(); | ||
expect(fse.copy).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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,65 @@ | ||
'use strict'; | ||
|
||
const { resolve, join, basename } = require('path'); | ||
const fse = require('fs-extra'); | ||
const chalk = require('chalk'); | ||
const inquirer = require('inquirer'); | ||
|
||
// All directories that a template could need | ||
const TEMPLATE_CONTENT = ['api', 'components', 'config/functions/bootstrap.js', 'data']; | ||
|
||
async function createTemplate(templatePath) { | ||
// Get path to template directory: strapi-template-<name>/template | ||
const contentPath = join(templatePath, 'template'); | ||
let successMessage = 'create'; | ||
|
||
// Check if the correct template directory structure exists | ||
const exists = await fse.pathExists(contentPath); | ||
const templateBase = basename(templatePath); | ||
|
||
if (exists) { | ||
// Confirm the user wants to update the existing template | ||
const inquiry = await inquirer.prompt({ | ||
type: 'confirm', | ||
name: 'confirm', | ||
message: `${chalk.yellow(templateBase)} already exists. Do you want to replace it?`, | ||
}); | ||
|
||
if (!inquiry.confirm) { | ||
process.exit(0); | ||
} | ||
|
||
successMessage = 'update'; | ||
} | ||
|
||
// Create/update the template | ||
await fse.ensureDir(contentPath); | ||
console.log(`${chalk.cyan(successMessage)}: ${templatePath}`); | ||
} | ||
|
||
async function copyContent(templatePath) { | ||
const contentPath = join(templatePath, 'template'); | ||
|
||
TEMPLATE_CONTENT.forEach(async item => { | ||
try { | ||
await fse.copy(join(process.cwd(), item), join(contentPath, item)); | ||
const templateBase = basename(templatePath); | ||
const currentProjectBase = basename(process.cwd()); | ||
console.log( | ||
`${chalk.green( | ||
'success' | ||
)}: copy ${currentProjectBase}/${item} => ${templateBase}/template/${item}` | ||
); | ||
} catch (error) { | ||
console.error(`${chalk.red('error')}: ${error.message}`); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = async function exportTemplate(directory) { | ||
const dir = directory.startsWith('.') ? directory : `../${directory}`; | ||
const templatePath = resolve(dir); | ||
|
||
await createTemplate(templatePath); | ||
await copyContent(templatePath); | ||
}; |
This file was deleted.
Oops, something went wrong.