Add semver template check

This commit is contained in:
Rémi de Juvigny 2021-10-25 12:30:53 +02:00
parent a85d7afcf7
commit 3f4fb48a41
2 changed files with 23 additions and 10 deletions

View File

@ -5,6 +5,7 @@ const path = require('path');
const fse = require('fs-extra');
const _ = require('lodash');
const chalk = require('chalk');
const semver = require('semver');
const { getTemplatePackageInfo, downloadNpmTemplate } = require('./fetch-npm-template');
// Specify all the files and directories a template can have
@ -14,16 +15,7 @@ const allowedTemplateContents = {
'README.md': allowFile,
'.env.example': allowFile,
'package.json': allowFile,
src: {
'index.js': allowFile,
'bootstrap.js': allowFile,
admin: allowChildren,
api: allowChildren,
components: allowChildren,
middlewares: allowChildren,
policies: allowChildren,
plugins: allowChildren,
},
src: allowChildren,
data: allowChildren,
database: allowChildren,
public: allowChildren,
@ -55,6 +47,9 @@ module.exports = async function mergeTemplate(scope, rootPath) {
templatePath = downloadNpmTemplate(templatePackageInfo, templateParentPath);
}
// Make sure the template is compatible with this version of strapi
checkTemplateCompat(rootPath, scope.strapiVersion);
// Make sure the downloaded template matches the required format
const { templateConfig } = await checkTemplateRootStructure(templatePath, scope);
await checkTemplateContentsStructure(path.resolve(templatePath, 'template'));
@ -69,6 +64,22 @@ module.exports = async function mergeTemplate(scope, rootPath) {
}
};
function checkTemplateCompat({ rootPath, strapiVersion, templatePackageInfo }) {
const packageJSON = require(path.resolve(rootPath, 'package.json'));
const compatibleStrapiRange = packageJSON.strapi;
// Throw error if not compatible
const isCompatible = semver.satisfies(strapiVersion, compatibleStrapiRange);
if (!isCompatible) {
const { name, version } = templatePackageInfo;
throw new Error(`
The template ${chalk.green(
`${name}@${version}`
)} is not compatible with Strapi version ${strapiVersion}.
It will only work with Strapi versions in the range ${chalk.green(compatibleStrapiRange)}.
`);
}
}
/**
* Make sure the template has the required top-level structure
* @param {string} templatePath - Path of the locally downloaded template

View File

@ -22,6 +22,8 @@
"node-fetch": "^2.6.1",
"node-machine-id": "^1.1.10",
"ora": "^5.4.0",
"semver": "7.3.5",
"tar": "6.1.11",
"uuid": "^3.3.2"
},
"scripts": {