1- import { execSync } from 'child_process' ;
2- import * as crypto from 'crypto' ;
3- import * as fs from 'fs' ;
4- import * as path from 'path' ;
5-
6- import { appstash , resolve } from 'appstash' ;
7- import { replaceVariables , extractVariables } from 'create-gen-app' ;
1+ import { replaceVariables , extractVariables , TemplateCache } from 'create-gen-app' ;
82
93export interface CachedTemplateOptions {
104 templateUrl : string ;
115 outputDir : string ;
126 answers : Record < string , any > ;
137 cacheTool ?: string ;
8+ branch ?: string ;
9+ ttl ?: number ;
10+ maxAge ?: number ;
1411}
1512
1613export interface CachedTemplateResult {
@@ -20,82 +17,25 @@ export interface CachedTemplateResult {
2017}
2118
2219/**
23- * Get cached repository from appstash cache directory
24- * @param templateUrl - Repository URL
25- * @param cacheTool - Tool name for appstash (default: 'mymodule')
26- * @returns Cached repository path or null if not found
27- */
28- export function getCachedRepo ( templateUrl : string , cacheTool : string = 'mymodule' ) : string | null {
29- const dirs = appstash ( cacheTool , { ensure : true } ) ;
30- const repoHash = crypto . createHash ( 'md5' ) . update ( templateUrl ) . digest ( 'hex' ) ;
31- const cachePath = resolve ( dirs , 'cache' , 'repos' , repoHash ) ;
32-
33- if ( fs . existsSync ( cachePath ) ) {
34- return cachePath ;
35- }
36-
37- return null ;
38- }
39-
40- /**
41- * Clone repository to cache
42- * @param templateUrl - Repository URL
43- * @param cacheTool - Tool name for appstash (default: 'mymodule')
44- * @returns Path to cached repository
45- */
46- export function cloneToCache ( templateUrl : string , cacheTool : string = 'mymodule' ) : string {
47- const dirs = appstash ( cacheTool , { ensure : true } ) ;
48- const repoHash = crypto . createHash ( 'md5' ) . update ( templateUrl ) . digest ( 'hex' ) ;
49- const cachePath = resolve ( dirs , 'cache' , 'repos' , repoHash ) ;
50-
51- if ( ! fs . existsSync ( path . dirname ( cachePath ) ) ) {
52- fs . mkdirSync ( path . dirname ( cachePath ) , { recursive : true } ) ;
53- }
54-
55- const gitUrl = normalizeGitUrl ( templateUrl ) ;
56-
57- execSync ( `git clone ${ gitUrl } ${ cachePath } ` , {
58- stdio : 'inherit'
59- } ) ;
60-
61- const gitDir = path . join ( cachePath , '.git' ) ;
62- if ( fs . existsSync ( gitDir ) ) {
63- fs . rmSync ( gitDir , { recursive : true , force : true } ) ;
64- }
65-
66- return cachePath ;
67- }
68-
69- /**
70- * Normalize a URL to a git-cloneable format
71- * @param url - Input URL
72- * @returns Normalized git URL
73- */
74- function normalizeGitUrl ( url : string ) : string {
75- if ( url . startsWith ( 'git@' ) || url . startsWith ( 'https://' ) || url . startsWith ( 'http://' ) ) {
76- return url ;
77- }
78-
79- if ( / ^ [ \w - ] + \/ [ \w - ] + $ / . test ( url ) ) {
80- return `https://github.com/${ url } .git` ;
81- }
82-
83- return url ;
84- }
85-
86- /**
87- * Create project from cached template
20+ * Create project from cached template using the shared TemplateCache
8821 * @param options - Options for creating from cached template
8922 * @returns Result with output directory and cache information
9023 */
9124export async function createFromCachedTemplate ( options : CachedTemplateOptions ) : Promise < CachedTemplateResult > {
92- const { templateUrl, outputDir, answers, cacheTool = 'mymodule' } = options ;
25+ const { templateUrl, outputDir, answers, cacheTool = 'mymodule' , branch, ttl, maxAge } = options ;
26+
27+ const templateCache = new TemplateCache ( {
28+ enabled : true ,
29+ toolName : cacheTool ,
30+ ttl,
31+ maxAge,
32+ } ) ;
9333
9434 let templateDir : string ;
9535 let cacheUsed = false ;
9636 let cachePath : string | undefined ;
9737
98- const cachedRepo = getCachedRepo ( templateUrl , cacheTool ) ;
38+ const cachedRepo = templateCache . get ( templateUrl , branch ) ;
9939
10040 if ( cachedRepo ) {
10141 console . log ( `Using cached template from ${ cachedRepo } ` ) ;
@@ -104,7 +44,7 @@ export async function createFromCachedTemplate(options: CachedTemplateOptions):
10444 cachePath = cachedRepo ;
10545 } else {
10646 console . log ( `Cloning template to cache from ${ templateUrl } ` ) ;
107- templateDir = cloneToCache ( templateUrl , cacheTool ) ;
47+ templateDir = templateCache . set ( templateUrl , branch ) ;
10848 cachePath = templateDir ;
10949 }
11050
@@ -118,3 +58,6 @@ export async function createFromCachedTemplate(options: CachedTemplateOptions):
11858 cachePath
11959 } ;
12060}
61+
62+ // Re-export TemplateCache for convenience
63+ export { TemplateCache } from 'create-gen-app' ;
0 commit comments