-
Notifications
You must be signed in to change notification settings - Fork 7
feat(create-app): add custom template support via GitHub template sources #641
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
kabaros
merged 7 commits into
dhis2:master
from
derrick-nuby:feat/create-app-custom-template-via-git
Feb 24, 2026
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
470cd21
feat(create-app): add custom template support via GitHub specifiers
derrick-nuby 18b7525
refactor(create-app): reduce parser complexity and use node builtins
derrick-nuby 69f99e9
fix(create-app): run git clone without shell command strings
derrick-nuby ec1e867
refactor(create-app): resolve git templates via external resolver and…
derrick-nuby 7cd0714
refactor(create-app): align git template resolver and remove ignored …
derrick-nuby 471856f
fix(verify-commits): include 'labeled' type in pull request event tri…
derrick-nuby 54fc0ec
fix(verify-commits): update pull request event types for verification…
derrick-nuby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
packages/create-app/src/utils/isGitTemplateSpecifier.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| const githubHosts = new Set(['github.com', 'www.github.com']) | ||
| const shorthandPattern = /^([a-zA-Z0-9_.-]+)\/([^\s/]+)$/ | ||
|
|
||
| const parseRefAndSubdir = (rawTemplateSource, refAndSubdir) => { | ||
| if (refAndSubdir === undefined) { | ||
| return { ref: null, subdir: null } | ||
| } | ||
| if (!refAndSubdir) { | ||
| throw new Error( | ||
| `Invalid template source "${rawTemplateSource}". Ref cannot be empty after "#".` | ||
| ) | ||
| } | ||
|
|
||
| const [parsedRef, ...subdirParts] = refAndSubdir.split(':') | ||
| const ref = parsedRef || null | ||
| const subdir = subdirParts.length > 0 ? subdirParts.join(':') : null | ||
|
|
||
| if (!ref) { | ||
| throw new Error( | ||
| `Invalid template source "${rawTemplateSource}". Ref cannot be empty after "#".` | ||
| ) | ||
| } | ||
| if (subdir !== null && !subdir.trim()) { | ||
| throw new Error( | ||
| `Invalid template source "${rawTemplateSource}". Subdirectory cannot be empty after ":".` | ||
| ) | ||
| } | ||
|
|
||
| return { ref, subdir } | ||
| } | ||
|
|
||
| const parseGithubUrlSource = (sourceWithoutRef) => { | ||
| const parsedUrl = new URL(sourceWithoutRef) | ||
| if (!githubHosts.has(parsedUrl.host)) { | ||
| throw new Error( | ||
| `Unsupported template host "${parsedUrl.host}". Only github.com repositories are supported.` | ||
| ) | ||
| } | ||
|
|
||
| const pathParts = parsedUrl.pathname.split('/').filter(Boolean).slice(0, 2) | ||
| if (pathParts.length < 2) { | ||
| throw new Error( | ||
| `Invalid GitHub repository path in "${sourceWithoutRef}". Use "owner/repo".` | ||
| ) | ||
| } | ||
|
|
||
| return { | ||
| owner: pathParts[0], | ||
| repo: pathParts[1], | ||
| } | ||
| } | ||
|
|
||
| const parseGithubShorthandSource = (rawTemplateSource, sourceWithoutRef) => { | ||
| const match = sourceWithoutRef.match(shorthandPattern) | ||
| if (!match) { | ||
| throw new Error( | ||
| `Invalid template source "${rawTemplateSource}". Use "owner/repo", "owner/repo#ref", or "owner/repo#ref:subdir".` | ||
| ) | ||
| } | ||
|
|
||
| return { | ||
| owner: match[1], | ||
| repo: match[2], | ||
| } | ||
| } | ||
|
|
||
| const parseGitTemplateSpecifier = (templateSource) => { | ||
| const rawTemplateSource = String(templateSource || '').trim() | ||
| if (!rawTemplateSource) { | ||
| throw new Error('Template source cannot be empty.') | ||
| } | ||
|
|
||
| const [sourceWithoutRef, refAndSubdir, ...rest] = | ||
| rawTemplateSource.split('#') | ||
| if (rest.length > 0) { | ||
| throw new Error( | ||
| `Invalid template source "${rawTemplateSource}". Use at most one "#" to specify a ref.` | ||
| ) | ||
| } | ||
|
|
||
| const { ref, subdir } = parseRefAndSubdir(rawTemplateSource, refAndSubdir) | ||
| const sourceInfo = sourceWithoutRef.startsWith('https://') | ||
| ? parseGithubUrlSource(sourceWithoutRef) | ||
| : parseGithubShorthandSource(rawTemplateSource, sourceWithoutRef) | ||
|
|
||
| const owner = sourceInfo.owner | ||
| let repo = sourceInfo.repo | ||
|
|
||
| if (repo.endsWith('.git')) { | ||
| repo = repo.slice(0, -4) | ||
| } | ||
|
|
||
| if (!owner || !repo) { | ||
| throw new Error( | ||
| `Invalid template source "${rawTemplateSource}". Missing GitHub owner or repository name.` | ||
| ) | ||
| } | ||
|
|
||
| return { | ||
| owner, | ||
| repo, | ||
| ref, | ||
| subdir, | ||
| repoUrl: `https://github.com/${owner}/${repo}.git`, | ||
| raw: rawTemplateSource, | ||
| } | ||
| } | ||
|
|
||
| const isGitTemplateSpecifier = (templateSource) => { | ||
| const rawTemplateSource = String(templateSource || '').trim() | ||
| if (!rawTemplateSource) { | ||
| return false | ||
| } | ||
|
|
||
| if (rawTemplateSource.startsWith('https://')) { | ||
| return true | ||
| } | ||
|
|
||
| return /^[a-zA-Z0-9_.-]+\/[^\s/]+(?:#.+)?$/.test(rawTemplateSource) | ||
| } | ||
|
|
||
| module.exports = { | ||
| isGitTemplateSpecifier, | ||
| parseGitTemplateSpecifier, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| const os = require('node:os') | ||
| const path = require('node:path') | ||
| const { exec } = require('@dhis2/cli-helpers-engine') | ||
| const fs = require('fs-extra') | ||
| const { | ||
| isGitTemplateSpecifier, | ||
| parseGitTemplateSpecifier, | ||
| } = require('./isGitTemplateSpecifier') | ||
|
|
||
| const ensureTemplateDirectory = (templatePath, templateSource) => { | ||
|
derrick-nuby marked this conversation as resolved.
Outdated
|
||
| if (!fs.existsSync(templatePath)) { | ||
| throw new Error( | ||
| `Template path "${templatePath}" from source "${templateSource}" does not exist.` | ||
| ) | ||
| } | ||
| const stats = fs.statSync(templatePath) | ||
| if (!stats.isDirectory()) { | ||
| throw new Error( | ||
| `Template path "${templatePath}" from source "${templateSource}" is not a directory.` | ||
| ) | ||
| } | ||
| const packageJsonPath = path.join(templatePath, 'package.json') | ||
| if (!fs.existsSync(packageJsonPath)) { | ||
| throw new Error( | ||
| `Template source "${templateSource}" is missing "package.json" at "${templatePath}".` | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| const resolveSubdirectory = (repoPath, subdir, templateSource) => { | ||
|
derrick-nuby marked this conversation as resolved.
Outdated
|
||
| if (!subdir) { | ||
| return repoPath | ||
| } | ||
|
|
||
| const cleanedSubdir = subdir.replace(/^\/+/, '') | ||
| const resolvedTemplatePath = path.resolve(repoPath, cleanedSubdir) | ||
| const repoPathWithSep = `${path.resolve(repoPath)}${path.sep}` | ||
| const validPath = | ||
| resolvedTemplatePath === path.resolve(repoPath) || | ||
| resolvedTemplatePath.startsWith(repoPathWithSep) | ||
| if (!validPath) { | ||
| throw new Error( | ||
| `Invalid template subdirectory "${subdir}" in "${templateSource}". It resolves outside of the repository.` | ||
| ) | ||
| } | ||
| return resolvedTemplatePath | ||
| } | ||
|
|
||
| const resolveTemplateSource = async (templateSource, builtInTemplateMap) => { | ||
| const normalizedTemplateSource = String(templateSource || '').trim() | ||
| const builtInPath = builtInTemplateMap[normalizedTemplateSource] | ||
| if (builtInPath) { | ||
| ensureTemplateDirectory(builtInPath, normalizedTemplateSource) | ||
| return { | ||
| templatePath: builtInPath, | ||
| cleanup: async () => {}, | ||
| } | ||
| } | ||
|
|
||
| if (!isGitTemplateSpecifier(normalizedTemplateSource)) { | ||
| throw new Error( | ||
| `Unknown template "${normalizedTemplateSource}". Use one of [${Object.keys( | ||
| builtInTemplateMap | ||
| ).join(', ')}] or a GitHub template specifier like "owner/repo#ref:subdir".` | ||
| ) | ||
| } | ||
|
|
||
| const parsedSpecifier = parseGitTemplateSpecifier(normalizedTemplateSource) | ||
| const tempBase = fs.mkdtempSync( | ||
| path.join(os.tmpdir(), 'd2-create-template-source-') | ||
| ) | ||
| const clonedRepoPath = path.join(tempBase, 'repo') | ||
|
|
||
| try { | ||
| const gitCloneArgs = parsedSpecifier.ref | ||
| ? [ | ||
| 'clone', | ||
| '--depth', | ||
| '1', | ||
| '--branch', | ||
| parsedSpecifier.ref, | ||
| parsedSpecifier.repoUrl, | ||
| clonedRepoPath, | ||
| ] | ||
| : [ | ||
| 'clone', | ||
| '--depth', | ||
| '1', | ||
| parsedSpecifier.repoUrl, | ||
| clonedRepoPath, | ||
| ] | ||
| await exec({ | ||
| cmd: 'git', | ||
| args: gitCloneArgs, | ||
| pipe: false, | ||
| }) | ||
|
|
||
| const resolvedTemplatePath = resolveSubdirectory( | ||
| clonedRepoPath, | ||
| parsedSpecifier.subdir, | ||
| normalizedTemplateSource | ||
| ) | ||
| ensureTemplateDirectory( | ||
| resolvedTemplatePath, | ||
| normalizedTemplateSource | ||
| ) | ||
|
|
||
| return { | ||
| templatePath: resolvedTemplatePath, | ||
| cleanup: async () => { | ||
| fs.removeSync(tempBase) | ||
| }, | ||
| } | ||
| } catch (error) { | ||
| fs.removeSync(tempBase) | ||
| if (error instanceof Error && error.message) { | ||
| throw new Error( | ||
| `Failed to resolve template "${normalizedTemplateSource}": ${error.message}` | ||
| ) | ||
| } | ||
| throw new Error( | ||
| `Failed to resolve template "${normalizedTemplateSource}".` | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| module.exports = resolveTemplateSource | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.