This repository has been archived by the owner on May 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Gruntfile.js
204 lines (170 loc) · 7.89 KB
/
Gruntfile.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* jshint node:true */
const fetch = require('node-fetch');
const fs = require('fs');
const path = require('path');
const download = require('download');
const po = require('pofile');
const { exec } = require('child_process');
module.exports = function (grunt) {
'use strict';
const translationMeta = {};
grunt.initConfig({
prefix: 'sensei-wc-paid-courses',
minimumPercentageComplete: 30,
baseFileUrl: 'https://translate.wordpress.com/projects/sensei%2Fsensei-wc-paid-courses',
clean: {
tmp: "tmp/*"
}
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.registerTask( 'default', function () {
grunt.log.writeln( "########################################################### " );
grunt.log.writeln( "##### Sensei WC Paid Courses Language Pack Generator ###### " );
grunt.log.writeln( "########################################################### " );
grunt.log.writeln( " # Commands: \n" );
grunt.log.writeln( " grunt build:{version} = Build all the translation packages." );
});
grunt.registerTask('build', function (version) {
const done = this.async();
if (!fs.existsSync('./vendor/bin/wp')) {
grunt.log.error('Please run `composer install` to install dependencies');
done();
return;
}
if ( ! version ) {
grunt.log.error('Must be called with version number. Example: `grunt build:1.0.0`');
done();
return;
}
const basePackagePath = 'packages/' + version;
if ( fs.existsSync(basePackagePath) ) {
grunt.log.writeln('Updating packages for version ' + version);
fs.readdir(basePackagePath, (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(path.join(basePackagePath, file), err => {
if (err) throw err;
});
}
});
} else {
grunt.log.writeln('Creating packages for new version ' + version);
fs.mkdirSync(basePackagePath);
}
grunt.log.write("Fetching languages...");
fetch('https://translate.wordpress.com/api/projects/sensei/sensei-wc-paid-courses/')
.then(function (response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
})
.then(res => res.json())
.then(translations => {
grunt.log.writeln("done!");
if (typeof translations.translation_sets === 'array') {
grunt.log.error("Invalid set of translations received.");
done();
return;
}
const languageBuilds = [];
translations.translation_sets.forEach(function (ts) {
if (ts.percent_translated < grunt.config.get('minimumPercentageComplete')) {
grunt.log.writeln("Skipping " + ts.name + " as it is only " + ts.percent_translated + "% translated.");
return true;
}
languageBuilds.push(buildLanguage(basePackagePath, ts));
});
const buildPromise = Promise.all(languageBuilds);
buildPromise.then(res => {
const metaPath = basePackagePath + '/index.json';
const metadata = {};
metadata['built'] = (new Date()).toISOString();
metadata['version'] = version;
metadata['packages'] = translationMeta;
fs.writeFileSync(metaPath, JSON.stringify(metadata));
grunt.log.writeln("Built all packages!");
grunt.task.run('clean:tmp');
done();
});
})
.catch(error => {
grunt.log.writeln("error! (" + error + ")");
});
});
const downloadFile = (async (url, downloadPath) => {
await download(url).then(data => {
fs.writeFileSync(downloadPath, data);
})
});
const buildLanguage = (async (basePackagePath, ts) => {
await new Promise((resolve, reject) => {
const tmpLangPath = 'tmp/' + ts.wp_locale;
const tmpMoPath = tmpLangPath + '/' + grunt.config.get('prefix') + '-' + ts.wp_locale + '.mo';
const tmpPoPath = tmpLangPath + '/' + grunt.config.get('prefix') + '-' + ts.wp_locale + '.po';
if ( fs.existsSync(tmpLangPath) ) {
fs.readdir(tmpLangPath, (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(path.join(tmpLangPath, file), err => {
if (err) throw err;
});
}
});
} else {
fs.mkdirSync( tmpLangPath, { recursive: true } );
}
const downloads = [];
downloads.push(downloadFile(grunt.config.get('baseFileUrl') + '/' + ts.locale + '/default/export-translations/?format=po', tmpPoPath));
downloads.push(downloadFile(grunt.config.get('baseFileUrl') + '/' + ts.locale + '/default/export-translations/?format=mo', tmpMoPath));
const downloadPromises = Promise.all(downloads);
downloadPromises.then(function () {
if (!fs.existsSync(tmpPoPath) || !fs.existsSync(tmpMoPath)) {
reject();
throw Error("Unable to download files for " + ts.name);
}
})
.catch(err => {
grunt.log.error(err);
grunt.log.error("Unable to download files for " + ts.name);
})
.then(function () {
grunt.log.writeln("Downloaded all files for " + ts.name);
const zipDest = basePackagePath + '/' + ts.wp_locale + '.zip';
exec(
'cd ' + tmpLangPath + ' && ../../vendor/bin/wp i18n make-json --no-purge ' + path.parse( tmpPoPath ).base,
function ( error, stdout, stderr ) {
if ( error ) {
reject();
return;
}
fs.readdir(tmpLangPath, (err, files) => {
if (err) throw err;
const zip = new require('node-zip')();
for (const file of files) {
zip.file(file, fs.readFileSync(path.join(tmpLangPath, file)));
}
const data = zip.generate({
base64: false,
compression: 'DEFLATE'
});
fs.writeFileSync(zipDest, data, 'binary');
po.load(tmpPoPath, function (err, po) {
const metadata = {};
metadata['updated'] = false;
if ( typeof po.headers['PO-Revision-Date'] === 'string' ) {
metadata['updated'] = po.headers['PO-Revision-Date'];
}
translationMeta[ts.wp_locale] = metadata;
resolve();
});
});
}
);
})
.catch(err => {
grunt.log.error(err);
});
}).catch(err => [Error]);
});
};