forked from Synthetixio/synthetix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack.js
100 lines (80 loc) · 2.61 KB
/
pack.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
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
const fs = require('fs');
const path = require('path');
const mustache = require('mustache');
const execa = require('execa');
async function main() {
const template = fs.readFileSync(path.join(__dirname, 'config.template.yml'), 'utf-8');
// Mustache lingo:
// "partials" = snippets of code to be injected in a template
// Builds an object like:
/*
{
"job-abc": "<job content>",
"workflow-xyz": "<job content>",
...
"commands": "<generated list of all command file names>"
"workflows": "<generated list of all workflow file names>"
...
}
*/
const partials = {
...buildPartialsForDirectory(path.join(__dirname, 'src/commands')),
...buildPartialsForDirectory(path.join(__dirname, 'src/jobs')),
...buildPartialsForDirectory(path.join(__dirname, 'src/workflows')),
...readPartialsInDirectory(path.join(__dirname, 'src/snippets'), false, false),
};
// Get rid of all commented lines before processing partials.
const commentedLinesRegex = /^\s*#.*/gm;
Object.keys(partials).map(
key => (partials[key] = partials[key].replace(commentedLinesRegex, ''))
);
let output = mustache.render(template, {}, partials);
const emptyLinesRegex = /^\s*\n/gm;
output = output.replace(emptyLinesRegex, '');
const outputPath = path.join(__dirname, 'config.yml');
fs.writeFileSync(outputPath, output);
// Also run circleci validation if circleci is in path
try {
await execa('circleci', ['config', 'validate']);
} catch (error) {
console.log(error.stderr);
}
}
function buildPartialsForDirectory(dirPath) {
return {
[path.basename(dirPath)]: buildPartialsArrayFromDirectory(dirPath),
...readPartialsInDirectory(dirPath),
};
}
function buildPartialsArrayFromDirectory(dirPath) {
let array = '';
fs.readdirSync(dirPath).forEach(file => {
array += `{{> ${file}}}\n\n`;
});
return array;
}
function readPartialsInDirectory(dirPath, includeName = true, indent = true) {
const files = {};
fs.readdirSync(dirPath).forEach(file => {
const filePath = path.join(dirPath, file);
const fileName = file.split('.')[0];
const indentationString = indent ? ' ' : '';
if (includeName) {
files[file] = `${fileName}:\n${readFileWithIndentation(filePath, indentationString)}`;
} else {
files[file] = readFileWithIndentation(filePath, indentationString);
}
});
return files;
}
function readFileWithIndentation(filePath, indentationString) {
let contents = fs.readFileSync(filePath, 'utf-8');
contents = indentationString + contents.split('\n').join(`\n${indentationString}`);
return contents;
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});