Skip to content

Commit

Permalink
Fix Sparta Sync command and allow for local testing (#6)
Browse files Browse the repository at this point in the history
## Description

This PR has two main goals:
- fixing the fact that `sparta sync` overrides the repository even when the exercises already exists with the same SHA,
- and allowing the use of a local instance of Sparta API.

`sparta init` has now two hidden flags: `--force` (or `-f`) and `--spartaURL="url"`.
Both have default values so it will not change the usage for students.
`--spartaURL` is there to change the URL that the CLI will ask for the calendar (which can now be set to our `.local` instance) and will record that in the `config.json` that the CLI generates at init
`--force` is a convenience option to recreate an `exercises` directory when calling `sparta init`.


## Motivation and Context

We couldn't tell the previous students to use `sparta sync` even if they missed some day because it would have overwritten all their exercises.

## How Has This Been Tested?

Manually locally

## Types of changes

- ~Chore (non-breaking change which refactors / improves the existing code base)~
- Bug fix (non-breaking change which fixes an issue)
- ~New feature (non-breaking change which adds functionality)~
- ~Breaking change (fix or feature that would cause existing functionality to 
  change)~

## Checklist:

- ✅ : My code follows the code style of this project.
- ✅ : My change requires a change to the documentation.
- ✅ : I have updated the documentation accordingly.
- ✅ : I have read the [**CONTRIBUTING**][CONTRIBUTING_FILE] document.
- 🔴 : I have added tests to cover my changes.
- ✅ : All new and existing tests passed.

[CONTRIBUTING_FILE]: https://github.com/fewlinesco/guidelines/blob/master/CONTRIBUTING.adoc
  • Loading branch information
Fenntasy authored Feb 19, 2021
1 parent 2fc812c commit 4618fc8
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 25 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Sparta CLI
======
# Sparta CLI

# Installation

Expand All @@ -9,7 +8,9 @@ $ yarn install
```

# Usage

<!-- usage -->

```sh-session
$ npm install -g sparta
$ sparta COMMAND
Expand All @@ -21,13 +22,17 @@ USAGE
$ sparta COMMAND
...
```

<!-- usagestop -->

# Commands

<!-- commands -->
* [`sparta help [COMMAND]`](#sparta-help-command)
* [`sparta init`](#sparta-init)
* [`sparta sync`](#sparta-sync)
* [`sparta today`](#sparta-today)

- [`sparta help [COMMAND]`](#sparta-help-command)
- [`sparta init`](#sparta-init)
- [`sparta sync`](#sparta-sync)
- [`sparta today`](#sparta-today)

## `sparta help [COMMAND]`

Expand Down Expand Up @@ -58,6 +63,11 @@ EXAMPLE
$ sparta init
```

There are two flags that are hidden from the help (because we don't want students to use them):

- `--spartaURL="<url>"` will update the URL that will be called when talking to Sparta API.
- `--force` or `-f` will recreate the exercises repository (deleting the previous folder, beware).

## `sparta sync`

Updates all the courses for the past days
Expand All @@ -75,4 +85,5 @@ Downloads the exercises for the current day
USAGE
$ sparta today
```

<!-- commandsstop -->
2 changes: 1 addition & 1 deletion src/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Command, { flags } from "@oclif/command";
import Command from "@oclif/command";

import { SpartaError } from "./services/errors/sparta-error";

Expand Down
21 changes: 17 additions & 4 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { flags } from "@oclif/command";
import cli from "cli-ux";
import * as emoji from "node-emoji";

import Command from "../base";
import { loadConfig, ConfigInput, writeConfig } from "../config/config";
import { loadConfig, writeConfig } from "../config/config";
import initInstuctions from "../instructions/init";
import checkWorkspace from "../services/check-workspace";
import initExercicesRepository from "../services/init-exercises-repository";
Expand All @@ -14,19 +15,28 @@ export default class Init extends Command {

static examples = ["$ sparta init"];

static flags = {
force: flags.boolean({ char: "f", default: false, hidden: true }),
spartaURL: flags.string({
hidden: true,
default: "https://sparta.fewlines.dev",
}),
};

async run(): Promise<void> {
const configDir = this.config.configDir;
const userInput = await getUserInput();
const { flags } = this.parse(Init);

writeConfig(configDir, userInput);
writeConfig(configDir, { ...userInput, spartaURL: flags.spartaURL });

const config = loadConfig(configDir);

this.log(emoji.emojify(":crossed_fingers: Checking Workspace directory"));
checkWorkspace(config);

this.log(emoji.emojify(":robot_face: Initializing exercises repository"));
await initExercicesRepository(config);
await initExercicesRepository(config, flags.force);

cli.action.start(
emoji.emojify(":robot_face: Preparing the Sparta configuration"),
Expand All @@ -45,7 +55,10 @@ export default class Init extends Command {
}
}

async function getUserInput(): Promise<ConfigInput> {
async function getUserInput(): Promise<{
batchID: string;
sharedSecret: string;
}> {
const batchID = await cli.prompt("What is the ID of your batch ?");
const sharedSecret = await cli.prompt("Enter the Sparta secret token", {
type: "hide",
Expand Down
2 changes: 2 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ export interface Config {
exercisesCacheDir: string;
batchID: string;
sharedSecret: string;
spartaURL: string;
}

export interface ConfigInput {
batchID: string;
sharedSecret: string;
spartaURL: string;
}

export function loadConfig(configDir: string): Config {
Expand Down
6 changes: 5 additions & 1 deletion src/services/fetch-past-days-exercises-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export default async function fetchPastDaysExercisesPaths(
let calendar: Calendar;

try {
calendar = await fetchCalendar(config.batchID, config.sharedSecret);
calendar = await fetchCalendar(
config.spartaURL,
config.batchID,
config.sharedSecret,
);
} catch (error) {
throw new CalendarFetchError(error.message);
}
Expand Down
6 changes: 5 additions & 1 deletion src/services/fetch-today-exercises-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export default async function fetchTodayExercisesPath(
let calendar: Calendar;

try {
calendar = await fetchCalendar(config.batchID, config.sharedSecret);
calendar = await fetchCalendar(
config.spartaURL,
config.batchID,
config.sharedSecret,
);
} catch (error) {
throw new CalendarFetchError(error.message);
}
Expand Down
5 changes: 4 additions & 1 deletion src/services/init-exercises-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ export class ExercisesDirectoryExistsError extends SpartaError {

export default async function initExercicesRepository(
config: Config,
force = false,
): Promise<void> {
const directory = config.exercisesDir;

if (fs.existsSync(directory)) {
if (fs.existsSync(directory) && !force) {
throw new ExercisesDirectoryExistsError(directory);
} else if (force) {
fs.removeSync(directory);
}

fs.ensureDirSync(directory);
Expand Down
10 changes: 6 additions & 4 deletions src/services/update-today-exercises-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ export default async function updateDayExercisesDirectory(
const todayCurrentPath = path.join(exercisesDir, dayPath, "current");
const todaySHAPath = path.join(exercisesDir, dayPath, directorySHA);

fs.ensureDirSync(todaySHAPath);
fs.copySync(todayCachePath, todaySHAPath);
fs.removeSync(todayCurrentPath);
fs.ensureSymlinkSync(todaySHAPath, todayCurrentPath);
if (!fs.existsSync(todaySHAPath)) {
fs.ensureDirSync(todaySHAPath);
fs.copySync(todayCachePath, todaySHAPath);
fs.removeSync(todayCurrentPath);
fs.ensureSymlinkSync(todaySHAPath, todayCurrentPath);
}
}
12 changes: 5 additions & 7 deletions src/utils/fetch-calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import fetch from "node-fetch";
import Calendar from "../models/calendar";

export default async function fetchCalendar(
spartaURL: string,
batchID: string,
sharedSecret: string,
): Promise<Calendar> {
const response = await fetch(
`https://sparta.fewlines.dev/cli/calendar/${batchID}`,
{
headers: {
Authorization: `Bearer ${sharedSecret}`,
},
const response = await fetch(`${spartaURL}/cli/calendar/${batchID}`, {
headers: {
Authorization: `Bearer ${sharedSecret}`,
},
);
});

return response.json();
}

0 comments on commit 4618fc8

Please sign in to comment.