-
Notifications
You must be signed in to change notification settings - Fork 32
/
gulpfile.js
167 lines (135 loc) · 4.78 KB
/
gulpfile.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
// npm run release -- --addition,subtraction
const { series } = require('gulp');
const fs = require('fs');
const { join } = require('path');
const { exec } = require('child_process');
const { RunCommand } = require('@lerna/run');
const { PublishCommand } = require('@lerna/publish');
const git = require('simple-git/promise')(__dirname);
const sync = require('./scripts/preversion/sync.js');
const stableBranch = 'master';
const releaseBranch = 'release';
const ignored = ['.git', '.log', 'node_modules', '.lock', 'packages', '.md', 'package-lock'];
const packagesDirectory = join(__dirname, '/packages/');
let packagesDirNamesToRelease = [];
let packagesNamesToRelease = [];
const readDirPromise = (directory) => {
return new Promise((resolve, reject) => {
fs.readdir(directory, (err, files) => {
if (err) {
return reject(err);
}
resolve(files);
});
});
};
const getPackageName = (dirName) => {
return new Promise((resolve, reject) => {
fs.readFile(join(packagesDirectory, dirName, 'package.json'), 'UTF-8', (err, data) => {
if (err) {
return reject(err);
}
const pkgJson = JSON.parse(data);
resolve(pkgJson.name);
});
});
};
const validateReleasePackages = async () => {
packagesDirNamesToRelease = process.argv[3].replace('--', '').split(',');
const areNamesValidType = packagesDirNamesToRelease.every((name) => (typeof name === 'string') && name.length > 0);
if (!areNamesValidType) {
throw new Error(`Invalid packages names: ${JSON.stringify(packagesDirNamesToRelease)}`);
}
const areNamesExisting = packagesDirNamesToRelease.every((pkgName) => fs.existsSync(join(packagesDirectory, pkgName)));
if (!areNamesExisting) {
throw new Error(`Non-existent packages names: ${JSON.stringify(packagesDirNamesToRelease)}`);
}
packagesNamesToRelease = await Promise.all(packagesDirNamesToRelease.map((dirName) => getPackageName(dirName)));
console.log('All packages are validated and existing');
};
const checkout = async (branchName) => {
await git.checkout(branchName);
};
const checkoutRelease = async () => {
await checkout(releaseBranch);
};
const checkoutMaster = async () => {
await checkout(stableBranch);
};
const syncAllContentsExceptPackages = async () => {
const rootContents = await readDirPromise(__dirname);
const itemToSync = rootContents.filter((element) => !ignored.some((igElement) => element.includes(igElement)));
itemToSync.push('.gitignore');
for (const item of itemToSync) {
console.log(`checking out ${item}`);
await git.raw(['checkout', stableBranch, item]);
}
};
const syncPackagesToRelease = async () => {
for (const packageName of packagesDirNamesToRelease) {
console.log(`checking out ${packageName}`);
await git.raw(['checkout', stableBranch, `packages/${packageName}`]);
}
};
const yarnInstall = () => {
return new Promise((resolve, reject) => {
const child = exec('yarn', (err, stdout) => {
console.log(stdout);
});
child.on('error', reject);
child.on('exit', resolve);
});
};
const executeConditionalScript = async (script) => {
const scope = packagesNamesToRelease.length === 1 ?
packagesNamesToRelease[0] :
`{${packagesNamesToRelease.join(',')}}`;
const command = new RunCommand({ script, scope });
await command.runner;
};
const conditionalLernaTest = async () => {
await executeConditionalScript('test');
};
const conditionalLernaBuild = async () => {
await executeConditionalScript('build');
};
const versionSync = async () => {
await sync(git, false);
};
const addCommit = async (message) => {
await git.add('.');
await git.commit(message, undefined, { '--no-verify': null });
};
const commitPreReleaseSync = async () => {
await addCommit('Pre-release sync and build');
};
const commitIsolatedPackages = async () => {
await addCommit('Release package/s isolated');
};
const publish = async () => {
const command = new PublishCommand({});
await command.runner;
};
const stableSyncPush = async () => {
for (const packageName of packagesDirNamesToRelease) {
console.log(`checking out ${packageName}`);
await git.raw(['checkout', releaseBranch, `packages/${packageName}`]);
}
await addCommit('[post-release] Release and sync completed');
await git.push();
};
exports.release = series(
validateReleasePackages,
checkoutRelease,
syncAllContentsExceptPackages,
syncPackagesToRelease,
yarnInstall,
conditionalLernaTest,
conditionalLernaBuild,
commitPreReleaseSync,
versionSync,
commitIsolatedPackages,
publish,
checkoutMaster,
stableSyncPush
);