Skip to content

Commit

Permalink
feat(new): support to skip install
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Dec 20, 2021
1 parent 344ced1 commit d6bd867
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
57 changes: 26 additions & 31 deletions actions/new.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,42 +141,35 @@ const installPackages = async (
(option) => option.name === 'package-manager',
)!.value as string;

let packageManager: AbstractPackageManager;
if (dryRunMode) {
console.info();
console.info(chalk.green(MESSAGES.DRY_RUN_MODE));
console.info();
return;
}
if (inputPackageManager !== undefined) {
try {
packageManager = PackageManagerFactory.create(inputPackageManager);
await packageManager.install(installDirectory, inputPackageManager);
} catch (error) {
if (error && error.message) {
console.error(chalk.red(error.message));
}
try {
const packageManagerName =
inputPackageManager || (await askForPackageManager())['package-manager'];
const packageManager: AbstractPackageManager | undefined =
packageManagerName === PackageManager.NONE
? undefined
: PackageManagerFactory.create(packageManagerName);
// will not install if user select `none`
await packageManager?.install(installDirectory, packageManagerName);
} catch (error) {
if (error && error.message) {
console.error(chalk.red(error.message));
}
} else {
packageManager = await selectPackageManager();
await packageManager.install(
installDirectory,
packageManager.name.toLowerCase(),
);
}
};

const selectPackageManager = async (): Promise<AbstractPackageManager> => {
const answers: Answers = await askForPackageManager();
return PackageManagerFactory.create(answers['package-manager']);
};

const askForPackageManager = async (): Promise<Answers> => {
const questions: Question[] = [
generateSelect('package-manager')(MESSAGES.PACKAGE_MANAGER_QUESTION)([
PackageManager.NPM,
PackageManager.YARN,
PackageManager.PNPM
PackageManager.PNPM,
PackageManager.NONE,
]),
];
const prompt = inquirer.createPromptModule();
Expand Down Expand Up @@ -225,16 +218,18 @@ const printCollective = () => {
emptyLine();
};

const print = (color: string | null = null) => (str = '') => {
const terminalCols = retrieveCols();
const strLength = str.replace(/\u001b\[[0-9]{2}m/g, '').length;
const leftPaddingLength = Math.floor((terminalCols - strLength) / 2);
const leftPadding = ' '.repeat(Math.max(leftPaddingLength, 0));
if (color) {
str = (chalk as any)[color](str);
}
console.log(leftPadding, str);
};
const print =
(color: string | null = null) =>
(str = '') => {
const terminalCols = retrieveCols();
const strLength = str.replace(/\u001b\[[0-9]{2}m/g, '').length;
const leftPaddingLength = Math.floor((terminalCols - strLength) / 2);
const leftPadding = ' '.repeat(Math.max(leftPaddingLength, 0));
if (color) {
str = (chalk as any)[color](str);
}
console.log(leftPadding, str);
};

export const retrieveCols = () => {
const defaultCols = 80;
Expand Down
3 changes: 2 additions & 1 deletion lib/package-managers/package-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum PackageManager {
NPM = 'npm',
YARN = 'yarn',
PNPM = 'pnpm'
PNPM = 'pnpm',
NONE = 'none (skip installing packages)',
}

0 comments on commit d6bd867

Please sign in to comment.