UNPKG

4.82 kBJavaScriptView Raw
1const fs = require("fs");
2const path = require("path");
3const homeDir = require('os').homedir();
4const scriptDir = path.dirname(require.main.filename);
5const errors = require("./errors.js");
6const yaml = require("yaml");
7const jingJar = "jing-20131210.jar";
8const cwd = process.cwd();
9const sourceRepoPaths = [ "local", "std", "src", "locale", "modules", "cslschema", "cslmschema" ];
10const defaultConfig =
11 "path:\n"
12 + " styletests: false\n"
13 + " local: false\n"
14 + " std: false\n"
15 + " src: false\n"
16 + " locale: false\n"
17 + " modules: false\n"
18 + " cslschema: false\n"
19 + " cslmschema: false";
20const configInstructions =
21 "Configuration\n\n"
22 + "To use the cslrun command for building and\n"
23 + "running style tests, edit the styletests line\n"
24 + "in " + path.join(homeDir, ".cslrun.yaml") + ":\n\n"
25 + " styletests: path/to/styles/directory\n\n"
26 + "The path should be to the parent directory of\n"
27 + "subdirectories named for the styles to be tested.\n"
28/*
29 * Config priority:
30 * - User homeDir config
31 * - Current directory config
32 *
33 * Should allow paths to be set as relative to homeDir or absolute,
34 * but internally use always absolute for the config paths
35 *
36 */
37function makeAbsolute(basePath, config) {
38 if (config && "object" === typeof config.path && "undefined" === typeof config.path.length) {
39 for (var key in config.path) {
40 if (config.path[key]) {
41 if (!path.isAbsolute(config.path[key])) {
42 config.path[key] = path.join(basePath, config.path[key]);
43 }
44 }
45 }
46 } else {
47 var error = new Error("Corrupt file (fix, remove, or revert): " + path.join(basePath, "cslrun.yaml"));
48 throw error;
49 }
50}
51function getConfig(dirName, hiddenFile) {
52 var prefix = "";
53 if (hiddenFile) {
54 prefix = ".";
55 }
56 try {
57 var yamlSrc = fs.readFileSync(path.join(dirName, prefix + "cslrun.yaml")).toString();
58 var config = yaml.parse(yamlSrc);
59 } catch (err) {
60 var error = new Error("Unable to parse config file: " + path.join(dirName, prefix + "cslrun.yaml"));
61 throw error;
62 }
63 makeAbsolute(dirName, config);
64 return config;
65}
66try {
67 if (process.env.TRAVIS) {
68 var config = {
69 path: {}
70 };
71 } else {
72 if (!fs.existsSync(path.join(homeDir, ".cslrun.yaml"))) {
73 fs.writeFileSync(path.join(homeDir, ".cslrun.yaml"), defaultConfig);
74 }
75 // Flag looks for a hidden file, .cslrun.yaml
76 var config = getConfig(homeDir, true);
77 config.path.configdir = homeDir;
78 }
79 var pth = cwd;
80 while (path.basename(pth)) {
81 if (fs.existsSync(path.join(pth, "cslrun.yaml"))) {
82 if (pth === homeDir) {
83 throw new Error("Found cslrun.yaml configuration file (without a dot) in home directory " + homeDir + "\n\nIn the home directory ONLY, use .cslrun.yaml instead (with a dot---and it should already exist).\n");
84 break;
85 }
86 var extraConfig = getConfig(pth);
87 makeAbsolute(pth, extraConfig);
88 for (var key in extraConfig.path) {
89 config.path[key] = extraConfig.path[key];
90 }
91 config.path.configdir = pth;
92 break;
93 }
94 pth = path.dirname(pth);
95 }
96} catch (err) {
97 errors.errorHandler(err);
98}
99if (!config.path.styletests) {
100 errors.setupGuidance("Some setup required.");
101}
102
103/*
104 * Schemata locations
105 */
106if (!config.path.cslschema || !fs.existsSync(config.path.cslschema)
107 || !config.path.cslmschema || !fs.existsSync(config.path.cslmschema)) {
108 config.path.cslschema = require("citeproc-csl-schemata").csl;
109 config.path.cslmschema = require("citeproc-csl-schemata").cslm;
110}
111/*
112 * Locale locations
113 */
114if (!config.path.locale || !fs.existsSync(config.path.locale)
115 || !fs.readdirSync(config.path.locale).length) {
116
117 config.path.locale = require("citeproc-locales");
118}
119
120if (!config.path.modules || !fs.existsSync(config.path.modules)
121 || !fs.readdirSync(config.path.modules).length) {
122
123 config.path.modules = require("citeproc-juris-modules");
124}
125
126if (sourceRepoPaths.filter(k => config.path[k]).length < sourceRepoPaths.length) {
127 config.mode = "styleMode";
128} else {
129 config.mode = "fullMode"
130}
131config.path.cwd = cwd;
132config.path.scriptdir = scriptDir;
133config.path.jing = path.join(scriptDir, "jing", jingJar);
134if (process.env.TRAVIS) {
135 config.path.fixturedir = path.join(homeDir, ".cslTestFixtures");
136} else {
137 config.path.fixturedir = path.join(config.path.configdir, ".cslTestFixtures");
138}
139config.path.chai = path.join(scriptDir, "node_modules", "chai", "index.js");
140module.exports = config;