mirror of
https://github.com/strapi/strapi.git
synced 2025-12-13 07:55:33 +00:00
Support local templates
This commit is contained in:
parent
44d2cc0038
commit
114d69f60b
@ -18,11 +18,11 @@ function getPackageInfo(packageName) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} template - The name of the template as provided by the user. Can be a shorthand.
|
* @param {string} template - The name of the template as provided by the user.
|
||||||
* @returns {Object} - The full name of the template package's name on npm
|
* @returns {Object} - The full name of the template package's name on npm
|
||||||
*/
|
*/
|
||||||
async function getTemplatePackageInfo(template) {
|
async function getTemplatePackageInfo(template) {
|
||||||
// Check if template is a s horthand
|
// Check if template is a shorthand
|
||||||
try {
|
try {
|
||||||
const longhand = `@strapi/template-${template}`;
|
const longhand = `@strapi/template-${template}`;
|
||||||
const packageInfo = getPackageInfo(longhand);
|
const packageInfo = getPackageInfo(longhand);
|
||||||
|
|||||||
@ -34,14 +34,24 @@ const allowedTemplateContents = {
|
|||||||
* @param {string} rootPath project path
|
* @param {string} rootPath project path
|
||||||
*/
|
*/
|
||||||
module.exports = async function mergeTemplate(scope, rootPath) {
|
module.exports = async function mergeTemplate(scope, rootPath) {
|
||||||
// Parse template info
|
let templatePath;
|
||||||
const templatePackageInfo = await getTemplatePackageInfo(scope.template);
|
let templateParentPath;
|
||||||
console.log(templatePackageInfo);
|
let templatePackageInfo = {};
|
||||||
console.log(`Installing ${chalk.yellow(templatePackageInfo.name)} template.`);
|
const isLocalTemplate = Boolean(scope.template.match(/^file:/));
|
||||||
|
|
||||||
// Download template repository to a temporary directory
|
if (isLocalTemplate) {
|
||||||
const templateParentPath = await fse.mkdtemp(path.join(os.tmpdir(), 'strapi-'));
|
// Template is a local directory
|
||||||
const templatePath = downloadNpmTemplate(templatePackageInfo, templateParentPath);
|
console.log('Installing local template.');
|
||||||
|
templatePath = path.resolve(rootPath, '..', scope.template.match(/^file:(.*)?$/)[1]);
|
||||||
|
} else {
|
||||||
|
// Template should be an npm package. Fetch template info
|
||||||
|
templatePackageInfo = await getTemplatePackageInfo(scope.template);
|
||||||
|
console.log(`Installing ${chalk.yellow(templatePackageInfo.name)} template.`);
|
||||||
|
|
||||||
|
// Download template repository to a temporary directory
|
||||||
|
templateParentPath = await fse.mkdtemp(path.join(os.tmpdir(), 'strapi-'));
|
||||||
|
templatePath = downloadNpmTemplate(templatePackageInfo, templateParentPath);
|
||||||
|
}
|
||||||
|
|
||||||
// Make sure the downloaded template matches the required format
|
// Make sure the downloaded template matches the required format
|
||||||
const { templateConfig } = await checkTemplateRootStructure(templatePath, scope);
|
const { templateConfig } = await checkTemplateRootStructure(templatePath, scope);
|
||||||
@ -51,8 +61,10 @@ module.exports = async function mergeTemplate(scope, rootPath) {
|
|||||||
await mergePackageJSON({ rootPath, templateConfig, templatePackageInfo });
|
await mergePackageJSON({ rootPath, templateConfig, templatePackageInfo });
|
||||||
await mergeFilesAndDirectories(rootPath, templatePath);
|
await mergeFilesAndDirectories(rootPath, templatePath);
|
||||||
|
|
||||||
// Delete the downloaded template repo
|
// Delete the template directory if it was downloaded
|
||||||
await fse.remove(templateParentPath);
|
if (!isLocalTemplate) {
|
||||||
|
await fse.remove(templateParentPath);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -182,7 +194,7 @@ async function checkTemplateContentsStructure(templateContentsPath) {
|
|||||||
* @param {Object} config.templatePackageInfo.name - The name of the template's package on npm
|
* @param {Object} config.templatePackageInfo.name - The name of the template's package on npm
|
||||||
* @param {Object} config.templatePackageInfo.version - The name of the template's package on npm
|
* @param {Object} config.templatePackageInfo.version - The name of the template's package on npm
|
||||||
*/
|
*/
|
||||||
async function mergePackageJSON({ rootPath, templateConfig, templatePackageName }) {
|
async function mergePackageJSON({ rootPath, templateConfig, templatePackageInfo }) {
|
||||||
// Import the package.json as an object
|
// Import the package.json as an object
|
||||||
const packageJSON = require(path.resolve(rootPath, 'package.json'));
|
const packageJSON = require(path.resolve(rootPath, 'package.json'));
|
||||||
|
|
||||||
@ -200,7 +212,9 @@ async function mergePackageJSON({ rootPath, templateConfig, templatePackageName
|
|||||||
const mergedConfig = _.merge(packageJSON, templateConfig.package);
|
const mergedConfig = _.merge(packageJSON, templateConfig.package);
|
||||||
|
|
||||||
// Add starter info to package.json
|
// Add starter info to package.json
|
||||||
_.set(mergedConfig, 'strapi.template', templatePackageName);
|
if (templatePackageInfo.name) {
|
||||||
|
_.set(mergedConfig, 'strapi.template', templatePackageInfo.name);
|
||||||
|
}
|
||||||
|
|
||||||
// Save the merged config as the new package.json
|
// Save the merged config as the new package.json
|
||||||
const packageJSONPath = path.join(rootPath, 'package.json');
|
const packageJSONPath = path.join(rootPath, 'package.json');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user