-
Notifications
You must be signed in to change notification settings - Fork 3
/
merge.js
70 lines (60 loc) · 1.27 KB
/
merge.js
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
// https://stackoverflow.com/a/40022630
import fs from 'fs';
import { glob } from 'glob';
const outputPath = './snippets/';
const options = [
{
path: 'src/javascript/**/*.json',
filename: 'javascript',
},
{
path: 'src/vue/**/*.json',
filename: 'vue',
},
{
path: 'src/html/**/*.json',
filename: 'html',
},
{
path: 'src/css/**/*.json',
filename: 'css',
},
];
function handler(files, filename) {
try {
let output = {};
fs.existsSync(outputPath) || fs.mkdirSync(outputPath);
fs.accessSync(
outputPath,
fs.constants.R_OK | fs.constants.W_OK
);
files.forEach((file) => {
console.log('filename: %o', file);
const contents = JSON.parse(
fs.readFileSync(file, 'utf8')
);
Object.assign(output, contents);
});
fs.writeFileSync(
`${outputPath}${filename}.json`,
JSON.stringify(output, null, 4)
);
console.log(`Complete! :)`);
} catch (err) {
console.log(err);
console.error(
`${outputPath} ${err.code === 'ENOENT'
? 'does not exist'
: 'is read-only'
}`
);
console.log('Failed! :(');
}
}
function init() {
options.forEach(async ({ path, filename }) => {
const files = await glob(path);
handler(files, filename)
});
}
init();