-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·111 lines (99 loc) · 3.04 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
#!/usr/bin/env node
/* eslint-disable no-unused-vars */
const prompts = require('prompts')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const fs = require('fs-extra')
const path = require('path')
const sortPackageJson = require('sort-package-json')
const kebabCase = require('lodash.kebabcase')
const sanitize = require('sanitize-filename')
const repoUrls = {
js: 'https://github.com/extend-chrome/js-react-boilerplate.git',
ts: 'https://github.com/extend-chrome/ts-react-boilerplate.git',
}
const invalidName = `
A Chrome extension name cannot have more than 45 characters.
See: https://developer.chrome.com/docs/extensions/mv2/manifest/name/
`.trim()
const invalidVersion = `
A valid version has one to four dot-separated integers.
Examples: "1", "1.0", "2.0.3", "3.4.7.219"
See: https://developer.chrome.com/docs/extensions/mv2/manifest/version/
`.trim()
const invalidDescription = `
The description must be a no more than 132 characters.
See: https://developer.chrome.com/docs/extensions/mv2/manifest/description/
`.trim()
prompts([
{
type: 'text',
name: 'name',
message: 'Chrome extension package name:',
validate: (n) => n.length < 32 || invalidName,
},
{
type: 'text',
name: 'version',
message: 'First version number:',
initial: '1.0.0',
validate: (v) => !/[^.\d]/.test(v) || invalidVersion,
},
{
type: 'text',
name: 'author',
message: 'Author name:',
},
{
type: 'text',
name: 'description',
message: 'Description:',
validate: (d) => d.length <= 132 || invalidDescription,
},
{
type: 'select',
name: 'lang',
message: 'Which do you want to use?',
choices: [
{ title: 'JavaScript', value: 'js' },
{ title: 'TypeScript', value: 'ts' },
],
initial: 0,
},
])
.then(async ({ lang, author, description, name, version }) => {
const packageName = kebabCase(sanitize(name, { replacement: '-' }))
console.log(`Creating a Chrome extension in ./${packageName}`)
await exec(`git clone ${repoUrls[lang]} ${packageName}`)
const packageDir = path.join(process.cwd(), packageName)
const packageJsonPath = path.join(packageDir, 'package.json')
const { bugs, homepage, keywords, license, repository, ...packageJson } =
await fs.readJSON(packageJsonPath)
await fs.writeJSON(
packageJsonPath,
sortPackageJson({
...packageJson,
author,
description,
name: packageName,
version,
}),
{ spaces: 2 },
)
const manifestJsonPath = path.join(packageDir, 'src', 'manifest.json')
const manifestJson = await fs.readJSON(manifestJsonPath)
await fs.writeJSON(
manifestJsonPath,
{ ...manifestJson, name },
{ spaces: 2 },
)
const gitFolderPath = path.join(packageDir, '.git')
await fs.remove(gitFolderPath)
console.log(
'Success: Now just `npm install` using your favorite package manager and create your Chrome extension!',
)
})
.catch((error) => {
console.error(error)
process.exit(1)
})