forked from dutiyesh/chrome-extension-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
Β·287 lines (252 loc) Β· 7.47 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
#!/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 OVERRIDE_PAGES = ['newtab', 'bookmarks', 'history'];
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')
.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 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 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 }) {
const root = path.resolve(name);
let overridePageName;
if (overridePage) {
if (isOverridePageNameValid(overridePage)) {
overridePageName = overridePage === true ? 'newtab' : overridePage;
if (devtools) {
logOptionsConflictError();
}
} else {
logOverridePageError();
}
}
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 = {
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',
};
// Create package file in project directory
fs.writeFileSync(
path.join(root, 'package.json'),
JSON.stringify(appPackage, null, 2)
);
let command = 'npm';
let args = ['install', '--save-dev'];
// Add devDependencies
args.push(
'webpack',
'webpack-cli',
'webpack-merge',
'copy-webpack-plugin',
'size-plugin',
'mini-css-extract-plugin',
'css-loader',
'file-loader'
);
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 {
templateName = 'popup';
}
fs.copySync(path.resolve(__dirname, 'templates', templateName), root);
// Copy common webpack configuration file
fs.copySync(path.resolve(__dirname, 'config'), 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
fs.moveSync(
path.join(root, 'gitignore'),
path.join(root, '.gitignore'),
[]
);
// Setup the manifest file
const manifestDetails = {
name: prettifyAppName(name),
...appDetails,
};
let appManifest = {
manifest_version: 2,
...manifestDetails,
icons: {
16: 'icons/icon_16.png',
32: 'icons/icon_32.png',
48: 'icons/icon_48.png',
128: 'icons/icon_128.png',
},
background: {
scripts: ['background.js'],
persistent: false,
},
};
if (overridePageName) {
appManifest = {
...appManifest,
chrome_url_overrides: {
[overridePageName]: 'index.html',
},
};
} else if (devtools) {
appManifest = {
...appManifest,
devtools_page: 'devtools.html',
};
} else {
appManifest = {
...appManifest,
browser_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('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,
});