npx-run
Version:
Easily run scripts using npx.
43 lines (31 loc) • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.print = void 0;
var _chalk = _interopRequireDefault(require("chalk"));
var _globals = require("./globals");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const toString = item => {
if (typeof item === 'string') {
return item;
} else if (item[Symbol.iterator]) {
return [...item].join(' ');
}
return item;
};
const indentation = text => text.length - text.trimStart().length;
const unindent = (lines, startPos) => lines.map(line => line.slice(startPos));
const autoUnindent = text => {
const lines = text.split('\n');
const [, indented] = lines;
const startPos = indented ? indentation(indented) : 0;
const finalLines = indented ? lines.slice(1) : lines;
const unindentedLines = unindent(finalLines, startPos);
return unindentedLines.join('\n');
};
const print = (parts, ...args) => {
const text = (0, _chalk.default)(parts, ...args.map(toString));
_globals.process.stdout.write(`${autoUnindent(text)}\n`);
};
exports.print = print;