-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
Changes from 1 commit
8beb4b8
e652a53
c2d438b
a6eb49b
e71d573
c25e212
5bfbf33
aad0515
eec4aa2
c318c3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can put that directly in |
||
.description('Export project as Strapi template') | ||
.action(getLocalScript('exportTemplate')); | ||
|
||
program.parseAsync(process.argv); |
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']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only reason is to copy the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}; |
There was a problem hiding this comment.
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}