Comparing version 1.0.3 to 2.0.0
@@ -76,8 +76,13 @@ # Jimpex Options | ||
// If true, the statics folder should be relative to the project root directory, otherwise, | ||
// it should be relative to the app executable. | ||
onHome: true, | ||
// If true, the statics folder would be relative to the project root directory, otherwise, | ||
// it would be relative to the app executable. | ||
onHome: false, | ||
// The name of your static files folder. | ||
folder: 'statics', | ||
// The name of both the route and the folder, relative to whatever you defined with the | ||
// `onHome` option. | ||
route: 'statics', | ||
// By default, the folder will be the same as the `route`, but you can use this option | ||
// to define a relative path that won't affect the route. | ||
folder: '', | ||
} | ||
@@ -84,0 +89,0 @@ ``` |
@@ -314,3 +314,3 @@ # Built-in Services | ||
// Whether or not to delete the tempalte after generating the file. | ||
// Whether or not to delete the template after generating the file. | ||
deleteTemplateAfter: true, | ||
@@ -349,10 +349,7 @@ | ||
// Register the service | ||
this.register(htmlGeneratorCustom( | ||
'my-html-generator', | ||
{ | ||
template: 'template.tpl', | ||
file: 'my-index.html', | ||
... | ||
} | ||
)); | ||
this.register(htmlGeneratorCustom({ | ||
template: 'template.tpl', | ||
file: 'my-index.html', | ||
... | ||
})); | ||
} | ||
@@ -359,0 +356,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://homer0.github.io/jimpex/", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"repository": "homer0/jimpex", | ||
@@ -8,0 +8,0 @@ "author": "Leonardo Apiwan (@homer0) <me@homer0.com>", |
@@ -53,2 +53,3 @@ const Jimple = require('jimple'); | ||
version: '0.0.0', | ||
filesizeLimit: '15MB', | ||
configuration: { | ||
@@ -66,6 +67,6 @@ default: null, | ||
enabled: true, | ||
onHome: true, | ||
folder: 'statics', | ||
onHome: false, | ||
route: 'statics', | ||
folder: '', | ||
}, | ||
filesizeLimit: '15MB', | ||
express: { | ||
@@ -234,6 +235,7 @@ trustProxy: true, | ||
if (statics.enabled) { | ||
const { onHome, folder } = statics; | ||
const { onHome, route, folder } = statics; | ||
const joinFrom = onHome ? 'home' : 'app'; | ||
const staticsFolderPath = this.get('pathUtils').joinFrom(joinFrom, folder); | ||
this.express.use(`/${folder}`, express.static(staticsFolderPath)); | ||
const staticsRoute = route.startsWith('/') ? route.substr(1) : route; | ||
const staticsFolderPath = this.get('pathUtils').joinFrom(joinFrom, folder || staticsRoute); | ||
this.express.use(`/${staticsRoute}`, express.static(staticsFolderPath)); | ||
} | ||
@@ -240,0 +242,0 @@ |
@@ -13,3 +13,3 @@ /** | ||
* directory. | ||
* @property {Boolean} [hasFolder=true] Whether the configurations are inside a | ||
* @property {boolean} [hasFolder=true] Whether the configurations are inside a | ||
* sub directory or not. If `true`, a | ||
@@ -21,6 +21,6 @@ * configuration path would be | ||
* configuration. | ||
* @property {Boolean} [loadFromEnvironment=true] Whether or not to check for the | ||
* @property {boolean} [loadFromEnvironment=true] Whether or not to check for the | ||
* environment variable and load a | ||
* configuration based on its value. | ||
* @property {Boolean} [loadVersionFromConfiguration=true] If `true`, the app `version` will be | ||
* @property {boolean} [loadVersionFromConfiguration=true] If `true`, the app `version` will be | ||
* taken from the loaded configuration, | ||
@@ -37,8 +37,12 @@ * otherwise, when a configuration is | ||
* @typedef {Object} JimpexStaticsOptions | ||
* @property {Boolean} [enabled=true] Whether or not to include the middleware for static files. | ||
* @property {Boolean} [onHome=true] If `true`, the path to the statics folder will be relative | ||
* @property {boolean} [enabled=true] Whether or not to include the middleware for static files. | ||
* @property {boolean} [onHome=false] If `true`, the path to the statics folder will be relative | ||
* to the project root directory, otherwise, it will be | ||
* relative to the directory where the app executable file is | ||
* located. | ||
* @property {string} [folder='static'] The name of the folder for static files. | ||
* @property {string} [route='static'] The name of both the route and the folder, relative to | ||
* whatever you defined with the `onHome` option. | ||
* @property {string} [folder=''] By default, the folder will be the same as the `route`, | ||
* but you can use this option to define a relative path that | ||
* won't affect the route. | ||
*/ | ||
@@ -48,7 +52,7 @@ | ||
* @typedef {Object} JimpexExpressOptions | ||
* @property {Boolean} [trustProxy=true] Whether or not to enable the `trust proxy` option. | ||
* @property {Boolean} [disableXPoweredBy=true] Whether or not to remove the `x-powered-by` header. | ||
* @property {Boolean} [compression=true] Whether or not to add the `compression` middleware. | ||
* @property {Boolean} [bodyParser=true] Whether or not to add the `body-parser` middleware. | ||
* @property {Boolean} [multer=true] Whether or not to add the `multer` middleware. | ||
* @property {boolean} [trustProxy=true] Whether or not to enable the `trust proxy` option. | ||
* @property {boolean} [disableXPoweredBy=true] Whether or not to remove the `x-powered-by` header. | ||
* @property {boolean} [compression=true] Whether or not to add the `compression` middleware. | ||
* @property {boolean} [bodyParser=true] Whether or not to add the `body-parser` middleware. | ||
* @property {boolean} [multer=true] Whether or not to add the `multer` middleware. | ||
*/ | ||
@@ -58,5 +62,5 @@ | ||
* @typedef {Object} JimpexDefaultServicesOptions | ||
* @property {Boolean} [common=true] Whether or not to register all the `common` service providers. | ||
* @property {Boolean} [http=true] Whether or not to register all the `http` service providers. | ||
* @property {Boolean} [api=true] Whether or not to register all the `api` service providers. | ||
* @property {boolean} [common=true] Whether or not to register all the `common` service providers. | ||
* @property {boolean} [http=true] Whether or not to register all the `http` service providers. | ||
* @property {boolean} [api=true] Whether or not to register all the `api` service providers. | ||
*/ | ||
@@ -63,0 +67,0 @@ |
@@ -12,13 +12,13 @@ const extend = require('extend'); | ||
* Class constructor. | ||
* @param {SendFile} sendFile To be able to send the files as | ||
* responses. | ||
* @param {Array} [files=['index.html', 'favicon.icon']] The list of files to serve. Each item | ||
* can be a `string` or an `Object` with | ||
* the keys `origin` for the file route, | ||
* `output` for the file location | ||
* relative to the root, and `headers` | ||
* with the file custom headers for the | ||
* response. | ||
* @param {SendFile} sendFile To be able to send the files as | ||
* responses. | ||
* @param {Array} [files=['index.html', 'favicon.ico']] The list of files to serve. Each item | ||
* can be a `string` or an `Object` with | ||
* the keys `origin` for the file route, | ||
* `output` for the file location | ||
* relative to the root, and `headers` | ||
* with the file custom headers for the | ||
* response. | ||
*/ | ||
constructor(sendFile, files = ['index.html', 'favicon.icon']) { | ||
constructor(sendFile, files = ['index.html', 'favicon.ico']) { | ||
/** | ||
@@ -25,0 +25,0 @@ * A local reference for the `sendFile` service. |
@@ -39,3 +39,2 @@ const extend = require('extend'); | ||
* Class constructor. | ||
* @param {HTMLGeneratorOptions} [options] To customize the service. | ||
* @param {AppConfiguration} appConfiguration To read the values of the settings | ||
@@ -49,2 +48,3 @@ * that are going to be send to the | ||
* @param {FrontendFs} frontendFs To read the contents of the template. | ||
* @param {HTMLGeneratorOptions} [options] To customize the service. | ||
* @param {?HTMLGeneratorValuesService} [valuesService=null] If specified, instead of getting | ||
@@ -55,9 +55,8 @@ * the values from the app | ||
* @throws {Error} if `valuesService` is specified but it doesn't have a `getValues` method. | ||
* @todo Move `options` to the before last parameter as it's optional | ||
*/ | ||
constructor( | ||
options, | ||
appConfiguration, | ||
appLogger, | ||
frontendFs, | ||
options, | ||
valuesService = null | ||
@@ -210,5 +209,4 @@ ) { | ||
* just an empty object to continue the promise chain. | ||
* @todo Change it to a short circuit evaluation. | ||
*/ | ||
return deleteTemplateAfter ? this.frontendFs.delete(`./${template}`) : {}; | ||
return deleteTemplateAfter && this.frontendFs.delete(`./${template}`); | ||
}) | ||
@@ -238,5 +236,5 @@ .then(() => { | ||
* the html file when the server starts. | ||
* @param {HTMLGeneratorOptions} [options={}] Options to customize the service. | ||
* @param {string} [serviceName='htmlGenerator'] The name of the service that will | ||
* be register into the app. | ||
* @param {HTMLGeneratorOptions} [options={}] Options to customize the service. | ||
* @param {?string} [valuesServiceName=null] The name of a service used to read | ||
@@ -246,8 +244,6 @@ * the values that will be injected in | ||
* @return {Provider} | ||
* @todo Move `serviceName` as the second parameter in case the implementation wants to change just | ||
* the options. | ||
*/ | ||
const htmlGeneratorCustom = ( | ||
options = {}, | ||
serviceName = 'htmlGenerator', | ||
options = {}, | ||
valuesServiceName = null | ||
@@ -262,6 +258,6 @@ ) => provider((app) => { | ||
return new HTMLGenerator( | ||
options, | ||
app.get('appConfiguration'), | ||
app.get('appLogger'), | ||
app.get('frontendFs'), | ||
options, | ||
valuesService | ||
@@ -268,0 +264,0 @@ ); |
@@ -84,3 +84,3 @@ const JimpleMock = require('/tests/mocks/jimple.mock'); | ||
]; | ||
const expectedStaticsFolder = 'home/statics'; | ||
const expectedStaticsFolder = 'app/statics'; | ||
const expectedMiddlewares = [ | ||
@@ -141,3 +141,3 @@ ['compression-middleware'], | ||
expect(pathUtils.joinFrom).toHaveBeenCalledTimes(1); | ||
expect(pathUtils.joinFrom).toHaveBeenCalledWith('home', sut.options.statics.folder); | ||
expect(pathUtils.joinFrom).toHaveBeenCalledWith('app', sut.options.statics.route); | ||
expect(appConfiguration.get).toHaveBeenCalledTimes(1); | ||
@@ -364,3 +364,3 @@ expect(appConfiguration.get).toHaveBeenCalledWith('version'); | ||
it('should be able to set the statics folder on the app directory', () => { | ||
it('should be able to set the statics route relative to the home directory', () => { | ||
/** | ||
@@ -390,3 +390,3 @@ * App directory: Where the executable file is located. | ||
statics: { | ||
onHome: false, | ||
onHome: true, | ||
}, | ||
@@ -397,5 +397,55 @@ }); | ||
expect(pathUtils.joinFrom).toHaveBeenCalledTimes(1); | ||
expect(pathUtils.joinFrom).toHaveBeenCalledWith('app', sut.options.statics.folder); | ||
expect(pathUtils.joinFrom).toHaveBeenCalledWith('home', sut.options.statics.route); | ||
}); | ||
it('should be able to set a the statics folder with a path different from the route', () => { | ||
/** | ||
* App directory: Where the executable file is located. | ||
* Home directory: Where the app is executed from (`process.cwd`). | ||
*/ | ||
// Given | ||
class Sut extends Jimpex { | ||
boot() {} | ||
} | ||
const pathUtils = { | ||
joinFrom: jest.fn((from, rest) => path.join(from, rest)), | ||
}; | ||
JimpleMock.service('pathUtils', pathUtils); | ||
const defaultConfig = {}; | ||
const rootRequire = jest.fn(() => defaultConfig); | ||
JimpleMock.service('rootRequire', rootRequire); | ||
const appConfiguration = { | ||
loadFromEnvironment: jest.fn(), | ||
get: jest.fn(), | ||
}; | ||
JimpleMock.service('appConfiguration', appConfiguration); | ||
const staticsRoute = '/some/statics'; | ||
const staticsFolder = '../statics'; | ||
let sut = null; | ||
const expectedMiddlewares = [ | ||
['compression-middleware'], | ||
[staticsRoute, path.join('home', staticsFolder)], | ||
['body-parser-json'], | ||
['body-parser-urlencoded'], | ||
['multer-any'], | ||
]; | ||
// When | ||
sut = new Sut(true, { | ||
statics: { | ||
onHome: true, | ||
route: staticsRoute, | ||
folder: staticsFolder, | ||
}, | ||
}); | ||
// Then | ||
expect(sut.options.statics.folder).toBe(staticsFolder); | ||
expect(expressMock.static).toHaveBeenCalledTimes(1); | ||
expect(pathUtils.joinFrom).toHaveBeenCalledTimes(1); | ||
expect(pathUtils.joinFrom).toHaveBeenCalledWith('home', staticsFolder); | ||
expect(expressMock.mocks.use).toHaveBeenCalledTimes(expectedMiddlewares.length); | ||
expectedMiddlewares.forEach((useCall) => { | ||
expect(expressMock.mocks.use).toHaveBeenCalledWith(...useCall); | ||
}); | ||
}); | ||
it('shouldn\'t add the default services if their flags are `false`', () => { | ||
@@ -800,3 +850,3 @@ // Given | ||
let sut = null; | ||
const expectedStaticsFolder = 'home/statics'; | ||
const expectedStaticsFolder = 'app/statics'; | ||
const expectedMiddlewares = [ | ||
@@ -857,3 +907,3 @@ ['compression-middleware'], | ||
let sut = null; | ||
const expectedStaticsFolder = 'home/statics'; | ||
const expectedStaticsFolder = 'app/statics'; | ||
const expectedMiddlewares = [ | ||
@@ -913,3 +963,3 @@ ['compression-middleware'], | ||
let sut = null; | ||
const expectedStaticsFolder = 'home/statics'; | ||
const expectedStaticsFolder = 'app/statics'; | ||
const expectedMiddlewares = [ | ||
@@ -916,0 +966,0 @@ ['compression-middleware'], |
@@ -29,6 +29,6 @@ const JimpleMock = require('/tests/mocks/jimple.mock'); | ||
sut = new HTMLGenerator( | ||
options, | ||
appConfiguration, | ||
appLogger, | ||
frontendFs | ||
frontendFs, | ||
options | ||
); | ||
@@ -63,6 +63,6 @@ // Then | ||
sut = new HTMLGenerator( | ||
options, | ||
appConfiguration, | ||
appLogger, | ||
frontendFs, | ||
options, | ||
valuesService | ||
@@ -88,6 +88,6 @@ ); | ||
expect(() => new HTMLGenerator( | ||
options, | ||
appConfiguration, | ||
appLogger, | ||
frontendFs, | ||
options, | ||
valuesService | ||
@@ -109,6 +109,6 @@ )) | ||
sut = new HTMLGenerator( | ||
options, | ||
appConfiguration, | ||
appLogger, | ||
frontendFs | ||
frontendFs, | ||
options | ||
); | ||
@@ -132,6 +132,6 @@ result = sut.getFile(); | ||
sut = new HTMLGenerator( | ||
options, | ||
appConfiguration, | ||
appLogger, | ||
frontendFs | ||
frontendFs, | ||
options | ||
); | ||
@@ -163,6 +163,6 @@ return sut.getValues() | ||
sut = new HTMLGenerator( | ||
options, | ||
appConfiguration, | ||
appLogger, | ||
frontendFs, | ||
options, | ||
valuesService | ||
@@ -193,6 +193,6 @@ ); | ||
sut = new HTMLGenerator( | ||
options, | ||
appConfiguration, | ||
appLogger, | ||
frontendFs | ||
frontendFs, | ||
options | ||
); | ||
@@ -219,6 +219,6 @@ return sut.getValues() | ||
sut = new HTMLGenerator( | ||
options, | ||
appConfiguration, | ||
appLogger, | ||
frontendFs | ||
frontendFs, | ||
options | ||
); | ||
@@ -259,6 +259,6 @@ result = sut.whenReady(); | ||
sut = new HTMLGenerator( | ||
options, | ||
appConfiguration, | ||
appLogger, | ||
frontendFs | ||
frontendFs, | ||
options | ||
); | ||
@@ -317,6 +317,6 @@ return sut.generateHTML() | ||
sut = new HTMLGenerator( | ||
options, | ||
appConfiguration, | ||
appLogger, | ||
frontendFs | ||
frontendFs, | ||
options | ||
); | ||
@@ -361,6 +361,6 @@ return sut.generateHTML() | ||
sut = new HTMLGenerator( | ||
options, | ||
appConfiguration, | ||
appLogger, | ||
frontendFs | ||
frontendFs, | ||
options | ||
); | ||
@@ -415,3 +415,3 @@ return sut.generateHTML() | ||
// When | ||
htmlGeneratorCustom(name)(app); | ||
htmlGeneratorCustom({}, name)(app); | ||
[[serviceName, serviceFn]] = app.set.mock.calls; | ||
@@ -486,3 +486,3 @@ [[eventName, eventFn]] = events.once.mock.calls; | ||
// When | ||
htmlGeneratorCustom(name, {}, myValuesServiceName)(app); | ||
htmlGeneratorCustom({}, name, myValuesServiceName)(app); | ||
[[serviceName, serviceFn]] = app.set.mock.calls; | ||
@@ -489,0 +489,0 @@ [[eventName, eventFn]] = events.once.mock.calls; |
444625
6799