Skip to content
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
14 changes: 7 additions & 7 deletions core/loadConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

/* Dependencies */
import fs from 'node:fs';
import { promises as fs } from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import yargs from 'yargs';
Expand All @@ -20,7 +20,7 @@ import SaplingError from '../lib/SaplingError.js';
*
* @returns {object} Config
*/
export function digest() {
export async function digest() {
let config = {};

const argv = yargs(hideBin(process.argv)).argv;
Expand Down Expand Up @@ -74,9 +74,9 @@ export function digest() {
const configPath = path.join(this.dir, this.configFile || 'config.json');

/* Load the configuration */
if (fs.existsSync(configPath)) {
if (await this.utils.exists(configPath)) {
/* If we have a config file, let's load it */
const file = fs.readFileSync(configPath);
const file = await fs.readFile(configPath);

/* Parse and merge the config, or throw an error if it's malformed */
try {
Expand Down Expand Up @@ -110,9 +110,9 @@ export function digest() {
/* Check if there's a separate production config */
const prodConfigPath = path.join(this.dir, (this.configFile && this.configFile.replace('.json', `.${process.env.NODE_ENV}.json`)) || `config.${process.env.NODE_ENV}.json`);

if (fs.existsSync(prodConfigPath)) {
if (await this.utils.exists(prodConfigPath)) {
/* If we have a config file, let's load it */
const file = fs.readFileSync(prodConfigPath);
const file = await fs.readFile(prodConfigPath);

config = {};
Object.assign(config, defaultConfig);
Expand Down Expand Up @@ -143,7 +143,7 @@ export function digest() {
*/
export default async function loadConfig(next) {
/* Digest config */
this.config = digest.call(this);
this.config = await digest.call(this);
console.log('CONFIG', this.config);

/* Set the app name */
Expand Down
104 changes: 56 additions & 48 deletions core/loadController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

/* Dependencies */
import fs from 'node:fs';
import { promises as fs } from 'node:fs';
import path from 'node:path';

import { console } from '../lib/Cluster.js';
Expand All @@ -15,47 +15,51 @@ import Templating from '../lib/Templating.js';
*
* @returns {object} Controller
*/
export function digest() {
export async function digest() {
let controller = {};

/* Generate a controller from the available views */
if ((this.config.autoRouting === 'on' || this.config.autoRouting === true) && this.config.viewsDir !== null) {
const viewsPath = path.join(this.dir, this.config.viewsDir);

if (fs.existsSync(viewsPath) && fs.lstatSync(viewsPath).isDirectory()) {
/* Load all views in the views directory */
const views = this.utils.getFiles(viewsPath);
if (await this.utils.exists(viewsPath)) {
const viewsLstat = await fs.lstat(viewsPath);

/* Go through each view */
for (const view_ of views) {
const segments = path.relative(this.dir, view_).split('/');
if (viewsLstat.isDirectory()) {
/* Load all views in the views directory */
const views = await this.utils.getFiles(viewsPath);

/* Filter out the views where any segment begins with _ */
const protectedSegments = segments.filter(item => {
const re = /^_/;
return re.test(item);
});
/* Go through each view */
for (const view_ of views) {
const segments = path.relative(this.dir, view_).split('/');

if (protectedSegments.length > 0) {
continue;
}
/* Filter out the views where any segment begins with _ */
const protectedSegments = segments.filter(item => {
const re = /^_/;
return re.test(item);
});

/* Filter out any files that do not use the correct file extension */
if (this.config.extension !== null && view_.split('.').slice(-1)[0] !== this.config.extension) {
continue;
}
if (protectedSegments.length > 0) {
continue;
}

/* Filter out filesystem bits */
const view = view_.replace(path.resolve(this.dir, this.config.viewsDir), '').replace(`.${this.config.extension}`, '');
let route = view.replace('/index', '');
/* Filter out any files that do not use the correct file extension */
if (this.config.extension !== null && view_.split('.').slice(-1)[0] !== this.config.extension) {
continue;
}

/* Make sure root index is a slash and not an empty key */
if (route === '') {
route = '/';
}
/* Filter out filesystem bits */
const view = view_.replace(path.resolve(this.dir, this.config.viewsDir), '').replace(`.${this.config.extension}`, '');
let route = view.replace('/index', '');

/* Make sure root index is a slash and not an empty key */
if (route === '') {
route = '/';
}

/* Create an automatic GET route for a given view */
controller[route] = view.replace(/^\/+/g, '');
/* Create an automatic GET route for a given view */
controller[route] = view.replace(/^\/+/g, '');
}
}
}
}
Expand All @@ -64,26 +68,30 @@ export function digest() {
const controllerPath = path.join(this.dir, this.config.routes || '');

/* Load the controller file */
if (fs.existsSync(controllerPath) && fs.lstatSync(controllerPath).isFile()) {
/* Parse and merge the controller, or throw an error if it's malformed */
try {
/* Load the controller file */
const file = fs.readFileSync(controllerPath);
const routes = JSON.parse(file.toString());

/* Remove file extension */
for (const route of Object.keys(routes)) {
routes[route] = routes[route].split('.').slice(0, -1).join('.');
}
if (await this.utils.exists(controllerPath)) {
const controllerLstat = await fs.lstat(controllerPath);

if (controllerLstat.isFile()) {
/* Parse and merge the controller, or throw an error if it's malformed */
try {
/* Load the controller file */
const file = await fs.readFile(controllerPath);
const routes = JSON.parse(file.toString());

/* Remove file extension */
for (const route of Object.keys(routes)) {
routes[route] = routes[route].split('.').slice(0, -1).join('.');
}

/* Merge routes if autorouting, replace routes if not */
if (this.config.autoRouting === 'on' || this.config.autoRouting === true) {
Object.assign(controller, routes);
} else {
controller = routes;
/* Merge routes if autorouting, replace routes if not */
if (this.config.autoRouting === 'on' || this.config.autoRouting === true) {
Object.assign(controller, routes);
} else {
controller = routes;
}
} catch (error) {
console.error(`Controller at path: \`${controllerPath}\` could not be loaded.`, error);
}
} catch (error) {
console.error(`Controller at path: \`${controllerPath}\` could not be loaded.`, error);
}
}

Expand All @@ -102,7 +110,7 @@ export default async function loadController(next) {
await this.templating.importDriver();

/* Digest controller */
this.controller = digest.call(this, this.config);
this.controller = await digest.call(this, this.config);
console.log('CONTROLLER', this.controller);

if (next) {
Expand Down
6 changes: 3 additions & 3 deletions core/loadHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

/* Dependencies */
import fs from 'node:fs';
import { promises as fs } from 'node:fs';
import path from 'node:path';

import { console } from '../lib/Cluster.js';
Expand All @@ -23,14 +23,14 @@ export async function digest() {
const formattedHooks = {};

/* Load the hooks file */
if (fs.existsSync(hooksPath)) {
if (await this.utils.exists(hooksPath)) {
/* If we have a hooks file, let's load it */
let file = null;
let hooks = {};

/* Read the hooks file, or throw an error if it can't be done */
try {
file = fs.readFileSync(hooksPath);
file = await fs.readFile(hooksPath);
} catch {
throw new SaplingError(`Hooks at ${hooksPath} could not be read.`);
}
Expand Down
12 changes: 6 additions & 6 deletions core/loadModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

/* Dependencies */
import fs from 'node:fs';
import { promises as fs } from 'node:fs';
import path from 'node:path';

import { console } from '../lib/Cluster.js';
Expand All @@ -16,14 +16,14 @@ import Storage from '../lib/Storage.js';
*
* @returns {object} Schema
*/
export function digest() {
export async function digest() {
const modelPath = path.join(this.dir, this.config.modelsDir);
const schema = {};
let files = {};

/* Load all models in the model directory */
if (fs.existsSync(modelPath)) {
files = fs.readdirSync(modelPath);
if (await this.utils.exists(modelPath)) {
files = await fs.readdir(modelPath);
} else {
console.warn(`Models directory \`${modelPath}\` does not exist`);
}
Expand All @@ -38,7 +38,7 @@ export function digest() {
continue;
}

const model = fs.readFileSync(path.join(modelPath, file));
const model = await fs.readFile(path.join(modelPath, file));

/* Read the model JSON into the schema */
try {
Expand Down Expand Up @@ -80,7 +80,7 @@ export function digest() {
*/
export default async function loadModel(next) {
/* Create a storage instance based on the models */
this.storage = new Storage(this, digest.call(this));
this.storage = new Storage(this, await digest.call(this));

if (next) {
next();
Expand Down
8 changes: 4 additions & 4 deletions core/loadPermissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

/* Dependencies */
import fs from 'node:fs';
import { promises as fs } from 'node:fs';
import path from 'node:path';

import { console } from '../lib/Cluster.js';
Expand All @@ -16,14 +16,14 @@ import SaplingError from '../lib/SaplingError.js';
*
* @returns {object} Permissions
*/
export function digest() {
export async function digest() {
/* Load the permissions file */
const permissionsPath = path.join(this.dir, this.config.permissions);
const formattedPerms = {};
let loadedPerms = {};

try {
loadedPerms = JSON.parse(fs.readFileSync(permissionsPath));
loadedPerms = JSON.parse(await fs.readFile(permissionsPath));
} catch {
console.warn(`Permissions at path: ${permissionsPath} not found.`);
}
Expand Down Expand Up @@ -77,7 +77,7 @@ export function digest() {
*/
export default async function loadPermissions(next) {
/* Digest permissions */
this.permissions = digest.call(this);
this.permissions = await digest.call(this);

/* Loop over the urls in permissions */
for (const url of Object.keys(this.permissions)) {
Expand Down
4 changes: 2 additions & 2 deletions drivers/render/Html.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

/* Dependencies */
import fs from 'node:fs';
import { promises as fs } from 'node:fs';
import path from 'node:path';
import _ from 'underscore';
import SaplingError from '../../lib/SaplingError.js';
Expand Down Expand Up @@ -37,7 +37,7 @@ export default class HTML extends Interface {
/* Read the template file */
let html = '';
try {
html = fs.readFileSync(path.resolve(this.viewsPath, template), 'utf8');
html = await fs.readFile(path.resolve(this.viewsPath, template), 'utf8');
} catch (error) {
return new SaplingError(error);
}
Expand Down
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

/* Require native clustering bits */
import cluster from 'node:cluster';
import fs from 'node:fs';
import { promises as fs } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import process from 'node:process';
Expand All @@ -20,6 +20,8 @@ import yargs from 'yargs';
/* eslint-disable-next-line node/file-extension-in-import */
import { hideBin } from 'yargs/helpers';

import Utils from './lib/Utils.js';

import App from './app.js';


Expand All @@ -30,12 +32,12 @@ const argv = yargs(hideBin(process.argv)).argv;
const configPath = path.join(process.cwd(), 'config.json');
let sessionAvailable = false;

if (fs.existsSync(configPath)) {
/* If we have a config file, let's load it */
const file = fs.readFileSync(configPath);

/* If we have a config file, let's load it */
if (await new Utils().exists(configPath)) {
/* Parse config, or throw an error if it's malformed */
try {
const file = await fs.readFile(configPath);

const c = JSON.parse(file.toString());
if ('session' in c && 'driver' in c.session) {
sessionAvailable = true;
Expand Down
Loading