@lenne.tech/cli-plugin-helper
Version:
Helper plugin for Gluegun CLI Projects
148 lines • 5.08 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Npm = void 0;
const find = require("find-file-up");
const fs = require("fs");
const path_1 = require("path");
/**
* npm functions
*/
class Npm {
/**
* Constructor for integration of toolbox
*/
constructor(toolbox) {
this.toolbox = toolbox;
}
/**
* Get package.json
*/
getPackageJson() {
return __awaiter(this, arguments, void 0, function* (options = {}) {
// Toolbox features
const { filesystem, helper, print: { error }, } = this.toolbox;
// Prepare options
const opts = Object.assign({
cwd: filesystem.cwd(),
errorMessage: 'No package.json found!',
showError: false,
}, options);
// Find package.json
const path = yield find('package.json', opts.cwd);
if (!path) {
if (opts.showError) {
error(opts.errorMessage);
}
return { data: null, path: '' };
}
// Everything ok
return { data: yield helper.readFile(path), path };
});
}
/**
* Set data for package.json
*/
setPackageJson(data_1) {
return __awaiter(this, arguments, void 0, function* (data, options = {}) {
if (typeof data === 'object') {
data = JSON.stringify(data, null, 2);
}
// Path to package.json
const { path } = yield this.getPackageJson(options);
if (!path) {
return;
}
// Write
try {
fs.unlinkSync(path);
fs.writeFileSync(path, data);
}
catch (e) {
return '';
}
// Done
return path;
});
}
/**
* Install npm packages
*/
install() {
return __awaiter(this, arguments, void 0, function* (options = {}) {
// Toolbox features
const { filesystem, print: { spin }, system, } = this.toolbox;
// Prepare options
const opts = Object.assign({
cwd: filesystem.cwd(),
errorMessage: 'No package.json found!',
showError: false,
}, options);
// Find package.json
const { path } = yield this.getPackageJson(opts);
if (!path) {
return false;
}
// Install npm packages
const npmSpin = spin('Install npm packages');
yield system.run(`cd ${(0, path_1.dirname)(path)} && npm i`);
npmSpin.succeed();
return true;
});
}
/**
* Update package.json
*/
update() {
return __awaiter(this, arguments, void 0, function* (options = {}) {
// Toolbox features
const { filesystem, print: { spin }, system, } = this.toolbox;
// Prepare options
const opts = Object.assign({
cwd: filesystem.cwd(),
errorMessage: 'No package.json found!',
install: false,
showError: false,
}, options);
// Find package.json
const { path } = yield this.getPackageJson(opts);
if (!path) {
return false;
}
// Check ncu
if (!system.which('ncu')) {
const installNcuSpin = spin('Install ncu');
yield system.run('npm i -g npm-check-updates');
installNcuSpin.succeed();
}
// Update package.json
const updateSpin = spin('Update package.json');
yield system.run(`ncu -u --packageFile ${path}`);
updateSpin.succeed();
// Install packages
if (opts.install) {
const installSpin = spin('Install npm packages');
yield system.run(`cd ${(0, path_1.dirname)(path)} && npm i`);
installSpin.succeed();
}
// Success
return true;
});
}
}
exports.Npm = Npm;
/**
* Extend toolbox
*/
exports.default = (toolbox) => {
toolbox.npm = new Npm(toolbox);
};
//# sourceMappingURL=npm.extension.js.map