forked from SmartRSS/Smart-RSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
214 lines (189 loc) · 6.82 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
205
206
207
208
209
210
211
212
213
module.exports = function (grunt) {
const {join, dirname} = require('path');
const {readdirSync, lstatSync} = require('fs');
const cleanup = function () {
const defaultConfig = {
removeFromManifest: [],
};
const config = Object.assign(defaultConfig, this.data);
const root = join(__dirname, 'dist', this.target);
const manifestPath = join(root, 'manifest.json');
const originalManifest = grunt.file.readJSON(manifestPath);
let newManifest = Object.assign({}, originalManifest);
if (config.csp) {
newManifest['content_security_policy'] = originalManifest[config.csp];
delete newManifest[config.csp];
}
if (config.permissions) {
newManifest['permissions'] = originalManifest[config.permissions];
delete newManifest[config.permissions];
}
if (config.removeFromManifest) {
config.removeFromManifest.forEach((item) => {
delete newManifest[item];
});
}
console.log(newManifest);
grunt.file.write(manifestPath, JSON.stringify(newManifest, null, 2));
};
const commit = function (level = 'patch') {
let {exec} = require('child_process');
let done = this.async();
exec('git add *', (err, stdout, stderr) => {
if (err) {
console.log(`stderr: ${stderr}`);
done(false);
return;
}
exec(`git commit -m "auto version bump: ${level}"`, (err) => {
if (err) {
done(false);
return;
}
done(true);
});
});
};
const bumpVersion = function (level = 'patch') {
const semver = require('semver');
const manifestPath = join(__dirname, 'src/manifest.json');
const manifest = grunt.file.readJSON(manifestPath);
manifest.version = semver.inc(manifest.version, level);
grunt.file.write(manifestPath, JSON.stringify(manifest, null, 2));
};
const scan = (dir) => {
const filesList = [];
readdirSync(dir).forEach((file) => {
if (file[0] === '.') {
return;
}
const filePath = join(dir, file);
if (lstatSync(filePath).isDirectory()) {
filesList.push(...scan(filePath));
return;
}
filesList.push(filePath);
});
return filesList;
};
const zip = function () {
const defaultConfig = {
dirname: this.target,
};
const config = Object.assign(defaultConfig, this.data);
const root = join(__dirname, 'dist', config.dirname);
const manifestPath = join(root, 'manifest.json');
const filesList = scan(root);
const version = getVersion(manifestPath);
const AdmZip = require('adm-zip');
const zipFile = new AdmZip();
filesList.forEach((file) => {
// TODO: shorten somehow, make sure that `path` doesn't contain leading slashes nor backslashes
let path = dirname(file).replace(root + '/', '').replace(root + '\\', '').replace(root, '');
zipFile.addLocalFile(file, path);
});
zipFile.writeZip(join(__dirname, 'dist', 'SmartRSS_v' + version + '_' + this.target + '.zip'));
};
const getVersion = function (manifest) {
return grunt.file.readJSON(manifest).version;
};
const stripComments = function () {
const defaultConfig = {
dirname: this.target,
};
const config = Object.assign(defaultConfig, this.data);
const multilineComment = /^[\t\s]*\/\*\*?[^!][\s\S]*?\*\/[\r\n]/gm
const specialComments = /^[\t\s]*\/\*!\*?[^!][\s\S]*?\*\/[\r\n]/gm
const singleLineComment = /^[\t\s]*(\/\/)[^\n\r]*[\n\r]/gm
const root = join(__dirname, 'dist', config.dirname);
const filesList = scan(root);
filesList.forEach((filePath) => {
if (!filePath.endsWith('.js')) {
return;
}
const contents = grunt.file.read(filePath)
.replace(multilineComment, '')
.replace(singleLineComment, '')
.replace(specialComments, '');
grunt.file.write(filePath, contents);
});
}
// Project configuration.
grunt.initConfig({
watch: {
scripts: {
files: ['src/**/*'],
tasks: ['prepare'],
options: {
spawn: true
}
}
},
cleanup: {
firefox: {
removeFromManifest: [
'chromium_content_security_policy',
'chromium_permissions'
]
},
chromium: {
removeFromManifest: [
'browser_specific_settings',
'developer'
],
permissions: 'chromium_permissions',
csp: 'chromium_content_security_policy'
}
},
copy: {
firefox: {
files: [{
expand: true,
cwd: './src/',
src: [
'**/*',
'!images/chrome-small-tile.png'
],
filter: 'isFile',
dest: './dist/firefox/'
}]
},
chromium: {
files: [{
expand: true,
cwd: './src/',
src: [
'**/*',
'!images/chrome-small-tile.png'
],
filter: 'isFile',
dest: './dist/chromium/'
}]
}
},
zip: {
firefox: {},
chromium: {}
},
comments: {
firefox: {},
chromium: {}
},
});
grunt.registerTask('bump-version', '', bumpVersion);
grunt.registerTask('commit', '', commit);
grunt.registerTask('package', ['prepare', 'zip']);
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.registerMultiTask('cleanup', '', cleanup);
grunt.registerMultiTask('zip', '', zip);
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerMultiTask('comments', '', stripComments)
grunt.registerTask('prepare', ['copy', 'cleanup', 'comments']);
grunt.registerTask('release', '', function (level = 'patch') {
if (!['major', 'minor', 'patch'].includes(level)) {
console.error('Wrong update level, aborting');
return false;
}
grunt.task.run(['bump-version:' + level, 'commit:' + level, 'copy', 'cleanup', 'comments', 'zip']);
});
};