Merge pull request #436 from TheOdinProject/plopGenerator

Replace generator-exercise with plop generator
This commit is contained in:
Eric Olkowski
2024-03-03 13:33:51 -05:00
committed by GitHub
23 changed files with 1938 additions and 5853 deletions
+12 -3
View File
@@ -2,18 +2,16 @@
These JavaScript exercises are intended to complement the JavaScript content on The Odin Project (TOP). They should only be done when instructed during the course of the curriculum.
**Note:** The `generator-exercise` file is not actually an exercise; it is a script that generates exercises. It was created to help efficiently write these exercises.
## Contributing
If you have a suggestion to improve an exercise, an idea for a new exercise, or notice an issue with an exercise, please feel free to open an issue after thoroughly reading our [contributing guide](https://github.com/TheOdinProject/.github/blob/main/CONTRIBUTING.md).
## How To Use These Exercises
1. Fork and clone this repository. To learn how to fork a repository, see the GitHub documentation on how to [fork a repo](https://docs.github.com/en/get-started/quickstart/fork-a-repo).
- Copies of repositories on your machine are called clones. If you need help cloning to your local environment you can learn how from the GitHub documentation on [cloning a repository](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository-from-github/cloning-a-repository).
1. Before you start working on any exercises, you should first ensure you have the following installed:
- **NPM**. You should have installed NPM already in our [Installing Node.js](https://www.theodinproject.com/paths/foundations/courses/foundations/lessons/installing-node-js) lesson. Just in case you need to check, type `npm --version` in your terminal. If you get back `Command 'npm' not found, but can be installed with:`, **do not follow the instructions in the terminal** to install with `apt-get` as this causes permission issues. Instead, go back to the installation lesson and install Node with NVM by following the instructions there.
- **Jest**. After cloning this repository to your local machine and installing NPM, go into the newly created directory (`cd javascript-exercises`) and run `npm install`. This will install Jest and set up the testing platform based on our preconfigured settings. (Note: if you get warnings that packages are out of date or contain vulnerabilities, you can safely ignore them for these exercises.)
@@ -23,6 +21,7 @@ If you have a suggestion to improve an exercise, an idea for a new exercise, or
- A `solutions` directory that contains a solution and the same test file with all of the tests unskipped.
To complete an exercise, you'll need to go to the exercise directory with `cd exerciseName` in the terminal and run `npm test exerciseName.spec.js`. This should run the test file and show you the output. When you first run a test, it will fail. This is by design! You must open the exercise file and write the code needed to get the test to pass.
1. Some of the exercises have test conditions defined in their spec file as `test.skip` compared to `test`. This is purposeful. After you pass one `test`, you will change the next `test.skip` to `test` and test your code again. You'll do this until all conditions are satisfied. **All tests must pass at the same time**, and you should not have any `test.skip` instances by the time you finish an exercise.
1. Once you successfully finish an exercise, check the `solutions` directory within each exercise to compare it with yours.
- You should not be checking the solution for an exercise until you finish it!
@@ -36,3 +35,13 @@ The first exercise, `helloWorld`, will walk you through the process in-depth.
## Debugging
To debug functions, you can run the tests in the Visual Studio Code debugger terminal. You can open this by clicking the "Run and Debug" icon on the left or pressing <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>D</kbd>, then clicking JavaScript Debug Terminal. You will be able to set breakpoints as you would in the Chrome DevTools debugger. You can run `npm test exerciseName.spec.js` to then execute your code up until your breakpoint and step through your code as necessary. **NOTE**: To take advantage of the debugger, you **MUST** run the script in the debugger terminal, not the bash or zsh terminal.
## Adding a new exercise
To add a new exercise:
1. Be sure you've ran `npm install` at the root of the `javascript-exercises` directory
2. Run the command `npm run generate`
3. When prompted, enter the name of the new exercise in camelCase syntax
After entering an exercise name, a new directory with the necessary files will be created. You will then need to update the `README.md` and `spec.js` files as well as the files in the `solution` directory of the new exercise.
-11
View File
@@ -1,11 +0,0 @@
root = true
[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false
-2
View File
@@ -1,2 +0,0 @@
coverage
**/templates
-1
View File
@@ -1 +0,0 @@
* text=auto
-2
View File
@@ -1,2 +0,0 @@
node_modules
coverage
-5
View File
@@ -1,5 +0,0 @@
language: node_js
node_js:
- 7
- 6
- 4
-9
View File
@@ -1,9 +0,0 @@
{
"generator-node": {
"promptValues": {
"authorName": "Cody Loyd",
"authorEmail": "codyloyd@gmail.com",
"authorUrl": "codyloyd.com"
}
}
}
-21
View File
@@ -1,21 +0,0 @@
The MIT License (MIT)
Copyright (c) 2017 Cody Loyd <codyloyd@gmail.com> (codyloyd.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-36
View File
@@ -1,36 +0,0 @@
# generator-exercise [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url]
> generates boilerplate for The Odin Project exercises
## Installation
First, install [Yeoman](http://yeoman.io) and generator-exercise using [npm](https://www.npmjs.com/) (we assume you have pre-installed [node.js](https://nodejs.org/)).
```bash
npm install -g yo
npm install -g generator-exercise
```
Then generate your new project:
```bash
yo exercise
```
## Getting To Know Yeoman
* Yeoman has a heart of gold.
* Yeoman is a person with feelings and opinions, but is very easy to work with.
* Yeoman can be too opinionated at times but is easily convinced not to be.
* Feel free to [learn more about Yeoman](http://yeoman.io/).
## License
MIT © [Cody Loyd](codyloyd.com)
[npm-image]: https://badge.fury.io/js/generator-exercise.svg
[npm-url]: https://npmjs.org/package/generator-exercise
[travis-image]: https://travis-ci.org/codyloyd/generator-exercise.svg?branch=master
[travis-url]: https://travis-ci.org/codyloyd/generator-exercise
[daviddm-image]: https://david-dm.org/codyloyd/generator-exercise.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/codyloyd/generator-exercise
@@ -1,41 +0,0 @@
'use strict';
const Generator = require('yeoman-generator');
const chalk = require('chalk');
const yosay = require('yosay');
module.exports = class extends Generator {
prompting() {
// Have Yeoman greet the user.
this.log(chalk.red('Let\'s do this'));
const prompts = [{
type: 'input',
name: 'title',
message: 'Enter the exercise title',
default: 'title'
}];
return this.prompt(prompts).then(props => {
// To access props later use this.props.someAnswer;
this.props = props;
});
}
writing() {
this.fs.copyTpl(
this.templatePath(`title.js`),
this.destinationPath(`${this.props.title}.js`),
{title: this.props.title}
);
this.fs.copyTpl(
this.templatePath(`title.spec.js`),
this.destinationPath(`${this.props.title}.spec.js`),
{title: this.props.title}
);
this.fs.copyTpl(
this.templatePath(`README.md`),
this.destinationPath(`README.md`),
{title: this.props.title}
);
}
};
@@ -1,2 +0,0 @@
# Exercise XX - <%= title %>
@@ -1 +0,0 @@
<%= title %>
@@ -1,5 +0,0 @@
let <%= title %> = function() {
};
module.exports = <%= title %>;
@@ -1,8 +0,0 @@
let <%= title %> = require('./<%=title%>')
describe('<%=title%>', function() {
test('EDITME', function() {
expect(<%=title%>()).toEqual(' ');
});
});
File diff suppressed because it is too large Load Diff
-51
View File
@@ -1,51 +0,0 @@
{
"name": "generator-exercise",
"version": "0.0.0",
"description": "generates boilerplate for The Odin Project exercises",
"homepage": "theodinproject.com",
"author": {
"name": "Cody Loyd",
"email": "codyloyd@gmail.com",
"url": "codyloyd.com"
},
"files": [
"generators"
],
"main": "generators/index.js",
"keywords": [
"lame",
"yeoman-generator"
],
"devDependencies": {
"yeoman-test": "^1.6.0",
"yeoman-assert": "^3.0.0",
"nsp": "^3.2.1",
"eslint": "^4.1.0",
"eslint-config-xo-space": "^0.16.0",
"jest": "^19.0.2",
"jest-cli": "^20.0.0"
},
"dependencies": {
"chalk": "^1.1.3",
"extend": "^3.0.2",
"yeoman-generator": "^1.0.0",
"yosay": "^2.0.0"
},
"jest": {
"testEnvironment": "node"
},
"scripts": {
"prepublish": "nsp check",
"pretest": "eslint . --fix",
"test": "jest"
},
"eslintConfig": {
"extends": "xo-space",
"env": {
"jest": true,
"node": true
}
},
"repository": "git@github.com:TheOdinProject/javascript-exercises.git",
"license": "MIT"
}
+30
View File
@@ -0,0 +1,30 @@
const { readdir } = require("fs/promises");
function splitDirectoryName(directoryName) {
return {
exerciseNumber: directoryName.match(/\d+/),
exerciseName: directoryName.match(/[a-z]+/i),
};
}
async function getLatestExerciseDirectory() {
try {
const files = await readdir("./");
return files.findLast((file) => /^\d+_\w+$/.test(file));
} catch (err) {
console.error(err);
}
}
async function createExerciseDirectoryName(directoryName) {
const latestExerciseDirectory = await getLatestExerciseDirectory();
const latestExerciseNumber = parseInt(latestExerciseDirectory.match(/^\d+/));
if (latestExerciseDirectory === `${latestExerciseNumber}_${directoryName}`) {
throw new Error(`Exercise already exists with name "${directoryName}"`);
}
return `${latestExerciseNumber + 1}_${directoryName}`;
}
module.exports = { createExerciseDirectoryName, splitDirectoryName };
+25
View File
@@ -0,0 +1,25 @@
const { writeFile } = require("fs/promises");
const { join } = require("path");
const { splitDirectoryName } = require("./helpers");
async function writeExercise(exercisePath) {
const { exerciseName } = splitDirectoryName(exercisePath);
const isSolutionFile = exercisePath.includes("/solution");
const exerciseContent = `const ${exerciseName} = function() {
${isSolutionFile ? "// Replace this comment with the solution code" : ""}
};
// Do not edit below this line
module.exports = ${exerciseName};
`;
await writeFile(
join(
exercisePath,
`${exerciseName}${isSolutionFile ? "-solution" : ""}.js`
),
exerciseContent
);
}
module.exports = { writeExercise };
+35
View File
@@ -0,0 +1,35 @@
const { writeFile } = require("fs/promises");
const { join } = require("path");
const { splitDirectoryName } = require("./helpers");
async function writeExerciseSpec(exercisePath) {
const { exerciseName } = splitDirectoryName(exercisePath);
const isSolutionFile = exercisePath.includes("/solution");
const trueExerciseName = isSolutionFile
? `${exerciseName}-solution`
: exerciseName;
const exerciseSpecContent = `const ${exerciseName} = require('./${trueExerciseName}');
describe('${exerciseName}', () => {
test('First test description', () => {
// Replace this comment with any other necessary code, and update the expect line as necessary
expect(${exerciseName}()).toBe('');
});
test${isSolutionFile ? "" : ".skip"}('Second test description', () => {
// Replace this comment with any other necessary code, and update the expect line as necessary
expect(${exerciseName}()).toBe('');
});
});
`;
await writeFile(
join(exercisePath, `${trueExerciseName}.spec.js`),
exerciseSpecContent
);
}
module.exports = { writeExerciseSpec };
+15
View File
@@ -0,0 +1,15 @@
const { writeFile } = require("fs/promises");
const { join } = require("path");
const { splitDirectoryName } = require("./helpers");
async function writeReadme(exercisePath) {
const { exerciseNumber, exerciseName } = splitDirectoryName(exercisePath);
const readmeContent = `# Exercise ${exerciseNumber} - ${exerciseName}
Description of the exercise goes here.
`;
await writeFile(join(exercisePath, "README.md"), readmeContent);
}
module.exports = { writeReadme };
+1769 -20
View File
File diff suppressed because it is too large Load Diff
+6 -3
View File
@@ -14,21 +14,24 @@
},
"homepage": "https://github.com/TheOdinProject/javascript-exercises#readme",
"devDependencies": {
"case-anything": "^2.1.13",
"eslint": "^8.47.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.28.1",
"jest": "^29.6.4",
"jest-cli": "^29.6.4"
"jest-cli": "^29.6.4",
"plop": "^4.0.1"
},
"scripts": {
"test": "jest"
"test": "jest",
"generate": "plop"
},
"eslintConfig": {
"root": true
},
"jest": {
"testPathIgnorePatterns": [
"generator-exercise/"
"generators/"
]
}
}
+46
View File
@@ -0,0 +1,46 @@
const { mkdir } = require("fs/promises");
const { join } = require("path");
const { camelCase } = require("case-anything");
const { createExerciseDirectoryName } = require("./generators/helpers");
const { writeReadme } = require("./generators/writeReadme");
const { writeExercise } = require("./generators/writeExercise");
const { writeExerciseSpec } = require("./generators/writeExerciseSpec");
module.exports = function (plop) {
plop.setActionType("createExercise", async function (answers) {
const { exerciseName } = answers;
if (!exerciseName) {
throw new Error(
`Invalid exerciseName. Expected: valid string. Actual: "${exerciseName}"`
);
}
const camelExerciseName = camelCase(exerciseName);
const exerciseDirectoryName = await createExerciseDirectoryName(
camelExerciseName
);
const basePath = join("./", exerciseDirectoryName);
const solutionPath = join(basePath, "solution");
await mkdir(basePath);
await mkdir(solutionPath);
await writeReadme(basePath);
await writeExercise(basePath);
await writeExercise(solutionPath);
await writeExerciseSpec(basePath);
await writeExerciseSpec(solutionPath);
});
plop.setGenerator("Basic", {
description: "Create a basic JavaScript exercise.",
prompts: [
{
type: "input",
name: "exerciseName",
message: "What is the name of the exercise? (camelCase)",
},
],
actions: [{ type: "createExercise" }],
});
};