Skip to content

Support Projects-compatible theme boilerplate #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/cli-lib/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ async function fetchReleaseData(repoName, tag = '') {
async function downloadProject(
repoName,
tag = '',
releaseType = GITHUB_RELEASE_TYPES.RELEASE
releaseType = GITHUB_RELEASE_TYPES.RELEASE,
ref
) {
try {
let zipUrl;
if (releaseType === GITHUB_RELEASE_TYPES.REPOSITORY) {
logger.log(`Fetching ${releaseType} with name ${repoName}...`);
zipUrl = `https://api.github.com/repos/HubSpot/${repoName}/zipball`;
zipUrl = `https://api.github.com/repos/HubSpot/${repoName}/zipball/${ref}`;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The github api supports passing in a branch name to specify which branch to download (it defaults to "main" or "master" I think). That means we won't be using any sort of published release when we download the project-compatible version of the boilerplate. Whatever is currently in that branch will get downloaded.

The alternative route is to create a separate repo for the project-compatible version of the boilerplate, with its own release. IMO downloading the branch should be okay, but I figured I should still mention the alternative.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should account for ref = undefined when building this string. Otherwise, you get a 404 running hs create api-sample.

Otherwise, I think this approach works!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, updated in fe42874

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking more into this, and realized there's another option. We could tag the latest commit in the boilerplate branch (w/out linking it to a new release), and then the CLI would download that specific tagged commit. I still think what we have in place now is totally fine since it will be easier to iterate on the boilerplate project. Maybe down the road we could update the flow to use a tagged commit if we're running into issues.

} else {
const releaseData = await fetchReleaseData(repoName, tag);
if (!releaseData) return;
Expand Down Expand Up @@ -180,9 +181,9 @@ function cleanupTemp(tmpDir) {
* @returns {Boolean} `true` if successful, `false` otherwise.
*/
async function createProject(dest, type, repoName, sourceDir, options = {}) {
const { themeVersion, projectVersion, releaseType } = options;
const { themeVersion, projectVersion, releaseType, ref } = options;
const tag = projectVersion || themeVersion;
const zip = await downloadProject(repoName, tag, releaseType);
const zip = await downloadProject(repoName, tag, releaseType, ref);
if (!zip) return false;
const { extractDir, tmpDir } = (await extractProjectZip(repoName, zip)) || {};
const success =
Expand Down
13 changes: 12 additions & 1 deletion packages/cli/commands/create/website-theme.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
const { createProject } = require('@hubspot/cli-lib/projects');
const { GITHUB_RELEASE_TYPES } = require('@hubspot/cli-lib/lib/constants');
const { getIsInProject } = require('../../lib/projects');

const PROJECT_BOILERPLATE_REF = 'cms-boilerplate-developer-projects';

module.exports = {
dest: ({ name, assetType }) => name || assetType,
execute: ({ dest, assetType, options }) => {
execute: async ({ dest, assetType, options }) => {
const isInProject = await getIsInProject(dest);

if (isInProject) {
options.ref = PROJECT_BOILERPLATE_REF;
// releaseType has to be 'REPOSITORY' to download a specific branch
options.releaseType = GITHUB_RELEASE_TYPES.REPOSITORY;
}
createProject(dest, assetType, 'cms-theme-boilerplate', 'src', options);
},
};
13 changes: 12 additions & 1 deletion packages/cli/lib/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,24 @@ const writeProjectConfig = (configPath, config) => {
}
};

const getProjectConfig = async _dir => {
const getIsInProject = async _dir => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const configPath = await getProjectConfigPath(_dir);
return !!configPath;
};

const getProjectConfigPath = async _dir => {
const projectDir = _dir ? path.resolve(getCwd(), _dir) : getCwd();

const configPath = findup(PROJECT_CONFIG_FILE, {
cwd: projectDir,
nocase: true,
});

return configPath;
};

const getProjectConfig = async _dir => {
const configPath = await getProjectConfigPath(_dir);
if (!configPath) {
return { projectConfig: null, projectDir: null };
}
Expand Down Expand Up @@ -381,6 +391,7 @@ const makeGetTaskStatus = taskType => {
module.exports = {
writeProjectConfig,
getProjectConfig,
getIsInProject,
createProjectConfig,
validateProjectConfig,
showWelcomeMessage,
Expand Down