-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
46 lines (39 loc) · 1.39 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
const fs = require('fs');
const path = require('path');
const findup = require('findup');
const sortScripts = require('sort-scripts');
function findPkg(dir) {
try {
return path.join(findup.sync(dir, 'package.json'), 'package.json');
} catch (err) {
console.log(err);
throw new Error('No package.json file found');
}
}
module.exports = function SCRIPTS(originalContent, options = {}, config) {
let pkgPath;
if (options && options.pkg) {
pkgPath = path.resolve(path.dirname(config.originalPath), options.pkg);
} else {
pkgPath = findPkg(config.originalPath);
}
const { scripts } = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
const rows = originalContent.trim().split('\n').map(s => s.trim());
const headerIndex = rows.findIndex(row => row.startsWith('|-'));
const details = (headerIndex !== -1 ? rows.slice(headerIndex + 1) : rows)
.map(row => row.split('|').map(s => s.trim()).filter(s => !!s))
.filter(([script]) => !!script)
.reduce((obj, [script, description]) => {
obj[script.replace(/`/g, '')] = description;
return obj;
}, {});
return ['| Script | Description |', '|--------|-------------|']
.concat(
sortScripts(scripts).map(([name, script]) => {
const description = details[name] || `\`${script}\``;
const label = `\`${name}\``;
return `| ${label} | ${description} |`;
})
)
.join('\n');
};