mirror of
https://github.com/strapi/strapi.git
synced 2025-07-27 10:56:36 +00:00

Create starter CLI Add spaces option Extract project basename Add starter shortcut Refactor git init commit Replace concurrently with npm-run-all Move fetch repo function to dedicated file Allow shortcut to accept external org repo Fix package config & readme Fix versions Add task prefixes Update readme [Starter CLI] error handling (#9600) * Display error message + help for missing arguments * Add cleaner error messages * Bump commander from v6.1.0 to v7.1.0 * Capture and log errors from commander * Simplify cli argument errors * Provide more human readable error messages * Replace throw with console log * Add logger Add starter tracking Add wrapper for tracking keys Update root config Keep template in scope root fix strapi config Fix open frontend Remove open-cli Update for rename frontend->starter update description Update tracking Fix tests fix e2e tests Make sure packageJsonStrapi does not override unwanted keys & make it optional to avoid errors simplify metadata strapi Allow stater or frontend folder for smooth migration of existing starters Udpate dep Handle error
90 lines
2.3 KiB
JavaScript
90 lines
2.3 KiB
JavaScript
'use strict';
|
|
|
|
const tar = require('tar');
|
|
const fetch = require('node-fetch');
|
|
const parseGitUrl = require('git-url-parse');
|
|
const chalk = require('chalk');
|
|
const stopProcess = require('./stop-process');
|
|
|
|
function getShortcut(starter) {
|
|
let full_name;
|
|
// Determine if it is another organization
|
|
if (starter.includes('/')) {
|
|
const [org, project] = starter.split('/');
|
|
full_name = `${org}/strapi-starter-${project}`;
|
|
} else {
|
|
full_name = `strapi/strapi-starter-${starter}`;
|
|
}
|
|
|
|
return {
|
|
full_name,
|
|
usedShortcut: true,
|
|
};
|
|
}
|
|
/**
|
|
* @param {string} repo The path to repo
|
|
*/
|
|
async function getDefaultBranch(repo) {
|
|
const response = await fetch(`https://api.github.com/repos/${repo}`);
|
|
|
|
if (!response.ok) {
|
|
stopProcess(
|
|
`Could not find the starter information for ${chalk.yellow(
|
|
repo
|
|
)}. Make sure it is publicly accessible on github.`
|
|
);
|
|
}
|
|
|
|
const { default_branch } = await response.json();
|
|
|
|
return default_branch;
|
|
}
|
|
|
|
/**
|
|
* @param {string} starterUrl Github url to starter project
|
|
*/
|
|
async function getRepoInfo(starter) {
|
|
const repoInfo = await parseGitUrl(starter);
|
|
const { name, full_name, ref, protocols, source } = repoInfo;
|
|
|
|
if (protocols.length === 0) {
|
|
return getShortcut(starter);
|
|
}
|
|
|
|
if (source !== 'github.com') {
|
|
stopProcess(`Github URL not found for: ${chalk.yellow(starter)}`);
|
|
}
|
|
|
|
return {
|
|
name,
|
|
full_name,
|
|
ref,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @param {string} starterUrl Github url for strapi starter
|
|
* @param {string} tmpDir Path to temporary directory
|
|
*/
|
|
async function downloadGithubRepo(starterUrl, tmpDir) {
|
|
const { full_name, ref, usedShortcut } = await getRepoInfo(starterUrl);
|
|
const default_branch = await getDefaultBranch(full_name);
|
|
|
|
const branch = ref ? ref : default_branch;
|
|
|
|
// Download from GitHub
|
|
const codeload = `https://codeload.github.com/${full_name}/tar.gz/${branch}`;
|
|
|
|
const response = await fetch(codeload);
|
|
if (!response.ok) {
|
|
const message = usedShortcut ? `using the shortcut` : `using the url`;
|
|
stopProcess(`Could not download the repository ${message}: ${chalk.yellow(`${starterUrl}`)}`);
|
|
}
|
|
|
|
await new Promise(resolve => {
|
|
response.body.pipe(tar.extract({ strip: 1, cwd: tmpDir })).on('close', resolve);
|
|
});
|
|
}
|
|
|
|
module.exports = { getRepoInfo, downloadGithubRepo };
|