@@ -13,6 +13,23 @@ const tokens = [
1313 { match : '{{year}}' , key : 'year' } ,
1414] ;
1515
16+ /**
17+ *
18+ * @param {String } templateFilePath
19+ * @param {Number[] } days
20+ * @returns {Array<Promise> }
21+ */
22+ const doCreate = async ( templateFilePath , year , days ) => {
23+ const template = await readFile ( templateFilePath , { encoding : 'utf-8' } ) ;
24+ return days . map ( ( day ) =>
25+ writeFile (
26+ getSolutionFileName ( day ) ,
27+ replaceTokens ( tokens , { year, day } , template ) ,
28+ 'utf-8'
29+ )
30+ ) ;
31+ } ;
32+
1633/**
1734 * Creates the solution files in the cwd.
1835 */
@@ -23,24 +40,20 @@ export const createSolutionFiles = async ({ year } = {}) => {
2340 throw new Error ( 'null or undefined year' ) ;
2441 }
2542
26- // create directory if doesn't exist.
27- const solutionsDir = getConfigValue ( 'paths.solutionsDir' ) ;
28- await ensureDir ( solutionsDir ) ;
29-
30- // load the contents of the template solution
31- const templateSolutionFile = await readFile (
32- getConfigValue ( 'paths.templates.solution' ) ,
33- { encoding : 'utf-8' }
34- ) ;
35-
36- // create each template solution file.
37- const createFilePromises = getConfigValue ( 'aoc.validation.days' ) . map ( ( day ) =>
38- writeFile (
39- getSolutionFileName ( day ) ,
40- replaceTokens ( tokens , { year, day } , templateSolutionFile ) ,
41- 'utf-8'
42- )
43- ) ;
43+ await ensureDir ( getConfigValue ( 'paths.solutionsDir' ) ) ;
4444
45- await Promise . all ( createFilePromises ) ;
45+ await Promise . all ( [
46+ // all days but the last day share a default solution template.
47+ ...doCreate (
48+ getConfigValue ( 'paths.templates.solutionDefault' ) ,
49+ year ,
50+ getConfigValue ( 'aoc.validation.days' ) . slice ( 0 , - 1 )
51+ ) ,
52+ // last day is special and needs a different solution template.
53+ ...doCreate (
54+ getConfigValue ( 'paths.templates.solutionLastDay' ) ,
55+ year ,
56+ getConfigValue ( 'aoc.validation.days' ) . slice ( - 1 )
57+ ) ,
58+ ] ) ;
4659} ;
0 commit comments