-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.js
More file actions
143 lines (112 loc) · 4.22 KB
/
Copy pathexport.js
File metadata and controls
143 lines (112 loc) · 4.22 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var fs = require('fs');
var configPath = __dirname + '/config.json';
var elementsPath = __dirname + '/elements.json';
var sys = require('sys');
var exec = require('child_process').exec;
if (!fs.existsSync(configPath)) {
console.log('Please create a config.json file before beginning. See config.json.tpl for an example.');
return;
}
var config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
var elements = JSON.parse(fs.readFileSync(elementsPath, 'utf8'));
// Do these in reverse order so the org pages stays in alphabetical order.
var remainingElements = Object.keys(elements).reverse();
// Create the working directory if we need to.
var checkoutDir = __dirname + '/' + config.checkoutDir;
if (!fs.existsSync(checkoutDir)) {
console.log('No working checkout dir found. Cloning repos into: ' + checkoutDir);
fs.mkdirSync(checkoutDir);
cloneRepos();
return;
} else {
// We can start.
exportNextElement();
}
function cloneRepos() {
var remainingRepos = remainingElements;
var index = 0;
eachRepo();
function eachRepo() {
var repo = remainingRepos[index];
// If there is no repo remaining, start exporting.
if (!repo) {
return exportNextElement();
}
console.log('Processing repo: ' + repo);
// Create the element working directory.
var elementRepoPath = checkoutDir + repo;
index++;
// Clone the repos in order.
fetchRepo(elementRepoPath, repo);
}
function fetchRepo(elementRepoPath, elementName) {
var command = [
'cd ' + checkoutDir,
'git clone ' + config.remote + elementName + '.git'
].join(' && ');
console.log('Running: ' + command);
exec(command, function() {
console.log('args: ', arguments);
eachRepo();
});
}
}
function exportNextElement() {
var elementKey = remainingElements.shift();
// If there is no key, we're done.
if (!elementKey) {
return done();
}
var element = elements[elementKey];
element.name = elementKey;
element.repoFolder = checkoutDir + element.name + '/';
element.gaiaElementFolder = config.gaiaPath + 'shared/elements/gaia_' + element.name;
console.log('Exporting: ' + elementKey);
copyElement(element);
}
function copyElement(element) {
// If the element folder does not exist, create it.
if (!fs.existsSync(element.repoFolder + 'element')) {
fs.mkdirSync(element.repoFolder + 'element');
}
// Format templates
formatTemplatesSync(element);
var command = [
'cp -R ' + element.gaiaElementFolder + '/* ' + element.repoFolder + 'element/',
// Super hack for component utils.
// These will be removed in the future, but for now copy the file and replace any reference to it in examples
'cp ' + element.gaiaElementFolder + '/../../js/component_utils.js ' + element.repoFolder + 'element/.',
'cd ' + element.repoFolder,
'find ./ -type f -name "*.html" -exec sed -i -e \'s/\\.\\.\\/\\.\\.\\/\\.\\.\\/js\\/component_utils\\.js/\\.\\.\\/component_utils\\.js/g\' {} \\;',
'rm element/examples/*.html-e 2>/dev/null',
// Inclusion hack for other web components.
// Instead of including other repositories here for components, we simply change the paths of the script files. E.g.,
// <script src="../../gaia_buttons/script.js"></script> becomes:
// <script src="http://gaia-elements.github.io/buttons/element/script.js"></script>
'find ./ -type f -name "*.html" -exec sed -i -e \'s/\\.\\.\\/\\.\\.\\/gaia_\\([A-Za-z]*\\)/http:\\/\\/gaia-elements\\.github\\.io\\/\\1\\/element/g\' {} \\;',
'rm element/examples/*.html-e 2>/dev/null',
'cd ' + element.repoFolder,
'git add *',
'git commit -m "[' + element.name + '] ' + config.commitMessage + '"',
// Commented out intentionally. You better be sure you want to do this.
//'git push origin && git checkout gh-pages && git merge master && git push origin && git checkout master'
].join(' && ');
console.log('Running: ' + command);
exec(command, function() {
console.log('args: ', arguments);
exportNextElement(element);
});
}
function formatTemplatesSync(element) {
var files = [
'README.md'
];
files.forEach(function(file) {
var fileContent = fs.readFileSync(__dirname + '/templates/' + file, 'utf8');
fileContent = fileContent.replace(/\{name\}/g, element.name);
fs.writeFileSync(element.repoFolder + file, fileContent, 'utf8');
});
}
function done() {
console.log('All done.');
}