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

add export-template command to cli #10331

Merged
merged 10 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add export-template command to cli
  • Loading branch information
markkaylor committed May 31, 2021
commit 8beb4b821b5bba4bdc7738cae9213dd5f1972c1e
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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep some naming convention here: {kind}:{action}

.arguments('<name>')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can put that directly in command('command <directory>')

.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'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to copy the config ? this could favor some config leaking by mistakes if this isn't required I would avoid it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reason is to copy the config/functions/bootstrap.js to get the seed script. I could target the config/functions directory instead or the bootstrap.js file directly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the point but a user that would be using this command would most likely not have a seed script. The looks like a command that is only needed in the expansion squad :) I think this should be in another cli actually :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok yeah I see what you are saying. Originally I was thinking as an internal tool, but I do think it would be great to expose this to the community so we can encourage them to make templates/starters. cc @remidej @malgamves

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love the idea!! This would be very useful for a lot of people should it be an option in the CLI!! Plus not to mention it would take the friction out of creating templates/starters. Def for opening it up to everyone


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}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should just use the parameter as a directory directly with a ../ or a prefix so you can select the folder you want the template to go to.

await createTemplate(templatePath);
// Copy content from current Strapi project to template directory
await copyContent(templatePath);
};