Skip to content

Commit 7be984f

Browse files
author
yashpandit
committed
Add CRA Generator
1 parent 459de42 commit 7be984f

File tree

6 files changed

+121
-9
lines changed

6 files changed

+121
-9
lines changed

generator.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
var path = require('path');
44
var isValid = require('is-valid-app');
55

6-
module.exports = function(app) {
6+
module.exports = app => {
77
// return if the generator is already registered
88
if (!isValid(app, 'generate-react')) return;
99

10+
app.use(require('generate-project'));
11+
app.register(
12+
'create-react-app',
13+
require('./lib/generators/create-react-app')
14+
);
15+
1016
/**
1117
* Generate a `index.js` file to the current working directory. Learn how to [customize
1218
* behavior(#customization) or override built-in templates.
@@ -18,8 +24,6 @@ module.exports = function(app) {
1824
* @api public
1925
*/
2026

21-
task(app, 'react', 'index.js');
22-
2327
/**
2428
* Alias for running the [react](#react) task with the following command:
2529
*
@@ -30,18 +34,19 @@ module.exports = function(app) {
3034
* @api public
3135
*/
3236

33-
app.task('default', ['react']);
37+
app.task('default', ['project']);
3438
};
3539

3640
/**
3741
* Create a task with the given `name` and glob `pattern`
3842
*/
3943

40-
function task(app, name, pattern) {
41-
app.task(name, function() {
42-
return app.src(pattern, {cwd: __dirname})
44+
function task(app, name, pattern, dest = '') {
45+
app.task(name, () => {
46+
return app
47+
.src(pattern, { cwd: __dirname })
4348
.pipe(app.renderFile('*'))
4449
.pipe(app.conflicts(app.cwd))
45-
.pipe(app.dest(app.cwd));
50+
.pipe(app.dest(path.join(app.cwd, dest)));
4651
});
4752
}

lib/constants.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
YARN: "yarn",
3+
NPM: "npm",
4+
NPX: "npx",
5+
ADD: "add",
6+
INSTALL: "install",
7+
CREATE_REACT_APP: "create-react-app",
8+
REDUX: "redux",
9+
REACT_REDUX: "react-redux",
10+
MOBX: "mobx",
11+
STYLED_COMPONENTS: "styled-components",
12+
SCSS: "node-sass",
13+
LESS: "less",
14+
ROUTER: "react-router-dom",
15+
ENZYME: "enzyme",
16+
JEST: "jest"
17+
};

lib/generators/create-react-app.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const spawn = require("../spawn");
2+
const path = require("path");
3+
const { prompt } = require("enquirer");
4+
const { NPX, CREATE_REACT_APP } = require("../constants.js");
5+
6+
module.exports = app => {
7+
app.task("create-react-app", async () => {
8+
let dest = app.options.name;
9+
if (!dest) {
10+
const answers = await prompt({
11+
type: "text",
12+
name: "appName",
13+
message: "What would you like the app name to be?"
14+
});
15+
dest = answers.appName;
16+
}
17+
await spawn(NPX, [CREATE_REACT_APP, dest]);
18+
});
19+
20+
app.task("create-react-app-templates", async () => {
21+
const dest = path.join(app.cwd, app.options.name);
22+
const answers = await prompt([
23+
{
24+
type: "text",
25+
name: "description",
26+
message: "What description would you like to use for your website?"
27+
},
28+
{
29+
type: "text",
30+
name: "title",
31+
message: "What title would you like to use for your website?"
32+
}
33+
]);
34+
35+
return app
36+
.src("create-react-app/public/*.*", {
37+
cwd: path.join(__dirname, "../../templates")
38+
})
39+
.pipe(app.renderFile("*", answers).on("error", console.error))
40+
.pipe(app.conflicts(dest))
41+
.pipe(app.dest(dest));
42+
});
43+
44+
app.task("default", ["create-react-app", "create-react-app-templates"]);
45+
};

lib/spawn.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
3+
const spawn = require("child_process").spawn;
4+
5+
const defaults = {
6+
stdio: "inherit",
7+
cwd: process.cwd()
8+
};
9+
10+
// simple wrapper around cli commands
11+
module.exports = async (cmd, args, options) => {
12+
return new Promise((resolve, reject) => {
13+
const cp = spawn(cmd, args, { ...defaults, ...options });
14+
cp.on("error", reject);
15+
cp.on("close", code => {
16+
if (code > 0) {
17+
return reject(code);
18+
}
19+
resolve(code);
20+
});
21+
});
22+
};

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
"test": "mocha"
2222
},
2323
"dependencies": {
24-
"is-valid-app": "^0.3.0"
24+
"is-valid-app": "^0.3.0",
25+
"enquirer": "^2.3.2",
26+
"generate": "^0.14.0",
27+
"generate-project": "^1.0.0"
2528
},
2629
"keywords": [
2730
"generate",
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="<%= description %>"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
14+
<title><%= title %></title>
15+
</head>
16+
<body>
17+
<noscript>You need to enable JavaScript to run this app.</noscript>
18+
<div id="root"></div>
19+
</body>
20+
</html>

0 commit comments

Comments
 (0)