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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 2x 2x 2x 1x 3x 3x 2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 10x 10x 3x 1x 3x 2x 2x 3x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 2x 2x 2x 2x 2x 2x 2x 9x 1x 1x 1x 2x 2x 2x 2x 1x 1x 6x 1x 2x 2x 5x 4x 5x 5x 4x 4x 4x 8x 8x 8x 9x 7x 6x 6x 6x 6x 7x 1x 1x 1x 1x 1x 9x 1x 1x 1x 8x 8x 9x 9x 1x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import * as colorette from 'colorette';
import inquirer from 'inquirer';
import fs from 'fs';
import path from 'path';
import pure from '../index.js';
const formats = ['csv', 'json', 'txt', 'none'];
const template = '{{random.number}}; {{internet.password}}; {{address.city}};\n';
const templatejson = '{ "number": {{random.number}}, "pass": "{{internet.password}}" }';
function defaultTemplate(answers) {
if (answers.formatType === 'json') {
return templatejson;
}
return template;
}
function validateTemplate(answer) {
if (!/^\s*$/.test(answer)) {
return true;
}
return 'You need to pass a valid string!';
}
function validateRows(answer) {
if (!Number.isNaN(answer) && answer !== '') {
return true;
}
return 'You need to pass a valid number';
}
function conditionalPath(answers) {
return answers.formatType !== 'none';
}
function validatePath(answer) {
if (!/^\s*$/.test(answer)) {
if (fs.existsSync(answer)) {
return true;
}
return 'No such directory. You need to pass a valid path!';
}
return 'You need to pass a valid string!';
}
function conditionalName(answers) {
return answers.formatType !== 'none';
}
function generator(arg) {
if (arg.locale) {
if (pure.possibleLocales.indexOf(arg.locale) === -1) {
throw new Error(colorette.red(`\nš The following locale is not supported: ${arg.locale}`));
} else {
pure.setLocale(arg.locale);
}
}
return new Promise((resolve, reject) => {
inquirer
.prompt([
{
type: 'list',
name: 'formatType',
message: 'What format to use',
choices: formats
},
{
type: 'editor',
name: 'templateStr',
message: 'What template to use to generate',
default: defaultTemplate.bind(this),
validate: validateTemplate.bind(this)
},
{
type: 'input',
name: 'rows',
message: 'How many rows to generate',
default: 1,
validate: validateRows.bind(this)
},
{
type: 'confirm',
name: 'uniqueRows',
message: 'Unique informations',
default: false
},
{
type: 'input',
name: 'savePath',
message: 'What path to save',
default: process.cwd(),
when: conditionalPath.bind(this),
validate: validatePath.bind(this)
},
{
type: 'input',
name: 'saveName',
message: 'What filename to save',
default: 'generated',
when: conditionalName.bind(this),
validate: validateTemplate.bind(this)
}
])
.then(answers => {
let templateStr = '';
let generated = '';
// Unique rows information
if (answers.formatType === 'json' && !answers.uniqueRows) {
templateStr = [];
for (let index = 0; index < answers.rows; index += 1) {
templateStr.push(answers.templateStr.replace(/\n/g, ''));
}
generated = pure.fake.parse(`[${templateStr}]`);
} else if (answers.formatType === 'json' && answers.uniqueRows) {
generated = [];
for (let index = 0; index < answers.rows; index += 1) {
generated.push(
pure.unique.exec(pure.fake.parse, [answers.templateStr.replace(/\n/g, '')])
);
}
generated = `[${generated}]`;
} else if (answers.uniqueRows) {
for (let index = 0; index < answers.rows; index += 1) {
generated += pure.unique.exec(pure.fake.parse, [answers.templateStr]);
}
} else {
for (let index = 0; index < answers.rows; index += 1) {
templateStr += `${answers.templateStr}`;
}
generated = pure.fake.parse(templateStr);
}
// Save output
let pathJoin = '';
if (answers.formatType !== 'none') {
if (answers.savePath.includes(process.cwd())) {
const filename = `/${answers.saveName}.${answers.formatType}`;
pathJoin = path.join(answers.savePath, filename);
fs.writeFileSync(pathJoin, generated);
console.log(colorette.gray('\nGenerated āļø'));
} else {
const filename = `/${answers.saveName}.${answers.formatType}`;
pathJoin = path.join(process.cwd(), answers.savePath, filename);
fs.writeFileSync(pathJoin, generated);
console.log(colorette.gray('\nGenerated āļø'));
}
} else {
console.log(colorette.gray('\nResult šØļø'));
console.log(colorette.blue(generated));
}
resolve({ pathJoin, generated });
})
.catch(error => {
reject(new Error(colorette.red(`\nš The following error has occurred: \n${error}`)));
});
});
}
export default {
defaultTemplate,
validateTemplate,
validateRows,
conditionalPath,
validatePath,
conditionalName,
generator
};
|