UNPKG

@xarc/run

Version:

concurrent or serial run npm scripts, javascript tasks, and more

59 lines (49 loc) 1.64 kB
"use strict"; const logger = require("../lib/logger"); const chalk = require("chalk"); const readPkgUp = require("read-pkg-up"); const myPkg = require("../package.json"); const config = require("./config"); const env = require("./env"); module.exports = (xrun, options) => { const readPkg = readPkgUp.sync(); if (!readPkg) { return; } const Pkg = readPkg.packageJson; const pkgName = chalk.magenta(readPkg.path.replace(process.cwd(), ".")); if (Pkg.scripts && options.npm !== false) { const scripts = {}; for (const k in Pkg.scripts) { if (!k.startsWith("pre") && !k.startsWith("post")) { const pre = `pre${k}`; const post = `post${k}`; scripts[k] = xrun.serial( Pkg.scripts.hasOwnProperty(pre) && pre, xrun.exec(Pkg.scripts[k], "npm"), Pkg.scripts.hasOwnProperty(post) && post ); } else { scripts[k] = xrun.exec(Pkg.scripts[k], "npm"); } } xrun.load("npm", scripts); /* istanbul ignore else */ if (env.get(env.xrunPackagePath) !== readPkg.path) { logger.log(`Loaded npm scripts from ${pkgName} into namespace ${chalk.magenta("npm")}`); } env.set(env.xrunPackagePath, readPkg.path); } const pkgOptField = config.getPkgOpt(Pkg); const pkgConfig = pkgOptField && Pkg[pkgOptField]; if (pkgConfig) { const tasks = Object.assign({}, pkgConfig.tasks); /* istanbul ignore else */ if (Object.keys(tasks).length > 0) { xrun.load("pkg", tasks); logger.log( `Loaded ${myPkg.name} tasks from ${pkgName} into namespace ${chalk.magenta("pkg")}` ); } } };