@lenne.tech/cli-plugin-helper
Version:
Helper plugin for Gluegun CLI Projects
194 lines • 6.88 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;
});
}
/**
* Detect which package manager is used in the project
*/
detectPackageManager(projectPath) {
const { filesystem } = this.toolbox;
// Check for lock files in the project directory
if (filesystem.exists(`${projectPath}/pnpm-lock.yaml`)) {
return 'pnpm';
}
if (filesystem.exists(`${projectPath}/yarn.lock`)) {
return 'yarn';
}
// Default to npm
return 'npm';
}
/**
* 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(),
detectPackageManager: true,
errorMessage: 'No package.json found!',
showError: false,
}, options);
// Find package.json
const { path } = yield this.getPackageJson(opts);
if (!path) {
return false;
}
const projectDir = (0, path_1.dirname)(path);
const packageManager = opts.detectPackageManager ? this.detectPackageManager(projectDir) : 'npm';
// Install packages with the appropriate package manager
const installSpin = spin(`Install packages using ${packageManager}`);
let installCommand;
switch (packageManager) {
case 'pnpm':
installCommand = 'pnpm install';
break;
case 'yarn':
installCommand = 'yarn install';
break;
case 'npm':
default:
installCommand = 'npm i';
break;
}
yield system.run(`cd ${projectDir} && ${installCommand}`);
installSpin.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 projectDir = (0, path_1.dirname)(path);
const packageManager = this.detectPackageManager(projectDir);
const installSpin = spin(`Install packages using ${packageManager}`);
let installCommand;
switch (packageManager) {
case 'pnpm':
installCommand = 'pnpm install';
break;
case 'yarn':
installCommand = 'yarn install';
break;
case 'npm':
default:
installCommand = 'npm i';
break;
}
yield system.run(`cd ${projectDir} && ${installCommand}`);
installSpin.succeed();
}
// Success
return true;
});
}
}
exports.Npm = Npm;
/**
* Extend toolbox
*/
exports.default = (toolbox) => {
toolbox.npm = new Npm(toolbox);
};
//# sourceMappingURL=npm.extension.js.map
;