forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.js
More file actions
55 lines (47 loc) · 1.55 KB
/
Copy pathformat.js
File metadata and controls
55 lines (47 loc) · 1.55 KB
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
#!/usr/bin/env node
'use strict';
/**
* Usage: node format.js
*
* Author: Carlos Abraham Hernandez
* https://abranhe.com (abraham@abranhe.com)
*/
const path = require('path');
const glob = require('glob');
const chalk = require('chalk');
const decamelize = require('decamelize');
const shell = require('child_process').execSync;
const getFiles = (src, callback) => {
glob(src + '/**', callback);
};
getFiles('../', (err, res) => {
if (err) {
console.log('Error', err);
} else {
res.map((file) => {
// Accept only valid C++ Files (.cpp,.hpp,.h)
if (path.extname(file) !== '.cpp' && path.extname(file) !== '.hpp' && path.extname(file) !== '.h') {
return;
}
// Only avilable for Linux/Macos
// https://stackoverflow.com/a/41030518/7602110
// Can be replaced in the future
shell(`mv ${file.replace(' ', '\\ ')} ${decamelize(file.replace(' ', '_'))}`);
if (file.replace(' ', '\\ ') !== decamelize(file)) {
console.log(
`The file ${chalk.red(file)} was successfuly changed to ${chalk.green(decamelize(file.replace(' ', '_')))}`
);
// Change file on git history
// https://stackoverflow.com/a/16071375/7602110
shell(`cd ../\
git mv --force ${file.replace(' ', '\\ ').replace('../', '')} ${decamelize(
file.replace(' ', '_').replace('../', '')
)}`);
}
// Replace for convention .h for .hpp
if (path.extname(file) === '.h') {
shell(`mv ${file} ${file.replace(/\.[^\.]+$/, '.hpp')}`);
}
});
}
});