Skip to content

Commit

Permalink
fix: fixed bug where files were being extracted to the wrong location
Browse files Browse the repository at this point in the history
  • Loading branch information
atrnh committed Jun 22, 2023
1 parent d4dc369 commit 50c35d2
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#!/usr/bin/env node
import process from "node:process";
import { homedir, tmpdir } from "node:os";
import { join, parse as parsePath } from "node:path";
import { writeFileSync, unlinkSync, existsSync } from "node:fs";
import { Command } from "commander";
import chalk from "chalk";
import { Command } from "commander";
import extract from "extract-zip";
import { existsSync, unlinkSync, writeFileSync } from "node:fs";
import { homedir, tmpdir } from "node:os";
import { join, parse as parsePath } from "node:path";
import process from "node:process";

import {
NAME,
DESCRIPTION,
BASE_URL,
EXERCISE_URL,
HOMEWORK_URL,
DEMO_URL,
DESCRIPTION,
DESTINATION,
EXERCISE_URL,
HOMEWORK_URL,
NAME,
} from "./src/constants.mjs";
import {
afterSuccess,
Expand All @@ -26,7 +26,6 @@ import {
const cmd = new Command();

let tempfile;
let projDir;

const logTask = (msg, opts = {}) => {
const { prefix, prefixChalk } = opts;
Expand Down Expand Up @@ -78,18 +77,12 @@ const extractZip = async (zipFile, destination) => {
const pathParts = parsePath(fileName);
const dest = join(destination, fileName);

if (!pathParts.dir) {
projDir = join(destination, pathParts.name);
}

if (!pathParts.dir && existsSync(dest)) {
throw new Error(`\
Can't extract files because ${fileName} already exists in ${abbreviateHome(
destination
)}.
Can't extract files because ${abbreviateHome(destination)} already exists.
If you really want to overwrite it, delete ${abbreviateHome(
dest
destination
)} and try again.
`);
}
Expand Down Expand Up @@ -170,15 +163,20 @@ const run = async (cmd, slug, opts) => {
logSubtask(`Temporarily saved file to ${tempfile}`);

// Extract zip file to destination
const destination = join(opts.path, opts.homework ? "homework" : "");
const destination = join(
opts.path,
opts.demo ? "demos" : opts.homework ? "homework" : "",
slug
);
await extractZip(tempfile, destination);
logSubtask(`Extracting files to ${abbreviateHome(destination)}`);

console.log(chalk.bold(`${chalk.bgGreen(" ✔ ")} Success!\n`));
console.log(afterSuccess(abbreviateHome(destination)));
} catch (err) {
cmd.error(err.message);
}

console.log(chalk.bold(`${chalk.bgGreen(" ✔ ")} Success!\n`));
console.log(afterSuccess(abbreviateHome(projDir)));
cleanup();
};

Expand Down

0 comments on commit 50c35d2

Please sign in to comment.