forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.js
More file actions
169 lines (144 loc) · 4.1 KB
/
common.js
File metadata and controls
169 lines (144 loc) · 4.1 KB
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
const fs = require('fs-extra');
const path = require('path');
const execa = require('execa');
const Listr = require('listr');
const semver = require('semver');
const chalk = require('chalk');
const rootDir = path.join(__dirname, '../');
const packages = [
'core',
'angular'
];
function readPkg(project) {
const packageJsonPath = packagePath(project);
return JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
}
function writePkg(project, pkg) {
const packageJsonPath = packagePath(project);
const text = JSON.stringify(pkg, null, 2);
return fs.writeFileSync(packageJsonPath, text);
}
function packagePath(project) {
return path.join(rootDir, project, 'package.json');
}
function projectPath(project) {
return path.join(rootDir, project);
}
function checkGit(tasks) {
tasks.push(
{
title: 'Check current branch',
task: () => execa.stdout('git', ['symbolic-ref', '--short', 'HEAD']).then(branch => {
if (branch !== 'master') {
throw new Error(`Not on "master" branch`);
}
})
},
{
title: 'Check local working tree',
task: () => execa.stdout('git', ['status', '--porcelain']).then(status => {
if (status !== '') {
throw new Error(`Unclean working tree. Commit or stash changes first.`);
}
})
},
{
title: 'Check remote history',
task: () => execa.stdout('git', ['rev-list', '--count', '--left-only', '@{u}...HEAD']).then(result => {
if (result !== '0') {
throw new Error(`Remote history differs. Please pull changes.`);
}
})
}
);
}
const isValidVersion = input => Boolean(semver.valid(input));
function preparePackage(tasks, package, version) {
const projectRoot = projectPath(package);
const pkg = readPkg(package);
const projectTasks = [];
if (version) {
projectTasks.push({
title: `${pkg.name}: validate new version`,
task: () => {
if (!isVersionGreater(pkg.version, version)) {
throw new Error(`New version \`${version}\` should be higher than current version \`${pkg.version}\``);
}
}
});
}
projectTasks.push({
title: `${pkg.name}: install npm dependencies`,
task: async () => {
await fs.remove(path.join(projectRoot, 'node_modules'))
await execa('npm', ['i'], { cwd: projectRoot });
}
});
if (package !== 'core') {
projectTasks.push({
title: `${pkg.name}: npm link @ionic/core`,
task: () => execa('npm', ['link', '@ionic/core'], { cwd: projectRoot })
});
if (version) {
projectTasks.push({
title: `${pkg.name}: update ionic/core dep to ${version}`,
task: () => {
updateDependency(pkg, "@ionic/core", version);
writePkg(package, pkg);
}
});
}
}
if (version) {
projectTasks.push({
title: `${pkg.name}: lint`,
task: () => execa('npm', ['run', 'lint'], { cwd: projectRoot })
});
}
projectTasks.push({
title: `${pkg.name}: build`,
task: () => execa('npm', ['run', 'build'], { cwd: projectRoot })
});
if (version) {
projectTasks.push({
title: `${pkg.name}: test`,
task: () => execa('npm', ['test'], { cwd: projectRoot })
});
}
if (package === 'core') {
projectTasks.push({
title: `${pkg.name}: npm link`,
task: () => execa('npm', ['link'], { cwd: projectRoot })
});
}
// Add project tasks
tasks.push({
title: `Prepare ${chalk.bold(pkg.name)}`,
task: () => new Listr(projectTasks)
});
}
function updateDependency(pkg, dependency, version) {
if (pkg.dependencies && pkg.dependencies[dependency]) {
pkg.dependencies[dependency] = version;
}
if (pkg.devDependencies && pkg.devDependencies[dependency]) {
pkg.devDependencies[dependency] = version;
}
}
function isVersionGreater(oldVersion, newVersion) {
if (!isValidVersion(newVersion)) {
throw new Error('Version should be a valid semver version.');
}
return semver.gt(newVersion, oldVersion);
}
module.exports = {
isValidVersion,
isVersionGreater,
readPkg,
writePkg,
rootDir,
projectPath,
checkGit,
packages,
preparePackage
};