turbo-gulp
Version:
Gulp tasks to boost high-quality projects.
54 lines (53 loc) • 1.81 kB
JavaScript
import { spawn } from "child_process";
import { posix as posixPath } from "path";
import { nameTask, registerBaseTasks, resolveTargetBase, } from "./_base";
/**
* Resolve absolute paths and dependencies for the provided target.
*
* @param target Non-resolved target.
* @return Resolved target.
*/
function resolveLibTarget(target) {
const base = resolveTargetBase(target);
return Object.assign({}, base, { mainModule: target.mainModule });
}
/**
* Generates and registers gulp tasks for the provided node target.
*
* @param gulp Gulp instance used to generate tasks manipulating files.
* @param targetOptions Target configuration.
*/
export function generateNodeTasks(gulp, targetOptions) {
const target = resolveLibTarget(targetOptions);
const result = registerBaseTasks(gulp, targetOptions);
const absMain = posixPath.join(target.buildDir, `${target.mainModule}.js`);
// run
result.run = nameTask(`${target.name}:run`, () => {
return spawn("node", [absMain, ...process.argv.splice(1)], { stdio: "inherit" });
});
// start
const startTasks = [];
if (result.clean !== undefined) {
startTasks.push(result.clean);
}
startTasks.push(result.build);
startTasks.push(result.run);
result.start = nameTask(target.name, gulp.series(startTasks));
return result;
}
/**
* Generates and registers gulp tasks for the provided node target.
*
* @param gulp Gulp instance where the tasks will be registered.
* @param targetOptions Target configuration.
*/
export function registerNodeTasks(gulp, targetOptions) {
const tasks = generateNodeTasks(gulp, targetOptions);
for (const key in tasks) {
const task = tasks[key];
if (task !== undefined) {
gulp.task(task);
}
}
return tasks;
}