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

Revert "formatting" #27

Merged
merged 1 commit into from
Dec 7, 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
8 changes: 4 additions & 4 deletions scripts/ProjectSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const PROJECTS_PATH = resolve(__dirname, "..", "projects.json");

class ProjectSet {
hasRead = false;
projectSet = new Set();
projectSet = new Set()

read() {
// If the program has already read from the projects file...
Expand All @@ -23,11 +23,11 @@ class ProjectSet {
}

toArray() {
return [...this.projectSet].sort((a, b) => a - b);
return [...this.projectSet].sort((a, b) => a - b)
}

has(id) {
return this.projectSet.has(id);
return this.projectSet.has(id)
}

add(id) {
Expand All @@ -36,7 +36,7 @@ class ProjectSet {

save() {
// The "null, 2" part prettifies the JSON file
writeFileSync(PROJECTS_PATH, JSON.stringify(this.toArray(), null, 2));
writeFileSync(PROJECTS_PATH, JSON.stringify(this.toArray(), null, 2))
}
}

Expand Down
35 changes: 17 additions & 18 deletions scripts/add-project.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import fetch from "node-fetch";
import { read, has, add, save } from "./ProjectSet";
import saveProject from "./save-project";
import collateProjects from "./collate-projects";
const fetch = require("node-fetch")
const projectSet = require("./ProjectSet")
const saveProject = require("./save-project");
const collateProjects = require('./collate-projects');

read();
projectSet.read()

const id = process.env.PROJECT_ID || process.argv.slice(2)[0];
if (!id) throw new Error("A project ID was not provided to the client.");
if (has(id))
throw new Error("This project already is being listened to");
const id = process.env.PROJECT_ID || process.argv.slice(2)[0]
if (!id) throw new Error('A project ID was not provided to the client.')
if (projectSet.has(id)) throw new Error("This project already is being listened to")

fetch(`https://api.scratch.mit.edu/projects/${id}/`).then((res) => {
if (!res.ok)
throw new Error(`Cannot fetch ${res.url} because of error ${res.status}`);
console.log("Archiving: ", id);
saveProject(id);
add(id);
save();
collateProjects();
});
fetch(`https://api.scratch.mit.edu/projects/${id}/`)
.then((res) => {
if (!res.ok) throw new Error(`Cannot fetch ${res.url} because of error ${res.status}`)
console.log("Archiving: ", id);
saveProject(id)
projectSet.add(id)
projectSet.save()
collateProjects()
})
30 changes: 14 additions & 16 deletions scripts/collate-projects.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import { readdirSync, existsSync, readFileSync, writeFileSync } from "fs";
import { resolve } from "path";
const fs = require("fs");
const path = require("path");

const DIST_PROJECT_FILE = resolve(__dirname, "..", "dist", "projects.json");
const DIST_PATH = resolve(__dirname, "..", "dist");
const PROJECTS_FOLDER = resolve(DIST_PATH, "projects");
const DIST_PROJECT_FILE = path.resolve(__dirname, "..", "dist", "projects.json");
const DIST_PATH = path.resolve(__dirname, '..', 'dist')
const PROJECTS_FOLDER = path.resolve(DIST_PATH, 'projects')

const jsons = [];

const collateProjects = () => {
const nodes = readdirSync(PROJECTS_FOLDER);
const nodes = fs.readdirSync(PROJECTS_FOLDER);
for (const node of nodes) {
const jsonLocation = resolve(PROJECTS_FOLDER, node, "metadata.json");
if (existsSync(jsonLocation)) {
const file = readFileSync(jsonLocation, { encoding: "utf-8" });
jsons.push(JSON.parse(file));
jsons.sort(function (a, b) {
return b + a;
});
const jsonLocation = path.resolve(PROJECTS_FOLDER, node, 'metadata.json');
if (fs.existsSync(jsonLocation)) {
const file = fs.readFileSync(jsonLocation, { encoding: 'utf-8' })
jsons.push(JSON.parse(file))
jsons.sort(function(a, b){return b + a})
}
}

writeFileSync(DIST_PROJECT_FILE, JSON.stringify(jsons));
};
fs.writeFileSync(DIST_PROJECT_FILE, JSON.stringify(jsons))
}

export default collateProjects;
module.exports = collateProjects
21 changes: 9 additions & 12 deletions scripts/save-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,19 @@ const saveProject = async (id) => {
filename = "project.sb";

fs.writeFileSync(path.resolve(PROJECT_FOLDER, filename), buffer);
return "1.4";
return "1.4"
}

const text = buffer.toString();
const parsed = JSON.parse(text);

if (parsed.info) {
version = "2.0";
} else if (parsed.meta) {
version = "3.0";
}

fs.writeFileSync(
path.resolve(PROJECT_FOLDER, filename),
JSON.stringify(parsed, null, 2)
);
fs.writeFileSync(path.resolve(PROJECT_FOLDER, filename), JSON.stringify(parsed, null, 2));

return version;
};
Expand All @@ -96,17 +93,17 @@ const saveProject = async (id) => {
);
const ocularStats = await saveJSON(
`https://my-ocular.jeffalo.net/api/user/${metadata.author.username}`,
"status.json"
);
'status.json'
)
fs.writeFileSync(
path.resolve(PROJECT_FOLDER, "metadata.json"),
JSON.stringify(
{
id: metadata.id,
title: metadata.title || `Untitled Project ${metadata.id}`,
author: metadata.author.username || "Unknown",
pfp: metadata.author.profile.images["60x60"],
color: ocularStats.color || false,
title: (metadata.title || `Untitled Project ${metadata.id}`),
author: (metadata.author.username || 'Unknown'),
pfp: metadata.author.profile.images['60x60'],
color: (ocularStats.color || false),
created: metadata.history.created,
modified: metadata.history.modified,
version,
Expand Down
18 changes: 9 additions & 9 deletions scripts/update-projects.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { read, toArray } from "./ProjectSet";
import saveProject from "./save-project";
import collateProjects from "./collate-projects";
const projectSet = require("./ProjectSet")
const saveProject = require("./save-project");
const collateProjects = require('./collate-projects');

read();
projectSet.read()

const updateProjects = async () => {
console.log("Updating Projects...");
for (const id of toArray()) {
for (const id of projectSet.toArray()) {
try {
await saveProject(id);
console.log(`Saved ${id}!`);
} catch (e) {
console.error(`Failed to save ${id} because of an error: ${e}`);
console.log(`Saved ${id}!`)
} catch(e) {
console.error(`Failed to save ${id} because of an error: ${e}`)
}
}
console.log("Collating Projects...");
console.log('Collating Projects...')
collateProjects();
console.log("Updates Completed!");
};
Expand Down
Loading