mirror of
https://github.com/strapi/strapi.git
synced 2025-11-14 17:19:01 +00:00
refactor
This commit is contained in:
parent
a6eb49b75d
commit
e71d573ffb
@ -8,54 +8,31 @@ const inquirer = require('inquirer');
|
|||||||
// All directories that a template could need
|
// All directories that a template could need
|
||||||
const TEMPLATE_CONTENT = ['api', 'components', 'config/functions/bootstrap.js', 'data'];
|
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');
|
* @param {string} templatePath Absolute path to template content directory
|
||||||
let successMessage = 'create';
|
* @param {string} rootBase Name of the root directory
|
||||||
|
*/
|
||||||
// Check if the correct template directory structure exists
|
async function copyContent(templatePath, rootBase) {
|
||||||
const exists = await fse.pathExists(contentPath);
|
for (const item of TEMPLATE_CONTENT) {
|
||||||
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 {
|
try {
|
||||||
await fse.copy(join(process.cwd(), item), join(contentPath, item));
|
await fse.copy(join(process.cwd(), item), join(templatePath, item));
|
||||||
const templateBase = basename(templatePath);
|
|
||||||
const currentProjectBase = basename(process.cwd());
|
const currentProjectBase = basename(process.cwd());
|
||||||
console.log(
|
console.log(
|
||||||
`${chalk.green(
|
`${chalk.green(
|
||||||
'success'
|
'success'
|
||||||
)}: copy ${currentProjectBase}/${item} => ${templateBase}/template/${item}`
|
)}: copy ${currentProjectBase}/${item} => ${rootBase}/template/${item}`
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`${chalk.red('error')}: ${error.message}`);
|
console.error(`${chalk.red('error')}: ${error.message}`);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} rootPath Absolute path to the root directory
|
||||||
|
*/
|
||||||
async function writeTemplateJson(rootPath) {
|
async function writeTemplateJson(rootPath) {
|
||||||
try {
|
try {
|
||||||
await fse.writeJSON(join(rootPath, 'template.json'), {});
|
await fse.writeJSON(join(rootPath, 'template.json'), {});
|
||||||
@ -65,22 +42,51 @@ async function writeTemplateJson(rootPath) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function configExists(templatePath) {
|
/**
|
||||||
const jsonConfig = await fse.pathExists(join(templatePath, 'template.json'));
|
*
|
||||||
const functionConfig = await fse.pathExists(join(templatePath, 'template.js'));
|
* @param {string} rootPath Absolute path to the root directory
|
||||||
|
* @returns boolean
|
||||||
|
*/
|
||||||
|
async function templateConfigExists(rootPath) {
|
||||||
|
const jsonConfig = await fse.pathExists(join(rootPath, 'template.json'));
|
||||||
|
const functionConfig = await fse.pathExists(join(rootPath, 'template.js'));
|
||||||
|
|
||||||
return jsonConfig || functionConfig;
|
return jsonConfig || functionConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = async function generateTemplate(directory) {
|
module.exports = async function generateTemplate(directory) {
|
||||||
const dir = directory.startsWith('.') ? directory : `../${directory}`;
|
const dir = directory.startsWith('.') ? directory : `../${directory}`;
|
||||||
const templatePath = resolve(dir);
|
const rootPath = resolve(dir);
|
||||||
|
|
||||||
await createTemplate(templatePath);
|
// Get path to template directory: <rootPath>/template
|
||||||
await copyContent(templatePath);
|
const templatePath = join(rootPath, 'template');
|
||||||
|
|
||||||
const exists = await configExists(templatePath);
|
// Check if the correct template directory structure exists
|
||||||
if (!exists) {
|
const exists = await fse.pathExists(templatePath);
|
||||||
await writeTemplateJson(templatePath);
|
const rootBase = basename(rootPath);
|
||||||
|
|
||||||
|
if (exists) {
|
||||||
|
// Confirm the user wants to update the existing template
|
||||||
|
const inquiry = await inquirer.prompt({
|
||||||
|
type: 'confirm',
|
||||||
|
name: 'confirm',
|
||||||
|
message: `${chalk.yellow(rootBase)} already exists. Do you want to replace it?`,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!inquiry.confirm) {
|
||||||
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create or replace the template content directory
|
||||||
|
await fse.ensureDir(templatePath);
|
||||||
|
// Copy the content
|
||||||
|
await copyContent(templatePath, rootBase);
|
||||||
|
// Create config file if it doesn't exist
|
||||||
|
const configExists = await templateConfigExists(rootPath);
|
||||||
|
if (!configExists) {
|
||||||
|
await writeTemplateJson(rootPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`${chalk.green('success')}: generated template at ${chalk.yellow(rootPath)}`);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user