-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·114 lines (99 loc) · 2.76 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#! /usr/local/bin/node
const { spawn } = require("child_process");
const fs = require("fs");
const colors = require("colors");
const commandLineArgs = require("command-line-args");
const getUsage = require("command-line-usage");
const {
babelrc,
gitignore,
index,
packageJSON,
webpackConfig
} = require("./templates");
const { dependencies, devDependencies } = require("./args");
// define the flags that are needed for this tool.
const optionDefinitions = [{ name: "help", alias: "h", type: Boolean }];
// define the usage sections
const sections = [
{
header: "create-react-npm-component",
content: "generates a skeleton project for a react component to upload to npm"
},
{
header: "Usage",
content: ["$ create-react-npm-component [project-name]"]
},
{
header: "Options",
optionList: [
{
name: "help",
alias: "h",
description: "Print this usage guide."
}
]
}
];
const Do = (cmd, args, callback) => {
const p = spawn(cmd, args, { stdio: ["pipe", "pipe", "pipe"] });
p.stdout.pipe(process.stdout);
p.stderr.pipe(process.stderr);
process.stdin.pipe(p.stdin);
p.on("close", () => {
if (callback) {
callback();
}
});
};
// the main function
const Main = () => {
// get the dictionary of options passed in via the command line
const options = commandLineArgs(optionDefinitions);
const project = process.argv[2];
if (options.help || !project) {
const usage = getUsage(sections);
console.log(usage); // eslint-disable-line no-console
return;
}
fs.mkdirSync(project);
process.chdir(project);
fs.mkdirSync("build");
fs.mkdirSync("src");
// write some boilerplate files...
console.log(colors.green("\nwriting boilerplate files..."));
const files = [gitignore, webpackConfig, index, packageJSON];
files.forEach(file => {
let f;
if (typeof file.content === "object") {
f = JSON.stringify(file.content);
} else {
f = file.content;
}
fs.writeFile(`./${file.name}`, f, { mode: "644" }, err => {
if (err) {
console.log(`error writing ${file.name}`);
console.log(err);
}
});
});
// start and run the user through npm init...
console.log(colors.green("\nstarting npm init..."));
Do("npm", ["init"], () => {
// install dependencies...
console.log(
colors.green("\ninstalling npm dependencies, this may take a while...")
);
Do("npm", dependencies, () => {
// install dev dependencies...
console.log(
colors.green("\ninstalling dev dependencies, this may take a while...")
);
Do("npm", devDependencies, () => {
console.log(`\n[ ${project} ] created successfully`);
console.log("press any key to exit");
});
});
});
};
Main();