@controlplane/cli
Version:
Control Plane Corporation CLI
42 lines • 1.43 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.edit = void 0;
const tmp = require("tmp");
const fs = require("fs");
const child_process_1 = require("child_process");
async function edit(contents, format) {
const file = tmp.fileSync({
postfix: '.' + format,
});
fs.writeFileSync(file.name, contents, {
encoding: 'utf-8',
});
return new Promise((resolve, reject) => {
launchEditor(file.name, {}, (code) => {
if (code != 0) {
reject(new Error('Editor did not exit cleanly'));
}
const newContents = fs.readFileSync(file.name).toString('utf-8');
if (contents != newContents) {
resolve(newContents);
}
else {
// no changes to the file
resolve(undefined);
}
});
});
}
exports.edit = edit;
// https://github.com/substack/node-editor/blob/master/index.js
function launchEditor(path, opts, cb) {
var ed = /^win/.test(process.platform) ? 'notepad' : 'vim';
var editor = opts.editor || process.env.VISUAL || process.env.EDITOR || ed;
var args = editor.split(/\s+/);
var bin = args.shift();
var ps = (0, child_process_1.spawn)(bin, args.concat([path]), { stdio: 'inherit' });
ps.on('exit', function (code, sig) {
cb(code, sig);
});
}
//# sourceMappingURL=editor.js.map
;