Skip to content

Commit 9681e06

Browse files
authored
Merge pull request #108 from codesy/es6-refactor
Es6 refactor
2 parents e90ef58 + d1629ea commit 9681e06

18 files changed

+294
-261
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ The codesy.io widget is an add-on for Firefox, Chrome, and Opera that adds codes
99
* `git clone https://github.com/codesy/widgets.git`
1010
2. Install requirements:
1111
* `cd widgets`
12-
* `npm install`
12+
* `npm install` (You can install globally with `npm install -g`)
1313
3. Run these gulp tasks to watch changes to files and compile extensions.
14-
* gulp dev-chrome-unpacked - creates addon directory and watches for changes
15-
* gulp dev-chrome-packed - creates addon zip file and watches for changes
16-
* gulp dev-firefox-unpacked - creates addon directory and watches for changes
17-
* gulp dev-firefox-packed - creates addon xpi file and watches for changes
14+
* gulp dev-chrome-unpacked - creates chrome/ directory and watches for changes
15+
* gulp dev-chrome-packed - creates a zip file in /chrome and watches for changes
16+
* gulp dev-firefox-unpacked - creates firefox/ directory and watches for changes
17+
* gulp dev-firefox-packed - creates an xpi in firefox/ file and watches for changes
1818

1919
combined tasks:
2020
* gulp dev-packed - creates addon packages and watches for changes
@@ -36,14 +36,12 @@ combined tasks:
3636

3737
### Chrome and Opera
3838
1. Run 'gulp publish-chrome'
39-
* Runs gulp coffee to compile .js files
4039
* Removes debug lines from .js files e.g. console.log
4140
* Creates a zip file for uploading to the chrome store. The zip file contains the manifest.json file.
4241
2. Upload the codesy.zip file to the chrome or opera store
4342

4443
### Firefox
4544
1. Run 'gulp publish-firefox'
46-
* Runs gulp coffee to compile .js files
4745
* Removes debug lines from .js files e.g. console.log
4846
* Creates an xpi file for uploading to the moz store. The file contains the manifest.json file.
4947
2. Upload the xpi file to the moz store

build/codesy-0.0.0.5.xpi

-78.5 KB
Binary file not shown.
Binary file not shown.

gulpfile.js

Lines changed: 79 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
var fs = require('fs');
2-
var gulp = require('gulp');
3-
var gutil = require('gulp-util');
4-
var coffee = require('gulp-coffee');
5-
var stripDebug = require('gulp-strip-debug');
6-
var mergeStream = require('merge-stream');
7-
var mergeJSON = require('gulp-merge-json');
8-
var zip = require('gulp-zip');
9-
var jeditor = require("gulp-json-editor");
10-
var rename = require('gulp-rename')
1+
const fs = require('fs');
2+
const gulp = require('gulp');
3+
const stripDebug = require('gulp-strip-debug');
4+
const mergeStream = require('merge-stream');
5+
const mergeJSON = require('gulp-merge-json');
6+
const zip = require('gulp-zip');
7+
const jeditor = require("gulp-json-editor");
8+
const rename = require('gulp-rename')
9+
const headerComment = require('gulp-header-comment');
1110

1211
// Settings for building packages
13-
var settings = {
12+
const settings = {
1413
name: 'codesy',
15-
version: '0.0.0.5',
14+
version: '0.0.0.6',
1615
source: './src',
17-
destination: './build',
16+
destination: './dist',
1817
static_files: {
1918
source: './static',
2019
glob: ['css/*', 'js/*.js', 'img/*.png']
@@ -26,12 +25,12 @@ var settings = {
2625
firefox: {
2726
source: './src/firefox',
2827
destination: 'firefox',
29-
extension: '.xpi'
28+
extension: 'xpi'
3029
},
3130
chrome: {
3231
source: './src/chrome',
3332
destination: 'chrome',
34-
extension: '.zip'
33+
extension: 'zip'
3534

3635
}
3736
}
@@ -42,112 +41,104 @@ var settings = {
4241
// destination: (optional) path where files will go. If destination is not included,
4342
// the functions will return a stream of files.
4443

45-
compile_coffee = function(options) {
46-
this.source = options.source
47-
this.destination = options.destination
44+
javascript_src = function(options) {
4845
return (
49-
function(_this) {
46+
function({source, destination}) {
5047
return function() {
51-
console.log("compile "+_this.source + "/*.coffee files")
52-
// next line compile coffee files in source directory and ./src root
53-
var compiled_stream = gulp.src([_this.source + '/*.coffee', settings.source +'/*.coffee'])
54-
.pipe(coffee({bare: true}).on('error', gutil.log))
55-
if (_this.destination){
56-
return compiled_stream.pipe(gulp.dest(_this.destination + '/js'))
48+
console.log(`gather src ${source}/*.js files`)
49+
console.log(`gather src ${settings.source}/*.js files`)
50+
51+
const js_files = gulp.src([`${source}/*.js`, `${settings.source}/*.js`])
52+
.pipe(headerComment(`codesy widget version ${settings.version}`))
53+
54+
if (destination){
55+
console.log(` destination ${destination}/js`);
56+
return js_files.pipe(gulp.dest(`${destination}/js`))
5757
} else {
58-
return compiled_stream
58+
return js_files
5959
}
6060
}
6161
}
62-
)(this)
62+
)(options)
6363
}
6464

6565
static_files = function(destination) {
66-
this.destination = destination
67-
return (
68-
function(_this) {
69-
return function() {
70-
var static_stream = gulp.src(settings.static_files.glob,
71-
{ base: settings.static_files.source,
72-
cwd: settings.static_files.source
73-
})
74-
if (_this.destination){
75-
return static_stream.pipe(gulp.dest( _this.destination ))
76-
} else {
77-
return static_stream
66+
return (
67+
function(destination,{glob, source}) {
68+
return function() {
69+
const static_stream = gulp.src(glob, { base: source, cwd: source })
70+
if (destination){
71+
return static_stream.pipe(gulp.dest( destination ))
72+
} else {
73+
return static_stream
74+
}
75+
}
7876
}
79-
}
80-
}
81-
)(this)
77+
)(destination,settings.static_files)
8278
}
8379

8480
// this function needs to include dev server details in the options object:
8581
// dev_server: object with domain and port
8682

87-
var manifest = function (options){
88-
this.options = options
83+
const manifest = function (options){
8984
return (
90-
function(_this) {
85+
function({source, destination}) {
9186
return function() {
92-
var common = gulp.src(settings.source + '/manifest.json')
93-
var additions = gulp.src(_this.options.source+'/manifest_additions.json')
87+
const common = gulp.src(`${settings.source}/manifest.json`)
88+
const additions = gulp.src(`${source}/manifest_additions.json`)
9489
manifest_stream = mergeStream(additions, common)
9590
.pipe(mergeJSON('manifest.json'))
9691
.pipe(jeditor(function(json) {
9792
json.version=settings.version
9893
return json
9994
}))
100-
if (_this.destination){
101-
return manifest_stream.pipe(gulp.dest(_this.destination));
95+
if (destination){
96+
return manifest_stream.pipe(gulp.dest(destination));
10297
} else {
10398
return manifest_stream
10499
}
105100
}
106-
})(this)
101+
})(options)
107102
}
108103

109-
var add_dev_server = function (manifest_stream) {
110-
var warning = ['THIS IS NOT the production manifest.'],
111-
dev_permission =["https://",settings.dev_server.domain,":",settings.dev_server.port,"/"],
112-
dev_match =["https://",settings.dev_server.domain,"/"]
104+
const add_dev_server = function (manifest_stream) {
105+
({domain, port} = settings.dev_server)
106+
const warning = 'THIS IS NOT the production manifest.',
107+
dev_permission =`https://${domain}:${port}/`,
108+
dev_match =`https://${domain}/`
113109
return manifest_stream
114110
.pipe(jeditor(function(json) {
115-
json.DEV_WARNING=warning.join("")
116-
json.permissions.push(dev_permission.join(""))
117-
json.content_scripts[1].matches.push(dev_match.join(""))
111+
json.DEV_WARNING=warning
112+
json.permissions.push(dev_permission)
113+
json.content_scripts[1].matches.push(dev_match)
118114
return json
119115
}))
120116
}
121117

122-
var package = function (options, zipped, for_dev){
123-
this.options = options
124-
this.zipped = zipped
125-
this.for_dev = for_dev
118+
const package = function (options, zipped, for_dev){
126119
return (
127-
function(_this) {
120+
function({source, destination: dest, extension: ext}, zipped, for_dev) {
128121
return function() {
129-
var package_name, destination, package_stream
130-
var static_stream = (new static_files())()
131-
var manifest_stream = (new manifest({source:_this.options.source}))()
132-
var js_stream = (new compile_coffee({source:_this.options.source}))()
133-
.pipe(rename(function (path) {
134-
path.dirname += "/js";
135-
}))
136-
137-
if (_this.for_dev){
122+
console.log(`package source: ${source}`);
123+
let package_name, destination, package_stream;
124+
let static_stream = (new static_files())()
125+
let manifest_stream = (new manifest({source}))()
126+
const js_stream = (new javascript_src( {source} ))()
127+
.pipe(rename( (path)=>path.dirname += "/js" ))
128+
129+
if (for_dev){
138130
manifest_stream = add_dev_server (manifest_stream)
139-
package_name = settings.name + '-dev' + _this.options.extension
140-
destination = _this.options.destination
141-
131+
package_name = `${settings.name}-${settings.version}.dev.${ext}`
142132
} else {
143133
js_stream.pipe(stripDebug())
144-
package_name = settings.name + '-' + settings.version + _this.options.extension
145-
destination = settings.destination
134+
package_name = `${settings.name}-${settings.version}.${ext}`
146135
}
136+
destination = for_dev ? dest : settings.destination
137+
console.log(`package dest: ${destination}`);
147138

148139
package_stream = mergeStream (manifest_stream,js_stream,static_stream)
149140

150-
if (_this.zipped) {
141+
if (zipped) {
151142
package_stream
152143
.pipe(zip(package_name))
153144
.pipe(gulp.dest(destination))
@@ -157,22 +148,19 @@ var package = function (options, zipped, for_dev){
157148
}
158149
}
159150
}
160-
)(this)
151+
)(options, zipped, for_dev)
161152
}
162153

163-
var watch_dev = function (options, task) {
154+
const watch_dev = function ({source}, task) {
164155
console.log("start watching");
165-
var manifest_files = [settings.source + '/manifest.json',options.source + '/manifest_additions.json']
166-
var coffee_files = [options.source + '/*.coffee', settings.source + '/*.coffee']
167-
// watch static files
168-
gulp.watch(settings.static_files.source + '/**', task)
169-
// watch manifest files
156+
const manifest_files = [`${settings.source}/manifest.json`,`${source}/manifest_additions.json`]
157+
const js_files = [`${source}/*.js`, `${settings.source}/*.js`]
158+
gulp.watch(`${settings.static_files.source}/**`, task)
170159
gulp.watch(manifest_files, task)
171-
gulp.watch(coffee_files, task)
160+
gulp.watch(js_files, task)
172161
}
173162

174163
// DEV TASKS
175-
176164
gulp.task('dev-chrome-unpacked', ['chrome-unpacked'], function() {
177165
watch_dev(settings.chrome,['chrome-unpacked'])
178166
})
@@ -182,16 +170,18 @@ gulp.task('dev-chrome-packed', ['chrome-dev-zip'], function() {
182170
})
183171

184172
gulp.task('dev-firefox-unpacked', ['firefox-unpacked'], function() {
185-
watch_dev(settings.chrome,['firefox-unpacked'])
173+
watch_dev(settings.firefox,['firefox-unpacked'])
186174
})
187175

188176
gulp.task('dev-firefox-packed', ['firefox-dev-xpi'], function() {
189-
watch_dev(settings.chrome,['firefox-dev-xpi'])
177+
watch_dev(settings.firefox,['firefox-dev-xpi'])
190178
})
191179

192180
gulp.task('dev-unpacked',['dev-chrome-unpacked','dev-firefox-unpacked'])
193181
gulp.task('dev-packed',['dev-chrome-packed','dev-firefox-packed'])
194182

183+
// FF dev must use file
184+
gulp.task('dev-mixed',['dev-chrome-unpacked','dev-firefox-packed'])
195185

196186

197187
// FILE BUILDING TASKS
@@ -208,11 +198,10 @@ gulp.task('chrome-dev-zip', (new package(settings.chrome, true, true)))
208198
// create chrome dev directroy in the chrome.source directory with dev settings
209199
gulp.task('chrome-unpacked', (new package(settings.chrome, false, true)))
210200

211-
212201
// create xpi for FF prod
213202
gulp.task('publish-firefox', (new package(settings.firefox, true, false)))
214203

215-
// create zip for chrome
204+
// create zip for chrome and opera
216205
gulp.task('publish-chrome', (new package(settings.chrome, true, false)))
217206

218207
gulp.task('publish-all',['publish-firefox','publish-chrome'])

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
"dependencies": {
1414
"gulp": "^3.9.0",
1515
"gulp-coffee": "2.3.1",
16+
"gulp-header-comment": "^0.1.0",
1617
"gulp-json-editor": "2.2.1",
18+
"gulp-merge-json": "0.5.0",
1719
"gulp-rename": "^1.2.0",
1820
"gulp-strip-debug": "1.0.2",
19-
"gulp-util": "^3.0.1",
2021
"gulp-zip": "2.0.3",
21-
"merge-stream": "0.1.7",
22-
"gulp-merge-json":"0.5.0"
22+
"jquery": "3.2.0",
23+
"merge-stream": "0.1.7"
2324
},
2425
"scripts": {
2526
"test": "echo \"Error: no test specified\" && exit 1"

src/chrome/manifest_additions.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
{
2-
"background": {
3-
"persistent": true,
4-
"scripts": [
5-
"js/csp.js"
6-
]
7-
},
82
"version_name": "invitation-only beta"
93
}

src/csp.coffee

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)