Skip to content

Commit

Permalink
feat: Removes calls to process.pwd() in favor of using context.root (#…
Browse files Browse the repository at this point in the history
…116)

* feat: use ctx.root instead of process.cwd() in several commands

* test: upgrading patch version of jest to avoid worker system bug
  • Loading branch information
kpsroka authored and grabbou committed Jan 23, 2019
1 parent 7ca57a5 commit 1c18109
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 263 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"flow-bin": "^0.87.0",
"glob": "^7.1.3",
"husky": "^1.3.1",
"jest": "^24.0.0-alpha.12",
"jest": "^24.0.0-alpha.13",
"lerna": "^3.10.6",
"micromatch": "^3.1.10",
"mkdirp": "^0.5.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/install/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const spawnOpts = {

log.heading = 'rnpm-install';

function install(args) {
function install(args, ctx) {
const name = args[0];

let res = PackageManager.add(name);
let res = PackageManager.add(name, ctx.root);

if (res.status) {
process.exit(res.status);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/install/uninstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const spawnOpts = {

log.heading = 'rnpm-install';

function uninstall(args) {
function uninstall(args, ctx) {
const name = args[0];

let res = spawnSync('react-native', ['unlink', name], spawnOpts);
Expand All @@ -27,7 +27,7 @@ function uninstall(args) {
process.exit(res.status);
}

res = PackageManager.remove(name);
res = PackageManager.remove(name, ctx.root);

if (res.status) {
process.exit(res.status);
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/src/library/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ const walk = require('../util/walk');
/**
* Creates a new native library with the given name
*/
async function library(argv, config, args) {
async function library(argv, ctx, args) {
if (!isValidPackageName(args.name)) {
throw new Error(
`${args.name} is not a valid name for a project. Please use a valid ` +
`identifier name (alphanumeric).`
);
}

const root = process.cwd();
const libraries = path.resolve(root, 'Libraries');
const libraries = path.resolve(ctx.root, 'Libraries');
const libraryDest = path.resolve(libraries, args.name);
const source = path.resolve(
'node_modules',
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/link/getProjectDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const EXCLUDED_PROJECTS = [
* Returns an array of dependencies that should be linked/checked.
*/
module.exports = function getProjectDependencies(cwd) {
const pjson = require(path.join(cwd || process.cwd(), './package.json'));
const pjson = require(path.join(cwd, './package.json'));
return Object.keys(pjson.dependencies || {}).filter(
name => EXCLUDED_PROJECTS.includes(name) === false
);
Expand Down
12 changes: 5 additions & 7 deletions packages/cli/src/upgrade/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/

import type { ContextT } from '../core/types.flow';

const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
Expand All @@ -16,14 +19,9 @@ const copyProjectTemplateAndReplace = require('../generator/copyProjectTemplateA
/**
* Migrate application to a new version of React Native.
* See http://facebook.github.io/react-native/docs/upgrading.html
*
* IMPORTANT: Assumes the cwd() is the project directory.
* The code here must only be invoked via the CLI:
* $ cd MyAwesomeApp
* $ react-native upgrade
*/
function validateAndUpgrade() {
const projectDir = process.cwd();
function validateAndUpgrade(argv: *, ctx: ContextT) {
const projectDir = ctx.root;

const packageJSON = JSON.parse(
fs.readFileSync(path.resolve(projectDir, 'package.json'), 'utf8')
Expand Down
16 changes: 10 additions & 6 deletions packages/cli/src/util/PackageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ const spawnOpts = {
*
* @param {String} yarnCommand Yarn command to be executed eg. yarn add package
* @param {String} npmCommand Npm command to be executed eg. npm install package
* @param {string} projectDir Directory to run the command in
* @return {object} spawnSync's result object
*/
function callYarnOrNpm(yarnCommand, npmCommand) {
function callYarnOrNpm(yarnCommand, npmCommand, projectDir) {
let command;

const projectDir = process.cwd();
const isYarnAvailable =
yarn.getYarnVersionIfAvailable() && yarn.isGlobalCliUsingYarn(projectDir);

Expand All @@ -46,24 +46,28 @@ function callYarnOrNpm(yarnCommand, npmCommand) {
/**
* Install package into project using npm or yarn if available
* @param {[type]} packageName Package to be installed
* @param {string} projectDir Root directory of the project
* @return {[type]} spawnSync's result object
*/
function add(packageName) {
function add(packageName, projectDir) {
return callYarnOrNpm(
`yarn add ${packageName}`,
`npm install ${packageName} --save`
`npm install ${packageName} --save`,
projectDir
);
}

/**
* Uninstall package from project using npm or yarn if available
* @param {[type]} packageName Package to be uninstalled
* @param {string} projectDir Root directory of the project
* @return {Object} spawnSync's result object
*/
function remove(packageName) {
function remove(packageName, projectDir) {
return callYarnOrNpm(
`yarn remove ${packageName}`,
`npm uninstall --save ${packageName}`
`npm uninstall --save ${packageName}`,
projectDir
);
}

Expand Down
Loading

0 comments on commit 1c18109

Please sign in to comment.