mirror of
https://github.com/strapi/strapi.git
synced 2025-11-12 00:03:40 +00:00
Apply PR feedback
This commit is contained in:
parent
9c33487894
commit
0e76ce4af9
@ -31,9 +31,10 @@ function readStarterJson(filePath, starter) {
|
|||||||
/**
|
/**
|
||||||
* @param {string} rootPath - Path to the project directory
|
* @param {string} rootPath - Path to the project directory
|
||||||
* @param {string} projectName - Name of the project
|
* @param {string} projectName - Name of the project
|
||||||
|
* @param {boolean} useYarn - Use yarn instead of npm
|
||||||
*/
|
*/
|
||||||
async function initPackageJson(rootPath, projectName) {
|
async function initPackageJson(rootPath, projectName, useYarn) {
|
||||||
const packageManager = hasYarn() ? 'yarn --cwd' : 'npm run --prefix';
|
const packageManager = useYarn ? 'yarn --cwd' : 'npm run --prefix';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fse.writeJson(
|
await fse.writeJson(
|
||||||
@ -85,8 +86,13 @@ async function installWithLogs(path) {
|
|||||||
console.log(`Dependencies installed ${chalk.green('successfully')}.`);
|
console.log(`Dependencies installed ${chalk.green('successfully')}.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getStarterInfo(starter) {
|
/**
|
||||||
|
* @param {string} starter The name of the starter as provided by the user
|
||||||
|
* @param {boolean} useYarn Use yarn instead of npm
|
||||||
|
*/
|
||||||
|
async function getStarterInfo(starter, useYarn) {
|
||||||
const isLocalStarter = ['./', '../', '/'].some(filePrefix => starter.startsWith(filePrefix));
|
const isLocalStarter = ['./', '../', '/'].some(filePrefix => starter.startsWith(filePrefix));
|
||||||
|
|
||||||
let starterPath;
|
let starterPath;
|
||||||
let starterParentPath;
|
let starterParentPath;
|
||||||
let starterPackageInfo = {};
|
let starterPackageInfo = {};
|
||||||
@ -97,25 +103,31 @@ async function getStarterInfo(starter) {
|
|||||||
starterPath = resolve(starter);
|
starterPath = resolve(starter);
|
||||||
} else {
|
} else {
|
||||||
// Starter should be an npm package. Fetch starter info
|
// Starter should be an npm package. Fetch starter info
|
||||||
starterPackageInfo = await getStarterPackageInfo(starter);
|
starterPackageInfo = await getStarterPackageInfo(starter, useYarn);
|
||||||
console.log(`Installing ${chalk.yellow(starterPackageInfo.name)} starter.`);
|
console.log(`Installing ${chalk.yellow(starterPackageInfo.name)} starter.`);
|
||||||
|
|
||||||
// Download starter repository to a temporary directory
|
// Download starter repository to a temporary directory
|
||||||
starterParentPath = await fse.mkdtemp(join(os.tmpdir(), 'strapi-'));
|
starterParentPath = await fse.mkdtemp(join(os.tmpdir(), 'strapi-'));
|
||||||
starterPath = await downloadNpmStarter(starterPackageInfo, starterParentPath);
|
starterPath = await downloadNpmStarter(starterPackageInfo, starterParentPath, useYarn);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { isLocalStarter, starterPath, starterParentPath, starterPackageInfo };
|
return { isLocalStarter, starterPath, starterParentPath, starterPackageInfo };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Object} projectArgs - The arguments for create a project
|
* @param {Object} projectArgs - The arguments for create a project
|
||||||
* @param {string|null} projectArgs.projectName - The name/path of project
|
* @param {string|null} projectArgs.projectName - The name/path of project
|
||||||
* @param {string|null} projectArgs.starter - The npm package of the starter
|
* @param {string|null} projectArgs.starter - The npm package of the starter
|
||||||
* @param {Object} program - Commands for generating new application
|
* @param {Object} program - Commands for generating new application
|
||||||
*/
|
*/
|
||||||
module.exports = async function buildStarter({ projectName, starter }, program) {
|
module.exports = async function buildStarter({ projectName, starter }, program) {
|
||||||
const { isLocalStarter, starterPath, starterParentPath, starterPackageInfo } = getStarterInfo();
|
const hasYarnInstalled = hasYarn();
|
||||||
|
const {
|
||||||
|
isLocalStarter,
|
||||||
|
starterPath,
|
||||||
|
starterParentPath,
|
||||||
|
starterPackageInfo,
|
||||||
|
} = await getStarterInfo(starter, hasYarnInstalled);
|
||||||
|
|
||||||
// Project directory
|
// Project directory
|
||||||
const rootPath = resolve(projectName);
|
const rootPath = resolve(projectName);
|
||||||
@ -149,9 +161,13 @@ module.exports = async function buildStarter({ projectName, starter }, program)
|
|||||||
const generateStrapiAppOptions = {
|
const generateStrapiAppOptions = {
|
||||||
...program,
|
...program,
|
||||||
starter: starterPackageInfo.name,
|
starter: starterPackageInfo.name,
|
||||||
template: `${starterJson.template.name}@${starterJson.template.version}`,
|
|
||||||
run: false,
|
run: false,
|
||||||
};
|
};
|
||||||
|
if (starterPackageInfo.version) {
|
||||||
|
starterPackageInfo.template = `${starterJson.template.name}@${starterJson.template.version}`;
|
||||||
|
} else {
|
||||||
|
starterPackageInfo.template = starterJson.template.name;
|
||||||
|
}
|
||||||
|
|
||||||
// Create strapi app using the template
|
// Create strapi app using the template
|
||||||
await generateNewApp(join(rootPath, 'backend'), generateStrapiAppOptions);
|
await generateNewApp(join(rootPath, 'backend'), generateStrapiAppOptions);
|
||||||
@ -162,7 +178,7 @@ module.exports = async function buildStarter({ projectName, starter }, program)
|
|||||||
await installWithLogs(frontendPath);
|
await installWithLogs(frontendPath);
|
||||||
|
|
||||||
// Setup monorepo
|
// Setup monorepo
|
||||||
initPackageJson(rootPath, projectBasename);
|
initPackageJson(rootPath, projectBasename, hasYarnInstalled);
|
||||||
|
|
||||||
// Add gitignore
|
// Add gitignore
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -7,10 +7,22 @@ const stopProcess = require('./stop-process');
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the package version on npm. Will fail if the package does not exist
|
* Gets the package version on npm. Will fail if the package does not exist
|
||||||
* @param {string} packageName - Name to look up on npm, may include a specific version
|
* @param {string} packageName Name to look up on npm, may include a specific version
|
||||||
|
* @param {boolean} useYarn Yarn instead of npm
|
||||||
* @returns {Object}
|
* @returns {Object}
|
||||||
*/
|
*/
|
||||||
async function getPackageInfo(packageName) {
|
async function getPackageInfo(packageName, useYarn) {
|
||||||
|
// Use yarn if possible because it's faster
|
||||||
|
if (useYarn) {
|
||||||
|
const { stdout } = await execa.shell(`yarn info ${packageName} --json`);
|
||||||
|
const yarnInfo = JSON.parse(stdout);
|
||||||
|
return {
|
||||||
|
name: yarnInfo.data.name,
|
||||||
|
version: yarnInfo.data.version,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback to npm
|
||||||
const { stdout } = await execa.shell(`npm view ${packageName} name version --silent`);
|
const { stdout } = await execa.shell(`npm view ${packageName} name version --silent`);
|
||||||
// Use regex to parse name and version from CLI result
|
// Use regex to parse name and version from CLI result
|
||||||
const [name, version] = stdout.match(/(?<=')(.*?)(?=')/gm);
|
const [name, version] = stdout.match(/(?<=')(.*?)(?=')/gm);
|
||||||
@ -18,14 +30,16 @@ async function getPackageInfo(packageName) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} starter - The name of the starter as provided by the user.
|
* Get the version and full package name of the starter
|
||||||
* @returns {Object} - The full name of the starter package's name on npm
|
* @param {string} starter - The name of the starter as provided by the user
|
||||||
|
* @param {boolean} useYarn - Use yarn instead of npm
|
||||||
|
* @returns {Object} - Full name and version of the starter package on npm
|
||||||
*/
|
*/
|
||||||
async function getStarterPackageInfo(starter) {
|
async function getStarterPackageInfo(starter, useYarn) {
|
||||||
// Check if starter is a shorthand
|
// Check if starter is a shorthand
|
||||||
try {
|
try {
|
||||||
const longhand = `@strapi/starter-${starter}`;
|
const longhand = `@strapi/starter-${starter}`;
|
||||||
const packageInfo = await getPackageInfo(longhand);
|
const packageInfo = await getPackageInfo(longhand, useYarn);
|
||||||
// Hasn't crashed so it is indeed a shorthand
|
// Hasn't crashed so it is indeed a shorthand
|
||||||
return packageInfo;
|
return packageInfo;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -40,16 +54,24 @@ async function getStarterPackageInfo(starter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Download a starter package from the npm registry
|
||||||
* @param {Object} packageInfo - Starter's npm package information
|
* @param {Object} packageInfo - Starter's npm package information
|
||||||
* @param {string} packageInfo.name
|
* @param {string} packageInfo.name
|
||||||
* @param {string} packageInfo.version
|
* @param {string} packageInfo.version
|
||||||
* @param {string} parentDir - Path inside of which we install the starter.
|
* @param {string} parentDir - Path inside of which we install the starter
|
||||||
|
* @param {boolean} useYarn - Use yarn instead of npm
|
||||||
*/
|
*/
|
||||||
async function downloadNpmStarter({ name, version }, parentDir) {
|
async function downloadNpmStarter({ name, version }, parentDir, useYarn) {
|
||||||
// Download from npm
|
// Download from npm, using yarn if possible
|
||||||
await execa.shell(`npm install ${name}@${version} --no-save --silent`, {
|
if (useYarn) {
|
||||||
cwd: parentDir,
|
await execa.shell(`yarn add ${name}@${version} --no-lockfile --silent`, {
|
||||||
});
|
cwd: parentDir,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await execa.shell(`npm install ${name}@${version} --no-save --silent`, {
|
||||||
|
cwd: parentDir,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Return the path of the actual starter
|
// Return the path of the actual starter
|
||||||
const exactStarterPath = path.dirname(
|
const exactStarterPath = path.dirname(
|
||||||
|
|||||||
@ -4,9 +4,8 @@ const execa = require('execa');
|
|||||||
|
|
||||||
module.exports = function hasYarn() {
|
module.exports = function hasYarn() {
|
||||||
try {
|
try {
|
||||||
const { exitCode } = execa.sync('yarn --version', { shell: true });
|
const { code } = execa.shellSync('yarnpkg --version');
|
||||||
|
if (code === 0) return true;
|
||||||
if (exitCode === 0) return true;
|
|
||||||
return false;
|
return false;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user