Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Sparta Sync command and allow for local testing #6

Merged
merged 2 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}