feat: initial boilerplate
This commit is contained in:
62
api/libraries/utilities/dedent.ts
Normal file
62
api/libraries/utilities/dedent.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Removes excess indentation caused by using multiline template strings.
|
||||
*
|
||||
* Ported from `dedent-js` solution.
|
||||
*
|
||||
* @see https://github.com/MartinKolarik/dedent-js
|
||||
*
|
||||
* @param templateStrings - Template strings to dedent.
|
||||
*
|
||||
* @example
|
||||
* {
|
||||
* nested: {
|
||||
* examples: [
|
||||
* dedent(`
|
||||
* I am 8 spaces off from the beginning of this file.
|
||||
* But I will be 2 spaces based on the trimmed distance
|
||||
* of the first line.
|
||||
* `),
|
||||
* ]
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
export function dedent(templateStrings: TemplateStringsArray | string, ...values: any[]) {
|
||||
const matches = [];
|
||||
const strings = typeof templateStrings === "string" ? [templateStrings] : templateStrings.slice();
|
||||
|
||||
// Remove trailing whitespace.
|
||||
|
||||
strings[strings.length - 1] = strings[strings.length - 1].replace(/\r?\n([\t ]*)$/, "");
|
||||
|
||||
// Find all line breaks to determine the highest common indentation level.
|
||||
|
||||
for (let i = 0; i < strings.length; i++) {
|
||||
const match = strings[i].match(/\n[\t ]+/g);
|
||||
if (match) {
|
||||
matches.push(...match);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the common indentation from all strings.
|
||||
|
||||
if (matches.length) {
|
||||
const size = Math.min(...matches.map((value) => value.length - 1));
|
||||
const pattern = new RegExp(`\n[\t ]{${size}}`, "g");
|
||||
for (let i = 0; i < strings.length; i++) {
|
||||
strings[i] = strings[i].replace(pattern, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Remove leading whitespace.
|
||||
|
||||
strings[0] = strings[0].replace(/^\r?\n/, "");
|
||||
|
||||
// Perform interpolation.
|
||||
|
||||
let string = strings[0];
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
string += values[i] + strings[i + 1];
|
||||
}
|
||||
|
||||
return string;
|
||||
}
|
||||
41
api/libraries/utilities/generate.ts
Normal file
41
api/libraries/utilities/generate.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Traverse path and look for a `generate.ts` file in each folder found under
|
||||
* the given path. If a `generate.ts` file is found it is imported so its content
|
||||
* is executed.
|
||||
*
|
||||
* @param path - Path to resolve `generate.ts` files.
|
||||
* @param filter - Which folders found under the given path to ignore.
|
||||
*/
|
||||
export async function generate(path: string, filter: string[] = []): Promise<void> {
|
||||
const generate: string[] = [];
|
||||
for await (const entry of Deno.readDir(path)) {
|
||||
if (entry.isDirectory === true) {
|
||||
const moduleName = path.split("/").pop();
|
||||
if (moduleName === undefined) {
|
||||
continue;
|
||||
}
|
||||
if (filter.length > 0 && filter.includes(moduleName) === false) {
|
||||
continue;
|
||||
}
|
||||
const filePath = `${path}/${entry.name}/.tasks/generate.ts`;
|
||||
if (await hasFile(filePath)) {
|
||||
generate.push(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const filePath of generate) {
|
||||
await import(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
async function hasFile(filePath: string) {
|
||||
try {
|
||||
await Deno.lstat(filePath);
|
||||
} catch (err) {
|
||||
if (!(err instanceof Deno.errors.NotFound)) {
|
||||
throw err;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user