Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | 24x 24x 24x 24x 24x 24x 32x 32x 32x 32x 32x 32x 32x 32x 16x 16x 16x 32x 32x 32x 32x 32x 168x 28x 28x 28x 28x 28x 28x 24x 4x 4x 4x 4x 4x 4x 40x 40x 40x 40x 40x 40x 40x 40x | import {join} from 'path';
import {existsSync} from 'fs-extra';
import merge from 'lodash.merge';
import {format} from './common';
import BasicEditor from './BasicEditor';
const {assign} = Object;
const silent = () => {};
class Common extends BasicEditor {
create() {
const self = this;
const {contents, path} = self;
self.created || (existsSync(path) || self.write(contents));
return self;
}
read() {
const {fs, path} = this;
return fs.exists(path) ? fs.read(path) : '';
}
write(contents) {
const self = this;
const {esm, fs, path, prependedContents, queue} = self;
const exportString = esm ? 'export default ' : 'module.exports = ';
const formatted = `${prependedContents}${exportString}${format(contents)}`.replace(/\r*\n$/g, ';');
queue
.add(() => fs.write(path, formatted))
.then(() => self.created = existsSync(path))
.catch(silent);
return assign(self, {contents});
}
extend(code) {
this.contents = merge(this.contents, code);
this.write(this.contents);
return this;
}
prepend(code) {
const self = this;
const {contents, prependedContents} = self;
Eif (typeof code === 'string' && code.length > 0) {
self.prependedContents = `${code}\n${prependedContents}`.replace(/\n*$/, '\n\n');
}
return self.write(contents);
}
}
/**
* Create and edit a JS module with a fluent API
* @param {string} filename Name of file to edit
* @param {string} [contents=''] Contents of file
* @param {Object} options Options to configure module
* @param {boolean} [options.esm=false] Select to use 'module.exports =' (false) or 'export default' (true)
* @return {ModuleEditor} ModuleEditor class (extends {@link BasicEditor})
*/
export const createModuleEditor = (filename, contents = '', options = {esm: false}) => class ModuleEditor extends Common {
prependedContents = '';
created = false;
constructor(cwd = process.cwd()) {
super();
const {esm} = options;
const path = join(cwd, filename);
assign(this, {contents, esm, path});
}
};
/**
* Create and edit a JS module (with function export) using a fluent API
* @param {string} filename Name of file to edit
* @param {string} [contents=''] Contents of file
* @param {Object} options Options to configure module
* @param {string[]} [options.params=[]] Params to use as function parameters. ex: ['a', 'b'] becomes (a, b) => {}
* @param {boolean} [options.esm=false] Select to use 'module.exports =' (false) or 'export default' (true)
* @return {ModuleEditor} ModuleEditor class (extends {@link BasicEditor})
*/
export const createFunctionModuleEditor = (filename, contents = '', options = {params: [], esm: false}) => class ModuleEditor extends Common {
prependedContents = '';
created = false;
constructor(cwd = process.cwd()) {
super();
const {esm, params} = options;
const path = join(cwd, filename);
assign(this, {
contents,
esm,
path,
params: params || []
});
}
write(contents) {
const self = this;
const {esm, fs, params, path, prependedContents, queue} = self;
const exportString = esm ? `export default (${params.join(', ')}) => (` : `module.exports = (${params.join(', ')}) => (`;
const formatted = `${prependedContents}${exportString}${format(contents)}`.replace(/\r*\n$/g, ');');
queue
.add(() => fs.write(path, formatted))
.then(() => self.created = existsSync(path))
.catch(silent);
return assign(self, {contents});
}
};
export default createModuleEditor; |