Skip to content

Commit c63f6a3

Browse files
author
Gitlab-CI
committed
fix(quality gate): fix duplicate code
1 parent b6e6fc4 commit c63f6a3

File tree

4 files changed

+41
-42
lines changed

4 files changed

+41
-42
lines changed

src/cli/cmds/copy.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,7 @@
22
const async = require('async');
33
const path = require('path');
44
const fs = require('fs');
5-
const { spawn: spawnDefault } = require('child_process');
6-
7-
const spawn = (command, cb) => {
8-
const split = command.split(' ');
9-
const program = split[0];
10-
const args = split.slice(1);
11-
const child = spawnDefault(program, args || []);
12-
const outputList = [];
13-
child.stdout.setEncoding('utf8');
14-
child.stderr.setEncoding('utf8');
15-
child.stdout.on('data', (data) => outputList.push(data) && console.log(data.replace(/\n$/, '')));
16-
child.stderr.on('data', (data) => outputList.push(data) && console.log(data.replace(/\n$/, '')));
17-
child.on('close', (code) => code === 1 ? cb(new Error(`child process exited with code ${code}`, [outputList.join('')])) : cb(null, [outputList.join('')]));
18-
};
5+
const { spawn } = require('../utils');
196

207
exports.command = 'copy';
218
exports.desc = 'Copy template documentation from `internal/templates/docs` to `docs`.';

src/cli/cmds/intl.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,7 @@
22
const path = require('path');
33
const fs = require('fs');
44
const async = require('async');
5-
const { spawn: spawnDefault, exec: execDefault } = require('child_process');
6-
7-
const exec = (command, cb) => {
8-
execDefault(command, {
9-
maxBuffer: 1024 * 1024,
10-
}, cb);
11-
};
12-
const spawn = (command, cb) => {
13-
const split = command.split(' ');
14-
const program = split[0];
15-
const args = split.slice(1);
16-
const child = spawnDefault(program, args || []);
17-
const outputList = [];
18-
child.stdout.setEncoding('utf8');
19-
child.stderr.setEncoding('utf8');
20-
child.stdout.on('data', (data) => outputList.push(data) && console.log(data.replace(/\n$/, '')));
21-
child.stderr.on('data', (data) => outputList.push(data) && console.log(data.replace(/\n$/, '')));
22-
child.on('close', (code) => code === 1 ? cb(new Error(`child process exited with code ${code}`, [outputList.join('')])) : cb(null, [outputList.join('')]));
23-
};
5+
const { spawn, exec } = require('../utils');
246

257
const emptyLink = (pkg, lang, isPrivate) => isPrivate ? `*empty* ([edit now](${pkg.bugs.url.split('/issues')[0]}/edit/dev/translate/${lang}.json))` : `*empty*`;
268
require('shelljs/global');

src/cli/cmds/variable.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@
22
const async = require('async');
33
const path = require('path');
44
const glob = require('glob');
5-
const fs = require('fs');
6-
7-
const sedReplace = (input, before, after, output, cb = () => {}) => {
8-
const file = fs.readFileSync(input, 'utf8');
9-
const re = new RegExp(escapeRegExp(before), 'gm');
10-
const newFile = file.replace(re, after);
11-
fs.writeFileSync(output, newFile, 'utf8');
12-
cb();
13-
};
5+
const { sedReplace } = require('../utils');
146

157
function escapeRegExp(text) {
168
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');

src/cli/utils.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const fs = require('fs');
2+
const { spawn: spawnDefault, exec: execDefault } = require('child_process');
3+
4+
const exec = (command, cb) => {
5+
execDefault(command, {
6+
maxBuffer: 1024 * 1024,
7+
}, cb);
8+
};
9+
const spawn = (command, cb) => {
10+
const split = command.split(' ');
11+
const program = split[0];
12+
const args = split.slice(1);
13+
const child = spawnDefault(program, args || []);
14+
const outputList = [];
15+
child.stdout.setEncoding('utf8');
16+
child.stderr.setEncoding('utf8');
17+
child.stdout.on('data', (data) => outputList.push(data) && console.log(data.replace(/\n$/, '')));
18+
child.stderr.on('data', (data) => outputList.push(data) && console.log(data.replace(/\n$/, '')));
19+
child.on('close', (code) => code === 1 ? cb(new Error(`child process exited with code ${code}`, [outputList.join('')])) : cb(null, [outputList.join('')]));
20+
};
21+
22+
const sedReplace = (input, before, after, output, cb = () => {}) => {
23+
const file = fs.readFileSync(input, 'utf8');
24+
const re = new RegExp(escapeRegExp(before), 'gm');
25+
const newFile = file.replace(re, after);
26+
fs.writeFileSync(output, newFile, 'utf8');
27+
cb();
28+
};
29+
30+
function escapeRegExp(text) {
31+
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
32+
}
33+
34+
module.exports = {
35+
exec,
36+
spawn,
37+
sedReplace,
38+
};

0 commit comments

Comments
 (0)