Skip to content

Commit

Permalink
Adds logging before fetch license call in case the call takes a long …
Browse files Browse the repository at this point in the history
…time or fails. Updates log emojis for async logs.
  • Loading branch information
theenoahmason committed Jun 5, 2024
1 parent 7e4085e commit 5e312a1
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ const tmpThemeReadmePath = path.join(tmpThemePath, 'README.md');
const gitURL = 'https://github.com/dreamsicle-io/wp-theme-assets.git';
const gitBranch = 'master';

// Construct license settings.
const licenseAPIEndpoint = 'https://api.github.com/licenses';
const licenseAPIDocsURL = 'https://docs.github.com/en/rest/licenses/licenses';

/**
* Construct option definitions.
* @type {OptionDef[]}
Expand Down Expand Up @@ -604,7 +608,7 @@ function clonePackage() {
logInfo({
title: 'Cloning package',
description: `${gitURL} (${gitBranch})`,
emoji: '📥',
emoji: '',
verbose: options.verbose,
dataLabel: 'Package information',
data: {
Expand Down Expand Up @@ -756,7 +760,7 @@ function replaceRename() {
*/
async function fetchLicense(slug) {
const formattedSlug = encodeURIComponent(slug.toLowerCase());
const response = await fetch(`https://api.github.com/licenses/${formattedSlug}`, {
const response = await fetch(`${licenseAPIEndpoint}/${formattedSlug}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Expand All @@ -775,8 +779,25 @@ async function fetchLicense(slug) {
async function writeLicense() {
try {
if (options.themeLicense === 'UNLICENSED') {
// If `UNLICENSED` write the file with only `UNLICENSED` as its content.
fs.writeFileSync(tmpThemeLicPath, 'UNLICENSED', { encoding: 'utf8' });
} else {
// Let the user know we are fetching the license asynchronously, in case
// the API takes a while to respond.
logInfo({
title: 'Fetching license',
description: `Fetching license for SPDX ID: "${options.themeLicense}"`,
emoji: '⏳',
verbose: options.verbose,
dataLabel: 'Request information',
data: {
spdx: options.themeLicense,
provider: 'GitHub',
endpoint: licenseAPIEndpoint,
documentation: licenseAPIDocsURL,
},
});
// Fetch the license from GitHub.
const license = await fetchLicense(options.themeLicense);
logInfo({
title: 'License fetched',
Expand All @@ -791,8 +812,10 @@ async function writeLicense() {
api: license.url,
},
});
// Write the license content.
fs.writeFileSync(tmpThemeLicPath, license.body, { encoding: 'utf8' });
}
// Log license generation success.
logInfo({
title: 'License written',
description: path.relative(tmpThemePath, tmpThemeLicPath),
Expand Down

0 comments on commit 5e312a1

Please sign in to comment.