Skip to content

Commit

Permalink
feat: skip bumping yarn version if project is inside git work tree
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed Nov 6, 2023
1 parent 1eaeabd commit c565b3f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 30 deletions.
10 changes: 2 additions & 8 deletions jest/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,16 @@ function getExecaOptions(options: SpawnOptions) {

const cwd = isRelative ? path.resolve(__dirname, options.cwd) : options.cwd;

let env = Object.assign({}, process.env, {FORCE_COLOR: '0'});
let env = Object.assign({}, process.env, {FORCE_COLOR: '0'}, options.env);

if (options.nodeOptions) {
env.NODE_OPTIONS = options.nodeOptions;
}

if (options.nodePath) {
env.NODE_PATH = options.nodePath;
}

if (options.env) {
env = {
...env,
...options.env,
};
}

return {
cwd,
env,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@ import execa from 'execa';
import fs from 'fs';
import path from 'path';

const createGitRepository = async (folder: string) => {
const loader = getLoader();

export const checkGitInstallation = async (): Promise<boolean> => {
try {
await execa('git', ['--version'], {stdio: 'ignore'});
return true;
} catch {
loader.fail('Unable to initialize Git repo. `git` not in $PATH.');
return;
return false;
}
};

export const checkIfFolderIsGitRepo = async (
folder: string,
): Promise<boolean> => {
try {
await execa('git', ['rev-parse', '--is-inside-work-tree'], {
stdio: 'ignore',
cwd: folder,
});
loader.succeed(
'New project is already inside of a Git repo, skipping git init.',
);
return;
} catch {}
return true;
} catch {
return false;
}
};

export const createGitRepository = async (folder: string) => {
const loader = getLoader();

loader.start('Initializing Git repository');

Expand Down Expand Up @@ -62,5 +67,3 @@ const createGitRepository = async (folder: string) => {
logger.debug(e as string);
}
};

export default createGitRepository;
53 changes: 43 additions & 10 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ import {getBunVersionIfAvailable} from '../../tools/bun';
import {getNpmVersionIfAvailable} from '../../tools/npm';
import {getYarnVersionIfAvailable} from '../../tools/yarn';
import {createHash} from 'crypto';
import createGitRepository from './createGitRepository';
import {
createGitRepository,
checkGitInstallation,
checkIfFolderIsGitRepo,
} from './git';
import deepmerge from 'deepmerge';
import semver from 'semver';
import {executeCommand} from '../../tools/executeCommand';
Expand All @@ -50,6 +54,7 @@ type Options = {

interface TemplateOptions {
projectName: string;
shouldBumpYarnVersion: boolean;
templateUri: string;
npm?: boolean;
pm?: PackageManager.PackageManager;
Expand Down Expand Up @@ -90,12 +95,7 @@ function doesDirectoryExist(dir: string) {
}

async function setProjectDirectory(directory: string) {
if (doesDirectoryExist(directory)) {
throw new DirectoryAlreadyExistsError(directory);
}

try {
fs.mkdirSync(directory, {recursive: true});
process.chdir(directory);
} catch (error) {
throw new CLIError(
Expand Down Expand Up @@ -129,6 +129,7 @@ function setEmptyHashForCachedDependencies(projectName: string) {

async function createFromTemplate({
projectName,
shouldBumpYarnVersion,
templateUri,
npm,
pm,
Expand Down Expand Up @@ -199,7 +200,7 @@ async function createFromTemplate({

createDefaultConfigFile(projectDirectory, loader);

if (packageManager === 'yarn') {
if (packageManager === 'yarn' && shouldBumpYarnVersion) {
await bumpYarnVersion(false, projectDirectory);
}

Expand Down Expand Up @@ -363,12 +364,14 @@ async function createProject(
projectName: string,
directory: string,
version: string,
shouldBumpYarnVersion: boolean,
options: Options,
) {
const templateUri = createTemplateUri(options, version);

return createFromTemplate({
projectName,
shouldBumpYarnVersion,
templateUri,
npm: options.npm,
pm: options.pm,
Expand Down Expand Up @@ -420,6 +423,7 @@ export default (async function initialize(
const root = process.cwd();
const version = options.version || DEFAULT_VERSION;
const directoryName = path.relative(root, options.directory || projectName);
const projectFolder = path.join(root, directoryName);

if (options.pm && !checkPackageManagerAvailability(options.pm)) {
logger.error(
Expand All @@ -428,10 +432,39 @@ export default (async function initialize(
return;
}

await createProject(projectName, directoryName, version, options);
if (doesDirectoryExist(projectFolder)) {
throw new DirectoryAlreadyExistsError(projectFolder);
} else {
fs.mkdirSync(projectFolder, {recursive: true});
}

let shouldBumpYarnVersion = true;
let shouldCreateGitRepository = false;

const projectFolder = path.join(root, directoryName);
const isGitAvailable = await checkGitInstallation();

if (isGitAvailable) {
const isFolderGitRepo = await checkIfFolderIsGitRepo(projectFolder);

if (isFolderGitRepo) {
shouldBumpYarnVersion = false;
} else {
shouldCreateGitRepository = true; // Initialize git repo after creating project
}
} else {
logger.warn(
'Git is not installed on your system. This might cause some features to work incorrectly.',
);
}

await createProject(
projectName,
directoryName,
version,
shouldBumpYarnVersion,
options,
);

await createGitRepository(projectFolder);
shouldCreateGitRepository && (await createGitRepository(projectFolder));
printRunInstructions(projectFolder, projectName);
});

0 comments on commit c565b3f

Please sign in to comment.