Apply PR feedback

This commit is contained in:
Rémi de Juvigny 2021-11-23 12:57:53 +01:00
parent 9c33487894
commit 0e76ce4af9
3 changed files with 62 additions and 25 deletions

View File

@ -31,9 +31,10 @@ function readStarterJson(filePath, starter) {
/**
* @param {string} rootPath - Path to the project directory
* @param {string} projectName - Name of the project
* @param {boolean} useYarn - Use yarn instead of npm
*/
async function initPackageJson(rootPath, projectName) {
const packageManager = hasYarn() ? 'yarn --cwd' : 'npm run --prefix';
async function initPackageJson(rootPath, projectName, useYarn) {
const packageManager = useYarn ? 'yarn --cwd' : 'npm run --prefix';
try {
await fse.writeJson(
@ -85,8 +86,13 @@ async function installWithLogs(path) {
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));
let starterPath;
let starterParentPath;
let starterPackageInfo = {};
@ -97,25 +103,31 @@ async function getStarterInfo(starter) {
starterPath = resolve(starter);
} else {
// 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.`);
// Download starter repository to a temporary directory
starterParentPath = await fse.mkdtemp(join(os.tmpdir(), 'strapi-'));
starterPath = await downloadNpmStarter(starterPackageInfo, starterParentPath);
starterPath = await downloadNpmStarter(starterPackageInfo, starterParentPath, useYarn);
}
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.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) {
const { isLocalStarter, starterPath, starterParentPath, starterPackageInfo } = getStarterInfo();
const hasYarnInstalled = hasYarn();
const {
isLocalStarter,
starterPath,
starterParentPath,
starterPackageInfo,
} = await getStarterInfo(starter, hasYarnInstalled);
// Project directory
const rootPath = resolve(projectName);
@ -149,9 +161,13 @@ module.exports = async function buildStarter({ projectName, starter }, program)
const generateStrapiAppOptions = {
...program,
starter: starterPackageInfo.name,
template: `${starterJson.template.name}@${starterJson.template.version}`,
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
await generateNewApp(join(rootPath, 'backend'), generateStrapiAppOptions);
@ -162,7 +178,7 @@ module.exports = async function buildStarter({ projectName, starter }, program)
await installWithLogs(frontendPath);
// Setup monorepo
initPackageJson(rootPath, projectBasename);
initPackageJson(rootPath, projectBasename, hasYarnInstalled);
// Add gitignore
try {

View File

@ -7,10 +7,22 @@ const stopProcess = require('./stop-process');
/**
* 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}
*/
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`);
// Use regex to parse name and version from CLI result
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.
* @returns {Object} - The full name of the starter package's name on npm
* Get the version and full package name of the starter
* @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
try {
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
return packageInfo;
} 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 {string} packageInfo.name
* @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) {
// Download from npm
await execa.shell(`npm install ${name}@${version} --no-save --silent`, {
cwd: parentDir,
});
async function downloadNpmStarter({ name, version }, parentDir, useYarn) {
// Download from npm, using yarn if possible
if (useYarn) {
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
const exactStarterPath = path.dirname(

View File

@ -4,9 +4,8 @@ const execa = require('execa');
module.exports = function hasYarn() {
try {
const { exitCode } = execa.sync('yarn --version', { shell: true });
if (exitCode === 0) return true;
const { code } = execa.shellSync('yarnpkg --version');
if (code === 0) return true;
return false;
} catch (err) {
return false;