Skip to content

Commit 3196961

Browse files
committed
Add math helper
1 parent 3a432f3 commit 3196961

4 files changed

Lines changed: 49 additions & 38 deletions

File tree

src/helpers/math.helper.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { describe, expect, test } from '@jest/globals';
2+
import mathHelper from './math.helper';
3+
4+
describe('Test math helper js', () => {
5+
test('generate random string', () => {
6+
expect(mathHelper.random(10, { lower: true, upper: false, numeric: false, symbol: false })).toMatch(/^[a-z]{10}$/);
7+
expect(mathHelper.random(10, { lower: false, upper: true, numeric: false, symbol: false })).toMatch(/^[A-Z]{10}$/);
8+
expect(mathHelper.random(10, { lower: false, upper: false, numeric: true, symbol: false })).toMatch(/^[0-9]{10}$/);
9+
expect(mathHelper.random(10, { lower: false, upper: false, numeric: false, symbol: true })).toMatch(/^[~`!@#$%^&*()_+-={}[\]:";'<>?,./|\\]{10}$/);
10+
});
11+
});

src/helpers/math.helper.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default {
2+
random: function (length: number, { lower = true, upper = true, numeric = true, symbol = false } = {}) {
3+
let mask = '';
4+
if (lower) mask += 'abcdefghijklmnopqrstuvwxyz';
5+
if (upper) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
6+
if (numeric) mask += '0123456789';
7+
if (symbol) mask += '~`!@#$%^&*()_+-={}[]:";\'<>?,./|\\';
8+
let result = '';
9+
for (var i = length; i > 0; --i) result += mask[Math.floor(Math.random() * mask.length)];
10+
return result;
11+
}
12+
}

src/helpers/wp.helper.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
import { Engine } from 'php-parser';
22

3-
function parseWpConfig(content: string, options: any = {}) {
4-
const engine = new Engine(options);
5-
const { children } = engine.parseCode(content, '');
6-
let database, dbHost = 'localhost', dbPort = 3306, dbUser;
7-
for (const child of children as any[]) {
8-
if (child.expression?.what?.name === 'define') {
9-
const [ name, value ] = child.expression.arguments;
10-
if (name.kind === 'string' && value.kind === 'string') {
11-
if (name.value === 'DB_NAME') {
12-
database = value.value as string;
13-
}
14-
if (name.value === 'DB_HOST') {
15-
const [ host, port ] = value.value.split(':');
16-
dbHost = host ?? 'localhost';
17-
if (port !== undefined) {
18-
dbPort = parseInt(port);
3+
export default {
4+
parseWpConfig: function (content: string, options: any = {}) {
5+
const engine = new Engine(options);
6+
const { children } = engine.parseCode(content, '');
7+
let database, dbHost = 'localhost', dbPort = 3306, dbUser;
8+
for (const child of children as any[]) {
9+
if (child.expression?.what?.name === 'define') {
10+
const [ name, value ] = child.expression.arguments;
11+
if (name.kind === 'string' && value.kind === 'string') {
12+
if (name.value === 'DB_NAME') {
13+
database = value.value as string;
14+
}
15+
if (name.value === 'DB_HOST') {
16+
const [ host, port ] = value.value.split(':');
17+
dbHost = host ?? 'localhost';
18+
if (port !== undefined) {
19+
dbPort = parseInt(port);
20+
}
21+
}
22+
if (name.value === 'DB_USER') {
23+
dbUser = value.value as string;
1924
}
20-
}
21-
if (name.value === 'DB_USER') {
22-
dbUser = value.value as string;
2325
}
2426
}
2527
}
26-
}
27-
return { database, dbHost, dbPort, dbUser };
28-
}
29-
30-
export default {
31-
parseWpConfig,
28+
return { database, dbHost, dbPort, dbUser };
29+
},
3230
}

src/services/install.service.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,16 @@ import shell from 'shelljs';
44
import inquirer from 'inquirer';
55
import { Sequelize, QueryTypes } from 'sequelize';
66
import wpHelper from '@/helpers/wp.helper.js';
7+
import mathHelper from '@/helpers/math.helper';
78
import { MySQLCredential } from '@/types/common.js';
89

9-
function random(length: number, { lower = true, upper = true, numeric = true, symbol = false } = {}) {
10-
let mask = '';
11-
if (lower) mask += 'abcdefghijklmnopqrstuvwxyz';
12-
if (upper) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
13-
if (numeric) mask += '0123456789';
14-
if (symbol) mask += '~`!@#$%^&*()_+-={}[]:";\'<>?,./|\\';
15-
let result = '';
16-
for (var i = length; i > 0; --i) result += mask[Math.floor(Math.random() * mask.length)];
17-
return result;
18-
}
19-
2010
async function configSetup(wpDir: string, domain: string, nickname: string, database: MySQLCredential) {
2111
console.log('Setup OS and MySQL database');
2212

2313
let { host, port, username, password } = database;
2414
let wpConfigDbUser = undefined;
2515
let wpDb = nickname;
26-
let wpDbPassword = random(20, { symbol: true });
16+
let wpDbPassword = mathHelper.random(20, { symbol: true });
2717

2818
// check php version for default
2919
const matches = shell.exec('php -v').stdout.match(/^PHP ([0-9]\.[0-9])/);
@@ -63,7 +53,7 @@ async function configSetup(wpDir: string, domain: string, nickname: string, data
6353
}
6454

6555
// setting wordpress config
66-
shell.sed('-i', 'put your unique phrase here', random(32, { symbol: true }), wpConfig);
56+
shell.sed('-i', 'put your unique phrase here', mathHelper.random(32, { symbol: true }), wpConfig);
6757
shell.sed('-i', 'database_name_here', wpDb, wpConfig);
6858
shell.sed('-i', 'username_here', nickname, wpConfig);
6959
shell.sed('-i', 'password_here', wpDbPassword, wpConfig);

0 commit comments

Comments
 (0)