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

Convert to ESM #222

Merged
merged 16 commits into from
Aug 2, 2021
36 changes: 34 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
node-version: '16'

- name: Install dependencies
run: npm install
run: npm ci

- name: Run linter
run: npm run lint
Expand All @@ -28,3 +28,35 @@ jobs:

- name: Report coverage
run: npm run test:send

test-node-14:
name: Test Node.js v14
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install dependencies
run: npm ci

- name: Run test
run: npm run test

test-node-12:
name: Test Node.js v12
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '12'

- name: Install dependencies
run: npm ci

- name: Run test
run: npm run test
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ node_modules
npm-debug.log
env
tmp
.nyc_output
coverage
coverage.lcov
.DS_Store
53 changes: 28 additions & 25 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@


/* System dependencies */
const async = require('async');
import async from 'async';

/* Internal dependencies */
const { console } = require('./lib/Cluster');
const Response = require('./lib/Response');
const Utils = require('./lib/Utils');
import { console } from './lib/Cluster.js';
import Response from './lib/Response.js';
import Utils from './lib/Utils.js';

import parseMethodRouteKey from './core/parseMethodRouteKey.js';
import runHook from './core/runHook.js';


/**
Expand Down Expand Up @@ -43,51 +46,51 @@ class App {

/* Load everything */
async.series([
callback => require('./core/loadConfig').call(this, callback),
callback => {
async callback => await (await import('./core/loadConfig.js')).default.call(this, callback),
async callback => {
if (options.loadServer !== false) {
require('./core/loadServer').call(this, options, callback);
await (await import('./core/loadServer.js')).default.call(this, options, callback);
}
},
callback => {
async callback => {
if (options.loadModel !== false) {
require('./core/loadModel').call(this, callback);
await (await import('./core/loadModel.js')).default.call(this, callback);
}
},
callback => {
async callback => {
if (options.loadPermissions !== false) {
require('./core/loadPermissions').call(this, callback);
await (await import('./core/loadPermissions.js')).default.call(this, callback);
}
},
callback => {
async callback => {
if (options.loadController !== false) {
require('./core/loadController').call(this, callback);
await (await import('./core/loadController.js')).default.call(this, callback);
}
},
callback => {
async callback => {
if (options.loadHooks !== false) {
require('./core/loadHooks').call(this, callback);
await (await import('./core/loadHooks.js')).default.call(this, callback);
}
},
callback => {
async callback => {
if (options.loadViews !== false) {
require('./core/loadCustomTags').call(this, callback);
await (await import('./core/loadCustomTags.js')).default.call(this, callback);
}
},
callback => {
require('./core/loadModules').call(this, callback);
async callback => {
await (await import('./core/loadModules.js')).default.call(this, callback);
},
callback => {
async callback => {
if (options.loadViews !== false) {
for (const route in this.controller) {
if ({}.hasOwnProperty.call(this.controller, route)) {
require('./core/initRoute').call(this, route, this.controller[route]);
await (await import('./core/initRoute.js')).default.call(this, route, this.controller[route]);
}
}
}

if (options.loadREST !== false) {
require('./core/loadRest').call(this, callback);
await (await import('./core/loadRest.js')).default.call(this, callback);
}
},
callback => {
Expand All @@ -111,8 +114,8 @@ class App {
}

/* Load remaining methods */
parseMethodRouteKey = require('./core/parseMethodRouteKey');
runHook = require('./core/runHook');
parseMethodRouteKey = parseMethodRouteKey;
runHook = runHook;
}

module.exports = App;
export default App;
10 changes: 5 additions & 5 deletions core/initRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@


/* Dependencies */
const { console } = require('../lib/Cluster');
const Response = require('../lib/Response');
const SaplingError = require('../lib/SaplingError');
import { console } from '../lib/Cluster.js';
import Response from '../lib/Response.js';
import SaplingError from '../lib/SaplingError.js';


/**
Expand All @@ -18,7 +18,7 @@ const SaplingError = require('../lib/SaplingError');
* @param {string} route Name of the route to be loaded
* @param {function} view Chain callback
*/
module.exports = async function (route, view) {
export default async function initRoute(route, view) {
console.log('Loaded route', `${route}`);

/* Create a handler for incoming requests */
Expand All @@ -38,4 +38,4 @@ module.exports = async function (route, view) {
/* Save the routes for later */
this.routeStack.get.push(route);
this.routeStack.post.push(route);
};
}
20 changes: 12 additions & 8 deletions core/loadConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@


/* Dependencies */
const argv = require('yargs').argv;
const fs = require('fs');
const path = require('path');
const _ = require('underscore');
import yargs from 'yargs';
/* eslint-disable-next-line node/file-extension-in-import */
import { hideBin } from 'yargs/helpers';
import fs from 'fs';
import path from 'path';
import _ from 'underscore';

const { console } = require('../lib/Cluster');
const SaplingError = require('../lib/SaplingError');
import { console } from '../lib/Cluster.js';
import SaplingError from '../lib/SaplingError.js';


/**
Expand All @@ -21,7 +23,9 @@ const SaplingError = require('../lib/SaplingError');
*
* @param {function} next Chain callback
*/
module.exports = async function (next) {
export default async function loadConfig(next) {
const argv = yargs(hideBin(process.argv)).argv;

/* Default configuration values */
const defaultConfig = {
publicDir: 'public',
Expand Down Expand Up @@ -141,4 +145,4 @@ module.exports = async function (next) {
if (next) {
next();
}
};
}
18 changes: 9 additions & 9 deletions core/loadController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@


/* Dependencies */
const fs = require('fs');
const path = require('path');
import fs from 'fs';
import path from 'path';

const { console } = require('../lib/Cluster');
const Templating = require('../lib/Templating');
import { console } from '../lib/Cluster.js';
import Templating from '../lib/Templating.js';


/**
* Load the controller JSON file into routes.
*
* @param {function} next Chain callback
*/
module.exports = async function (next) {
export default async function loadController(next) {
/* Load templating engine */
this.templating = new Templating(this);
await this.templating.importDriver();

this.controller = {};

Expand Down Expand Up @@ -71,11 +72,10 @@ module.exports = async function (next) {

/* Load the controller file */
if (fs.existsSync(controllerPath) && fs.lstatSync(controllerPath).isFile()) {
/* If we have a controller file, let's load it */
const file = fs.readFileSync(controllerPath);

/* 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 */
Expand All @@ -99,4 +99,4 @@ module.exports = async function (next) {
if (next) {
next();
}
};
}
4 changes: 2 additions & 2 deletions core/loadCustomTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @param {function} next Chain callback
*/
module.exports = async function (next) {
export default async function loadCustomTags(next) {
await this.templating.renderer.registerTags({

/**
Expand Down Expand Up @@ -51,4 +51,4 @@ module.exports = async function (next) {
if (next) {
next();
}
};
}
16 changes: 8 additions & 8 deletions core/loadHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@


/* Dependencies */
const fs = require('fs');
const path = require('path');
import fs from 'fs';
import path from 'path';

const { console } = require('../lib/Cluster');
const Response = require('../lib/Response');
const SaplingError = require('../lib/SaplingError');
import { console } from '../lib/Cluster.js';
import Response from '../lib/Response.js';
import SaplingError from '../lib/SaplingError.js';


/**
* Load the hooks JSON file, and listen to non-data API hooks.
*
* @param {function} next Chain callback
*/
module.exports = async function (next) {
export default async function loadHooks(next) {
/* Location of the hooks file */
const hooksPath = path.join(this.dir, this.config.hooks);

Expand Down Expand Up @@ -49,7 +49,7 @@ module.exports = async function (next) {
for (const hook of Object.keys(hooks)) {
const { method, route } = this.parseMethodRouteKey(hook);

this.hooks[`${method} ${route}`] = require(path.join(this.dir, this.config.hooksDir, hooks[hook]));
this.hooks[`${method} ${route}`] = (await import(path.join(this.dir, this.config.hooksDir, hooks[hook]))).default;

/* Initialise hook if it doesn't exist in the controller */
if (!(route in this.controller) && !route.startsWith('/data') && !route.startsWith('data')) {
Expand All @@ -72,4 +72,4 @@ module.exports = async function (next) {
if (next) {
next();
}
};
}
14 changes: 7 additions & 7 deletions core/loadModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@


/* Dependencies */
const fs = require('fs');
const path = require('path');
import fs from 'fs';
import path from 'path';

const { console } = require('../lib/Cluster');
const SaplingError = require('../lib/SaplingError');
const Storage = require('../lib/Storage');
import { console } from '../lib/Cluster.js';
import SaplingError from '../lib/SaplingError.js';
import Storage from '../lib/Storage.js';


/**
Expand All @@ -20,7 +20,7 @@ const Storage = require('../lib/Storage');
*
* @param {function} next Chain callback
*/
module.exports = async function (next) {
export default async function loadModel(next) {
const modelPath = path.join(this.dir, this.config.modelsDir);
const structure = {};
let files = {};
Expand Down Expand Up @@ -85,4 +85,4 @@ module.exports = async function (next) {
if (next) {
next();
}
};
}
10 changes: 5 additions & 5 deletions core/loadModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@


/* Dependencies */
const Notifications = require('../lib/Notifications');
const Uploads = require('../lib/Uploads');
const User = require('../lib/User');
import Notifications from '../lib/Notifications.js';
import Uploads from '../lib/Uploads.js';
import User from '../lib/User.js';


/**
* Load all separate modules as needed
*
* @param {function} next Chain callback
*/
module.exports = async function (next) {
export default async function loadModules(next) {
this.user = new User(this);

if (this.config.mail) {
Expand All @@ -30,4 +30,4 @@ module.exports = async function (next) {
if (next) {
next();
}
};
}
Loading