xpm
Version:
The xPack project manager command line tool
184 lines • 7.96 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.XpmLiquid = exports.filterWin32Path = exports.filterPosixPath = exports.filterPath = void 0;
const node_assert_1 = require("node:assert");
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const process = __importStar(require("process"));
const util = __importStar(require("util"));
const liquidjs_1 = require("liquidjs");
function _isPrimitive(value) {
return (typeof value !== 'object' && typeof value !== 'function') ||
value === null;
}
function _isJsonObject(value) {
return value !== undefined && !_isPrimitive(value) && !Array.isArray(value);
}
function _joinMultiLineProperties(properties) {
const result = {};
Object.entries(properties).forEach(([key, value]) => {
result[key] = Array.isArray(value) ? value.join(os.EOL) : value;
});
return result;
}
function filterPath(input) {
const fixed = (os.platform() === 'win32')
? input.replace(/[^a-zA-Z0-9\\:]+/g, '-')
: input.replace(/[^a-zA-Z0-9/]+/g, '-');
return fixed.replace(/--/g, '-');
}
exports.filterPath = filterPath;
function filterPosixPath(input) {
const fixed = input.replace(/[^a-zA-Z0-9/]+/g, '-');
return fixed.replace(/--/g, '-');
}
exports.filterPosixPath = filterPosixPath;
function filterWin32Path(input) {
const fixed = input.replace(/[^a-zA-Z0-9\\:]+/g, '-');
return fixed.replace(/--/g, '-');
}
exports.filterWin32Path = filterWin32Path;
class XpmLiquid {
constructor(log) {
this.log = log;
this.engine = new liquidjs_1.Liquid({
strictFilters: true,
strictVariables: true,
trimTagLeft: false,
trimTagRight: false,
trimOutputLeft: false,
trimOutputRight: false,
greedy: false,
lenientIf: true
});
this.engine.registerFilter('path_basename', (p, ...arg) => path.basename(p, ...arg));
this.engine.registerFilter('path_dirname', (p) => path.dirname(p));
this.engine.registerFilter('path_normalize', (p) => path.normalize(p));
this.engine.registerFilter('path_join', (p, ...args) => path.join(p, ...args));
this.engine.registerFilter('path_relative', (from, to) => path.relative(from, to));
this.engine.registerFilter('path_posix_basename', (p, ...arg) => path.posix.basename(p, ...arg));
this.engine.registerFilter('path_posix_dirname', (p) => path.posix.dirname(p));
this.engine.registerFilter('path_posix_normalize', (p) => path.posix.normalize(p));
this.engine.registerFilter('path_posix_join', (p, ...args) => path.posix.join(p, ...args));
this.engine.registerFilter('path_posix_relative', (from, to) => path.posix.relative(from, to));
this.engine.registerFilter('path_win32_basename', (p, ...arg) => path.win32.basename(p, ...arg));
this.engine.registerFilter('path_win32_dirname', (p) => path.win32.dirname(p));
this.engine.registerFilter('path_win32_normalize', (p) => path.win32.normalize(p));
this.engine.registerFilter('path_win32_join', (p, ...args) => path.win32.join(p, ...args));
this.engine.registerFilter('path_win32_relative', (from, to) => path.win32.relative(from, to));
this.engine.registerFilter('util_format', (format, ...args) => {
return util.format(format, ...args);
});
this.engine.registerFilter('to_filename', (p) => filterPath(p));
}
prepareMap(packageJson, buildConfigurationName) {
(0, node_assert_1.strict)(packageJson);
(0, node_assert_1.strict)(typeof os.version === 'function', 'Mandatory os.version available only since 12.x');
const liquidMap = {
env: process.env,
os: {
EOL: os.EOL,
arch: os.arch(),
constants: {
signals: os.constants.signals,
errno: os.constants.errno
},
cpus: os.cpus(),
endianness: os.endianness(),
homedir: os.homedir(),
hostname: os.hostname(),
platform: os.platform(),
release: os.release(),
tmpdir: os.tmpdir(),
type: os.type(),
version: (os.version())
},
path: {
delimiter: path.delimiter,
sep: path.sep,
win32: {
delimiter: path.win32.delimiter,
sep: path.win32.sep
},
posix: {
delimiter: path.posix.delimiter,
sep: path.posix.sep
}
},
package: packageJson
};
liquidMap.properties = {};
if (_isJsonObject(packageJson.xpack)) {
if (_isJsonObject(packageJson.xpack.properties)) {
liquidMap.properties =
_joinMultiLineProperties(packageJson.xpack.properties);
}
if (buildConfigurationName !== undefined &&
buildConfigurationName !== null &&
buildConfigurationName.trim() !== '') {
if (packageJson.xpack.buildConfigurations === undefined) {
throw new Error('package.json has no buildConfigurations');
}
const buildConfiguration = packageJson.xpack.buildConfigurations[buildConfigurationName];
if (buildConfiguration === undefined) {
throw new Error('package.json has no buildConfiguration.' +
`${buildConfigurationName}`);
}
liquidMap.configuration = {
...buildConfiguration,
name: buildConfigurationName
};
if (_isJsonObject(buildConfiguration.properties)) {
liquidMap.properties = {
...liquidMap.properties,
...(_joinMultiLineProperties(buildConfiguration.properties))
};
}
}
}
return liquidMap;
}
async performSubstitutions(input, map) {
(0, node_assert_1.strict)(map);
if (input === '') {
return input;
}
const { log } = this;
let current = input;
let substituted = current;
while (current.includes('{{') || current.includes('{%')) {
substituted = await this.engine.parseAndRender(current, map);
log.trace(`XpmLiquidMap.performSubstitutions(): '${substituted}'`);
if (substituted === current) {
break;
}
current = substituted;
}
return substituted;
}
}
exports.XpmLiquid = XpmLiquid;
//# sourceMappingURL=xpm-liquid.js.map