1 | const fs = require("fs");
|
2 | const path = require("path");
|
3 | const homeDir = require('os').homedir();
|
4 | const scriptDir = path.dirname(require.main.filename);
|
5 | const errors = require("./errors.js");
|
6 | const yaml = require("yaml");
|
7 | const jingJar = "jing-20131210.jar";
|
8 | const cwd = process.cwd();
|
9 | const sourceRepoPaths = [ "local", "std", "src", "locale", "modules", "cslschema", "cslmschema" ];
|
10 | const 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";
|
20 | const 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 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function 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 | }
|
51 | function 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 | }
|
66 | try {
|
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 |
|
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 | }
|
99 | if (!config.path.styletests) {
|
100 | errors.setupGuidance("Some setup required.");
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | if (!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 |
|
113 |
|
114 | if (!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 |
|
120 | if (!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 |
|
126 | if (sourceRepoPaths.filter(k => config.path[k]).length < sourceRepoPaths.length) {
|
127 | config.mode = "styleMode";
|
128 | } else {
|
129 | config.mode = "fullMode"
|
130 | }
|
131 | config.path.cwd = cwd;
|
132 | config.path.scriptdir = scriptDir;
|
133 | config.path.jing = path.join(scriptDir, "jing", jingJar);
|
134 | if (process.env.TRAVIS) {
|
135 | config.path.fixturedir = path.join(homeDir, ".cslTestFixtures");
|
136 | } else {
|
137 | config.path.fixturedir = path.join(config.path.configdir, ".cslTestFixtures");
|
138 | }
|
139 | config.path.chai = path.join(scriptDir, "node_modules", "chai", "index.js");
|
140 | module.exports = config;
|