Skip to content

Commit 213d1a8

Browse files
author
alexlee-dev
committed
🔧 Add check for existing directory and use option
1 parent 3a26247 commit 213d1a8

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [0.8.0] - _Unreleased_
8+
## [0.8.0] - 2020-06-01
9+
10+
### ✏️ Shell Option
911

1012
### Added
1113

14+
- Check if directory exists before executing install commands
15+
- Use `shell` option for `spawn` command if `platform === 'win32'`
16+
1217
### Changed
1318

1419
### Removed

src/init.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const createProjectDirectory = async (
6666
);
6767
} catch (error) {
6868
spinner.fail();
69-
console.log("")
69+
console.log("");
7070
throw new Error(error);
7171
}
7272
};
@@ -88,12 +88,22 @@ export const installDependencies = async (
8888
const installCommand = "npm";
8989
let installArgs = ["install", "--save"];
9090
installArgs = installArgs.concat(dependencies);
91-
// * Create a process that installs the dependencies
92-
await executeCommand(installCommand, installArgs, { cwd: root });
93-
spinner.succeed("Dependencies installed successfully");
91+
// * Verify that the directory exists 1st
92+
const pathExists = await fs.pathExists(root);
93+
if (pathExists) {
94+
// * Create a process that installs the dependencies
95+
await executeCommand(installCommand, installArgs, {
96+
cwd: root,
97+
shell: process.platform === "win32",
98+
});
99+
spinner.succeed("Dependencies installed successfully");
100+
} else {
101+
spinner.fail(`Path: ${root} does not exist.`);
102+
throw new Error(`Path: ${root} does not exist.`);
103+
}
94104
} catch (error) {
95105
spinner.fail();
96-
console.log("")
106+
console.log("");
97107
throw new Error(error);
98108
}
99109
};
@@ -124,12 +134,22 @@ export const installDevDependencies = async (
124134
installArgs = installArgs.concat(devDependencies);
125135
}
126136

127-
// * Creates a process that installs the dev dependencies
128-
await executeCommand(installCommand, installArgs, { cwd: root });
129-
spinner.succeed("DevDependencies installed successfully");
137+
// * Verify that the directory exists 1st
138+
const pathExists = await fs.pathExists(root);
139+
if (pathExists) {
140+
// * Create a process that installs the dependencies
141+
await executeCommand(installCommand, installArgs, {
142+
cwd: root,
143+
shell: process.platform === "win32",
144+
});
145+
spinner.succeed("DevDependencies installed successfully");
146+
} else {
147+
spinner.fail(`Path: ${root} does not exist.`);
148+
throw new Error(`Path: ${root} does not exist.`);
149+
}
130150
} catch (error) {
131151
spinner.fail();
132-
console.log("")
152+
console.log("");
133153
throw new Error(error);
134154
}
135155
};
@@ -190,7 +210,7 @@ export const copyTemplateFiles = async (
190210
spinner.succeed("Template files copied successfully");
191211
} catch (error) {
192212
spinner.fail();
193-
console.log("")
213+
console.log("");
194214
throw new Error(error);
195215
}
196216
};
@@ -242,7 +262,7 @@ export const replaceTemplateValues = async (
242262
spinner.succeed("Values in template files replaced successfully");
243263
} catch (error) {
244264
spinner.fail();
245-
console.log("")
265+
console.log("");
246266
throw new Error(error);
247267
}
248268
};
@@ -283,7 +303,7 @@ export const createTSConfig = async (
283303
spinner.succeed("tsconfig.json created successfully");
284304
} catch (error) {
285305
spinner.fail();
286-
console.log("")
306+
console.log("");
287307
throw new Error(error);
288308
}
289309
};

src/util.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import validateProjectName from "validate-npm-package-name";
1414
export const executeCommand = async (
1515
command: string,
1616
args?: string[],
17-
options?: { cwd?: string }
17+
options?: { cwd?: string; shell?: boolean }
1818
): Promise<void | { code: number; signal: any }> =>
1919
new Promise((resolve, reject) => {
2020
const cp = spawn(command, args, options);
@@ -60,7 +60,9 @@ export const cleanupError = async (
6060
const rootExists = await fs.pathExists(root);
6161

6262
if (rootExists) {
63-
await executeCommand("rimraf", [root]);
63+
await executeCommand("rimraf", [root], {
64+
shell: process.platform === "win32",
65+
});
6466
}
6567
} catch (error) {
6668
throw new Error(error);

0 commit comments

Comments
 (0)