feat(create-mcfly): handle cancel event
This commit is contained in:
parent
2e79b4cc7f
commit
47edf0408d
3 changed files with 41 additions and 26 deletions
2
package-lock.json
generated
2
package-lock.json
generated
|
@ -4017,7 +4017,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"packages/create-mcfly": {
|
"packages/create-mcfly": {
|
||||||
"version": "0.3.7",
|
"version": "0.3.12",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"consola": "^3.2.3",
|
"consola": "^3.2.3",
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { consola } from "consola";
|
||||||
import { colorize } from "consola/utils";
|
import { colorize } from "consola/utils";
|
||||||
import { downloadTemplate } from "giget";
|
import { downloadTemplate } from "giget";
|
||||||
import { spawnSync } from "node:child_process";
|
import { spawnSync } from "node:child_process";
|
||||||
import path from 'node:path';
|
import path from "node:path";
|
||||||
|
|
||||||
const [, , directoryArg] = process.argv;
|
const [, , directoryArg] = process.argv;
|
||||||
|
|
||||||
|
@ -37,8 +37,12 @@ async function create() {
|
||||||
consola.success(`Using ${directory} as name.`);
|
consola.success(`Using ${directory} as name.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const safeDirectory = getSafeDirectory(directory);
|
if (typeof directory !== "string") {
|
||||||
const hasErrors = await downloadTemplateToDirectory(safeDirectory);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
directory = getSafeDirectory(directory);
|
||||||
|
const hasErrors = await downloadTemplateToDirectory(directory);
|
||||||
|
|
||||||
if (!hasErrors) {
|
if (!hasErrors) {
|
||||||
/**
|
/**
|
||||||
|
@ -46,39 +50,46 @@ async function create() {
|
||||||
*/
|
*/
|
||||||
const prompts = [
|
const prompts = [
|
||||||
{
|
{
|
||||||
prompt: `Would you like to install the dependencies to ${safeDirectory}?`,
|
prompt: `Would you like to install the dependencies to ${colorize(
|
||||||
|
"bold",
|
||||||
|
directory
|
||||||
|
)}?`,
|
||||||
info: "This might take some time depending on your connectivity.",
|
info: "This might take some time depending on your connectivity.",
|
||||||
startMessage: "Installing dependencies using npm...",
|
startMessage: "Installing dependencies using npm...",
|
||||||
command: `npm`,
|
command: `npm`,
|
||||||
subCommand: 'install',
|
subCommand: "install",
|
||||||
error: `Install dependencies later with ${colorize("yellow", "npm install")}`
|
error: `Install dependencies later with ${colorize(
|
||||||
|
"yellow",
|
||||||
|
"npm install"
|
||||||
|
)}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
prompt: "Would you like to initialize your git repository?",
|
prompt: "Would you like to initialize your git repository?",
|
||||||
startMessage: "Initializing git repository...",
|
startMessage: "Initializing git repository...",
|
||||||
command: `git`,
|
command: `git`,
|
||||||
subCommand: 'init',
|
subCommand: "init",
|
||||||
error: 'Initialize git repository later with `git init`'
|
error: `Initialize git repository later with ${colorize(
|
||||||
|
"yellow",
|
||||||
|
"git init"
|
||||||
|
)}`,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const intentions = await askPrompts(prompts, safeDirectory);
|
const intentions = await askPrompts(prompts, directory);
|
||||||
showResults(safeDirectory, intentions[0]);
|
if (!!intentions && intentions.length > 0)
|
||||||
|
showResults(directory, intentions[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns string that is safe for commands
|
* Returns string that is safe for commands
|
||||||
* @param {string} directory
|
* @param {string} directory
|
||||||
* @returns string
|
* @returns string | undefined
|
||||||
*/
|
*/
|
||||||
function getSafeDirectory(directory) {
|
function getSafeDirectory(directory) {
|
||||||
|
|
||||||
const dir = /\s/.test(directory) ? `"${directory}"` : directory;
|
|
||||||
|
|
||||||
const { platform } = process;
|
const { platform } = process;
|
||||||
const locale = path[platform === `win32` ? `win32` : `posix`];
|
const locale = path[platform === `win32` ? `win32` : `posix`];
|
||||||
const localePath = dir.split(path.sep).join(locale.sep);
|
const localePath = directory.split(path.sep).join(locale.sep);
|
||||||
return path.normalize(localePath);
|
return path.normalize(localePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,9 +108,9 @@ async function downloadTemplateToDirectory(directory) {
|
||||||
await downloadTemplate("github:ayoayco/mcfly/templates/basic", {
|
await downloadTemplate("github:ayoayco/mcfly/templates/basic", {
|
||||||
dir: directory,
|
dir: directory,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (ㆆ_ㆆ) {
|
||||||
consola.error(e);
|
consola.error(ㆆ_ㆆ.message);
|
||||||
consola.info('Try a different name.')
|
consola.info("Try a different name.\n");
|
||||||
hasErrors = true;
|
hasErrors = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,7 +121,7 @@ async function downloadTemplateToDirectory(directory) {
|
||||||
*
|
*
|
||||||
* @param {Array<PromptAction>} prompts
|
* @param {Array<PromptAction>} prompts
|
||||||
* @param {string} cwd
|
* @param {string} cwd
|
||||||
* @returns Array<boolean>
|
* @returns Array<boolean> | undefined
|
||||||
*/
|
*/
|
||||||
async function askPrompts(prompts, cwd) {
|
async function askPrompts(prompts, cwd) {
|
||||||
const results = [];
|
const results = [];
|
||||||
|
@ -120,6 +131,10 @@ async function askPrompts(prompts, cwd) {
|
||||||
type: "confirm",
|
type: "confirm",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (typeof userIntends !== "boolean") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (userIntends) {
|
if (userIntends) {
|
||||||
p.info && consola.info(p.info);
|
p.info && consola.info(p.info);
|
||||||
consola.start(p.startMessage);
|
consola.start(p.startMessage);
|
||||||
|
@ -128,12 +143,12 @@ async function askPrompts(prompts, cwd) {
|
||||||
cwd,
|
cwd,
|
||||||
shell: true,
|
shell: true,
|
||||||
timeout: 100_000,
|
timeout: 100_000,
|
||||||
stdio: 'inherit'
|
stdio: "inherit",
|
||||||
})
|
});
|
||||||
consola.success("Done!");
|
consola.success("Done!");
|
||||||
} catch (e) {
|
} catch (ㆆ_ㆆ) {
|
||||||
consola.error(e);
|
consola.error(ㆆ_ㆆ.message);
|
||||||
consola.info(p.error);
|
consola.info(p.error + "\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
results.push(userIntends);
|
results.push(userIntends);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "create-mcfly",
|
"name": "create-mcfly",
|
||||||
"version": "0.3.7",
|
"version": "0.3.12",
|
||||||
"description": "Create a new McFly app",
|
"description": "Create a new McFly app",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"bin": {
|
"bin": {
|
||||||
|
|
Loading…
Reference in a new issue