-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.js
executable file
·161 lines (138 loc) · 4.59 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
#!/usr/bin/env node
import { exec } from 'child_process';
import fs from 'fs-extra';
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 Git from 'simple-git';
const git = Git();
const cwd = process.cwd();
const manifests = ['package.json', 'manifest.json', 'manifest-chrome.json'];
const dryrun = false;
const faker = () => new Promise(resolve => setTimeout(resolve, 2000));
function run (cmd) {
if (dryrun) return faker();
return new Promise((resolve, reject) => {
exec(cmd, (err, out) => (err ? reject(err) : resolve(out)));
});
}
function getVersion (manifest) {
const pkgPath = path.join(cwd, manifest || manifests[0]);
const pkg = fs.readJsonSync(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 = fs.readJSONSync(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 }) => {
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 = 'Zipping source...';
spinner.start();
const cmd = `rm -rf ~/Desktop/${app.name} && ` +
`mkdir ~/Desktop/${app.name} && ` +
`cp -R assets ~/Desktop/${app.name} && ` +
`cp *.* ~/Desktop/${app.name} && ` +
`cp LICENSE ~/Desktop/${app.name} && ` +
`rm -f ~/Desktop/${app.name}/assets/icon.sketch && ` +
`rm -f ~/Desktop/${app.name}/assets/screen.png && ` +
`rm -f ~/Desktop/${app.name}/assets/.DS_Store && ` +
`rm -f ~/Desktop/${app.name}/package.json && ` +
`rm -f ~/Desktop/${app.name}/package-lock.json && ` +
`rm -f ~/Desktop/${app.name}/release.js && ` +
`rm -f ~/Desktop/${app.name}/jsconfig.json && ` +
`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 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(e => console.error(e));
})
.then(() => {
spinner.text = 'Source zipped to ' + chalk.cyan('Desktop') + '.';
spinner.succeed();
console.log(chalk.cyan('All done!'));
process.exit(0);
})
.catch(e => {
spinner.text = '' + e;
spinner.fail();
});
}
release();