-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
187 lines (166 loc) · 5.2 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#! /usr/bin/env node
'use strict';
import { execa } from 'execa';
import { fileURLToPath } from 'url';
import { Listr } from 'listr2';
import fs from 'fs';
import inquirer from 'inquirer';
import path from 'path';
import validFilename from 'valid-filename';
const dependencies = [
// Parcel dependencies
// https://github.com/postcss/postcss/wiki/PostCSS-8-for-end-users#parcel
'parcel@nightly',
// Tailwind CSS dependencies
'tailwindcss',
'postcss',
'autoprefixer',
];
const questions = [
{
type: 'input',
name: 'root',
message: 'Name your project root directory',
validate: (value) =>
validFilename(value.trim()) ? true : 'Please enter a valid path',
},
];
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const tasks = new Listr([
{
title: 'Create directory structure',
task: (context) => {
const root = context.root;
const tree = ['src/css', 'src/img', 'src/js'];
if (fs.existsSync(root)) {
throw new Error(`Project directory ${root} already exists`);
}
fs.mkdirSync(root);
tree.forEach((branch) => {
const dir = path.join(root, branch);
fs.mkdirSync(dir, { recursive: true }, (err) => {
if (err) throw new Error(`Cannot create directory ${dir}: ${err}`);
});
});
},
},
{
title: 'Create project files',
task: () => {
return new Listr([
{
title: 'Create Tailwind CSS configuration',
task: async (context) => {
const root = context.root;
const source = path.join(
__dirname,
'templates',
'tailwind.config.js'
);
const destination = path.join(root, 'tailwind.config.js');
fs.copyFile(source, destination, (err) => {
if (err)
throw new Error(
`Cannot create Tailwind CSS configuration: ${err}`
);
});
},
},
{
title: 'Create PostCSS configuration',
task: async (context) => {
const root = context.root;
const source = path.join(__dirname, 'templates', '.postcssrc');
const destination = path.join(root, '.postcssrc');
fs.copyFile(source, destination, (err) => {
if (err)
throw new Error(`Cannot create PostCSS configuration: ${err}`);
});
},
},
{
title: 'Create gitignore file',
task: async (context) => {
const root = context.root;
const source = path.join(__dirname, 'templates', '_gitignore');
const destination = path.join(root, '.gitignore');
fs.copyFile(source, destination, (err) => {
if (err) throw new Error(`Cannot create gitignore file: ${err}`);
});
},
},
]);
},
},
{
title: 'Create templates',
task: () => {
return new Listr([
{
title: 'index.html',
task: async (context) => {
const root = context.root;
const source = path.join(__dirname, 'templates', 'index.html');
const destination = path.join(root, 'src', 'index.html');
fs.copyFile(source, destination, (err) => {
if (err) throw new Error(`Cannot create index.html: ${err}`);
});
},
},
{
title: 'main.css',
task: async (context) => {
const root = context.root;
const source = path.join(__dirname, 'templates', 'main.css');
const destination = path.join(root, 'src', 'css', 'main.css');
fs.copyFile(source, destination, (err) => {
if (err) throw new Error(`Cannot create main.css: ${err}`);
});
},
},
]);
},
},
{
title: 'Set up dependencies',
task: () => {
return new Listr([
{
title: 'Create package.json',
task: async (context) => {
const root = context.root;
const source = path.join(__dirname, 'templates', '_package.json');
const destination = path.join(root, 'package.json');
fs.copyFile(source, destination, (err) => {
if (err) throw new Error(`Cannot create package.json: ${err}`);
});
},
},
{
title: 'Install dependencies with Yarn',
task: async (context, task) => {
const root = context.root;
const params = ['add', '-D'];
process.chdir(root);
await execa('yarn', [...params, ...dependencies]).catch(() => {
context.yarn = false;
task.skip('Yarn not available');
});
},
},
{
title: 'Install dependencies with npm',
enabled: (context) => context.yarn === false,
task: async () => {
const params = ['install', '--save-dev'];
await execa('npm', [...params, ...dependencies]);
},
},
]);
},
},
]);
inquirer.prompt(questions).then((answers) => {
tasks.run(answers).catch((err) => console.log(err));
});