-
Notifications
You must be signed in to change notification settings - Fork 315
/
migratedPages.js
117 lines (100 loc) · 3.36 KB
/
migratedPages.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
/**
* Copyright (c) Moodle Pty Ltd.
*
* Moodle is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Moodle is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moodle. If not, see <http://www.gnu.org/licenses/>.
*/
const path = require('path');
const fs = require('fs');
/* eslint-disable-next-line import/no-extraneous-dependencies */
const yaml = require('js-yaml');
const obsoleteDocs = require('./data/obsoletePages.json');
/**
* A list of documents which have been migrated with their source and destination paths shown.
*/
/* eslint-disable-next-line no-restricted-properties */
const migratedDocs = yaml.load(fs.readFileSync(path.join(
__dirname,
'data/migratedPages.yml',
), 'utf8'));
const isObsolete = (legacyPath) => obsoleteDocs.indexOf(legacyPath) !== -1;
/**
* Whether the specified path has been migrated.
*
* @returns {bool}
*/
const isMigrated = (legacyPath) => (typeof migratedDocs[legacyPath] !== 'undefined');
/**
* Get the path to the new doc from a legacy doc path.
*
* @param legacyPath {string}
* @returns {object}
*/
const getMigratedDoc = (legacyPath) => {
if (!isMigrated(legacyPath)) {
return null;
}
// Fetch the first match.
const migratedItem = migratedDocs[legacyPath][0];
let { filePath } = migratedItem;
if (filePath.startsWith('/')) {
filePath = filePath.substr(1);
}
return {
filePath,
slug: migratedItem.slug,
};
};
const getAbsoluteDirectory = (relativePath) => {
const absolutePath = path.join(process.env.PWD, relativePath);
/* eslint-disable-next-line no-restricted-properties */
const pathStat = fs.statSync(absolutePath);
if (pathStat.isFile()) {
return path.dirname(absolutePath);
}
return absolutePath;
};
/**
* Get the path to the new doc relative to the file it was in.
*
* This has to consider whether the file is in the same docs instance or not due to versioning.
*
* @param {string} legacyPath
* @param {string} usedIn
* @returns {string}
*/
const getMigrationLink = (legacyPath, usedIn) => {
const replacementFile = getMigratedDoc(legacyPath);
if (!replacementFile) {
return null;
}
const relativeUsedIn = path.relative(process.env.PWD, usedIn);
const replacementIsGeneral = replacementFile.filePath.startsWith('general/');
const usedInIsGeneral = relativeUsedIn.startsWith('general/');
if (replacementIsGeneral || usedInIsGeneral) {
// If the file is in /general, always return the slug.
return replacementFile.slug;
}
const absRelativeUsedIn = getAbsoluteDirectory(relativeUsedIn);
const absReplacementFile = path.join(__dirname, replacementFile.filePath);
const relativeLink = path.relative(absRelativeUsedIn, absReplacementFile);
if (relativeLink.startsWith('.')) {
return relativeLink;
}
return `./${relativeLink}`;
};
module.exports = {
isMigrated,
isObsolete,
getMigrationLink,
};