UNPKG

@area17/a17-boilerplate

Version:

The official AREA 17 boilerplate

148 lines (121 loc) 3.82 kB
'use strict'; const fs = require('fs'); const path = require('path'); const readPkgUp = require('read-pkg-up'); const which = require('which'); let _readPkgUp$sync = readPkgUp.sync({ cwd: fs.realpathSync(process.cwd()) }); let pkgPath = _readPkgUp$sync.path; let appDirectory = path.dirname(pkgPath); let fromRoot = function () { for (var _len = arguments.length, p = Array(_len), _key = 0; _key < _len; _key++) { p[_key] = arguments[_key]; } return path.join.apply(path, [appDirectory].concat(p)); }; let hasFile = function () { return fs.existsSync(fromRoot.apply(undefined, arguments)); }; const mergeManifest = (def, cus) => Object.keys(def).reduce((acc, key) => { // Element is an array if (Array.isArray(def[key])) { // If key exist in custom manifest, create a new array with uniques values between default manifest and custom manifest if (key in cus) { acc[key] = [...new Set(def[key].concat(cus[key]))]; return acc; } acc[key] = def[key]; return acc; } // Element is an object and present inside custom config if (typeof def[key] === 'object' && key in cus) { acc[key] = { ...def[key], ...cus[key], } } else { acc[key] = def[key]; } return acc; }, {}); //copy from kcd-scripts let resolveBin = function(modName) { var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, _ref$executable = _ref.executable, executable = _ref$executable === undefined ? modName : _ref$executable, _ref$cwd = _ref.cwd, cwd = _ref$cwd === undefined ? process.cwd() : _ref$cwd; var pathFromWhich = void 0; try { pathFromWhich = fs.realpathSync(which.sync(executable)); } catch (_error) { // ignore _error } try { var modPkgPath = require.resolve(`${modName}/package.json`); var modPkgDir = path.dirname(modPkgPath); var _require = require(modPkgPath), bin = _require.bin; var binPath = typeof bin === 'string' ? bin : bin[executable]; var fullPathToBin = path.join(modPkgDir, binPath); if (fullPathToBin === pathFromWhich) { return executable; } return fullPathToBin.replace(cwd, '.'); } catch (error) { if (pathFromWhich) { return executable; } throw error; } }; let hereRelative = function(p) { return path.join(__dirname, p).replace(process.cwd(),'.'); }; let attemptResolve = function() { try { var _require; return (_require = require).resolve.apply(_require, arguments); } catch (error) { return null; } }; let getManifest = () => { // Default manifest.json living in the A17 boilerplate let relativePath = __dirname.replace(process.cwd(), '.'); const defaultManifestFile = path.resolve(path.join(relativePath,'./config/manifest.json')); // Manifest.json living in the project const hasRoot = Boolean(hasFile('./manifest.json')); const hasConfig = Boolean(hasFile('./config/manifest.json')); const hasFrontend = Boolean(hasFile('./frontend/manifest.json')); // Find where is the manifest.json is in the project let manifestFile = './manifest.json'; if(hasFrontend) manifestFile = './frontend/manifest.json'; if(hasConfig) manifestFile = './config/manifest.json'; if (!hasFile(manifestFile)) { return require(defaultManifestFile); } return mergeManifest(require(defaultManifestFile), require(path.resolve(manifestFile))); }; let desire = (dependency, fallback) => { try { require.resolve(dependency); } catch (err) { return fallback; } return require(dependency); // eslint-disable-line import/no-dynamic-require }; module.exports = { appDirectory, hasFile, fromRoot, resolveBin, hereRelative, attemptResolve, getManifest, desire };