-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
copy-entries.mjs
76 lines (60 loc) · 2.05 KB
/
copy-entries.mjs
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
import { rmSync, readdirSync } from 'fs';
import fse from 'fs-extra';
import { program } from 'commander';
import { execa } from 'execa';
import semver from 'semver';
import cmp from 'semver-compare';
const { copySync } = fse;
program
.argument('project')
.argument('[version]')
.option('-m, --min-version [version]');
program.parse();
const [project, version] = program.args;
const { minVersion } = program.opts();
const oldDocsPath = '/Users/mansona/temp/old-docs'
if (version) {
// only apply for the specified version
copyEntries(project, version);
} else {
// do all versions
const fullProjectVersions = readdirSync(
`${oldDocsPath}/json-docs/${project}`
).filter((v) => v.match(/\d+\.\d+\.\d+/));
const projectVersions = fullProjectVersions.map((v) => {
let [, major, minor] = v.match(/(\d+)\.(\d+)\.\d+/);
return `${major}.${minor}`;
});
const uniqueProjectVersions = [...new Set(projectVersions)];
const highestPatchVersions = uniqueProjectVersions.map((uniqVersion) => {
const sortedPatchVersions = fullProjectVersions
.filter((projectVersion) => {
return semver.satisfies(projectVersion, uniqVersion);
})
.sort(cmp);
return sortedPatchVersions[sortedPatchVersions.length - 1];
})
.sort(cmp)
.filter((version) => {
if (minVersion) {
return semver.gte(version, minVersion);
} else {
return true;
}
});
for (let version of highestPatchVersions) {
await copyEntries(project, version)
}
}
async function copyEntries(project, version) {
rmSync(`json-docs/${project}/${version}`, { recursive: true, force: true });
try {
rmSync(`rev-index/${project}-${version}.json`);
} catch {
// ignore
}
copySync(`${oldDocsPath}/json-docs/${project}/${version}`, `./json-docs/${project}/${version}`, {recursive: true});
copySync(`${oldDocsPath}/rev-index/${project}-${version}.json`, `./rev-index/${project}-${version}.json`);
await execa('npm', ['run', 'fix:files'], { stdio: 'inherit'});
await execa('npm', ['run', 'test'], { stdio: 'inherit'});
}