Skip to content

Commit 1f4c4a9

Browse files
committed
Refactoring project to use an external
configuration.
1 parent 97d5f13 commit 1f4c4a9

20 files changed

+174
-225
lines changed

.gitignore

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/ci/c8/
22
/ci/jsdoc/
3-
/lib/easy/**/*.*
4-
/lib/expert/**/*.*
5-
/lib/hard/**/*.*
6-
/lib/medium/**/*.*
3+
/lib/easy/
4+
/lib/expert/
5+
/lib/hard/
6+
/lib/medium/
77
/node_modules/
8-
/test/easy/**/*.*
9-
/test/expert/**/*.*
10-
/test/hard/**/*.*
11-
/test/medium/**/*.*
8+
/test/easy/
9+
/test/expert/
10+
/test/hard/
11+
/test/medium/
1212
/.ashrc
1313
/.env
1414
/docker-compose.override.yml

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
### Changed
99
- Node 21 => 22.
10-
- Configurations, inputs, outputs and default codes are now in a separate project.
10+
- Configurations, inputs, outputs and default codes are now in a separated project.
11+
- Default codes and tests are now generated in the `easy`, `medium`, `hard` and `expert` sub-directories
12+
of the `lib` and `test` directories.
1113

1214
## [2.0.0] - 2024-04-22
1315
### Added

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ A test executes the `execute()` method. You can add your code in and arround it.
5454

5555
Executing tests for a specific code :
5656
```shellsession
57-
user@host codingame-js-tests$ docker compose run --rm app npm test ./test/training/easy/unary/
57+
user@host codingame-js-tests$ docker compose run --rm app npm test ./test/easy/ASCIIArt/
5858
```
5959

6060

lib/configuration/ConfigurationConvertor.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFile } from 'node:fs/promises';
2-
import GeneratorCodeConfiguration from '../generator/GeneratorCodeConfiguration.js';
3-
import GeneratorTestConfiguration from '../generator/GeneratorTestConfiguration.js';
2+
import CodeGeneratorConfiguration from '../generator/CodeGeneratorConfiguration.js';
3+
import TestGeneratorConfiguration from '../generator/TestGeneratorConfiguration.js';
44
import ParsedConfiguration from '../parser/ParsedConfiguration.js';
55

66
/**
@@ -14,10 +14,10 @@ export default class ConfigurationConvertor {
1414
* Returns the code configuration for the generator.
1515
* @param {ParsedConfiguration} parsedConfiguration - the parsed configuration.
1616
* @param {string} defaultCodeFile - the file with the default code.
17-
* @return {GeneratorCodeConfiguration} the code configuration for the generator.
17+
* @return {CodeGeneratorConfiguration} the code configuration for the generator.
1818
* @throws Error if the default code file is not readable.
1919
*/
20-
static async getGeneratorCodeConfiguration(parsedConfiguration, defaultCodeFile) {
20+
static async getCodeGeneratorConfiguration(parsedConfiguration, defaultCodeFile) {
2121

2222
if((typeof defaultCodeFile) !== "string") {
2323
throw new Error("The default code file is not a string.");
@@ -27,8 +27,7 @@ export default class ConfigurationConvertor {
2727
let defaultCode = await readFile(defaultCodeFile, { encoding: 'utf8' });
2828
defaultCode = defaultCode.replace(/\n/g, "\n ").replace(/\n \n/g, "\n\n");
2929

30-
return new GeneratorCodeConfiguration(
31-
parsedConfiguration.path,
30+
return new CodeGeneratorConfiguration(
3231
parsedConfiguration.name,
3332
parsedConfiguration.link,
3433
defaultCode
@@ -41,10 +40,10 @@ export default class ConfigurationConvertor {
4140
/**
4241
* Returns the test configuration for the generator.
4342
* @param {ParsedConfiguration} parsedConfiguration - the parsed configuration.
44-
* @return {GeneratorTestConfiguration} the test configuration for the generator.
43+
* @return {TestGeneratorConfiguration} the test configuration for the generator.
4544
*/
46-
static getGeneratorTestConfiguration(parsedConfiguration) {
47-
return new GeneratorTestConfiguration(
45+
static getTestGeneratorConfiguration(parsedConfiguration) {
46+
return new TestGeneratorConfiguration(
4847
parsedConfiguration.path,
4948
parsedConfiguration.name,
5049
parsedConfiguration.testConfigurations

lib/generator/CGCodeGenerator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default class CGCodeGenerator extends FileGenerator {
2222

2323
/**
2424
* Generates the code.
25-
* @param {GeneratorCodeConfiguration} configuration - the code configuration.
25+
* @param {CodeGeneratorConfiguration} configuration - the code configuration.
2626
* @param {string} toPath - the path where to put the generated code.
2727
*/
2828
async generate(configuration, toPath) {

lib/generator/CGTestGenerator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default class CGTestGenerator extends FileGenerator {
3333

3434
/**
3535
* Generates the test.
36-
* @param {GeneratorTestConfiguration} configuration - the test configuration.
36+
* @param {TestGeneratorConfiguration} configuration - the test configuration.
3737
* @param {string} fromPath - the path where the config file is.
3838
* @param {string} toPath - the path where to put the generated test.
3939
*/

lib/generator/GeneratorCodeConfiguration.js renamed to lib/generator/CodeGeneratorConfiguration.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/**
2-
* The generator code configuration.
2+
* The code generator configuration.
33
*/
4-
export default class GeneratorCodeConfiguration {
4+
export default class CodeGeneratorConfiguration {
55

66
// Properties :
77

8-
#path;
98
#name;
109
#link;
1110
#defaultCode;
@@ -15,18 +14,15 @@ export default class GeneratorCodeConfiguration {
1514

1615
/**
1716
* The constructor.
18-
* @param {string} path - the path.
1917
* @param {string} name - the name.
2018
* @param {string} link - the link.
2119
* @param {string} defaultCode - the default code.
2220
*/
2321
constructor(
24-
path,
2522
name,
2623
link,
2724
defaultCode
2825
) {
29-
this.#path = path;
3026
this.#name = name;
3127
this.#link = link;
3228
this.#defaultCode = defaultCode;
@@ -35,15 +31,6 @@ export default class GeneratorCodeConfiguration {
3531

3632
// Accessors :
3733

38-
/**
39-
* Returns the path.
40-
* @returns {string} - the path.
41-
*/
42-
get path()
43-
{
44-
return this.#path;
45-
}
46-
4734
/**
4835
* Returns the name.
4936
* @returns {string} - the name.

lib/generator/FileGenerator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default class FileGenerator {
2626

2727
/**
2828
* Generates the file.
29-
* @param {GeneratorCodeConfiguration|GeneratorTestConfiguration} configuration - the configuration.
29+
* @param {CodeGeneratorConfiguration|TestGeneratorConfiguration} configuration - the configuration.
3030
* @param {string} toPath - the path where to put the generated file.
3131
* @throws {Error} this method must be implemented in sub-classes.
3232
*/
@@ -36,7 +36,7 @@ export default class FileGenerator {
3636

3737
/**
3838
* Generates the file content.
39-
* @param {GeneratorCodeConfiguration|GeneratorTestConfiguration} configuration - the configuration.
39+
* @param {CodeGeneratorConfiguration|TestGeneratorConfiguration} configuration - the configuration.
4040
* @param {string} path - the path.
4141
* @param {string} file - the file.
4242
* @param {string} templateFile - the template file.

lib/generator/FilesGenerator.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ import path from 'path';
1111
*/
1212
const CONFIG_FILE = 'config.json';
1313

14+
/**
15+
* The default code directory.
16+
*/
17+
const DEFAULT_CODE_DIRECTORY = 'code' + path.sep;
18+
1419
/**
1520
* The default code file.
1621
*/
17-
const DEFAULT_CODE_FILE = 'CGCode.dist';
22+
const DEFAULT_CODE_FILE = 'CGCode.js';
1823

1924

2025
/**
@@ -51,29 +56,18 @@ export default class FilesGenerator {
5156
* @param {string} testPath - the equivalent path of pathToScan in the tests directory.
5257
*/
5358
async generate(pathToScan, srcPath, testPath) {
54-
let pathContent = await readdir(pathToScan);
55-
pathContent = pathContent.filter(this.#removeInputOutputDirectories.bind(this));
56-
57-
const pathContentCount = pathContent.length;
59+
const difficultyDirectories = await readdir(pathToScan);
5860

59-
for(let pathContentIndex = 0; pathContentIndex < pathContentCount; pathContentIndex++) {
60-
const element = pathContent[pathContentIndex];
61+
for(const difficultyDirectory of difficultyDirectories) {
62+
const puzzleDirectories = await readdir(pathToScan + difficultyDirectory);
6163

62-
const elementStats = await stat(pathToScan + element);
63-
64-
if (elementStats.isDirectory() === true) {
65-
this.generate(
66-
pathToScan + element + path.sep,
67-
srcPath + element + path.sep,
68-
testPath + element + path.sep
69-
);
70-
}
64+
for(const puzzleDirectory of puzzleDirectories) {
65+
const puzzlePath = difficultyDirectory + path.sep + puzzleDirectory + path.sep;
7166

72-
if (element === CONFIG_FILE) {
7367
this.#generateFilesForConfiguration(
74-
pathToScan,
75-
srcPath,
76-
testPath
68+
pathToScan + puzzlePath,
69+
srcPath + puzzlePath,
70+
testPath + puzzlePath
7771
);
7872
}
7973
}
@@ -103,16 +97,16 @@ export default class FilesGenerator {
10397
async #generateFilesForConfiguration(configurationPath, srcPath, testPath) {
10498
const parsedConfiguration = await this.#configurationParser.getConfigurationFromFile(configurationPath + CONFIG_FILE);
10599

106-
const codeConfiguration = await ConfigurationConvertor.getGeneratorCodeConfiguration(
100+
const codeConfiguration = await ConfigurationConvertor.getCodeGeneratorConfiguration(
107101
parsedConfiguration,
108-
configurationPath + DEFAULT_CODE_FILE
102+
configurationPath + DEFAULT_CODE_DIRECTORY + DEFAULT_CODE_FILE
109103
);
110104
this.#codeGenerator.generate(
111105
codeConfiguration,
112106
srcPath
113107
);
114108

115-
const testConfiguration = await ConfigurationConvertor.getGeneratorTestConfiguration(parsedConfiguration);
109+
const testConfiguration = await ConfigurationConvertor.getTestGeneratorConfiguration(parsedConfiguration);
116110
this.#testGenerator.generate(
117111
testConfiguration,
118112
configurationPath,

lib/generator/GeneratorTestConfiguration.js renamed to lib/generator/TestGeneratorConfiguration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* The generator test configuration.
33
*/
4-
export default class GeneratorTestConfiguration {
4+
export default class TestGeneratorConfiguration {
55

66
// Properties :
77

0 commit comments

Comments
 (0)