-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
85 lines (69 loc) · 2.29 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
#!/usr/bin/env node
const path = require('path');
const fse = require('fs-extra');
const chalk = require('chalk');
const commander = require('commander');
const Confirm = require('prompt-confirm');
const packageJson = require('./package.json');
const program = new commander.Command(packageJson.name)
.version(packageJson.version)
.usage('[options]')
.option('-p, --public [value]', 'path to public folder with the manifest file')
.parse(process.argv);
const publicFolder = path.resolve(program.public || 'public');
const publicFiles = fse.readdirSync(publicFolder);
if(publicFiles.indexOf('manifest.json') === -1) {
console.log(chalk.red('There must be a manifest.json file in the public folder'));
process.exit(1);
}
Promise.resolve()
.then(checkOverwriteFile.bind(null, publicFiles, 'background.js'))
.then(checkOverwriteFile.bind(null, publicFiles, 'entry.js'))
.then(() => {
copyFile(publicFolder, 'background.js');
copyFile(publicFolder, 'entry.js');
const manifestJson = fse.readFileSync(path.join(publicFolder, 'manifest.json'));
const manifest = JSON.parse(manifestJson);
const newManifest = {
manifest_version: 2,
version: '1.0.0',
short_name: manifest.short_name,
name: manifest.name,
background: {
scripts: ['background.js'],
persistent: true,
},
web_accessible_resources: ['index.html'],
browser_action: {
default_icon: 'favicon.ico',
},
permissions: [
'*://*/*',
],
};
fse.writeFileSync(
path.join(publicFolder, 'manifest.json'),
JSON.stringify(newManifest, null, 2)
);
});
function checkOverwriteFile(publicFiles, filename) {
if(publicFiles.indexOf(filename) !== -1) {
const prompt = new Confirm({ name: filename, message: `Overwrite ${filename} file?`, default: false });
return prompt.run()
.then((answer) => {
if(!answer) process.exit(0);
});
}
return Promise.resolve();
}
function copyFile(publicFolder, filename) {
try {
const copyFilepath = path.resolve(__dirname, 'templates', filename);
const outputFilepath = path.join(publicFolder, filename);
fse.copySync(copyFilepath, outputFilepath);
} catch(err) {
console.log(chalk.red(`There was an error copying ${filename}`));
console.log(err);
process.exit(1);
}
}