Skip to content

Commit 520fdfe

Browse files
committed
feat: 🎸 add create react-app command
1 parent 6722f1c commit 520fdfe

13 files changed

Lines changed: 535 additions & 1766 deletions

File tree

lib/scripts/create.js

Whitespace-only changes.

lib/scripts/create/default.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import spawn from 'cross-spawn';
2+
import fs from 'fs-extra';
3+
import { getTemplatePath } from '../../utils/path';
4+
5+
export async function job(path, cwd) {
6+
const templatePath = getTemplatePath('default');
7+
8+
console.log();
9+
console.log('🤖 Add dependencies...');
10+
console.log();
11+
12+
spawn.sync('yarn', ['add', '-D', 'ngt-scripts'], { stdio: 'inherit', cwd });
13+
14+
console.log();
15+
console.log('📁 Copy default template...');
16+
console.log();
17+
18+
await fs.copy(templatePath, path);
19+
}

lib/scripts/create/index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import chalk from 'chalk';
2+
import meow from 'meow';
3+
4+
import { getPath } from '../../utils/path';
5+
import { job as defaultJob } from './default';
6+
7+
const {
8+
input: [projectType, pathArg],
9+
} = meow(chalk`
10+
{yellowBright Usage}
11+
$ ngt-cli create <projectType>
12+
13+
{whiteBright Project types:}
14+
- react-app
15+
`);
16+
17+
if (!projectType) {
18+
console.error(chalk`[{redBright.bold ERROR}] Missing project type`);
19+
process.exit(1);
20+
}
21+
22+
const { path, cwd } = getPath(pathArg);
23+
24+
switch (projectType) {
25+
case 'react-app':
26+
{
27+
const { job: projectJob } = require(`./${projectType}`);
28+
29+
[projectJob, defaultJob].map(async job => {
30+
try {
31+
await job(path, cwd);
32+
} catch (error) {
33+
console.error(error);
34+
process.exit(1);
35+
}
36+
});
37+
}
38+
break;
39+
40+
default:
41+
console.error(
42+
chalk`[{redBright.bold ERROR} Unknow project type {bold ${projectType}}]`,
43+
);
44+
}

lib/scripts/create/react-app.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import spawn from 'cross-spawn';
2+
3+
export async function job(dest, cwd) {
4+
console.log();
5+
console.log('🛠 Creating react app...');
6+
console.log();
7+
8+
spawn.sync('npx', ['create-react-app', dest], { stdio: 'inherit', cwd });
9+
}

lib/utils/path.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import path from 'path';
2+
import fs from 'fs-extra';
3+
import chalk from 'chalk';
4+
5+
export function getPath(pathArg) {
6+
const cwd = process.cwd();
7+
8+
if (!pathArg) {
9+
return { path: '.', cwd };
10+
}
11+
12+
if (path.isAbsolute(pathArg)) {
13+
return { path: pathArg, cwd };
14+
}
15+
16+
return { path: path.join(cwd, pathArg), cwd };
17+
}
18+
19+
export function getTemplatePath(templateName) {
20+
const templatePath = path.join(__dirname, `../../templates/${templateName}`);
21+
const pathExist = fs.pathExistsSync(templatePath);
22+
23+
if (!pathExist) {
24+
console.error(
25+
chalk`[{redBright.bold ERROR}] Template does not exist {bold ${templateName}}`,
26+
);
27+
process.exit(1);
28+
}
29+
30+
return templatePath;
31+
}

0 commit comments

Comments
 (0)