-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
release.js
executable file
·201 lines (171 loc) · 5.53 KB
/
release.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
#!/usr/bin/env node
import { exec } from 'child_process';
import fs from 'fs';
import path from 'path';
import semver from 'semver';
import ora from 'ora';
import chalk from 'chalk';
import indent from 'detect-indent';
import inquirer from 'inquirer';
import open from 'open';
import Git from 'simple-git';
const git = Git();
const cwd = process.cwd();
const manifests = ['package.json', 'src/manifest.json'];
const addonUrl = 'https://addons.mozilla.org/en-US/developers/addon/perfect-home/versions';
const chromeStoreDash = 'https://chrome.google.com/webstore/devconsole';
const dryrun = false;
const faker = () => new Promise(resolve => setTimeout(resolve, 200));
function run (cmd) {
if (dryrun) return faker();
return new Promise((resolve, reject) => {
exec(cmd, (err, out) => (err ? reject(err) : resolve(out)));
});
}
function getJson (_path) {
try {
const file = fs.readFileSync(_path, 'utf8');
const json = JSON.parse(file);
return json || {};
}
catch {
return {};
}
}
function getVersion (manifest) {
const pkgPath = path.join(cwd, manifest || manifests[0]);
const pkg = getJson(pkgPath);
const current = pkg.version || '0.0.0';
return {
name: pkg.name,
current: current,
nextMajor: semver.inc(current, 'major'),
nextMinor: semver.inc(current, 'minor'),
nextPatch: semver.inc(current, 'patch')
};
}
function bump (manifest, newVersion) {
const pkgPath = path.join(cwd, manifest);
const pkg = getJson(pkgPath);
const usedIndent = indent(fs.readFileSync(pkgPath, 'utf8')).indent || ' ';
pkg.version = newVersion;
if (!dryrun) fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, usedIndent) + '\n');
}
function commit (version) {
if (dryrun) return faker();
return new Promise((resolve, reject) => {
git
.add('./*')
.commit('Release v' + version)
.push(['origin', 'master'], err => {
if (err) reject(err);
else resolve({ version });
});
});
}
function release () {
const app = getVersion();
let spinner;
console.log('\n**************************************');
console.log('* *');
console.log(`* Releasing ${chalk.cyan(app.name)} *`);
console.log('* *');
console.log('**************************************\n');
inquirer
.prompt([
{
type: 'list',
name: 'version',
message: 'Bump version to:',
default: 1,
choices: [
{ value: app.current, name: 'current (' + app.current + ')' },
{ value: app.nextPatch, name: 'patch (' + app.nextPatch + ')' },
{ value: app.nextMinor, name: 'minor (' + app.nextMinor + ')' },
{ value: app.nextMajor, name: 'major (' + app.nextMajor + ')' },
new inquirer.Separator(),
{ value: 'custom', name: 'custom...' },
]
},
{
type: 'input',
name: 'version',
message: 'Enter the new version number:',
default: app.current,
when: answers => answers.version === 'custom',
filter: semver.clean,
validate: answer => semver.valid(answer) ? true : 'That\'s not a valid version number',
}
])
.then(({ version }) => {
app.version = version;
spinner = ora('').start();
// update package & manifest
manifests.forEach(m => {
spinner.text = `Updating ${m}...`;
bump(m, version);
spinner.text = `Updated ${chalk.cyan(m)} to ${chalk.cyan(version)}`;
spinner.succeed();
});
spinner.text = 'Committing to GitHub...';
spinner.start();
return commit(version); // commit code changes to github
})
.then(() => {
spinner.text = `Update ${chalk.cyan('pushed')} to Github.`;
spinner.succeed();
spinner.text = 'Building a ' + chalk.cyan('production') + ' version.';
spinner.start();
return run('gulp build --prod');
})
.then(() => {
spinner.text = 'Built a ' + chalk.cyan('production') + ' version.';
spinner.succeed();
spinner.text = 'Packing extensions...';
spinner.start();
const cmd = `rm -rf ~/Desktop/${app.name} && ` +
`mkdir ~/Desktop/${app.name} && ` +
`cp -R dist/ ~/Desktop/${app.name} && ` +
`cp LICENSE ~/Desktop/${app.name} && ` +
`rm -f ~/Desktop/${app.name}/manifest-chrome.json && ` +
// zip for firefox
`7z a ~/Desktop/${app.name}-firefox.zip ~/Desktop/${app.name}/* && ` +
// zip for chrome
`rm -f ~/Desktop/${app.name}/manifest.json && ` +
`cp dist/manifest-chrome.json ~/Desktop/${app.name}/manifest.json && ` +
`7z a ~/Desktop/${app.name}-chrome.zip ~/Desktop/${app.name}/ && ` +
`rm -rf ~/Desktop/${app.name}`;
return run(cmd).catch(() => {});
})
.then(() => {
spinner.text = 'Extensions packed.';
spinner.succeed();
spinner.text = 'Zipping source for Firefox submission.';
spinner.start();
const cmd = `rm -rf ~/Desktop/${app.name} && ` +
`mkdir ~/Desktop/${app.name} && ` +
`cp -R src/ ~/Desktop/${app.name}/src/ && ` +
`cp LICENSE ~/Desktop/${app.name} && ` +
`cp package.json ~/Desktop/${app.name} && ` +
`cp gulpfile.js ~/Desktop/${app.name} && ` +
`cp .editorconfig ~/Desktop/${app.name} && ` +
`cp .eslintrc ~/Desktop/${app.name} && ` +
`cp README.md ~/Desktop/${app.name} && ` +
`7z a ~/Desktop/${app.name}-source.zip ~/Desktop/${app.name}/ && ` +
`rm -rf ~/Desktop/${app.name}`;
return run(cmd).catch(() => {});
})
.then(() => {
spinner.text = 'Source zipped to ' + chalk.cyan('Desktop') + '!';
spinner.succeed();
console.log(chalk.cyan('All done!'));
if (!dryrun) open(addonUrl);
if (!dryrun) open(chromeStoreDash);
process.exit(0);
})
.catch(e => {
spinner.text = '' + e;
spinner.fail();
});
}
release();