dotfiles-generator
Version:
Utility to auto-generate dotfiles like .gitignore, .editorconfig etc. in your project.
69 lines (47 loc) • 2.52 kB
JavaScript
;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _fsExtra = require('fs-extra');
var _fsExtra2 = _interopRequireDefault(_fsExtra);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _utils = require('../lib/utils');
var _chalk = require('chalk');
var _chalk2 = _interopRequireDefault(_chalk);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
module.exports = function (files, options) {
var ERROR = 'ERROR',
INFO = 'INFO';
(0, _utils.logger)('Generating dotfiles...');
// Get the source path of dotfiles
var sourcePath = (0, _utils.getSourcePath)();
// Check if the source path is a valid directory
if (!(0, _utils.isDirectoryExist)(sourcePath)) {
throw new Error(sourcePath + ' is not a valid dotfiles source directory.');
}
// Set the destination path to copy dotfiles
var destinationPath = options.destination ? _path2.default.resolve(options.destination) : (0, _utils.getDestinationPath)();
// Check if the destination path is a valid directory
if (!(0, _utils.isDirectoryExist)(destinationPath)) return (0, _utils.logger)(destinationPath + ' does not exist, can not copy dotfiles.', ERROR);
/**
* Loop through dotfiles requests
*/
files.forEach(function (filename) {
var source = _path2.default.join(this.sourcePath, filename.substring(1, filename.length));
var destination = _path2.default.join(this.destinationPath, filename);
// Check if the dotfile is available for install
if (!(0, _utils.isPathExist)(source)) return (0, _utils.logger)(source + ' is not availble.', ERROR);
var isDestinationExist = (0, _utils.isPathExist)(destination);
if (!this.overwrite && isDestinationExist) {
(0, _utils.logger)(filename + _chalk2.default.yellow(' -> ') + destination + _chalk2.default.yellow(' [skipped]'), INFO);
return;
}
(0, _utils.logger)(filename + _chalk2.default.green(' -> ') + destination + (this.overwrite ? _chalk2.default.red(' [overwritten]') : _chalk2.default.green(' [done]')), INFO);
// Copy files
_fsExtra2.default.copySync(source, destination, {
overwrite: this.overwrite
});
}, _extends({
sourcePath: sourcePath,
destinationPath: destinationPath
}, options));
};