-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcli.js
117 lines (91 loc) · 3.1 KB
/
cli.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
115
116
117
#!/usr/bin/env node
const chalk = require('chalk');
const editJsonFile = require('edit-json-file');
const util = require('util');
const fs = require('fs');
const asyncMkdir = util.promisify(fs.mkdir);
const CURR_DIR = process.cwd();
const isWindows = process.platform === 'win32';
const createDirectoryContents = async (starterKitPath, newProjectPath) => {
const filesToCreate = fs.readdirSync(starterKitPath);
filesToCreate.forEach(file => {
const origFilePath = `${starterKitPath}/${file}`;
const stats = fs.statSync(origFilePath);
if (stats.isFile()) {
const contents = fs.readFileSync(origFilePath, 'utf8');
const writePath = `${CURR_DIR}/${newProjectPath}/${file}`;
fs.writeFileSync(writePath, contents, 'utf8');
} else if (stats.isDirectory()) {
fs.mkdirSync(`${CURR_DIR}/${newProjectPath}/${file}`);
createDirectoryContents(
`${starterKitPath}/${file}`,
`${newProjectPath}/${file}`
);
}
});
};
const editFiles = async projectName => {
let file = editJsonFile(`${__dirname}/starter-kit/package.json`);
file.set('name', projectName);
file.save();
if (fs.existsSync(`${__dirname}/starter-kit/gitignore`)) {
fs.rename(
`${__dirname}/starter-kit/gitignore`,
`${__dirname}/starter-kit/.gitignore`,
function (err) {
if (err) throw err;
}
);
}
};
const checkDirectory = async () => {
const length = process.argv.length;
if (length !== 3) {
throw 'You should input 1 argument as the project name';
} else {
const projectName = process.argv[2];
const directory = isWindows
? `${CURR_DIR}\\${projectName}`
: `${CURR_DIR}/${projectName}`;
console.log(`\n\nCreating a new starter kit in ${chalk.cyan(directory)}\n`);
return projectName;
}
};
const checkProjectName = async (projectPath) => {
if (fs.existsSync(projectPath)) {
throw `ERROR: Project already exists in directory:: ${projectPath}`;
}
};
const successText = (projectName) => {
const successText = `Success! Created ${chalk.cyan(
projectName
)} at ${chalk.cyan(CURR_DIR)}
\n\nRun the following to get started:\n\n\t
${chalk.cyan('cd')} ${projectName}
\n Changes directory
\n ${chalk.cyan('yarn install')}
\n Installs all dependencies.
\n\nAnd you're all set!
\nInside that directory, you can run several commands:
\n ${chalk.cyan('yarn build')}
\n Builds the app.
\n ${chalk.cyan('yarn lint')}
\n Checks if the code is linted correctly.
\n ${chalk.cyan('yarn test')}
\n Starts the test runner.
\n\n You can check the documentation here: https://github.com/postlight/nodejs-typescript-kit
`;
console.log(successText);
};
const run = async () => {
const projectName = await checkDirectory();
const starterKitPath = `${__dirname}/starter-kit/`;
await checkProjectName(`${CURR_DIR}/${projectName}`);
await editFiles(projectName);
await asyncMkdir(`${CURR_DIR}/${projectName}`);
await createDirectoryContents(starterKitPath, projectName);
successText(projectName);
};
run().catch(err => {
console.log(`${chalk.bgRed(err.message || err)}\n`);
});