Skip to content

Commit

Permalink
add export-template command to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
markkaylor committed May 17, 2021
1 parent 15c04d0 commit 074e5b7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/strapi/bin/strapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 230,11 @@ program
.option('-p, --password <password>', 'New password for the user')
.action(getLocalScript('admin-reset'));

// `$ strapi export-template <name>`
program
.command('export-template')
.arguments('<name>')
.description('Export project as Strapi template')
.action(getLocalScript('exportTemplate'));

program.parseAsync(process.argv);
71 changes: 71 additions & 0 deletions packages/strapi/lib/commands/exportTemplate.js
Original file line number Diff line number Diff line change
@@ -0,0 1,71 @@
'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 DIRECTORIES = ['api', 'components', 'config', 'data'];

async function createTemplate(templatePath) {
// Get path to template directory: strapi-template-<name>/template
const contentPath = join(templatePath, 'template');

try {
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}`);
} catch (error) {
console.error(`${chalk.red('error')}: ${error.message}`);
}
}

async function copyContent(templatePath) {
const contentPath = join(templatePath, 'template');

DIRECTORIES.forEach(async directory => {
try {
await fse.copy(join(process.cwd(), directory), join(contentPath, directory));

const templateBase = basename(templatePath);
const currentProjectBase = basename(process.cwd());
console.log(
`${chalk.green(
'success'
)}: copy ${currentProjectBase}/${directory} => ${templateBase}/template/${directory}`
);
} catch (error) {
console.error(`${chalk.red('error')}: ${error.message}`);
}
});
}

module.exports = async function exportTemplate(name) {
// Create the template directory
const templatePath = resolve(`../strapi-template-${name}`);
await createTemplate(templatePath);
// Copy content from current Strapi project to template directory
await copyContent(templatePath);
};

0 comments on commit 074e5b7

Please sign in to comment.