This repository has been archived by the owner on Mar 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gridtastic-scaffold.ts
87 lines (74 loc) · 2.05 KB
/
gridtastic-scaffold.ts
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
import fs from 'fs';
import pkgDir from 'pkg-dir';
import {log} from 'console';
import chalk from 'chalk';
import pascalcase from 'pascalcase';
import isGridsomeProject from './isGridsomeProject';
import arg from 'arg';
async function touchDir(dirPath: string): Promise<void> {
let fsPromises = fs.promises;
await fsPromises.mkdir(dirPath);
}
export default async (argv: string[]): Promise<void> => {
const args = arg({
'--template': Boolean,
'--page': Boolean,
'--component': Boolean,
'--layout': Boolean,
'--name': String,
'-l': '--layout',
'-c': '--component',
'-p': '--page',
'-t': '--template',
'-n': '--name',
'--help': Boolean,
'-h': '--help'
}, {argv});
if (args['--help']) {
console.log(chalk`
{bold Description}
Creates a new Vue file of page, component, template, or layout types.
{bold Usage}
$ gridtastic scaffold --page --name <name>
<name> will converted to PascalCase automatically.
`);
process.exit(0);
}
isGridsomeProject();
let {
'--name': name = null,
'--component': component = false,
'--template': template = false,
'--page': page = false,
'--layout': layout = false
} = args;
if (!(component || page || template || layout)) {
console.log(chalk.bold.red('Please provide one of the filetype options.'));
process.exit(1);
}
if (!name) {
console.log(chalk.bold.red('--name option must be provided.'));
process.exit(1);
}
const lookupTable = {
component,
template,
page,
layout
};
const pkgRoot = await pkgDir(__dirname);
for (let [type, value] of Object.entries(lookupTable)) {
if (value) {
let filename: string = pascalcase(name);
let pascalType: string = pascalcase(type);
if (fs.existsSync(`${pkgRoot}/${type}s/${filename}.vue`)) {
log(chalk.red(`${filename}.vue already exists!`));
process.exit(126);
} else {
fs.existsSync(`./src/${type}s`) || touchDir(`./src/${type}s`);
fs.copyFileSync(`${pkgRoot}/templates/${pascalType}.vue`, `./src/${type}s/${filename}.vue`);
log(chalk.green(`src/${type}s/${filename}.vue`));
}
}
}
};