-
Notifications
You must be signed in to change notification settings - Fork 97
/
index.js
executable file
Β·416 lines (371 loc) Β· 10.5 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/env node
'use strict';
const path = require('path');
const fs = require('fs-extra');
const chalk = require('chalk');
const spawn = require('cross-spawn');
const commander = require('commander');
const packageFile = require('./package.json');
const { checkAppName, prettifyAppName } = require('./utils/name');
const generateReadme = require('./scripts/readme');
const tryGitInit = require('./scripts/git-init');
let projectName;
const LANGUAGES = ['javascript', 'typescript'];
const OVERRIDE_PAGES = ['newtab', 'bookmarks', 'history'];
const TS_CONFIG = {
compilerOptions: {
rootDir: 'src',
target: 'es6',
module: 'commonjs',
esModuleInterop: true,
strict: true,
},
};
const program = new commander.Command(packageFile.name)
.version(packageFile.version)
.arguments('<project-directory>')
.usage(`${chalk.green('<project-directory>')} [options]`)
.action((name) => {
projectName = name;
})
.option(
'--override-page [page-name]',
'override default page like New Tab, Bookmarks, or History page'
)
.option('--devtools', 'add features to Chrome Developer Tools')
.option('--side-panel', 'add features to Chrome Side Panel')
.option(
'--language [language-name]',
'language like JavaScript and TypeScript'
)
.on('--help', () => {
console.log(` Only ${chalk.green('<project-directory>')} is required.`);
})
.parse(process.argv);
// Exit from the process if no project name is provided
if (typeof projectName === 'undefined') {
console.error('Please specify the project directory:');
console.log(
` ${chalk.cyan(program.name())} ${chalk.green('<project-directory>')}`
);
console.log();
console.log('For example:');
console.log(` ${chalk.cyan(program.name())} ${chalk.green('my-extension')}`);
console.log();
console.log(
`Run ${chalk.cyan(`${program.name()} --help`)} to see all options.`
);
process.exit(1);
}
function isOverridePageNameValid(name) {
if (name === true || OVERRIDE_PAGES.includes(name)) {
return true;
}
return false;
}
function isLanguageNameValid(name) {
if (name === true || LANGUAGES.includes(name)) {
return true;
}
return false;
}
function logOverridePageError() {
console.error(
`${chalk.red('Invalid page name passed to option:')} ${chalk.cyan(
'--override-page'
)}`
);
console.log();
console.log(
`You can pass page name as ${chalk.cyan('newtab')}, ${chalk.cyan(
'bookmarks'
)} or ${chalk.cyan('history')}.`
);
console.log();
console.log('For example:');
console.log(
` ${chalk.cyan(program.name())} ${chalk.green(
'my-extension'
)} ${chalk.cyan('--override-page')} ${chalk.green('newtab')}`
);
process.exit(1);
}
function logLanguageError() {
console.error(
`${chalk.red('Invalid language name passed to option:')} ${chalk.cyan(
'--language'
)}`
);
console.log();
console.log(
`You can pass language name as ${chalk.cyan('javascript')} or ${chalk.cyan(
'typescript'
)}.`
);
console.log();
console.log('For example:');
console.log(
` ${chalk.cyan(program.name())} ${chalk.green(
'my-extension'
)} ${chalk.cyan('--language')} ${chalk.green('typescript')}`
);
process.exit(1);
}
function logOptionsConflictError() {
console.error(
`${chalk.red(
'You have passed both "--override-page" and "--devtools" options'
)}`
);
console.log(` ${chalk.cyan('Only pass one of the option')}`);
console.log('');
process.exit(1);
}
function createExtension(
name,
{ overridePage, devtools, sidePanel, language }
) {
const root = path.resolve(name);
let overridePageName;
let languageName = 'javascript';
if (overridePage) {
if (isOverridePageNameValid(overridePage)) {
overridePageName = overridePage === true ? 'newtab' : overridePage;
if (devtools) {
logOptionsConflictError();
}
} else {
logOverridePageError();
}
}
if (language) {
if (isLanguageNameValid(language)) {
languageName = language === true ? 'javascript' : language;
} else {
logLanguageError();
}
}
checkAppName(name);
fs.ensureDirSync(name);
console.log(`Creating a new Chrome extension in ${chalk.green(root)}`);
console.log();
const appDetails = {
version: '0.1.0',
description: 'My Chrome Extension',
};
// Setup the package file
let appPackage = Object.assign(
{},
{
name: name,
},
appDetails,
{
private: true,
}
);
appPackage.scripts = {
watch:
'webpack --mode=development --watch --config config/webpack.config.js',
build: 'webpack --mode=production --config config/webpack.config.js',
pack: 'node pack.js',
repack: 'npm run build && npm run pack',
format:
'prettier --write --ignore-unknown "{config,public,src}/**/*.{html,css,js,ts,json}"',
};
// Create package file in project directory
fs.writeFileSync(
path.join(root, 'package.json'),
JSON.stringify(appPackage, null, 2)
);
// Create tsconfig file in project directory
if (languageName === 'typescript') {
fs.writeFileSync(
path.join(root, 'tsconfig.json'),
JSON.stringify(TS_CONFIG, null, 2)
);
}
let command = 'npm';
let args = ['install', '--save-dev'];
// Add devDependencies
args.push(
'webpack@^5.72.0',
'webpack-cli@^4.9.2',
'webpack-merge@^5.8.0',
'copy-webpack-plugin@^10.2.4',
'mini-css-extract-plugin@^2.6.0',
'css-loader@^6.7.1',
'file-loader@^6.2.0',
'prettier@^2.6.2',
'adm-zip@^0.5.10'
);
if (languageName === 'typescript') {
}
console.log('Installing packages. This might take a couple of minutes.');
console.log(
`Installing ${chalk.cyan('webpack')}, ${chalk.cyan(
'webpack-cli'
)} and few more...`
);
console.log();
// Install package dependencies
const proc = spawn.sync(command, args, { cwd: root, stdio: 'inherit' });
if (proc.status !== 0) {
console.error(`\`${command} ${args.join(' ')}\` failed`);
return;
}
// Copy template files to project directory
let templateName;
if (overridePageName) {
templateName = 'override-page';
} else if (devtools) {
templateName = 'devtools';
} else if (sidePanel) {
templateName = 'side-panel';
} else {
templateName = 'popup';
}
fs.copySync(
path.resolve(__dirname, 'templates', languageName, templateName),
root
);
// Copy common files between languages
fs.copySync(
path.resolve(__dirname, 'templates', 'shared', templateName),
root
);
fs.copySync(
path.resolve(__dirname, 'config', languageName),
path.join(root, 'config')
);
// Copy common webpack configuration file
fs.copySync(
path.resolve(__dirname, 'config', 'shared'),
path.join(root, 'config')
);
// Rename gitignore after the fact to prevent npm from renaming it to .npmignore
// See: https://github.com/npm/npm/issues/1862
// Source: https://github.com/facebook/create-react-app/blob/47e9e2c7a07bfe60b52011cf71de5ca33bdeb6e3/packages/react-scripts/scripts/init.js#L138
// Also followed same convention for other dotfiles.
const dotFilesSource = path.resolve(
__dirname,
'templates',
'shared',
'dotfiles'
);
fs.readdirSync(dotFilesSource).map(function (fileName) {
return fs.copyFileSync(
path.join(dotFilesSource, fileName),
path.join(root, '.' + fileName)
);
});
// Copy script to automatically generate zip file
fs.copyFileSync(
path.resolve(__dirname, 'templates', 'shared', 'pack.js'),
path.join(root, 'pack.js')
);
// Setup the manifest file
const manifestDetails = Object.assign(
{},
{
name: prettifyAppName(name),
},
appDetails
);
let appManifest = Object.assign(
{},
{
manifest_version: 3,
},
manifestDetails,
{
icons: {
16: 'icons/icon_16.png',
32: 'icons/icon_32.png',
48: 'icons/icon_48.png',
128: 'icons/icon_128.png',
},
background: {
service_worker: 'background.js',
},
}
);
if (overridePageName) {
appManifest = Object.assign({}, appManifest, {
chrome_url_overrides: {
[overridePageName]: 'index.html',
},
});
} else if (devtools) {
appManifest = Object.assign({}, appManifest, {
devtools_page: 'devtools.html',
});
} else if (sidePanel) {
appManifest = Object.assign({}, appManifest, {
side_panel: {
default_path: 'sidepanel.html',
},
permissions: ['sidePanel', 'tabs'],
});
} else {
appManifest = Object.assign({}, appManifest, {
action: {
default_title: manifestDetails.name,
default_popup: 'popup.html',
},
permissions: ['storage'],
content_scripts: [
{
matches: ['<all_urls>'],
run_at: 'document_idle',
js: ['contentScript.js'],
},
],
});
}
// Create manifest file in project directory
fs.writeFileSync(
path.join(root, 'public', 'manifest.json'),
JSON.stringify(appManifest, null, 2)
);
// Generate a README file
if (generateReadme(manifestDetails, root)) {
console.log('Generated a README file.');
console.log();
}
// Initialize a git repository
if (tryGitInit(root, name)) {
console.log('Initialized a git repository.');
console.log();
}
console.log(`Success! Created ${name} at ${root}`);
console.log('Inside that directory, you can run below commands:');
console.log();
console.log(chalk.cyan(` ${command} run watch`));
console.log(' Listens for files changes and rebuilds automatically.');
console.log();
console.log(chalk.cyan(` ${command} run build`));
console.log(' Bundles the app into static files for Chrome store.');
console.log();
console.log(chalk.cyan(` ${command} run format`));
console.log(' Formats all the files.');
console.log();
console.log('We suggest that you begin by typing:');
console.log();
console.log(` 1. ${chalk.cyan('cd')} ${name}`);
console.log(` 2. Run ${chalk.cyan(`${command} run watch`)}`);
console.log(` 3. Open ${chalk.cyan('chrome://extensions')}`);
console.log(` 4. Check the ${chalk.cyan('Developer mode')} checkbox`);
console.log(
` 5. Click on the ${chalk.cyan('Load unpacked extension')} button`
);
console.log(` 6. Select the folder ${chalk.cyan(name + '/build')}`);
console.log();
}
createExtension(projectName, {
overridePage: program.overridePage,
devtools: program.devtools,
sidePanel: program.sidePanel,
language: program.language,
});