Skip to content

Commit 671fab5

Browse files
committed
Normnalize paths and line endings in api data
Ensure api data uses POSIX paths and line endings fixes #13
1 parent 4a64962 commit 671fab5

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

src/intern-dev-api.ts

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ const cwd = process.cwd();
3636
log('Scrubbing file paths');
3737
scrubPaths(project);
3838

39+
log('Normalizing line endings');
40+
normalizeLineEndings(project);
41+
3942
const json = JSON.stringify(project.toObject(), null, '\t');
4043

4144
if (!existsSync('docs')) {
@@ -47,46 +50,75 @@ const outFile = join('docs', 'api.json');
4750
writeFileSync(outFile, json);
4851
log(`Wrote API data to ${outFile}`);
4952

53+
// Recursively walk an object, normalizing any line endings in strings
54+
function normalizeLineEndings(reflection: any) {
55+
walk(
56+
reflection,
57+
'__lenormalized__',
58+
(_, value) => typeof value === 'string' && /\r\n/.test(value),
59+
value => value.replace(/\r\n/g, '\n')
60+
);
61+
}
62+
5063
// Recursively walk an object, relativizing any paths
5164
function scrubPaths(reflection: any) {
52-
if (reflection['__visited__']) {
65+
walk(
66+
reflection,
67+
'__scrubbed__',
68+
(key, value) =>
69+
typeof value === 'string' &&
70+
(key === 'originalName' || key === 'fileName' || key === 'name'),
71+
scrubPath
72+
);
73+
}
74+
75+
// Relativize a path, or return the input if it's not an absolute path
76+
function scrubPath(value: string) {
77+
if (/".*"/.test(value)) {
78+
const testValue = value.replace(/^"/, '').replace(/"$/, '');
79+
if (isAbsolute(testValue)) {
80+
const newPath = `"${relative(cwd, testValue)}"`;
81+
return newPath.replace(/\\/g, '/');
82+
}
83+
} else if (isAbsolute(value)) {
84+
const newPath = relative(cwd, value);
85+
return newPath.replace(/\\/g, '/');
86+
}
87+
return value;
88+
}
89+
90+
// Walk a project reflection, modifying values as necessary
91+
function walk(
92+
reflection: any,
93+
sentinel: string,
94+
test: (key: string, value: any) => boolean,
95+
modify: (value: any) => any
96+
) {
97+
if (reflection[sentinel]) {
5398
return;
5499
}
55100

56-
reflection['__visited__'] = true;
101+
reflection[sentinel] = true;
57102

58103
if (Array.isArray(reflection)) {
59-
for (let item of reflection) {
104+
for (const item of reflection) {
60105
if (typeof item === 'object') {
61-
scrubPaths(item);
106+
walk(item, sentinel, test, modify);
62107
}
63108
}
64109
} else if (typeof reflection === 'object') {
65110
const keys = Object.keys(reflection);
66-
for (let key of keys) {
111+
for (const key of keys) {
67112
const value = reflection[key];
68113
if (value == null) {
69114
continue;
70115
}
71116

72-
if (key === 'originalName' || key === 'fileName' || key === 'name') {
73-
reflection[key] = scrubPath(value);
117+
if (test(key, value)) {
118+
reflection[key] = modify(value);
74119
} else if (typeof value === 'object') {
75-
scrubPaths(value);
120+
walk(value, sentinel, test, modify);
76121
}
77122
}
78123
}
79124
}
80-
81-
// Relativize a path, or return the input if it's not an absolute path
82-
function scrubPath(value: string) {
83-
if (/".*"/.test(value)) {
84-
const testValue = value.replace(/^"/, '').replace(/"$/, '');
85-
if (isAbsolute(testValue)) {
86-
return `"${relative(cwd, testValue)}"`;
87-
}
88-
} else if (isAbsolute(value)) {
89-
return relative(cwd, value);
90-
}
91-
return value;
92-
}

0 commit comments

Comments
 (0)