Skip to content
Draft
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 src/Elder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ class Elder {

/**
* Finalize hooks
* Import User Hooks.js
* Import User defined Hooks
* Validate Hooks
* Filter out hooks that are disabled.
*/

let hooksJs: Array<HookOptions> = [];
const hookSrcPath = path.resolve(this.settings.srcDir, './hooks.js');
const hookSrcPath = path.resolve(this.settings.srcDir, './hooks');

try {
const hooksReq = require(hookSrcPath);
Expand Down Expand Up @@ -182,12 +182,12 @@ class Elder {

/**
* Finalize Shortcodes
* Import User Shortcodes.js
* Import User defined Shortcodes
* Validate Shortcodes
*/

let shortcodesJs: ShortcodeDefs = [];
const shortcodeSrcPath = path.resolve(this.settings.srcDir, './shortcodes.js');
const shortcodeSrcPath = path.resolve(this.settings.srcDir, './shortcodes');

try {
const shortcodeReq = require(shortcodeSrcPath);
Expand Down
22 changes: 6 additions & 16 deletions src/__tests__/Elder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
process.cwd = () => 'test';

jest.mock('path', () => {
return {
resolve: (...strings) => strings.join('/').replace('./', '').replace('//', '/').replace('/./', '/'),
join: (...strings) => strings.join('/').replace('./', '').replace('//', '/').replace('/./', '/'),
posix: () => ({ dirname: () => '' }),
};
});

jest.mock('../routes/routes', () => () => ({
'route-a': {
hooks: [
Expand Down Expand Up @@ -85,9 +75,9 @@ describe('#Elder', () => {
jest.mock('fs-extra', () => ({
existsSync: () => true,
}));
jest.mock('test/___ELDER___/compiled/fakepath/Test.js', () => () => ({}), { virtual: true });
jest.mock(`${process.cwd()}/test/___ELDER___/compiled/fakepath/Test.js`, () => () => ({}), { virtual: true });
jest.mock(
'test/__ELDER__/hooks.js',
`${process.cwd()}/test/__ELDER__/hooks.js`,
() => ({
default: [
{
Expand All @@ -101,7 +91,7 @@ describe('#Elder', () => {
{ virtual: true },
);
jest.mock(
'test/src/plugins/elder-plugin-upload-s3/index.js',
`${process.cwd()}/test/src/plugins/elder-plugin-upload-s3/index.js`,
() => ({
hooks: [],
routes: { 'test-a': { hooks: [], template: 'fakepath/Test.svelte', all: [] }, 'test-b': { data: () => {} } },
Expand Down Expand Up @@ -132,9 +122,9 @@ describe('#Elder', () => {
jest.mock('fs-extra', () => ({
existsSync: () => true,
}));
jest.mock('test/___ELDER___/compiled/fakepath/Test.js', () => () => ({}), { virtual: true });
jest.mock(`${process.cwd()}/test/___ELDER___/compiled/fakepath/Test.js`, () => () => ({}), { virtual: true });
jest.mock(
'test/src/hooks.js',
`${process.cwd()}/test/src/hooks`,
() => ({
default: [
{
Expand All @@ -148,7 +138,7 @@ describe('#Elder', () => {
{ virtual: true },
);
jest.mock(
'test/src/plugins/elder-plugin-upload-s3/index.js',
`${process.cwd()}/test/src/plugins/elder-plugin-upload-s3/index.js`,
() => ({
hooks: [
{
Expand Down
25 changes: 9 additions & 16 deletions src/__tests__/hooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ jest.mock('../utils/Page', () => {
}));
});

process.cwd = () => 'test';

jest.mock('path', () => ({
resolve: (...strings) => strings.join('/').replace('./', '').replace('//', '/'),
posix: () => ({ dirname: () => '' }),
}));

jest.mock('fs-extra', () => ({
writeJSONSync: jest.fn(),
outputFileSync: jest
Expand Down Expand Up @@ -148,7 +141,7 @@ describe('#hooks', () => {
const hook = hooks.find((h) => h.name === 'elderConsoleLogErrors');
expect(
await hook.run({ settings: { worker: false }, request: { permalink: '/foo' }, errors: ['foo', 'bar'] }),
).toBe(undefined);
).toBeUndefined();
});
it('elderWriteHtmlFileToPublic', async () => {
const hook = hooks.find((h) => h.name === 'elderWriteHtmlFileToPublic');
Expand All @@ -159,21 +152,21 @@ describe('#hooks', () => {
errors: [],
settings: {},
}),
).toBe(null);
).toBeNull();
expect(
await hook.run({
request: { permalink: '/foo' },
htmlString: '<html>string</html>',
errors: [],
settings: { build: './build', locations: { public: './public' } },
settings: { build: './build', locations: { public: './public' }, distDir: process.cwd() },
}),
).toBe(null);
).toBeNull();
expect(
await hook.run({
request: { permalink: '/foo' },
htmlString: '<html>string</html>',
errors: [],
settings: { build: './build', locations: { public: './public' } },
settings: { build: './build', locations: { public: './public' }, distDir: process.cwd() },
}),
).toEqual({ errors: [new Error('Failed to write')] });
});
Expand All @@ -188,7 +181,7 @@ describe('#hooks', () => {
],
settings: { debug: { performance: true } },
}),
).toBe(undefined);
).toBeUndefined();
});
it('elderShowParsedBuildTimes', async () => {
const hook = hooks.find((h) => h.name === 'elderShowParsedBuildTimes');
Expand All @@ -206,15 +199,15 @@ describe('#hooks', () => {
],
settings: { debug: { performance: true } },
}),
).toBe(undefined);
).toBeUndefined();
});
it('elderWriteBuildErrors', async () => {
const hook = hooks.find((h) => h.name === 'elderWriteBuildErrors');
expect(
await hook.run({
errors: ['error1', 'error2'],
settings: { debug: { performance: true } },
settings: { debug: { performance: true }, rootDir: process.cwd() },
}),
).toBe(undefined);
).toBeUndefined();
});
});
10 changes: 6 additions & 4 deletions src/routes/routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
import * as path from 'path';
import glob from 'glob';
import kebabcase from 'lodash.kebabcase';
import type { RouteOptions } from './types';
Expand All @@ -16,23 +17,24 @@ function routes(settings: SettingsOptions) {
`,
);

const files = glob.sync(`${settings.srcDir}/routes/*/+(*.js|*.svelte)`);
const files = glob.sync(`${settings.srcDir}/routes/*/+(*.js|*.ts|*.svelte)`);

const ssrFolder = settings.$$internal.ssrComponents;

const ssrComponents = glob.sync(`${ssrFolder}/*.js`);

const routejsFiles = files.filter((f) => f.endsWith('/route.js'));
const routeJsRegex = /route.t|js$/;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const routeJsRegex = /route\./[tj]s$/;

const routejsFiles = files.filter((f) => routeJsRegex.test(f));

const output = routejsFiles.reduce((out, cv) => {
const routeName = cv.replace('/route.js', '').split('/').pop();
const routeName = path.dirname(cv).split('/').pop();
const capitalizedRoute = capitalizeFirstLetter(routeName);

const routeReq = require(cv);
const route: RouteOptions = routeReq.default || routeReq;
const filesForThisRoute = files
.filter((r) => r.includes(`/routes/${routeName}`))
.filter((r) => !r.includes('route.js'));
.filter((r) => !routeJsRegex.test(r));

if (!route.permalink) {
if (settings.debug.automagic) {
Expand Down