UNPKG

@fontoxml/fontoxml-development-tools

Version:
139 lines (122 loc) 5.74 kB
'use strict'; const Fotno = require('fotno'); const os = require('os'); const path = require('path'); const AssertableWritableStream = require('./AssertableWritableStream'); const addCheckForUpdates = require('./addCheckForUpdates'); const addModulesToApp = require('./addModulesToApp'); const createConfigFileInHomedir = require('./createConfigFileInHomedir'); const enrichRequestObject = require('./enrichRequestObject'); const warnIfNotInsideEditorRepository = require('./warnIfNotInsideEditorRepository'); const CONFIG_FILE_NAME = '.fdtrc'; class FontoXMLDevelopmentToolsApp extends Fotno { /** * Creates an instance of the FontoXMLDevelopmentToolsApp, which in turn is based on Fotno App. * @NOTE: The constructor arguments are different then the one used by Fotno. * * @param {object} [options] * An optional options object. * @param {boolean} [options.configFilename] * The filename to use for finding, loading, and saving configuration files. Default: '.fdtrc'; * @param {boolean} [options.configHomedirPath] * The homedir to use for storing configuration. Default: os.homedir() * @param {boolean} [options.configPath] * The path to the build in configuration file. Default: <the root directory of this package>. * @param {object} [options.fotnoOptions] * Options to pass along to the underlying fotno tool. * @param {boolean} [options.skipAddModules] * When set to true, skip loading of the built-in modules. * @param {boolean} [options.skipCheckForUpdates] * When set to true, skip checking for updates to the application and built-in modules. * @param {boolean} [options.skipCreateConfigFileInHomedir] * When set to true, skip creating a configuration file in the home directory on start if it does not exist. * @param {boolean} [options.skipEnrichRequestObject] * When set to true, skip adding the fdt property to the request object. * @param {boolean} [options.skipWarnIfNotInsideEditorRepository] * When set to true, skip the warning message which is shown when not running from inside an editor repository. * @param {boolean} [options.testMode] * When set to true, enable test mode. This is meant for unit tests and will do the folowing: * - Set configFilename to '.fdttestrc' (if no explicit value was set). * - Set skipCreateConfigFileInHomedir to true (if no explicit value was set). * - Set skipCheckForUpdates to true (if no explicit value was set). * - Set useTestOutput to true. * @param {boolean} [options.useTestOutput] * When set to true, enable test output. This is meant for unit tests and will do the folowing: * - Output everything to .testOutput stream which has some helper methods for unit testing. * @extends {Fotno} * @constructor */ constructor (options = {}) { /* istanbul ignore else: All tests should be run in testmode to prevent configuration conflicts */ if (options.testMode) { options.useTestOutput = true; options.fotnoOptions = Object.assign({}, options.fotnoOptions, { catchErrors: false }); if (!options.configFilename) { options.configFilename = '.fdttestrc'; } if (options.skipCreateConfigFileInHomedir === undefined) { options.skipCreateConfigFileInHomedir = true; } if (options.skipCheckForUpdates === undefined) { options.skipCheckForUpdates = true; } } let testOutput = null; /* istanbul ignore else: All tests should be run in testmode to prevent configuration conflicts */ if (options.useTestOutput) { if (!options.fotnoOptions || (options.fotnoOptions.stdout === undefined && options.fotnoOptions.silent === undefined)) { testOutput = new AssertableWritableStream({ stripAnsi: true }); options.fotnoOptions = Object.assign({}, options.fotnoOptions, { stdout: testOutput }); } } /* istanbul ignore if: All tests should skip creating the config file in the homedir */ if (!options.skipCreateConfigFileInHomedir) { createConfigFileInHomedir(CONFIG_FILE_NAME, options); } // Instantiate with a configuration file from either the application dir or your home dir super([ // Use config file in the user's homedir, this is also were config changes will be stored. options && options.configHomedirPath ? options.configHomedirPath : os.homedir(), // Find config file built into fontoxml development tools options && options.configPath ? options.configPath : path.join(__dirname, '..') ], options && options.configFilename ? options.configFilename : CONFIG_FILE_NAME, Object.assign({}, { appVersion: require(path.join(__dirname, '../package.json')).version, appName: 'fdt' }, options.fotnoOptions)); this.request = {}; this.testOutput = testOutput; /* istanbul ignore next */ if (!options.skipCheckForUpdates) { addCheckForUpdates(this); } if (!options.skipEnrichRequestObject) { enrichRequestObject(this); } if (!options.skipAddModules) { addModulesToApp(this); } if (!options.skipWarnIfNotInsideEditorRepository) { warnIfNotInsideEditorRepository(this); } } /** * Run a (sub) command based on args. It uses this.request as request by default. * * @param {Array<string>} args The arguments, specifiing which (sub) command to run with what options and parameters. * @param {Object} [request] Optionally a request object, should not be specified. Default: this.request. * @param {boolean} [skipErrorHandling] Skip logic that catches errors. * @return {Promise.<App>} */ run (args, request, skipErrorHandling) { return super.run(args, request || this.request, skipErrorHandling); } } module.exports = FontoXMLDevelopmentToolsApp;