Skip to content

Commit

Permalink
logs
Browse files Browse the repository at this point in the history
  • Loading branch information
edsilv committed Apr 29, 2021
1 parent 5598244 commit 21974d3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 77 deletions.
56 changes: 12 additions & 44 deletions Canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import { basename, dirname, extname, join } from "path";
import { Directory } from "./Directory";
import { IConfigJSON } from "./IConfigJSON";
import { promise as glob } from "glob-promise";
import { cloneJson, compare, fileExists, formatMetadata, generateImageTiles, getFileDimensions, getFormatByExtension, getFormatByExtensionAndType, getFormatByType, getLabel, getThumbnail, getTypeByExtension, getTypeByFormat, isDirectory, isURL, normaliseFilePath, normaliseType, readYml } from "./Utils";
import { cloneJson, compare, fileExists, formatMetadata, generateImageTiles, getFileDimensions, getFormatByExtension, getFormatByExtensionAndType, getFormatByType, getLabel, getThumbnail, getTypeByExtension, getTypeByFormat, isDirectory, isURL, log, normaliseFilePath, normaliseType, readYml, warn } from "./Utils";
import annotationBoilerplate from "./boilerplate/annotation.json";
import chalk from "chalk";
import config from "./config.json";
import imageServiceBoilerplate from "./boilerplate/imageservice.json";
import urljoin from "url-join";
Expand Down Expand Up @@ -91,14 +90,7 @@ export class Canvas {
if (!motivation) {
// assume painting
motivation = normaliseType(AnnotationMotivation.PAINTING);
console.warn(
chalk.yellow(
"motivation property missing in " +
file +
", guessed " +
motivation
)
);
warn(`motivation property missing in ${file} guessed ${motivation}`)
}

motivation = normaliseType(motivation);
Expand Down Expand Up @@ -143,11 +135,7 @@ export class Canvas {

if (type) {
annotationJson.body.type = type;
console.warn(
chalk.yellow(
"type property missing in " + file + ", guessed " + type
)
);
warn(`type property missing in ${file}, guessed ${type}`);
}
} else if (yml.format) {
// guess the type from the format
Expand All @@ -158,17 +146,13 @@ export class Canvas {

if (type) {
annotationJson.body.type = type;
console.warn(
chalk.yellow(
"type property missing in " + file + ", guessed " + type
)
);
warn(`type property missing in ${file}, guessed ${type}`);
}
}

if (!annotationJson.body.type) {
delete annotationJson.body.type;
console.warn(chalk.yellow("unable to determine type of " + file));
warn(`unable to determine type of ${file}`);
}

if (yml.format) {
Expand All @@ -183,11 +167,7 @@ export class Canvas {

if (format) {
annotationJson.body.format = format;
console.warn(
chalk.yellow(
"format property missing in " + file + ", guessed " + format
)
);
warn(`format property missing in ${file}, guessed ${format}`);
}
} else if (yml.value && extname(yml.value)) {
// guess the format from the extension
Expand All @@ -198,11 +178,7 @@ export class Canvas {

if (format) {
annotationJson.body.format = format;
console.warn(
chalk.yellow(
"format property missing in " + file + ", guessed " + format
)
);
warn(`format property missing in ${file}, guessed ${format}`);
}
} else if (yml.type) {
// can only guess the format from the type if there is one typeformat for this motivation.
Expand All @@ -213,17 +189,13 @@ export class Canvas {

if (format) {
annotationJson.body.format = format;
console.warn(
chalk.yellow(
"format property missing in " + file + ", guessed " + format
)
);
warn(`format property missing in ${file}, guessed ${format}`);
}
}

if (!annotationJson.body.format) {
delete annotationJson.body.format;
console.warn(chalk.yellow("unable to determine format of " + file));
warn(`unable to determine format of ${file}`);
}

if (yml.label) {
Expand Down Expand Up @@ -300,11 +272,7 @@ export class Canvas {
}

if (!canvasJson.items[0].items.length) {
console.warn(
chalk.yellow(
"Could not find any files to annotate onto " + this.directoryPath
)
);
warn(`Could not find any files to annotate onto ${this.directoryPath}`);
}

// if there's no thumb.[jpg, gif, png]
Expand Down Expand Up @@ -391,9 +359,9 @@ export class Canvas {

if (exists) {
this.infoYml = await readYml(ymlPath);
console.log(chalk.green("got metadata for: ") + this.directoryPath);
log(`got metadata for: ${this.directoryPath}`);
} else {
console.log(chalk.green("no metadata found for: ") + this.directoryPath);
log(`no metadata found for: ${this.directoryPath}`);
}

if (!this.infoYml.label) {
Expand Down
43 changes: 13 additions & 30 deletions Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { Canvas } from "./Canvas";
import { join, basename } from "path";
import { promise as glob } from "glob-promise";
import { URL } from "url";
import { cloneJson, compare, fileExists, formatMetadata, getLabel, getThumbnail, hasManifestsYml, readYml, writeJson } from "./Utils";
import chalk from "chalk";
import { cloneJson, compare, fileExists, formatMetadata, getLabel, getThumbnail, hasManifestsYml, log, readYml, warn, writeJson } from "./Utils";
// import urljoin from "url-join";
const urljoin = require("url-join");
// boilerplate json
Expand Down Expand Up @@ -53,7 +52,7 @@ export class Directory {

await Promise.all(
canvases.map(async (canvas: string) => {
console.log(chalk.green("creating canvas for: ") + canvas);
log(`creating canvas for: ${canvas}`);
this.items.push(new Canvas(canvas, this));
})
);
Expand All @@ -78,7 +77,7 @@ export class Directory {

await Promise.all(
directories.map(async (directory: string) => {
console.log(chalk.green("creating directory for: ") + directory);
log(`creating directory for: ${directory}`);
const name: string = basename(directory);
const url: string = urljoin(this.url.href, name);
const newDirectory: Directory = new Directory(
Expand Down Expand Up @@ -106,7 +105,7 @@ export class Directory {
});

paintableFiles.forEach((file: string) => {
console.log(chalk.green("creating canvas for: ") + file);
log(`creating canvas for: ${file}`);
this.items.push(new Canvas(file, this));
});
}
Expand All @@ -119,28 +118,16 @@ export class Directory {
await this._createIndexJson();

if (this.isCollection) {
console.log(chalk.green("created collection: ") + this.directoryPath);
log(`created collection: ${this.directoryPath}`);
// if there are canvases, warn that they are being ignored
if (this.items.length) {
console.warn(
chalk.yellow(
this.items.length +
" unused canvas directories (starting with an underscore) found in the " +
this.directoryPath +
" collection. Remove directories not starting with an underscore to convert into a manifest."
)
);
warn(`${this.items.length} unused canvas directories (starting with an underscore) found in the ${this.directoryPath} collection. Remove directories not starting with an underscore to convert into a manifest.`);
}
} else {
console.log(chalk.green("created manifest: ") + this.directoryPath);
log(`created manifest: ${this.directoryPath}`);
// if there aren't any canvases, warn that there should be
if (!this.items.length) {
console.warn(
chalk.yellow(
this.directoryPath +
" is a manifest, but no canvases (directories starting with an underscore) were found. Therefore it will not have any content."
)
);
warn(`${this.directoryPath} is a manifest, but no canvases (directories starting with an underscore) were found. Therefore it will not have any content.`);
}
}
}
Expand All @@ -155,9 +142,9 @@ export class Directory {

if (exists) {
this.infoYml = await readYml(ymlPath);
console.log(chalk.green("got metadata for: ") + this.directoryPath);
log(`got metadata for: ${this.directoryPath}`);
} else {
console.log(chalk.green("no metadata found for: ") + this.directoryPath);
log(`no metadata found for: ${this.directoryPath}`);
}

if (!this.infoYml.label) {
Expand Down Expand Up @@ -229,13 +216,9 @@ export class Directory {
this.indexJson.items.push(itemJson);
});

console.log(
chalk.green("parsed manifests.yml for: ") + this.directoryPath
);
log(`parsed manifests.yml for ${this.directoryPath}`);
} else {
console.log(
chalk.green("no manifests.yml found for: ") + this.directoryPath
);
log(`no manifests.yml found for: ${this.directoryPath}`);
}

// sort items
Expand Down Expand Up @@ -288,7 +271,7 @@ export class Directory {
const path: string = join(this.directoryPath, "index.json");
const json: string = JSON.stringify(this.indexJson, null, " ");

console.log(chalk.green("creating index.json for: ") + this.directoryPath);
log(`creating index.json for: ${this.directoryPath}`);

await writeJson(path, json);
}
Expand Down
19 changes: 19 additions & 0 deletions Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,22 @@ export const hasManifestsYml = (path: string): Promise<boolean> => {
export const isURL = (path: string): boolean => {
return isurl(path);
}

export const log = (message: string): void => {
console.log(
chalk.green(message)
);
}

export const warn = (message: string): void => {
console.warn(
chalk.yellow(message)
);
}

export const error = (message: string): void => {
console.warn(
chalk.red(message)
);
}

6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Directory } from "./Directory";
import { fileExists } from "./Utils";
import { fileExists, log } from "./Utils";
import chalk from "chalk";

export const build = async (
dir: string,
url: string,
virtualName?: string
): Promise<void> => {
console.log(chalk.white("started biiifing " + dir));
log(`started biiifing ${dir}`);

// validate inputs

Expand All @@ -25,5 +25,5 @@ export const build = async (

await directory.read();

console.log(chalk.white("finished biiifing " + dir));
log(`finished biiifing ${dir}`);
};

0 comments on commit 21974d3

Please sign in to comment.