From 5f304a36e5523ae953add65bad0dba1fa6dc07dd Mon Sep 17 00:00:00 2001 From: aleclarson Date: Sat, 29 Dec 2018 18:32:06 -0500 Subject: [PATCH] feat: improved yarn support Any commands with a --yarn flag will look for a "yarn.lock" file in the working directory. When a lockfile exists, the --yarn flag will be true by default. Other fixes: - actually use argv.yarn for the "link" command - actually use the `getPackageManager` function's argument - remove unused `showVersion` function --- src/index.ts | 10 ++++------ src/yalc.ts | 15 +++++++++------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index fc3c269..25c8c0c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,9 @@ import * as fs from 'fs-extra' -import * as path from 'path' +import { join } from 'path' import { PackageName } from './installations' const userHome = require('user-home') -const { join } = path - export const values = { myNameIs: 'yalc', ignoreFileName: '.yalcignore', @@ -56,7 +54,7 @@ export function getStorePackagesDir(): string { } export const getPackageStoreDir = (packageName: string, version = '') => - path.join(getStorePackagesDir(), packageName, version) + join(getStorePackagesDir(), packageName, version) export type PackageScripts = Partial<{ preinstall: string @@ -83,8 +81,8 @@ export interface PackageManifest { __JSONSpaces: number } -export const getPackageManager = (workingDir: string) => - fs.existsSync('yarn.lock') ? 'yarn' : 'npm' +export const getPackageManager = (cwd: string) => + fs.existsSync(join(cwd, 'yarn.lock')) ? 'yarn' : 'npm' export const execLoudOptions = { stdio: 'inherit' } diff --git a/src/yalc.ts b/src/yalc.ts index 7a40a39..a582f02 100644 --- a/src/yalc.ts +++ b/src/yalc.ts @@ -7,7 +7,8 @@ import { addPackages, updatePackages, removePackages, - getStoreMainDir + getStoreMainDir, + getPackageManager } from '.' import { showInstallations, cleanInstallations } from './installations' @@ -23,9 +24,8 @@ const getVersionMessage = () => { return pkg.version } -const showVersion = () => { - console.log(getVersionMessage()) -} +const isYarn = (cwd: string = process.cwd()) => + getPackageManager(cwd) === 'yarn' /* tslint:disable-next-line */ yargs @@ -53,6 +53,7 @@ yargs builder: () => { return yargs .default('sig', true) + .default('yarn', isYarn()) .boolean(['push', 'push-safe'].concat(publishFlags)) }, handler: argv => { @@ -100,6 +101,7 @@ yargs return yargs .default('force', undefined) .default('sig', true) + .default('yarn', isYarn()) .boolean(['safe'].concat(publishFlags)) }, handler: argv => { @@ -122,7 +124,7 @@ yargs describe: 'Add package from yalc repo to the project', builder: () => { return yargs - .default('yarn', false) + .default('yarn', isYarn()) .boolean(['file', 'dev', 'save-dev', 'link', 'yarn', 'pure']) .help(true) }, @@ -144,11 +146,12 @@ yargs command: 'link', describe: 'Link package from yalc repo to the project', builder: () => { - return yargs.default('yarn', true).help(true) + return yargs.default('yarn', isYarn()).help(true) }, handler: argv => { return addPackages(argv._.slice(1), { link: true, + yarn: argv.yarn, workingDir: process.cwd() }) }