-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpublish-docs.js
444 lines (372 loc) · 12.4 KB
/
publish-docs.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* eslint-env node */
const shell = require('shelljs');
const fs = require('fs');
const replace = require('replace-in-file');
const argv = require('yargs').argv;
const version = argv.v || '0.0.0-dev';
const pruneDev = argv.pruneDev !== undefined;
const removeSpecific = !!argv.remove;
const runSetup = argv.noSetup === undefined;
const runBuild = argv.noBuild === undefined;
const runCommit = argv.noCommit === undefined;
const runPush = argv.noPush === undefined;
const forcePush = !!argv.forcePush;
const pruneOutdated = argv.noPruneOutdated === undefined;
const runTeardown = argv.noTeardown === undefined;
const cleanOnFail = runTeardown && argv.noCleanOnFail === undefined;
const dryRun = !!argv.dryRun && argv.dryRun !== 'false';
const BASE_URL = '/lime-elements/';
if (argv.h !== undefined) {
shell.echo(`
usage: npm run docs:publish [-- [--v=<version>] [--remove=<pattern>]
[--pruneDev] [--noSetup] [--noBuild]
[--noCommit] [--noPush] [--noPruneOutdated]
[--noTeardown] [--dryRun] [--noCleanOnFail]
[--gitUser=<commit author name>]
[--gitEmail=<commit author email>]]
--v The version number for this release of the documentation.
Defaults to '0.0.0-dev'.
--dryRun Use dry-run mode. Do not push any changes.
--remove Removes all versions matching the given filename-pattern.
--pruneDev Alias for --remove=0.0.0-dev*
--noSetup Run no setup. Only use this if you have previously run the
setup step without running the teardown step afterward.
--noBuild Do not build the documentation.
--noCommit Do not commit any changes.
--noPush Do not push any commits.
--forcePush Force-push commit. Only for use by the automated publish run
when a new version has been released. Any less important
runs, like those publishing the docs for a pull request,
should NOT use this flag.
--noPruneOutdated
Do not remove docs for outdated patch versions.
--noTeardown Run no cleanup at end of script. Implies --noCleanOnFail.
--noCleanOnFail Do not run cleanup if script fails. Unless --noTeardown
is set, cleanup will still be run if script is successful.
--authorName Commit author name. Will update the local git config if set.
Will use existing git config if omitted.
--authorEmail Commit author email address. Will update the local git
config if set. Will use existing git config if omitted.
`);
} else if (removeSpecific || pruneDev) {
let commitMessage;
if (runSetup) {
cloneDocsRepo();
}
if (pruneDev) {
remove('0.0.0-dev*');
commitMessage = 'chore(docs): prune dev-versions';
}
if (removeSpecific) {
remove(argv.remove);
commitMessage = `chore(docs): remove ${argv.remove}`;
}
if (commitMessage && runCommit) {
commit(commitMessage);
}
if (runPush) {
push();
}
if (runTeardown) {
teardown(true);
}
} else {
if (runSetup) {
cloneDocsRepo();
}
if (runBuild) {
build();
}
if (runSetup) {
pullAndRebase();
}
if (runBuild) {
copyBuildOutput();
}
if (runCommit) {
commit();
}
if (pruneOutdated) {
pruneOldPatchVersions();
}
if (runPush) {
push();
}
if (runTeardown) {
teardown(true);
}
}
function cloneDocsRepo() {
if (!shell.which('git')) {
shell.echo('Sorry, this script requires git');
shell.exit(1);
}
if (
shell.exec(
'git clone --single-branch --branch gh-pages https://[email protected]/Lundalogik/lime-elements.git docsDist',
).code !== 0
) {
shell.echo('git clone failed!');
teardown();
shell.exit(1);
}
}
function pullAndRebase() {
shell.cd('docsDist');
if (shell.exec('git pull --rebase').code !== 0) {
shell.echo('git pull failed!');
shell.cd('..');
teardown();
shell.exit(1);
}
shell.cd('..');
}
function build() {
try {
let options = {
files: ['src/index.html'],
from: [
/href="\/favicon.svg"/g,
/<base href="\/">/g,
/="\/build/g,
/="\/style/g,
/="\/assets/g,
/\/kompendium.json/g,
],
to: [
`href="${BASE_URL}versions/${version}/favicon.svg"`,
`<base href="${BASE_URL}versions/${version}/">`,
`="${BASE_URL}versions/${version}/build`,
`="${BASE_URL}versions/${version}/style`,
`="${BASE_URL}versions/${version}/assets`,
`${BASE_URL}versions/${version}/kompendium.json`,
],
};
replace.sync(options);
options = {
files: ['stencil.config.docs.ts'],
from: /baseUrl: '\/'/g,
to: `baseUrl: '${BASE_URL}versions/${version}/'`,
};
replace.sync(options);
options = {
files: ['src/index.md'],
from: /<version\\>/g,
to: `${version}`,
};
replace.sync(options);
shell.exec('git diff --name-status');
} catch (error) {
shell.echo('Error occurred:', error);
teardown();
shell.exit(1);
}
if (shell.exec('npm install').code !== 0) {
shell.echo('npm install failed!');
teardown();
shell.exit(1);
}
if (shell.exec('npm run docs:build').code !== 0) {
shell.echo('docs:build failed!');
teardown();
shell.exit(1);
}
shell.exec('ls -la www');
}
function copyBuildOutput() {
// Create `versions` folder if it doesn't already exist.
try {
shell.mkdir('docsDist/versions');
} catch {
// If mkdir failed, it's almost certainly because the dir already exists.
// Just ignore the error.
}
shell.cd('docsDist/versions');
shell.echo('Removing old version folder if it already exists.');
shell.rm('-rf', version);
shell.cd('../..');
shell.echo('Copying new docs version into docsDist/versions/');
if (
shell.cp(
'-R',
`www${BASE_URL}versions/${version}`,
'docsDist/versions/',
).code !== 0
) {
shell.echo('copying output failed!');
teardown();
shell.exit(1);
}
if (
shell.cp('-R', 'www/kompendium.json', `docsDist/versions/${version}`)
.code !== 0
) {
shell.echo('copying kompendium.json failed!');
teardown();
shell.exit(1);
}
shell.echo(`Copying docs-index.html to 'docsDist/versions/${version}/'`);
if (
shell.cp('docs-index.html', `docsDist/versions/${version}/`).code !== 0
) {
shell.echo(
'[WARNING] Copying docs-index.html failed. Not critical, continuing.',
);
}
updateVersionList();
shell.echo('Copying docs-index.html from `latest` version');
if (
shell.cp(
'-f',
'docsDist/versions/latest/docs-index.html',
'docsDist/index.html',
).code !== 0
) {
shell.echo(
'[WARNING] Copying docs-index.html from `latest` failed. Not critical, continuing.',
);
}
}
function remove(pattern) {
shell.cd('docsDist/versions');
shell.rm('-rf', pattern);
shell.cd('../..');
updateVersionList();
}
function updateVersionList() {
shell.cd('docsDist');
const files = fs
.readdirSync('versions')
// We need to filter out the symlinks to `latest` and `next` since they
// are not actual versions. We don't use `next` anymore, but we have a
// static symlink pointing to `latest`, to avoid breaking existing
// links to `next`.
.filter((file) => file !== 'latest' && file !== 'next');
fs.writeFileSync(
'versions.js',
`window.versions = ${JSON.stringify(files)};`,
);
shell.cd('..');
// Keep only versions that begin with a digit. Any versions beginning with
// a letter are pull requests, pre-releases, or other special cases, not
// eligible as "Latest".
const fullVersions = files.filter((file) => file.match(/^\d.*/));
createSymlinkForRelease(fullVersions, 'latest');
}
function pruneOldPatchVersions() {
shell.cd('docsDist');
const files = fs
.readdirSync('versions')
// Never prune the `latest` and `next` symlinks.
.filter((file) => file !== 'latest' && file !== 'next');
shell.cd('..');
const fullVersionRegex = /^(\d*)\.(\d*)\.(\d*)$/;
const fullVersions = files.filter((file) => file.match(fullVersionRegex));
const collator = new Intl.Collator(undefined, {
numeric: true,
sensitivity: 'base',
});
// -------
// For any versions that have the same major and minor version, and only
// differ in the patch version, keep only the latest.
fullVersions.sort(collator.compare);
fullVersions.reverse();
let lastCheckedVersion = fullVersions.shift();
const versionsToDelete = [];
fullVersions.forEach((item) => {
const lastChecked = lastCheckedVersion.match(fullVersionRegex);
const current = item.match(fullVersionRegex);
if (lastChecked[1] === current[1] && lastChecked[2] === current[2]) {
versionsToDelete.push(item);
}
lastCheckedVersion = item;
});
// -------
versionsToDelete.forEach((item) => {
remove(item);
if (runCommit) {
commit(`chore(docs): remove ${argv.remove}`);
}
});
}
function createSymlinkForRelease(versions, alias) {
// We need to sort the strings alphanumerically, which javascript doesn't
// do by default. So I found this neat solution at
// https://blog.praveen.science/natural-sorting-in-javascript/#solution
// /ads
const collator = new Intl.Collator(undefined, {
numeric: true,
sensitivity: 'base',
});
const sortedVersions = [...versions];
sortedVersions.sort(collator.compare);
const versionToLink = sortedVersions.pop();
shell.echo(`Creating "${alias}"-link pointing to: ${versionToLink}`);
createSymlink(versionToLink, alias);
}
function createSymlink(folder, alias) {
shell.cd('docsDist/versions');
if (shell.ln('-sf', `${folder}`, alias).code !== 0) {
if (
shell.rm(alias).code !== 0 ||
shell.ln('-sf', `${folder}`, alias).code !== 0
) {
shell.echo(`Creating symlink '${alias}' failed!`);
shell.cd('../..');
teardown();
shell.exit(1);
}
}
shell.cd('../..');
}
function commit(message) {
message = message || `chore(docs): create docs ${version}`;
shell.cd('docsDist');
if (argv.authorName) {
shell.echo('setting git config user.name');
shell.exec(`git config --local user.name '${argv.authorName}'`);
}
if (argv.authorEmail) {
shell.echo('setting git config user.email');
shell.exec(`git config --local user.email '${argv.authorEmail}'`);
}
shell.exec('git add -A --ignore-errors');
if (shell.exec(`git commit -m "${message}"`).code !== 0) {
shell.echo('git commit failed!');
shell.cd('..');
teardown();
shell.exit(1);
}
shell.cd('..');
}
function push() {
shell.cd('docsDist');
if (forcePush) {
shell.echo('Using `git push --force`!');
}
if (dryRun) {
shell.exec('git log -1');
shell.echo('Dry-run, so skipping push.');
} else if (
shell.exec(
`git push ${
forcePush ? '--force' : ''
} https://[email protected]/Lundalogik/lime-elements.git HEAD:gh-pages`,
).code !== 0
) {
shell.echo('git push failed!');
shell.cd('..');
teardown();
shell.exit(1);
}
shell.cd('..');
}
function teardown(finished) {
if (finished || cleanOnFail) {
shell.exec(
'git checkout src/index.html src/index.md stencil.config.docs.ts',
);
shell.echo('Removing docs repo clone in docsDist.');
shell.exec('rm -rf docsDist');
}
}