forked from adobe/spectrum-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
193 lines (152 loc) · 6.23 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
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
const gulp = require('gulp');
const builder = require('./tools/bundle-builder');
const test = require('./tools/test-builder');
const site = require('./site/gulpfile.js');
const subrunner = require('./tools/bundle-builder/subrunner');
const through = require('through2');
Object.assign(exports, builder);
Object.assign(exports, test);
Object.assign(exports, site);
const path = require('path');
const fsp = require('fs').promises;
const semver = require('semver');
async function readPackage(component) {
try {
return JSON.parse(await fsp.readFile(path.join(component, 'package.json')));
}
catch(err) {
console.trace();
throw new Error(`Error while parsing JSON: for ${component}: ${err}`);
}
}
async function writePackage(component, package) {
return await fsp.writeFile(path.join(component, 'package.json'), JSON.stringify(package, null, 2));
}
async function checkPeerDependencies() {
let packagesDir = './components';
let components = (await fsp.readdir(packagesDir, { withFileTypes: true }))
.filter((dirent) => dirent.isDirectory() || dirent.isSymbolicLink())
.map((dirent) => path.join(packagesDir, dirent.name));
await Promise.all(components.map(async (component) => {
let package = await readPackage(component);
if (package.peerDependencies) {
Object.keys(package.peerDependencies).forEach((dependency) => {
let devDepVer = package.devDependencies[dependency].replace('^', '');
let peerDepVer = package.peerDependencies[dependency];
if (devDepVer) {
if (!semver.satisfies(devDepVer, peerDepVer)) {
console.error(`${component} has out of date peerDependencies ${dependency} (found ${devDepVer}, does not satisfy ${peerDepVer})`);
// Set a new peer dependency, stripping the beta version number
let newPeerDepVer = '^' + devDepVer.replace(/-\d+$/, '');
package.peerDependencies[dependency] = newPeerDepVer
console.error(` Updated ${dependency} to ${newPeerDepVer}`);
}
}
else {
throw new Error(`${component} has ${dependency} in peerDependencies, but not devDependencies!`);
}
});
await writePackage(component, package);
}
}));
};
async function releaseBundles() {
let bundlesDir = './bundles';
let bundles = (await fsp.readdir(bundlesDir, { withFileTypes: true }))
.filter((dirent) => dirent.isDirectory() || dirent.isSymbolicLink())
.map((dirent) => path.join(process.cwd(), bundlesDir, dirent.name));
await subrunner.runTaskOnPackages('release', bundles);
};
function graduatePeerDeps() {
function isPrerelease(version) {
return version.match(/-(alpha)|(beta)/);
}
return gulp.src('components/*/package.json')
.pipe(through.obj(function translateJSON(file, enc, cb) {
let data = JSON.parse(String(file.contents));
if (data.peerDependencies) {
for (let [peerDep, version] of Object.entries(data.peerDependencies)) {
if (isPrerelease(version)) {
version = version.replace('^', '');
let newVersion = `^${semver.major(version)}.${semver.minor(version)}.${semver.patch(version)}`;
console.log(`${data.name}: Graduating ${peerDep} to ${newVersion}`);
data.peerDependencies[peerDep] = newVersion;
}
}
}
file.contents = Buffer.from(JSON.stringify(data, null, 2));
cb(null, file);
}))
.pipe(gulp.dest('components/'));
}
function packageLint() {
return gulp.src('components/*/package.json')
.pipe(through.obj(function translateJSON(file, enc, cb) {
let data = JSON.parse(String(file.contents));
if (!data.license) {
data.license = 'Apache-2.0';
console.log(`${data.name}: Adding license=${data.license}`);
}
if (!data.publishConfig || data.publishConfig.access != 'public') {
console.log(`${data.name}: Adding publishConfig.access=public`);
data.publishConfig = data.publishConfig || {};
data.publishConfig.access = 'public';
}
if (!data.repository) {
console.error(`${data.name}: missing repository field!`);
}
else if (!data.repository.directory) {
data.repository.directory = 'components/' + data.name.split('/').pop();
console.log(`${data.name}: Adding missing repository.directory=${data.repository.directory}`);
}
if (data.author === '') {
console.log(`${data.name}: author field empty, deleting`);
delete data.author;
}
if (!data.homepage) {
data.homepage = 'https://opensource.adobe.com/spectrum-css/';
console.log(`${data.name}: Adding homepage=${data.homepage}`);
}
file.contents = Buffer.from(JSON.stringify(data, null, 2));
cb(null, file);
}))
.pipe(gulp.dest('components/'));
}
async function readmeLint() {
let packagesDir = './components';
let components = (await fsp.readdir(packagesDir, { withFileTypes: true }))
.filter((dirent) => dirent.isDirectory() || dirent.isSymbolicLink())
.map((dirent) => path.join(packagesDir, dirent.name));
await Promise.all(components.map(async(component) => {
let hasReadme = await fsp.access(path.join(component, 'README.md')).then(v => true).catch(e => false);
let package = await readPackage(component);
if (!hasReadme) {
console.log(`${package.name}: writing README...`);
let content = `# ${package.name}
> ${package.description}
This package is part of the [Spectrum CSS project](https://github.com/adobe/spectrum-css).
See the [Spectrum CSS documentation](https://opensource.adobe.com/spectrum-css/) and [Spectrum CSS on GitHub](https://github.com/adobe/spectrum-css) for details.
`;
await fsp.writeFile(path.join(component, 'README.md'), content);
}
}));
}
exports.graduatePeerDeps = graduatePeerDeps;
exports.readmeLint = readmeLint;
exports.packageLint = packageLint;
exports.checkPeerDependencies = checkPeerDependencies;
exports.version = gulp.series(
checkPeerDependencies,
builder.build
);
exports.dev = gulp.series(
exports.copySiteResources,
exports.dev
);
exports.devHeavy = gulp.series(
exports.copySiteResources,
exports.devHeavy
);
exports.buildDocs = builder.buildDocs;
exports.releaseBundles = releaseBundles;
exports.prepare = site.copySiteResources;