-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrewrite.js
58 lines (47 loc) · 1.81 KB
/
rewrite.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
const fs = require('node:fs');
const https = require('node:https');
const path = require('node:path');
const fetchData = (urlPath) => new Promise((resolve, reject) => {
https.get(
`https://api.github.com/repos/${urlPath}`,
{headers: {'user-agent': 'node'}},
async (res) => {
res.on('error', (err) => {
reject(err);
});
const chunks = [];
res.on('data', (chunk) => {
chunks.push(chunk);
});
res.on('end', () => {
const responseBody = Buffer.concat(chunks).toString();
try {
const parsed = JSON.parse(responseBody);
resolve(parsed);
} catch {
reject(responseBody);
}
});
});
});
const gitConfig = fs.readFileSync(path.join('.git', 'config')).toString();
const lines = gitConfig.split('\n');
const originUrl = lines.find((l) => l.includes('[email protected]'));
const originUrlPath = originUrl.split(':')[1].split('.')[0];
fetchData(originUrlPath).then(({name, description}) => {
const packageJson = JSON.parse(fs.readFileSync('package.json').toString());
packageJson.name = name;
packageJson.description = description;
packageJson.repository.url = `git+https://github.com/${originUrlPath}.git`;
packageJson.bugs.url = `https://github.com/${originUrlPath}/issues`;
packageJson.homepage = `https://github.com/${originUrlPath}#readme`;
const newPackageJsonContent = JSON.stringify(packageJson, null, 2) + '\n';
fs.writeFileSync('package.json', newPackageJsonContent);
const packageLockJson = JSON.parse(fs.readFileSync('package-lock.json').toString());
packageLockJson.name = name;
const newPackageLockJsonContent = JSON.stringify(packageLockJson, null, 2) + '\n';
fs.writeFileSync('package-lock.json', newPackageLockJsonContent);
const newReadmeContent = `# ${name}\n${description}\n`;
fs.writeFileSync('README.md', newReadmeContent);
fs.unlinkSync('rewrite.js');
});