Skip to content

Commit 36559af

Browse files
committed
Fixing code and test files generation when
the configuration has no JS code.
1 parent 4481a41 commit 36559af

File tree

3 files changed

+83
-35
lines changed

3 files changed

+83
-35
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
### Fixed
9+
- code and test files generation when the configuration has no JS code.
10+
711
## [3.9.0] - 2025-03-31
812
### Changed
913
- sinon 19.0.2 => 20.0.0.

bin/generate.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ const __dirname = dirname(fileURLToPath(import.meta.url)) + "/";
77
async function generate() {
88
console.log('Starting generation.');
99

10-
const filesGenerator = new FilesGenerator(__dirname + '../templates/');
10+
const filesGenerator = new FilesGenerator(__dirname + '../');
1111
await filesGenerator.generate(
12-
__dirname + '../node_modules/@cyrilverloop/codingame-configuration/config/',
13-
__dirname + '../lib/',
14-
__dirname + '../test/'
12+
__dirname + '../node_modules/@cyrilverloop/codingame-configuration/config/'
1513
);
1614

1715
console.log('Generation complete.');

lib/generator/FilesGenerator.js

Lines changed: 77 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ConfigurationConvertor from '../configuration/ConfigurationConvertor.js';
22
import CGCodeGenerator from './CGCodeGenerator.js';
33
import CGTestGenerator from './CGTestGenerator.js';
44
import ConfigurationParser from '../parser/ConfigurationParser.js';
5+
import { access, constants } from 'node:fs';
56
import { readdir, stat } from 'node:fs/promises';
67
import path from 'path';
78

@@ -16,6 +17,21 @@ const CONFIG_FILE = 'config.json';
1617
*/
1718
const DEFAULT_CODE_DIRECTORY = 'code' + path.sep;
1819

20+
/**
21+
* The default library directory.
22+
*/
23+
const DEFAULT_LIBRARY_DIRECTORY = 'lib' + path.sep;
24+
25+
/**
26+
* The default template directory.
27+
*/
28+
const DEFAULT_TEMPLATE_DIRECTORY = 'templates' + path.sep;
29+
30+
/**
31+
* The default test directory.
32+
*/
33+
const DEFAULT_TEST_DIRECTORY = 'test' + path.sep;
34+
1935
/**
2036
* The default code file.
2137
*/
@@ -29,6 +45,7 @@ export default class FilesGenerator {
2945

3046
// Properties :
3147

48+
#projectPath;
3249
#configurationParser;
3350
#codeGenerator;
3451
#testGenerator;
@@ -38,10 +55,12 @@ export default class FilesGenerator {
3855

3956
/**
4057
* The constructor.
41-
* @param {string} templatesPath - the templates path.
58+
* @param {string} projectPath - the project path.
4259
*/
43-
constructor(templatesPath) {
60+
constructor(projectPath) {
61+
this.#projectPath = projectPath;
4462
this.#configurationParser = new ConfigurationParser();
63+
const templatesPath = projectPath + DEFAULT_TEMPLATE_DIRECTORY;
4564
this.#codeGenerator = new CGCodeGenerator(templatesPath);
4665
this.#testGenerator = new CGTestGenerator(templatesPath);
4766
}
@@ -52,50 +71,77 @@ export default class FilesGenerator {
5271
/**
5372
* Generates the test files.
5473
* @param {string} pathToScan - the path to scan for a configuration file.
55-
* @param {string} srcPath - the equivalent path of pathToScan in the src directory.
56-
* @param {string} testPath - the equivalent path of pathToScan in the tests directory.
5774
*/
58-
async generate(pathToScan, srcPath, testPath) {
75+
async generate(pathToScan) {
5976
const difficultyDirectories = await readdir(pathToScan);
6077

6178
for(const difficultyDirectory of difficultyDirectories) {
6279
const puzzleDirectories = await readdir(pathToScan + difficultyDirectory);
6380

64-
for(const puzzleDirectory of puzzleDirectories) {
65-
const puzzlePath = difficultyDirectory + path.sep + puzzleDirectory + path.sep;
81+
this.#generateConfigurationsForDifficulty(
82+
puzzleDirectories,
83+
difficultyDirectory,
84+
pathToScan + difficultyDirectory + path.sep
85+
);
86+
}
87+
}
6688

67-
this.#generateFilesForConfiguration(
68-
pathToScan + puzzlePath,
69-
srcPath + puzzlePath,
70-
testPath + puzzlePath
89+
/**
90+
* Generates the configurations for the difficulty.
91+
* @param {string[]} puzzleDirectories - the puzzle directories.
92+
* @param {string} difficulty - the difficulty.
93+
* @param {string} difficultyPathToScan - the path to scan for a configuration file.
94+
*/
95+
async #generateConfigurationsForDifficulty(
96+
puzzleDirectories,
97+
difficulty,
98+
difficultyPathToScan
99+
) {
100+
for(const puzzleDirectory of puzzleDirectories) {
101+
const configurationPath = difficultyPathToScan + puzzleDirectory + path.sep;
102+
const defaultCodeFile = configurationPath + DEFAULT_CODE_DIRECTORY + DEFAULT_CODE_FILE;
103+
104+
try {
105+
access(
106+
defaultCodeFile,
107+
constants.F_OK,
108+
this.#generateFilesForConfiguration.bind(
109+
this,
110+
configurationPath,
111+
difficulty + path.sep + puzzleDirectory + path.sep
112+
)
71113
);
114+
} catch(error) {
72115
}
73116
}
74117
}
75118

76119
/**
77120
* Generates the files for the configuration.
78121
* @param {string} configurationPath - the path of the `config` directory.
79-
* @param {string} srcPath the path - where to generate the code.
80-
* @param {string} testPath the path - where to generate the test.
122+
* @param {string} namespacePath - the path where to generate the files.
123+
* @param {Error} error - the error if CGCode.js does not exist.
81124
*/
82-
async #generateFilesForConfiguration(configurationPath, srcPath, testPath) {
83-
const parsedConfiguration = await this.#configurationParser.getConfigurationFromFile(configurationPath + CONFIG_FILE);
84-
85-
const codeConfiguration = await ConfigurationConvertor.getCodeGeneratorConfiguration(
86-
parsedConfiguration,
87-
configurationPath + DEFAULT_CODE_DIRECTORY + DEFAULT_CODE_FILE
88-
);
89-
this.#codeGenerator.generate(
90-
codeConfiguration,
91-
srcPath
92-
);
93-
94-
const testConfiguration = await ConfigurationConvertor.getTestGeneratorConfiguration(parsedConfiguration);
95-
this.#testGenerator.generate(
96-
testConfiguration,
97-
configurationPath,
98-
testPath
99-
);
125+
async #generateFilesForConfiguration(configurationPath, namespacePath, error) {
126+
127+
if(error === null) {
128+
const parsedConfiguration = await this.#configurationParser.getConfigurationFromFile(configurationPath + CONFIG_FILE);
129+
130+
const codeConfiguration = await ConfigurationConvertor.getCodeGeneratorConfiguration(
131+
parsedConfiguration,
132+
configurationPath + DEFAULT_CODE_DIRECTORY + DEFAULT_CODE_FILE
133+
);
134+
this.#codeGenerator.generate(
135+
codeConfiguration,
136+
this.#projectPath + DEFAULT_LIBRARY_DIRECTORY + namespacePath
137+
);
138+
139+
const testConfiguration = await ConfigurationConvertor.getTestGeneratorConfiguration(parsedConfiguration);
140+
this.#testGenerator.generate(
141+
testConfiguration,
142+
configurationPath,
143+
this.#projectPath + DEFAULT_TEST_DIRECTORY + namespacePath
144+
);
145+
}
100146
}
101147
}

0 commit comments

Comments
 (0)