-
Notifications
You must be signed in to change notification settings - Fork 1
/
rename.js
41 lines (33 loc) · 1.32 KB
/
rename.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
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
const name = process.argv[2];
if (!name) {
console.error('You need to pass name `yarn rename new-package-name` or `node rename new-package-name`');
}
const packageFile = path.join(__dirname, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageFile));
packageJson.name = packageJson.name.replace('XXX', name);
packageJson.description = packageJson.description.replace('XXX', name);
packageJson.repository.url = packageJson.repository.url.replace('XXX', name);
packageJson.bugs.url = packageJson.bugs.url.replace('XXX', name);
packageJson.homepage = packageJson.homepage.replace('XXX', name);
packageJson.private = false;
const packageJsonNewContent = JSON.stringify(packageJson, null, 2);
if (!process.env.DRY) {
fs.writeFileSync(packageFile, packageJsonNewContent);
console.log(`Changed ${packageFile}`);
} else {
console.log(packageJsonNewContent);
}
const readmeFile = path.join(__dirname, 'README.md');
const readme = fs.readFileSync(readmeFile)
.toString()
.replace(/XXX/g, name)
.replace(/<!-- START RENAME -->[\s\S]*<!-- END RENAME -->/g, `\`\`\`\nyarn install ${name}\n\`\`\``);
if (!process.env.DRY) {
fs.writeFileSync(readmeFile, readme);
console.log(`Changed ${readmeFile}`);
} else {
console.log(readme);
}