-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (63 loc) · 3.11 KB
/
index.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
var fs = require('fs');
var path = require('path');
var slash = require('slash');
var { spawn } = require('child_process');
var del = require('del');
var preconfigs = require('./preconfig.json');
let getPath = p => {
return slash(path.isAbsolute(p) ? p : path.join(process.cwd(), p));
}
let getEnvironmentVariables = conigPath => {
return fs.existsSync(conigPath) ? JSON.parse(fs.readFileSync(conigPath)) : {};
}
exports.buildDocfx = (options = {
projectDir: '',
environment: '',
siteDir: ''
}) => {
if (!options.projectDir) {
throw new Error("The directory of the docfx project must be specified.");
}
const globalPreconfigs = preconfigs;
let docfxJsonPath = path.normalize(path.join(getPath(options.projectDir), 'docfx.json'));
let docfxPreconfigPath = path.normalize(path.join(getPath(options.projectDir), 'environment.json'));
let docfxGlobalConfigPath = path.normalize(path.join(getPath(options.projectDir), 'global.json'));
let globalConfigs = getEnvironmentVariables(docfxGlobalConfigPath);
let environmentConfigs = getEnvironmentVariables(docfxPreconfigPath);
let applyWarnAsErr = ``;
environmentConfigs.environment = options.environment || 'development';
globalPreconfigs['variables'] = environmentConfigs[environmentConfigs.environment];
if (fs.existsSync(getPath(options.siteDir))) {
del.sync(getPath(options.siteDir));
}
fs.mkdirSync(`${getPath(options.siteDir)}`);
fs.writeFileSync(
getPath(`${options.siteDir}/${globalPreconfigs._configFileName}`),
JSON.stringify(globalPreconfigs)
);
console.log(`Starting docfx build for: ${getPath(options.projectDir)}`);
if (globalConfigs._useWarningsAsErrors) {
applyWarnAsErr = `--warningsAsErrors`;
}
return spawn("dotnet", ["docfx", "build", applyWarnAsErr, `${path.normalize(getPath(docfxJsonPath))}`], { stdio: 'inherit' }).on('exit', (err) => {
if (err === 4294967295) {
console.log(`\x1b[31m`, `------------------------------------------------------------------------------------`);
console.log(`--------------------------- Bookmark/Hyperlink Errors -----------------------------`);
console.log(`-----------------------------------------------------------------------------------`);
console.log();
console.error(` Build failed with bookmark warnings marked in yellow above! `);
console.error(`These warnings indicate the specific topic and link that points to the code line.`);
console.log();
console.log(`-----------------------------------------------------------------------------------`);
console.log(`--------------------------- Error Code ` + err + ` ---------------------------------`);
console.log(`-----------------------------------------------------------------------------------`, `\x1b[0m`);
console.log();
} else {
console.log('Exiting code with Error: ' + err);
}
}).on('close', (err) => {
if (err) {
console.error(err);
}
});
}