Skip to content

Commit 48d3310

Browse files
committed
create different files
1 parent 5d3a83c commit 48d3310

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

src/config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,13 @@ const CONFIG = {
153153
source: join(__dirname, '..', 'templates', 'template-dataFile.json'),
154154
dest: join(cwd, 'aocr-data.json'),
155155
},
156-
solution: join(__dirname, '..', 'templates', 'template-solution.js'),
156+
solutionDefault: join(__dirname, '..', 'templates', 'template-solution.js'),
157+
solutionLastDay: join(
158+
__dirname,
159+
'..',
160+
'templates',
161+
'template-solution-last-day.js'
162+
),
157163
},
158164
},
159165
initialize: {

src/initialize/createSolutionFiles.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)