-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.js
executable file
·367 lines (335 loc) · 9.99 KB
/
generate.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env node
// Third-party modules
const { Command } = require('commander')
const path = require('path')
const fs = require('fs')
const childProcess = require('child_process')
const program = new Command()
const cwd = process.cwd().toString()
const vscodeSettings = '.vscode/settings.json'
// Import utils.js
const {
parseChapters,
updateListOfFiles,
existIncludes
} = require('./utils.js')
function createConfig (mode = 'full', source = '', project = '', configPath = '',
includesMap = '', nodeModulePath = '', workingDir = '', foliantConfig = '',
debug = false, format = '') {
let existIncludesMap = false
let listOfFiles = []
// Set validate-internal-links config
const validateIntLinksConf = {}
validateIntLinksConf.src = source || undefined
validateIntLinksConf.project = project || undefined
validateIntLinksConf.includesMap = includesMap || undefined
validateIntLinksConf.workingDir = workingDir || undefined
const customRules = [
defPath(nodeModulePath, '/node_modules/markdownlint-rules-foliant/lib/indented-fence'),
defPath(nodeModulePath, '/node_modules/markdownlint-rules-foliant/lib/non-literal-fence-label'),
defPath(nodeModulePath, '/node_modules/markdownlint-rules-foliant/lib/fenced-code-in-quote'),
defPath(nodeModulePath, '/node_modules/markdownlint-rules-foliant/lib/typograph'),
defPath(nodeModulePath, '/node_modules/markdownlint-rules-foliant/lib/validate-internal-links'),
defPath(nodeModulePath, '/node_modules/markdownlint-rules-foliant/lib/frontmatter-tags-exist')
]
if (fs.existsSync(foliantConfig) && format === 'cjs') {
listOfFiles = parseChapters(foliantConfig, source, listOfFiles)
existIncludesMap = existIncludes(foliantConfig)
if (existIncludesMap) {
updateListOfFiles(source, includesMap, listOfFiles)
}
}
const configFull = {
MD001: true,
MD004: { style: 'dash' },
MD005: true,
MD007: { indent: 4 },
MD009: true,
MD010: true,
MD012: { maximum: 5 },
MD013: {
line_length: 1000,
heading_line_length: 80
},
MD014: true,
MD018: true,
MD019: true,
MD022: true,
MD024: true,
MD025: true,
MD026: true,
MD027: true,
MD028: true,
MD029: false,
MD030: true,
MD031: true,
MD032: false,
MD033: false,
MD034: false,
MD036: true,
MD037: true,
MD038: true,
MD039: false,
MD040: true,
MD041: true,
MD045: true,
MD046: false,
MD047: true,
MD049: { style: 'underscore' },
MD050: { style: 'asterisk' },
MD051: false,
MD052: true,
MD053: false,
'indented-fence': true,
'non-literal-fence-label': true,
'fenced-code-in-quote': true,
typograph: true,
'validate-internal-links': validateIntLinksConf,
'frontmatter-tags-exist': false
}
const configSlim = {
MD001: true,
MD004: false,
MD005: false,
MD007: false,
MD009: false,
MD010: false,
MD012: false,
MD013: false,
MD014: false,
MD018: false,
MD019: false,
MD022: false,
MD024: false,
MD025: true,
MD026: false,
MD027: false,
MD028: false,
MD029: false,
MD030: false,
MD031: false,
MD032: false,
MD033: false,
MD034: false,
MD036: false,
MD037: false,
MD038: false,
MD039: false,
MD040: false,
MD041: true,
MD045: false,
MD046: false,
MD047: false,
MD048: false,
MD049: false,
MD050: false,
MD051: false,
MD052: true,
MD053: false,
'indented-fence': true,
'non-literal-fence-label': true,
'fenced-code-in-quote': true,
typograph: false,
'validate-internal-links': validateIntLinksConf,
'frontmatter-tags-exist': false
}
const configTypograph = {
MD001: false,
MD003: false,
MD004: false,
MD005: false,
MD007: false,
MD009: false,
MD010: false,
MD011: false,
MD012: false,
MD013: false,
MD014: false,
MD018: false,
MD019: false,
MD020: false,
MD021: false,
MD022: false,
MD023: false,
MD024: false,
MD025: false,
MD026: false,
MD027: false,
MD028: false,
MD029: false,
MD030: false,
MD031: false,
MD032: false,
MD033: false,
MD034: false,
MD035: false,
MD036: false,
MD037: false,
MD038: false,
MD039: false,
MD040: false,
MD041: false,
MD042: false,
MD043: false,
MD044: false,
MD045: false,
MD046: false,
MD047: false,
MD048: false,
MD049: false,
MD050: false,
MD051: false,
MD052: false,
MD053: false,
'indented-fence': false,
'non-literal-fence-label': false,
'fenced-code-in-quote': false,
typograph: true,
'validate-internal-links': false,
'frontmatter-tags-exist': false
}
let config
if (mode === 'slim') {
config = configSlim
} else if (mode === 'default') {
return null
} else if (mode === 'typograph') {
config = configTypograph
} else {
config = configFull
}
if (configPath !== '') {
mode = 'custom'
console.log(`using configuration from a file: ${configPath}`)
try {
const customConfig = JSON.parse(fs.readFileSync(path.resolve(cwd, configPath), 'utf-8'))
config = Object.assign({}, config, customConfig)
} catch (error) {
if (error instanceof SyntaxError) {
console.error('Invalid JSON format')
} else {
console.error(error)
}
mode = 'slim'
config = configSlim
}
}
if (debug) {
console.log(config)
}
const json = { customRules, config }
let configExist = false
if (format === 'cjs') {
const content = []
json.globs = listOfFiles
content.push('// @ts-check\n\n"use strict";\n\n')
if (json.config['validate-internal-links']) {
content.push("const path = require('path')")
content.push("const repoName = require('git-repo-name')")
}
content.push(`const json = ${JSON.stringify(json, null, 4)}`)
if (json.config['validate-internal-links']) {
content.push('json.config[\'validate-internal-links\'].project = repoName.sync({cwd: __dirname})')
content.push('json.config[\'validate-internal-links\'].workingDir = __dirname')
}
content.push('\n\nmodule.exports = json')
try {
fs.writeFileSync(path.resolve(cwd, '.markdownlint-cli2.cjs'), content.join('\n'), { mode: 0o777 })
} catch (error) {
console.error(error)
process.exit(1)
}
console.log(`${mode} markdownlint config '.markdownlint-cli2.cjs' created successfully!`)
// disabling globs used by the markdownlint extension
configExist = initVSCodeSettings([])
const existPackageJSON = fs.existsSync('package.json')
if (existPackageJSON) {
const data = JSON.parse(fs.readFileSync('package.json'))
data.dependencies['git-repo-name'] = '^1.0.1'
data.dependencies['markdownlint-rules-foliant'] = 'latest'
try {
fs.writeFileSync('package.json', JSON.stringify(data, null, 2))
} catch (error) {
console.error(error)
}
} else {
// write package.json
const packageJSONforCJS = {
dependencies: {
'markdownlint-rules-foliant': 'latest',
'git-repo-name': '^1.0.1'
}
}
fs.writeFileSync(path.resolve(cwd, 'package.json'), JSON.stringify(packageJSONforCJS, null, 2), { mode: 0o777 })
}
// install dependencies
childProcess.execSync('npm i .', { stdio: [0, 1, 2] })
if (!existPackageJSON) {
// remove package.json
fs.rmSync(path.resolve(cwd, 'package.json'))
fs.rmSync(path.resolve(cwd, 'package-lock.json'))
}
if (configExist) {
console.log(`${vscodeSettings} config updated successfully!`)
} else {
console.log(`${vscodeSettings} config created successfully!`)
}
} else {
const content = JSON.stringify({ customRules, config }, null, 4)
try {
fs.writeFileSync(path.resolve(cwd, '.markdownlint-cli2.jsonc'), content, { mode: 0o777 })
} catch (error) {
console.error(error)
process.exit(1)
}
console.log(`${mode} markdownlint config '.markdownlint-cli2.jsonc' created successfully!`)
}
}
function initVSCodeSettings (listOfFiles = []) {
const configExist = fs.existsSync(vscodeSettings)
let data = {
'markdownlint.lintWorkspaceGlobs': listOfFiles
}
if (configExist) {
const originalData = JSON.parse(fs.readFileSync(vscodeSettings))
data = Object.assign({}, originalData, data)
} else {
fs.mkdirSync(path.dirname(vscodeSettings), { recursive: true })
}
try {
fs.writeFileSync(vscodeSettings, JSON.stringify(data))
} catch (error) {
console.error(error)
process.exit(1)
}
return configExist
}
function defPath (nodeModulePath, relPath) {
if (nodeModulePath.length !== 0) {
return path.join(nodeModulePath, relPath)
} else {
return '.' + relPath
}
}
program
.name('create-markdownlint-config')
.description('script for generating markdownlint-cli2 config in foliant-project root')
.version('0.0.1')
.option('-m, --mode <mode>', 'full, slim, typograph or default config', 'slim')
.option('-s, --source <source>', 'relative path to source directory', '')
.option('-p, --project <project>', 'project name', '')
.option('-c, --config-path <path-to-config>', 'path to custom config', '')
.option('--includes-map <includes-map>', 'includes map path', '')
.option('--node-modules <node-modules-path>', 'custom path to node modules', '')
.option('-w, --working-dir <working-dir>', 'working dir', '')
.option('--foliant-config <config-path>',
'the configuration file is a foliant from which chapters', 'foliant.yml')
.option('--format <format>',
'config for markdownlint-cli2 (default"jsonc") or "cjs" format with automatic parameter detection',
'jsonc')
.option('-d, --debug', 'output of debugging information', false)
program.parse()
const options = program.opts()
createConfig(options.mode, options.source, options.project, options.configPath,
options.includesMap, options.nodeModules, options.workingDir,
options.foliantConfig, options.debug, options.format)