Skip to content

Commit 4e9a556

Browse files
committed
seperated dependency from package for installing dynamically
1 parent f89f30e commit 4e9a556

8 files changed

Lines changed: 164 additions & 11163 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# create-react-webpack

scripts/create.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const path = require("path");
22
const { copyDirectory, ErrorMessage, SuccessMessage } = require("./helper");
33
const { SPECIALCHAR, TEMPLATE_PATH } = require("./constants");
4+
const {
5+
installDependencies,
6+
getDependencies,
7+
getDevDependencies
8+
} = require("./dependencies");
49

510
var args = process.argv.slice(2);
611
var dirName = args[0];
@@ -19,5 +24,14 @@ if (dirName[0].match("^[A-Z0-9]")) {
1924
//using process.cwd for getting current path
2025
let destination = path.join(process.cwd() + "/" + args[0]);
2126
copyDirectory(TEMPLATE_PATH, destination);
27+
28+
let commands = [],
29+
options;
30+
31+
commands.push(getDependencies());
32+
commands.push(getDevDependencies());
33+
options = { cwd: destination, stdio: "inherit" };
34+
35+
installDependencies(commands, options);
2236
console.log(SuccessMessage("Successfully created the directory"));
2337
}

scripts/dependencies.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const child_process = require("child_process");
2+
const { SuccessMessage } = require("./helper");
3+
4+
const DEPENDENCIES = ["prop-types", "react", "react-dom", "react-hot-loader"];
5+
const DEV_DEPENDENCIES = [
6+
"@babel/cli",
7+
"@babel/core",
8+
"@babel/plugin-proposal-class-properties",
9+
"@babel/preset-env",
10+
"@babel/preset-react",
11+
"babel-loader",
12+
"brotli-webpack-plugin",
13+
"compression-webpack-plugin",
14+
"css-loader",
15+
"eslint",
16+
"eslint-plugin-react",
17+
"html-webpack-plugin",
18+
"husky",
19+
"jest",
20+
"prettier",
21+
"pretty-quick",
22+
"style-loader",
23+
"webpack",
24+
"webpack-cli",
25+
"webpack-dev-server",
26+
"webpack-manifest-plugin",
27+
"webpack-merge"
28+
];
29+
30+
/**
31+
* Returns npm command for installing dependencies
32+
*/
33+
function getDependencies() {
34+
var installCommand = "npm install --save";
35+
DEPENDENCIES.forEach(dependency => {
36+
installCommand += ` ${dependency} `;
37+
});
38+
return installCommand;
39+
}
40+
41+
/**
42+
* Returns npm command for installing dev-dependencies
43+
*/
44+
function getDevDependencies() {
45+
var installCommand = "npm install --save-dev";
46+
DEV_DEPENDENCIES.forEach(dependency => {
47+
installCommand += ` ${dependency} `;
48+
});
49+
return installCommand;
50+
}
51+
52+
/**
53+
*
54+
* @param {Array<String>} commands list of commands needs to be executed
55+
* @param {Object} options
56+
* @param {String} options.cwd directory where you want to execute command
57+
* @param {String} options.stdio process's stdio config
58+
*/
59+
function installDependencies(commands, options) {
60+
options.stdio = options.stdio || "inherit";
61+
//cleaning the npm cache
62+
child_process.execSync("npm cache clean --force", options);
63+
64+
if (commands) {
65+
try {
66+
commands.forEach(command => {
67+
console.log(SuccessMessage(command));
68+
child_process.execSync(command, options);
69+
});
70+
} catch (error) {
71+
throw error;
72+
}
73+
}
74+
}
75+
76+
module.exports = { installDependencies, getDependencies, getDevDependencies };

scripts/helper.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const chalk = require("chalk");
44
const { UNNECESSORYFOLDERS } = require("./constants");
55

66
/**
7-
*@summary copies the directory content from source to destination directory
7+
* @summary copies the directory content from source to destination directory
88
* @param {String} source path of source file
99
* @param {String} destination path of destination file
1010
*/
@@ -35,18 +35,24 @@ function copyDirectory(source, destination) {
3535
}
3636

3737
/**
38-
*@summary copies the file content from source to destination file
38+
* @summary copies the file content from source to destination file
3939
* @param {String} source path of source file
4040
* @param {String} destination path of destination file
4141
*/
4242
function copyFile(source, destination) {
43-
var inputFile = fs.createReadStream(source);
44-
var outputFile = fs.createWriteStream(destination);
45-
inputFile.pipe(outputFile);
43+
var inputFile, outputFile;
44+
if (source.match(".json$")) {
45+
inputFile = JSON.parse(fs.readFileSync(source, "utf8"));
46+
fs.writeFileSync(destination, JSON.stringify(inputFile, null, 2), "utf8");
47+
} else {
48+
inputFile = fs.createReadStream(source);
49+
outputFile = fs.createWriteStream(destination);
50+
inputFile.pipe(outputFile);
51+
}
4652
}
4753

4854
/**
49-
*
55+
* Creates a directory from input
5056
* @param {String} destination path of destination directory
5157
*/
5258
function createDirectory(destination) {

template/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# create-react-webpack
1+
# create-react-webpack

0 commit comments

Comments
 (0)