This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
352 lines (298 loc) · 11.4 KB
/
index.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
const paramCase = require('param-case')
, bootswatch = require('bootswatch/api/3.json').themes
, bareReact = require.resolve('generator-bare-react')
, colors = require('chalk')
const { Base } = require('yeoman-generator')
const PERMISSIONS =
[ 'tabs'
, 'bookmarks'
, 'cookies'
, 'history'
, 'management' ]
const DEV_DEPENDENCIES =
{ 'babelify': '~6.4.0'
, 'browserify': null // use latest
, 'envify': null
, 'trash': '~2.0.0' // promisified as of 3.0
, 'gulp-zip': null
, 'run-sequence': null
, 'through2': null
, 'uglifyify': null
, 'watchify': null
, 'concat-stream': null
, 'path-exists': null
, 'gulp-imagemin': null
, 'config-prompt': '~1.0.0'
, 'in-production': null
, 'vinyl-imitate': '~1.0.0'
, 'add-transforms': '~1.0.0' }
const REACT_DEV_DEPENDENCIES =
{ 'babel-plugin-react-transform': '~1.1.1'
, 'livereactload': '~2.1.0'
, 'react-proxy': '~1.1.1'
, 'livereactload-chrome': '~1.0.2' }
const MODULE_FORMATS =
{ commonjs: { name: 'CommonJS'
, snippet: `const assign = require('object-assign')` }
, es6: { name: 'ES6 modules'
, snippet: `import assign from 'object-assign'` }}
function bootswatchNames() {
return bootswatch.map(theme => theme.name)
}
const self = module.exports = class ChromeGenerator extends Base {
// TODO: rename all methods, use priorities
askFor() {
// In case a module was already generated or created
let { name, description } = this.fs.readJSON('package.json', {})
// Use existing manifest values as defaults
let { permissions = [], browser_action, page_action, options_page, omnibox
, content_scripts }
= this.fs.readJSON('app/manifest.json', {})
let questions =
[ { name: 'name'
, message: 'What would you like to call this extension?'
, default: paramCase(name || this.appname || 'my-chrome-extension')
, validate: (v) => paramCase(v).length ? true : 'You have to provide a name'
, filter: paramCase
},{ name: 'description'
, message: 'How would you like to describe this extension?'
, default: description || 'My Chrome Extension'
, validate: (v) => v.trim().length ? true : 'You have to provide a description'
, filter: (s) => s.trim()
},{ type: 'list'
, name: 'action'
, message: 'Would you like to use UI Action?'
, default: browser_action ? 'Browser' : page_action ? 'Page' : 'No'
, choices: [ 'No', 'Browser', 'Page' ]
},{ type: 'checkbox'
, name: 'uifeatures'
, message: 'Would you like more UI Features?'
, choices: [ { value: 'options', name: 'Options Page'
, checked: !!options_page }
, { value: 'contentscript', name: 'Content Scripts'
, checked: !!content_scripts }
, { value: 'omnibox', name: 'Omnibox'
, checked: !!omnibox }]
},{ type: 'confirm'
, name: 'bootstrap'
, store: true
, message: 'Would you like to use Bootstrap?'
, default: false
},{ type: 'list'
, name: 'bootstrapTheme'
, message: 'Which Bootstrap theme would you like?'
, when: (answers) => answers.bootstrap === true
, default: 'none'
, store: true
, choices: [ 'none', 'default' ].concat(bootswatchNames())
},{ type: 'checkbox'
, name: 'permissions'
, message: 'Which permissions do you need?'
, choices: PERMISSIONS.map(name => {
let checked = permissions.indexOf(name) >= 0
return { value: name, name, checked }
})
},{ type: 'confirm'
, name: 'esnext'
, message: 'Do you prefer ES6 over ES5?'
, default: true
, store: true
},{ type: 'list'
, name: 'modules'
, when: (answers) => answers.esnext
, message: 'Which module format do you prefer?'
, default: 'commonjs'
, store: true
, choices: Object.keys(MODULE_FORMATS).map(key => {
let { name, snippet } = MODULE_FORMATS[key]
return { name: `${name} ${colors.gray(snippet)}`, value: key }
})
}
]
let done = this.async()
this.prompt(questions, (answers) => {
let isChecked = (choices, value) => choices.indexOf(value) >= 0
, escape = (s) => s.replace(/\"/g, '\\"')
, name = this.appname = escape(answers.name)
, description = escape(answers.description)
// TODO: set display value for question
if (answers.bootstrapTheme === 'none') answers.bootstrapTheme = null
this.ctx = answers
this.manifest = { name, description, permissions: {} }
this.manifest.action = answers.action === 'No' ? 0 : answers.action === 'Browser' ? 1 : 2
this.manifest.options = isChecked(answers.uifeatures, 'options')
this.manifest.omnibox = isChecked(answers.uifeatures, 'omnibox')
this.manifest.contentscript = isChecked(answers.uifeatures, 'contentscript')
this.manifest.permissions.tabs = isChecked(answers.permissions, 'tabs')
this.manifest.permissions.bookmarks = isChecked(answers.permissions, 'bookmarks')
this.manifest.permissions.cookies = isChecked(answers.permissions, 'cookies')
this.manifest.permissions.history = isChecked(answers.permissions, 'history')
this.manifest.permissions.management = isChecked(answers.permissions, 'management')
let dependencies = {}
, devDependencies = { ...DEV_DEPENDENCIES }
if (this.manifest.action > 0 || this.manifest.options || this.manifest.contentscript) {
// Put additional babelrc in app dir, with livereactload config
this._copy('react.babelrc', 'app/.babelrc')
// Add livereactload dependencies
devDependencies = { ...devDependencies, ...REACT_DEV_DEPENDENCIES }
}
let tasks = answers.esnext
? this.templatePath('tasks/es6')
: this.templatePath('tasks/es5')
this.composeWith('nom'
, { options: { name
, description
, skipInstall: this.options.skipInstall
, skipCache: this.options.skipCache
, esnext: answers.esnext
, modules: answers.modules
, enable: [ 'gulp'
, 'npm' ]
, gulp: { tasks: tasks + '/**/*'
, ctx: this.ctx }
, npm: { dependencies
, main: false
, devDependencies }}}
, { local: require.resolve('generator-nom')
, link: 'strong' })
done()
})
}
_addReact(options) {
let { esnext, modules } = this.ctx
let link = { local: bareReact, link: 'strong' }, style
options =
{ skipInstall: this.options.skipInstall
, skipCache: this.options.skipCache
, esnext
, modules
, ...options }
this.composeWith('bare-react', { options }, link)
}
// TODO: remove legacy code and just stringify manifest as a whole
manifest() {
let manifest = {}
, permissions = []
, items = []
// add browser / page action field
if (this.manifest.action > 0) {
let action = { default_icon: { 19: 'images/icon-19.png'
, 38: 'images/icon-38.png' }
, default_title: this.manifest.name
, default_popup: 'lib/popup/popup.html' }
let type = this.manifest.action === 1 ? 'browser_action' : 'page_action'
manifest[type] = JSON.stringify(action, null, 2).replace(/\n/g, '\n ')
this._addReact
({ type: 'app'
, dest: 'app/lib/popup'
, name: 'Popup'
, router: false
, bootstrap: this.ctx.bootstrap
, children: [ 'PopupNinja' ] })
}
// add options page field.
if (this.manifest.options) {
let options_ui = { page: 'lib/options/options.html', chrome_style: true }
manifest.options_page = '"lib/options/options.html"'
manifest.options_ui = JSON.stringify(options_ui, null, 2).replace(/\n/g, '\n ')
this._addReact
({ type: 'app'
, bootstrap: this.ctx.bootstrap
, dest: 'app/lib/options'
, name: 'Options' })
}
// add omnibox keyword field.
if (this.manifest.omnibox) {
let omnibox = { keyword: this.manifest.name }
manifest.omnibox = JSON.stringify(omnibox, null, 2).replace(/\n/g, '\n ')
}
// add contentscript field.
if (this.manifest.contentscript) {
let contentscript = { matches: ['http://*/*', 'https://*/*']
, js: ['lib/content-script/index.js']
, run_at: 'document_end'
, all_frames: false }
manifest.content_scripts =
JSON.stringify([contentscript], null, 2).replace(/\n/g, '\n ')
this._addReact
({ type: 'app'
, dest: 'app/lib/content-script'
, name: 'ContentScript'
, append: true
, router: false
, bootstrap: false })
}
// add generate permission field.
for (let p in this.manifest.permissions) {
if (this.manifest.permissions[p]) {
permissions.push(p)
}
}
// add generic match pattern field.
if (this.manifest.permissions.tabs) {
permissions.push('http://*/*')
permissions.push('https://*/*')
}
if (permissions.length > 0) {
manifest.permissions =
JSON.stringify(permissions, null, 2).replace(/\n/g, '\n ')
}
for (let k in manifest) {
items.push([' "', k, '": ', manifest[k]].join(''))
}
this.manifest.items = items.length > 0 ? ',\n' + items.join(',\n') : ''
this._copyTpl('manifest.json', 'app/manifest.json', this.manifest)
}
actions() {
if (this.manifest.action !== 0) {
this._copy('popup.html', 'app/lib/popup/popup.html')
this._copy('images/icon-19.png', 'app/images/icon-19.png')
this._copy('images/icon-38.png', 'app/images/icon-38.png')
this._copyCSS('app/lib/popup')
}
}
eventpage() {
let pageName = 'background page'
, withAction = false
, action = this.manifest.action
if (action === 2) {
pageName = 'event page for Page Action'
withAction = true
} else if (action === 1) {
pageName = 'event page for Browser Action'
withAction = true
}
let ctx = { pageName, withAction }
let dir = this.ctx.esnext ? 'es6' : 'es5'
this._copyJS(`${dir}/background.js`, 'app/lib/background/index.js', ctx)
}
optionsHtml() {
if (this.manifest.options) {
this._copy('options.html', 'app/lib/options/options.html')
this._copyCSS('app/lib/options')
}
}
contentscript() {
if (this.manifest.contentscript) {
this._copyCSS('app/lib/content-script')
}
}
assets() {
let messages = '_locales/en/messages.json'
this._copyTpl(messages, `app/${messages}`, this.manifest)
this._copy('images/icon-16.png', 'app/images/icon-16.png')
this._copy('images/icon-128.png', 'app/images/icon-128.png')
}
_copy(src, dest = src) {
this.fs.copy(this.templatePath(src), this.destinationPath(dest))
}
_copyTpl(src, dest = src, ctx = {}) {
this.fs.copyTpl(this.templatePath(src), this.destinationPath(dest), ctx)
}
_copyJS(src, dest = src, ctx = {}) {
this._copyTpl('scripts/' + src, dest, ctx)
}
_copyCSS(dest) {
this._copyTpl('styles/index.css', dest + '/index.css', this.ctx)
}
}