* Check & prepare destination folder for extracting.
(cwd: string, folder: string, force?: boolean)
| 178 | * Check & prepare destination folder for extracting. |
| 179 | */ |
| 180 | function prepareFolder(cwd: string, folder: string, force?: boolean) { |
| 181 | const dest = path.join(cwd, folder); |
| 182 | |
| 183 | if (fs.existsSync(dest)) { |
| 184 | if (!fs.lstatSync(dest).isDirectory()) { |
| 185 | throw new Error( |
| 186 | `Destination path "${chalk.bold( |
| 187 | folder |
| 188 | )}" already exists and is not a directory.` |
| 189 | ); |
| 190 | } |
| 191 | if (!force && fs.readdirSync(dest).length !== 0) { |
| 192 | throw new Error( |
| 193 | `Destination path "${chalk.bold( |
| 194 | folder |
| 195 | )}" already exists and is not an empty directory. You may use ${cmd( |
| 196 | '--force' |
| 197 | )} or ${cmd('-f')} to override it.` |
| 198 | ); |
| 199 | } |
| 200 | } else if (dest !== cwd) { |
| 201 | try { |
| 202 | fs.mkdirSync(dest); |
| 203 | } catch (_e) { |
| 204 | throw new Error(`Could not create directory "${chalk.bold(folder)}".`); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return dest; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Guess which example user try to init |
no test coverage detected