@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
86 lines (84 loc) • 3.91 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const buffer_1 = require("buffer");
const path_1 = tslib_1.__importDefault(require("path"));
const plugin_error_1 = tslib_1.__importDefault(require("plugin-error"));
const through2_1 = tslib_1.__importDefault(require("through2"));
const vinyl_1 = tslib_1.__importDefault(require("vinyl"));
const util = tslib_1.__importStar(require("../utilities"));
const Resjson = tslib_1.__importStar(require("./resjson-convert"));
const PLUGIN_NAME = 'gulp-resjson';
function gulpResjson(options) {
// override options settings if not specified.
options = Object.assign({ definition: null, typescript: null, json: false, jsonSpace: null }, options || {});
return through2_1.default.obj(
/**
* Transform
*/
function (file, encoding, callback) {
let error = null;
try {
if (file.isNull()) {
// nothing to do
return callback(null, file);
}
if (file.isStream()) {
// file.contents is a Stream - https://nodejs.org/api/stream.html
this.emit('error', new plugin_error_1.default(PLUGIN_NAME, 'Streams not supported!'));
return callback(null, file);
}
else if (file.isBuffer()) {
const data = file.contents.toString('utf8');
const converter = new Resjson.ResJsonConverter(options);
converter.convert(data, file.base.indexOf('src\\resources') >= 0);
const path = path_1.default.parse(file.path);
if (options.definition) {
const dtFile = new vinyl_1.default({
cwd: '/',
base: path.dir,
path: path.dir + '/' + path.name + '.d.ts',
contents: buffer_1.Buffer.from(converter.contentDefinition, 'utf8')
});
this.push(dtFile);
}
if (options.typescript) {
const content = options.typescript === 'interface' ? converter.contentInterface : converter.contentTypescript;
const tsFile = new vinyl_1.default({
cwd: '/',
base: path.dir,
path: path.dir + '/' + path.name + '.ts',
contents: buffer_1.Buffer.from(content, 'utf8')
});
this.push(tsFile);
}
if (options.json) {
let baseDir = options.srcRoot || path.dir;
let fullDir = path.dir;
if (typeof options.localeOffset === 'number') {
const segments = path.dir.split('\\');
segments.length = segments.length - options.localeOffset;
fullDir = segments.join('\\');
segments.length = segments.length - 1;
baseDir = segments.join('\\');
}
const content = JSON.stringify(converter.outputJson, null, options.jsonSpace);
const jsonFile = new vinyl_1.default({
cwd: '/',
base: baseDir,
path: fullDir + '\\' + path.name + '.json',
contents: buffer_1.Buffer.from(content, 'utf8')
});
this.push(jsonFile);
}
}
}
catch (e) {
error = (!e.plugin || (e.plugin !== PLUGIN_NAME)) ?
util.extendError(new plugin_error_1.default({ plugin: PLUGIN_NAME, message: e.message }), e) : e;
}
callback(error);
});
}
module.exports = gulpResjson;
//# sourceMappingURL=index.js.map