Skip to content

Commit bdc4aee

Browse files
authored
Merge pull request #110 from codesy/promisy-fun
Promisy fun
2 parents 9681e06 + ecbba3e commit bdc4aee

File tree

5 files changed

+192
-216
lines changed

5 files changed

+192
-216
lines changed

README.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,16 @@ The codesy.io widget is an add-on for Firefox, Chrome, and Opera that adds codes
1010
2. Install requirements:
1111
* `cd widgets`
1212
* `npm install` (You can install globally with `npm install -g`)
13-
3. Run these gulp tasks to watch changes to files and compile extensions.
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
18-
19-
combined tasks:
20-
* gulp dev-packed - creates addon packages and watches for changes
21-
* gulp dev-unpacked - creates directories with addon files and watches all for changes
13+
3. See the list of gulp tasks build or watch changes to files and compile extensions:
2214

2315

2416
### To use the Chrome Extension
25-
1. Run one of the dev-chrome tasks above
17+
1. Run `workon-chrome-directory`
2618
2. Follow the [Unpacked Chrome Extensions
2719
docs](http://developer.chrome.com/extensions/getstarted.html#unpacked) and load the `chrome` directory
2820

2921
### To use the Firefox Add-on
30-
1. Run one of the dev-firefox tasks above
22+
1. Run `gulp workon-firefox-file`
3123
2. Goto the the //about:addons page
3224
3. Select 'Install Addon from File ...')
3325
4. Load the xpi file from the firefox directory
@@ -45,3 +37,26 @@ combined tasks:
4537
* Removes debug lines from .js files e.g. console.log
4638
* Creates an xpi file for uploading to the moz store. The file contains the manifest.json file.
4739
2. Upload the xpi file to the moz store
40+
41+
42+
## Gulp Tasks
43+
44+
#### build-{browser}-file:
45+
* **build-firefox-file**: create xpi for FF dev in the firefox.source directory with dev settings
46+
* **build-chrome-file**: create zip for chrome dev in the chrome.source directory with dev settings
47+
48+
#### build-{browser}-directory:
49+
* **build-firefox-directory**: create firefox dev directroy in the firefox.source directory with dev settings
50+
* **build-chrome-directory**: create chrome dev directroy in the chrome.source directory with dev settings
51+
52+
#### workon-{browser}-directory or workon-{browser}-directory:
53+
watches extension files and rebuilds
54+
* **workon-firefox-file**
55+
* **workon-firefox-directory**
56+
* **workon-chrome-file**
57+
* **workon-chrome-directory**
58+
59+
#### publish-{browser}-file
60+
* **publish-firefox-file**: create xpi for FF prod
61+
* **publish-chrome-file**: create zip for chrome and opera
62+
* **publish-all**: creates all browser extension files

gulpfile.js

Lines changed: 78 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -41,67 +41,47 @@ const settings = {
4141
// destination: (optional) path where files will go. If destination is not included,
4242
// the functions will return a stream of files.
4343

44-
javascript_src = function(options) {
45-
return (
46-
function({source, destination}) {
47-
return function() {
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`))
57-
} else {
58-
return js_files
59-
}
60-
}
44+
function javascript_src ({source, destination}) {
45+
return function() {
46+
console.log(`gather src ${source}/*.js files`)
47+
console.log(`gather src ${settings.source}/*.js files`)
48+
49+
const js_files = gulp.src([`${source}/*.js`, `${settings.source}/*.js`])
50+
.pipe(headerComment(`codesy widget version ${settings.version}`))
51+
52+
if (destination){
53+
console.log(` destination ${destination}/js`);
54+
return js_files.pipe(gulp.dest(`${destination}/js`))
55+
} else {
56+
return js_files
6157
}
62-
)(options)
58+
}
6359
}
6460

65-
static_files = function(destination) {
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-
}
76-
}
77-
)(destination,settings.static_files)
78-
}
61+
static_files = ({glob, source}) => gulp.src(glob, { base: source, cwd: source });
7962

8063
// this function needs to include dev server details in the options object:
8164
// dev_server: object with domain and port
8265

83-
const manifest = function (options){
84-
return (
85-
function({source, destination}) {
86-
return function() {
87-
const common = gulp.src(`${settings.source}/manifest.json`)
88-
const additions = gulp.src(`${source}/manifest_additions.json`)
89-
manifest_stream = mergeStream(additions, common)
66+
function manifest({source, destination}){
67+
return function() {
68+
const common = gulp.src(`${settings.source}/manifest.json`)
69+
const additions = gulp.src(`${source}/manifest_additions.json`)
70+
manifest_stream = mergeStream(additions, common)
9071
.pipe(mergeJSON('manifest.json'))
9172
.pipe(jeditor(function(json) {
9273
json.version=settings.version
9374
return json
9475
}))
95-
if (destination){
96-
return manifest_stream.pipe(gulp.dest(destination));
97-
} else {
98-
return manifest_stream
99-
}
76+
if (destination){
77+
return manifest_stream.pipe(gulp.dest(destination));
78+
} else {
79+
return manifest_stream
10080
}
101-
})(options)
81+
}
10282
}
10383

104-
const add_dev_server = function (manifest_stream) {
84+
function add_dev_server (manifest_stream) {
10585
({domain, port} = settings.dev_server)
10686
const warning = 'THIS IS NOT the production manifest.',
10787
dev_permission =`https://${domain}:${port}/`,
@@ -115,43 +95,38 @@ const add_dev_server = function (manifest_stream) {
11595
}))
11696
}
11797

118-
const package = function (options, zipped, for_dev){
119-
return (
120-
function({source, destination: dest, extension: ext}, zipped, for_dev) {
121-
return function() {
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){
130-
manifest_stream = add_dev_server (manifest_stream)
131-
package_name = `${settings.name}-${settings.version}.dev.${ext}`
132-
} else {
133-
js_stream.pipe(stripDebug())
134-
package_name = `${settings.name}-${settings.version}.${ext}`
135-
}
136-
destination = for_dev ? dest : settings.destination
137-
console.log(`package dest: ${destination}`);
138-
139-
package_stream = mergeStream (manifest_stream,js_stream,static_stream)
140-
141-
if (zipped) {
142-
package_stream
143-
.pipe(zip(package_name))
144-
.pipe(gulp.dest(destination))
145-
} else {
146-
package_stream
147-
.pipe(gulp.dest(destination));
148-
}
149-
}
98+
function package ({source, destination: dest, extension: ext}, zipped, for_dev){
99+
return function() {
100+
console.log(`package source: ${source}`);
101+
const package_name = `${settings.name}-${settings.version}${for_dev?'.dev':''}.${ext}`
102+
const destination = for_dev ? dest : settings.destination
103+
console.log(`package dest: ${destination}`);
104+
105+
let static_stream = static_files(settings.static_files)
106+
let manifest_stream = (new manifest({source}))()
107+
const js_stream = (new javascript_src( {source} ))()
108+
.pipe(rename( (path)=>path.dirname += "/js" ))
109+
110+
if (for_dev){
111+
manifest_stream = add_dev_server (manifest_stream)
112+
} else {
113+
js_stream.pipe(stripDebug())
150114
}
151-
)(options, zipped, for_dev)
115+
116+
const package_stream = mergeStream (manifest_stream,js_stream,static_stream)
117+
118+
if (zipped) {
119+
package_stream
120+
.pipe(zip(package_name))
121+
.pipe(gulp.dest(destination))
122+
} else {
123+
package_stream
124+
.pipe(gulp.dest(destination));
125+
}
126+
}
152127
}
153128

154-
const watch_dev = function ({source}, task) {
129+
function watch_src ({source}, task) {
155130
console.log("start watching");
156131
const manifest_files = [`${settings.source}/manifest.json`,`${source}/manifest_additions.json`]
157132
const js_files = [`${source}/*.js`, `${settings.source}/*.js`]
@@ -160,48 +135,29 @@ const watch_dev = function ({source}, task) {
160135
gulp.watch(js_files, task)
161136
}
162137

163-
// DEV TASKS
164-
gulp.task('dev-chrome-unpacked', ['chrome-unpacked'], function() {
165-
watch_dev(settings.chrome,['chrome-unpacked'])
166-
})
167-
168-
gulp.task('dev-chrome-packed', ['chrome-dev-zip'], function() {
169-
watch_dev(settings.chrome,['chrome-dev-zip'])
170-
})
171-
172-
gulp.task('dev-firefox-unpacked', ['firefox-unpacked'], function() {
173-
watch_dev(settings.firefox,['firefox-unpacked'])
174-
})
175-
176-
gulp.task('dev-firefox-packed', ['firefox-dev-xpi'], function() {
177-
watch_dev(settings.firefox,['firefox-dev-xpi'])
178-
})
138+
const browsers = ['firefox', 'chrome']
139+
140+
for (browser of browsers){
141+
const build_file_task = `build-${browser}-file`
142+
const build_directory_task = `build-${browser}-directory`
143+
const options = settings[browser]
144+
145+
// ADDON BUILDING TASKS
146+
gulp.task(build_file_task, (new package(options, true, true)))
147+
gulp.task(build_directory_task, (new package(options, false, true)))
148+
gulp.task(`publish-${browser}-file`, (new package(options, true, false)))
149+
150+
// WATCH TASKS
151+
gulp.task(`workon-${browser}-directory`, [build_directory_task],
152+
() => watch_src(options, [build_directory_task])
153+
);
154+
gulp.task(`workon-${browser}-file`, [build_file_task],
155+
() => watch_src(options, [build_file_task])
156+
);
157+
}
179158

180-
gulp.task('dev-unpacked',['dev-chrome-unpacked','dev-firefox-unpacked'])
181-
gulp.task('dev-packed',['dev-chrome-packed','dev-firefox-packed'])
159+
const publish_tasks = browsers.map((b)=>`publish-${b}-file`)
160+
gulp.task('publish-all',publish_tasks)
182161

183162
// FF dev must use file
184-
gulp.task('dev-mixed',['dev-chrome-unpacked','dev-firefox-packed'])
185-
186-
187-
// FILE BUILDING TASKS
188-
189-
// create xpi for FF dev in the firefox.source directory with dev settings
190-
gulp.task('firefox-dev-xpi', (new package(settings.firefox, true, true)))
191-
192-
// create firefox dev directroy in the firefox.source directory with dev settings
193-
gulp.task('firefox-unpacked', (new package(settings.firefox, false, true)))
194-
195-
// create zip for chrome dev in the chrome.source directory with dev settings
196-
gulp.task('chrome-dev-zip', (new package(settings.chrome, true, true)))
197-
198-
// create chrome dev directroy in the chrome.source directory with dev settings
199-
gulp.task('chrome-unpacked', (new package(settings.chrome, false, true)))
200-
201-
// create xpi for FF prod
202-
gulp.task('publish-firefox', (new package(settings.firefox, true, false)))
203-
204-
// create zip for chrome and opera
205-
gulp.task('publish-chrome', (new package(settings.chrome, true, false)))
206-
207-
gulp.task('publish-all',['publish-firefox','publish-chrome'])
163+
gulp.task('workon-mixed',['workon-chrome-directory','workon-firefox-file'])

src/csp.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1+
function makeCspAppender (domain='') {
2+
const csp_names = ['CONTENT-SECURITY-POLICY','X-WEBKIT-CSP']
3+
const name_finder = (name) => (csp_name) => csp_name === name.toUpperCase()
4+
const if_csp = (name) => csp_names.find(name_finder(name)) ? true : false
15

2-
githubFilter = {
3-
urls: ["https://github.com/*"],
4-
types: ["main_frame"]
5-
};
6+
const codesy_types = 'connect-src child-src script-src style-src';
7+
const is_codesy = (type) => codesy_types.indexOf(type) !== -1;
8+
const add_codesy = (accum, word) =>`${accum} ${word} ${is_codesy(word) ? domain : '' }`;
9+
const insert_domain = (csp) => csp.split(' ').reduce(add_codesy,'');
610

7-
headerOptions = ["responseHeaders", "blocking"]
8-
9-
const makeCspAppender = function(domain='') {
10-
const types = 'connect-src child-src script-src style-src font-src';
11-
const isType = (word) => types.indexOf(word) !== -1;
12-
const addDomain = (accum, word)=>`${accum} ${word} ${isType(word) ? domain : '' }`
13-
const isCSP = function (name) {
14-
name = name.toUpperCase()
15-
return (name === 'CONTENT-SECURITY-POLICY') || (name === 'X-WEBKIT-CSP');
16-
};
1711
return function({responseHeaders: headers}) {
1812
console.time('codesy map headers');
19-
const responseHeaders = headers.map(function({name, value: v}){
20-
value = isCSP(name) ? v.split(' ').reduce(addDomain,'') : v
13+
const responseHeaders = headers.map(function({name, value: original}){
14+
const value = if_csp(name) ? insert_domain(original) : original
2115
return {name,value}
2216
})
2317
console.timeEnd('codesy map headers');
@@ -27,7 +21,14 @@ const makeCspAppender = function(domain='') {
2721

2822
let codesyAppender = new makeCspAppender()
2923

30-
const setCodesyAppender = function(domain) {
24+
const githubFilter = {
25+
urls: ["https://github.com/*"],
26+
types: ["main_frame"]
27+
};
28+
29+
const headerOptions = ["responseHeaders", "blocking"]
30+
31+
function setCodesyAppender (domain) {
3132
if (chrome.webRequest.onHeadersReceived.hasListener(codesyAppender)) {
3233
chrome.webRequest.onHeadersReceived.removeListener(codesyAppender);
3334
}

0 commit comments

Comments
 (0)