Skip to content

Commit 9f0f6ef

Browse files
committed
check & mkdir project directory
1 parent 5894c8f commit 9f0f6ef

File tree

15 files changed

+408
-147
lines changed

15 files changed

+408
-147
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-vue-library",
3-
"version": "1.0.0",
3+
"version": "0.1.0",
44
"description": "a cli to say hello",
55
"main": "src/index.js",
66
"bin": {

src/cli.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import arg from 'arg';
22
import { prompt } from 'inquirer';
33
import chalk from 'chalk';
4-
import { logError } from './utils';
4+
import { logError, getAbsolutePath, isFileOrDirExists } from './utils';
55
import { createProject } from './main';
66
import pkg from '../package';
77

@@ -25,9 +25,11 @@ const getHelp = () => chalk`
2525
`;
2626

2727
function parseArgsIntoOptions(rowArgs) {
28+
const projectName = rowArgs[2];
29+
let projectDirectory;
2830
let args;
2931

30-
if (!rowArgs[2]) {
32+
if (!projectName) {
3133
console.error('Please specify the project directory:');
3234
console.log(
3335
` ${chalk.cyan('create-vue-library')} ${chalk.green(
@@ -46,6 +48,8 @@ function parseArgsIntoOptions(rowArgs) {
4648
process.exit(1);
4749
}
4850

51+
projectDirectory = getAbsolutePath(projectName);
52+
4953
try {
5054
args = arg(
5155
{
@@ -77,11 +81,26 @@ function parseArgsIntoOptions(rowArgs) {
7781
}
7882

7983
return {
80-
projectName: rowArgs[2],
84+
projectName,
85+
projectDirectory,
8186
template: args['--template'],
8287
};
8388
}
8489

90+
async function checkIsProjectDirectoryValid(options) {
91+
const isExists = await isFileOrDirExists(options.projectDirectory);
92+
93+
if (isExists) {
94+
console.log(
95+
`Uh oh! Looks like there's already a directory called ${chalk.red(
96+
options.projectName
97+
)}.`
98+
);
99+
console.log('Please try a different name or delete that folder.');
100+
process.exit(1);
101+
}
102+
}
103+
85104
async function proptForMissingOptions(options) {
86105
const defaultTemplate = 'basic';
87106

@@ -105,6 +124,9 @@ async function proptForMissingOptions(options) {
105124

106125
export async function cli(rowArgs) {
107126
let options = parseArgsIntoOptions(rowArgs);
127+
128+
await checkIsProjectDirectoryValid(options);
129+
108130
options = await proptForMissingOptions(options);
109131
await createProject(options);
110132
}

src/main.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
import path from 'path';
22
import fs from 'fs';
3-
import { promisify } from 'util';
43
import execa from 'execa';
54
import listr from 'listr';
65
import chalk from 'chalk';
7-
import ncp from 'ncp';
86
import { projectInstall } from 'pkg-install';
9-
import { logError } from './utils';
7+
import { logError, mkdir, copy, access } from './utils';
108

11-
const access = promisify(fs.access);
12-
const copy = promisify(ncp);
9+
async function createProjectDirectory(options) {
10+
try {
11+
await mkdir(options.projectDirectory);
12+
} catch (err) {
13+
return Promise.reject(new Error('Failed to initialize project directory'));
14+
}
15+
return;
16+
}
1317

1418
async function copyTemplateFiles(options) {
15-
return copy(options.templateDirectory, options.targetDirectory, {
19+
return copy(options.templateDirectory, options.projectDirectory, {
1620
clobber: false,
1721
});
1822
}
1923

2024
async function initGit(options) {
2125
const result = await execa('git', ['init'], {
22-
cwd: options.targetDirectory,
26+
cwd: options.projectDirectory,
2327
});
2428
if (result.failed) {
2529
return Promise.reject(new Error('Failed to initialize git'));
@@ -28,13 +32,6 @@ async function initGit(options) {
2832
}
2933

3034
export async function createProject(options) {
31-
// ! mkdir options.projectName
32-
33-
options = {
34-
...options,
35-
targetDirectory: options.targetDirectory || process.cwd(),
36-
};
37-
3835
const currentFileUrl = import.meta.url;
3936
const templateDir = path.resolve(
4037
new URL(currentFileUrl).pathname,
@@ -53,6 +50,10 @@ export async function createProject(options) {
5350
}
5451

5552
const tasks = new listr([
53+
{
54+
title: 'Initialize project directory',
55+
task: () => createProjectDirectory(options),
56+
},
5657
{
5758
title: 'Copy project files',
5859
task: () => copyTemplateFiles(options),
@@ -63,7 +64,7 @@ export async function createProject(options) {
6364
},
6465
{
6566
title: 'Install dependencies',
66-
task: () => projectInstall({ cwd: options.targetDirectory }),
67+
task: () => projectInstall({ cwd: options.projectDirectory }),
6768
},
6869
]);
6970

src/utils.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
1+
import fs from 'fs';
2+
import path from 'path';
13
import chalk from 'chalk';
4+
import ncp from 'ncp';
5+
import { promisify } from 'util';
26

37
export const logError = message =>
48
console.error(chalk`{red ERROR:} ${message}`);
9+
10+
export const access = promisify(fs.access);
11+
export const stat = promisify(fs.stat);
12+
export const copy = promisify(ncp);
13+
export const mkdir = promisify(fs.mkdir);
14+
15+
export const isDir = name =>
16+
access(name)
17+
.then(stats => stats.isDirectory())
18+
.catch(() => false);
19+
20+
export const isFile = name =>
21+
access(name)
22+
.then(stats => stats.isFile())
23+
.catch(() => false);
24+
25+
export const isFileOrDirExists = name =>
26+
stat(name)
27+
.then(() => true)
28+
.catch(() => false);
29+
30+
export const getAbsolutePath = (...paths) => {
31+
return path.resolve(process.cwd(), ...paths);
32+
};

templates/basic/babel.config.json renamed to templates/basic/.babelrc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
"chrome": "67",
1010
"safari": "11.1"
1111
},
12-
"corejs": "2",
12+
"corejs": "2",
13+
"modules": false,
1314
"useBuiltIns": "usage"
1415
}
1516
]
16-
]
17+
],
18+
"plugins": ["@babel/external-helpers"]
1719
}

templates/basic/example/app.vue

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
<template>
22
<div>
3-
<p>hello I am parcel vue</p>
4-
<MyComp />
5-
<MyComp2 />
3+
<HelloWorld />
64
</div>
75
</template>
86

97
<script>
10-
import { MyComp } from "../.";
11-
import { MyComp2 } from "../.";
8+
import { HelloWorld } from "../.";
129
export default {
1310
name: "App",
1411
components: {
15-
MyComp,
16-
MyComp2
17-
},
18-
mounted() {
19-
console.log("mounted");
12+
HelloWorld
2013
}
2114
};
2215
</script>
2316

2417
<style scoped>
25-
p {
26-
color: red;
27-
}
2818
</style>

templates/basic/example/package-lock.json

Lines changed: 42 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/basic/example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"vue": "../node_modules/vue"
1414
},
1515
"devDependencies": {
16+
"@babel/core": "^7.8.7",
1617
"parcel": "^1.12.4"
1718
},
1819
"dependencies": {

0 commit comments

Comments
 (0)