gulp-tateru-cli
Version:
Gulp plugin to build templates, using tateru-cli
58 lines (57 loc) • 2.57 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import PluginError from 'plugin-error';
import { core } from 'tateru-cli';
import through from 'through2';
import Vinyl from 'vinyl';
const PLUGIN_NAME = 'gulp-tateru-cli';
/**
* Gulp Plugin for Tateru CLI.
*
* @param options - The options to use for the plugin.
* @returns - Pipe Stream.
*/
export const gulpTateruCli = (options = {}) => {
return through.obj(function (file, _, callback) {
return __awaiter(this, void 0, void 0, function* () {
let parsedConfig;
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
return callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
const contentsConfig = file.contents.toString();
try {
parsedConfig = JSON.parse(contentsConfig);
}
catch (error) {
return callback(new PluginError(PLUGIN_NAME, 'Invalid JSON config file'));
}
try {
const files = yield core(Object.assign(Object.assign({}, options), { config: parsedConfig, cwd: file.cwd }));
yield Promise.all(files.map((generatedFile) => __awaiter(this, void 0, void 0, function* () {
const vinylFile = new Vinyl({
path: generatedFile.path,
cwd: generatedFile.cwd,
base: file.base,
contents: Buffer.from(generatedFile.contents || ''),
});
this.push(vinylFile);
})));
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
return callback(new PluginError(PLUGIN_NAME, errorMessage));
}
callback();
});
});
};