-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidycrap.js
executable file
·94 lines (81 loc) · 3.4 KB
/
tidycrap.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
#!/usr/bin/env node
// Get cmdline params
const program = require('commander');
program.storeOptionsAsProperties(false);
program.requiredOption('-u, --username <name>', 'Github username');
program.requiredOption('-p, --path <path>', `Path to the repo relative to ${__dirname} e.g. -p ../awesome-project`);
program.option('-n, --name <name>', 'Name of this repo (default: derived from path)');
program.parse(process.argv);
const options = program.opts();
const name = options.name || require('path').basename(options.path);
console.log(`Running tidycrap for ${options.username}'s ${name} project at ${options.path}\n`);
console.log("Adding gh-pages with yarn");
// Add gh-pages package
const { exec } = require('child_process');
exec('yarn add gh-pages', {
cwd: options.path
}, (err, stdout, stderr) => {
if (err) {
console.log(err, stdout, stderr);
return;
}
console.log('OK');
const homepage = `https://${options.username}.github.io/${name}`;
console.log('Modifying package.json');
// Modify package.json with github pages deets.
const packageJsonLocation = `${options.path}/package.json`;
const packageJson = require(packageJsonLocation);
packageJson.homepage = homepage;
packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.predeploy = 'yarn run build';
packageJson.scripts.deploy = 'gh-pages -d build';
const fs = require('fs');
fs.writeFile(packageJsonLocation, JSON.stringify(packageJson, undefined, 2), (err) => {
if (err) {
console.error(err);
return;
}
console.log('OK');
function tryDelete(path) {
try {
fs.unlinkSync(path);
} catch (e) {
console.error(e);
}
}
console.log('Removing CRA stuff I don\'t use');
tryDelete(`${options.path}/src/logo.svg`);
tryDelete(`${options.path}/public/logo192.png`);
tryDelete(`${options.path}/public/logo512.png`);
tryDelete(`${options.path}/public/manifest.json`);
tryDelete(`${options.path}/src/serviceWorker.js`);
fs.writeFileSync(`${options.path}/src/App.css`, "");
fs.writeFileSync(`${options.path}/src/index.css`, "");
fs.createReadStream(`${__dirname}/templates/index.html`).pipe(fs.createWriteStream(`${options.path}/public/index.html`));
fs.createReadStream(`${__dirname}/templates/App.js`).pipe(fs.createWriteStream(`${options.path}/src/App.js`));
fs.createReadStream(`${__dirname}/templates/index.js`).pipe(fs.createWriteStream(`${options.path}/src/index.js`));
console.log("OK");
console.log("Updating README.md with project title and demo link");
const data = fs.readFileSync(`${options.path}/README.md`);
const fd = fs.openSync(`${options.path}/README.md`, 'w+');
const capitalisedName = name.toUpperCase().substring(0, 1) + name.substring(1).toLowerCase();
const insertIntoReadme = `# ${capitalisedName}\n\nCheck out the [project demo](${homepage}).\n\n## Create React App stuff\n\n`;
fs.writeSync(fd, insertIntoReadme, 0, insertIntoReadme.length, 0)
fs.writeSync(fd, data, 0, data.length, insertIntoReadme.length)
fs.close(fd, (err) => {
if (err) console.log(err);
});
console.log("OK");
console.log('Committing');
exec('git add . && git commit -m "tidycrap."', {
cwd: options.path
}, (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
console.error(stderr);
return;
}
console.log('OK');
});
});
});