Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ESLint configuration #52

Merged
merged 12 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- attach_workspace:
at: ~/react-native-cli
- run: |
# yarn lint – disable temporarily until ESLint is added to the project
yarn lint
yarn flow-check
tests:
<<: *defaults
Expand Down
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/node_modules/**
**/debugger-ui/**
**/templates/**
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
],
"scripts": {
"test": "cd packages/local-cli && yarn jest; cd -",
"lint": "eslint packages",
"flow-check": "cd packages/local-cli && yarn flow check; cd -"
},
"devDependencies": {
"@callstack/eslint-config": "^3.0.2",
"eslint": "^5.10.0"
},
"eslintConfig": {
"extends": "@callstack",
"rules": {
"global-require": 0,
"no-console": 0
}
}
}
137 changes: 66 additions & 71 deletions packages/global-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,12 @@
// /!\ DO NOT MODIFY THIS FILE /!\
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

'use strict';
grabbou marked this conversation as resolved.
Show resolved Hide resolved

var fs = require('fs');
var path = require('path');
var exec = require('child_process').exec;
var execSync = require('child_process').execSync;
var chalk = require('chalk');
var prompt = require('prompt');
var semver = require('semver');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const chalk = require('chalk');
const prompt = require('prompt');
const semver = require('semver');
/**
* Used arguments:
* -v --version - to print current version of react-native-cli and react-native dependency
Expand All @@ -59,29 +56,29 @@ var semver = require('semver');
* - "/Users/home/react-native/react-native-0.22.0.tgz" - for package prepared with `npm pack`, useful for e2e tests
*/

var options = require('minimist')(process.argv.slice(2));
const cliOptions = require('minimist')(process.argv.slice(2));

var CLI_MODULE_PATH = function() {
const CLI_MODULE_PATH = function getCLIModulePath() {
return path.resolve(process.cwd(), 'node_modules', 'react-native', 'cli.js');
};

var REACT_NATIVE_PACKAGE_JSON_PATH = function() {
const REACT_NATIVE_PACKAGE_JSON_PATH = function getReactNativePath() {
return path.resolve(
process.cwd(),
'node_modules',
'react-native',
'package.json',
'package.json'
);
};

if (options._.length === 0 && (options.v || options.version)) {
if (cliOptions._.length === 0 && (cliOptions.v || cliOptions.version)) {
printVersionsAndExit(REACT_NATIVE_PACKAGE_JSON_PATH());
}

// Use Yarn if available, it's much faster than the npm client.
// Return the version of yarn installed on the system, null if yarn is not available.
function getYarnVersionIfAvailable() {
var yarnVersion;
let yarnVersion;
try {
// execSync returns a Buffer -> convert to string
if (process.platform.startsWith('win')) {
Expand All @@ -98,26 +95,25 @@ function getYarnVersionIfAvailable() {
try {
if (semver.gte(yarnVersion, '0.16.0')) {
return yarnVersion;
} else {
return null;
}
return null;
} catch (error) {
console.error('Cannot parse yarn version: ' + yarnVersion);
console.error(`Cannot parse yarn version: ${yarnVersion}`);
return null;
}
}

var cli;
var cliPath = CLI_MODULE_PATH();
let cli;
const cliPath = CLI_MODULE_PATH();
if (fs.existsSync(cliPath)) {
cli = require(cliPath);
}

var commands = options._;
const commands = cliOptions._;
if (cli) {
cli.run();
} else {
if (options._.length === 0 && (options.h || options.help)) {
if (cliOptions._.length === 0 && (cliOptions.h || cliOptions.help)) {
console.log(
[
'',
Expand All @@ -134,14 +130,14 @@ if (cli) {
' -v, --version use a specific version of React Native',
' --template use an app template. Use --template to see available templates.',
'',
].join('\n'),
].join('\n')
);
process.exit(0);
}

if (commands.length === 0) {
console.error(
'You did not pass any commands, run `react-native --help` to see a list of all available commands.',
'You did not pass any commands, run `react-native --help` to see a list of all available commands.'
);
process.exit(1);
}
Expand All @@ -152,14 +148,14 @@ if (cli) {
console.error('Usage: react-native init <ProjectName> [--verbose]');
process.exit(1);
} else {
init(commands[1], options);
init(commands[1], cliOptions);
}
break;
default:
console.error(
'Command `%s` unrecognized. ' +
'Make sure that you have run `npm install` and that you are inside a react-native project.',
commands[0],
commands[0]
);
process.exit(1);
break;
Expand All @@ -171,7 +167,7 @@ function validateProjectName(name) {
console.error(
'"%s" is not a valid name for a project. Please use a valid identifier ' +
'name (alphanumeric).',
name,
name
);
process.exit(1);
}
Expand All @@ -180,7 +176,7 @@ function validateProjectName(name) {
console.error(
'"%s" is not a valid name for a project. Please do not use the ' +
'reserved word "React".',
name,
name
);
process.exit(1);
}
Expand All @@ -206,15 +202,15 @@ function init(name, options) {
function createAfterConfirmation(name, options) {
prompt.start();

var property = {
const property = {
name: 'yesno',
message: 'Directory ' + name + ' already exists. Continue?',
message: `Directory ${name} already exists. Continue?`,
validator: /y[es]*|n[o]?/,
warning: 'Must respond yes or no',
default: 'no',
};

prompt.get(property, function(err, result) {
prompt.get(property, (err, result) => {
if (result.yesno[0] === 'y') {
createProject(name, options);
} else {
Expand All @@ -225,19 +221,19 @@ function createAfterConfirmation(name, options) {
}

function createProject(name, options) {
var root = path.resolve(name);
var projectName = path.basename(root);
const root = path.resolve(name);
const projectName = path.basename(root);

console.log(
'This will walk you through creating a new React Native project in',
root,
root
);

if (!fs.existsSync(root)) {
fs.mkdirSync(root);
}

var packageJson = {
const packageJson = {
name: projectName,
version: '0.0.1',
private: true,
Expand All @@ -249,18 +245,18 @@ function createProject(name, options) {
};
fs.writeFileSync(
path.join(root, 'package.json'),
JSON.stringify(packageJson),
JSON.stringify(packageJson)
);
process.chdir(root);

run(root, projectName, options);
}

function getInstallPackage(rnPackage) {
var packageToInstall = 'react-native';
var isValidSemver = semver.valid(rnPackage);
let packageToInstall = 'react-native';
const isValidSemver = semver.valid(rnPackage);
if (isValidSemver) {
packageToInstall += '@' + isValidSemver;
packageToInstall += `@${isValidSemver}`;
} else if (rnPackage) {
// for tar.gz or alternative paths
packageToInstall = rnPackage;
Expand All @@ -269,41 +265,40 @@ function getInstallPackage(rnPackage) {
}

function run(root, projectName, options) {
var rnPackage = options.version; // e.g. '0.38' or '/path/to/archive.tgz'
var forceNpmClient = options.npm;
var yarnVersion = !forceNpmClient && getYarnVersionIfAvailable();
var installCommand;
const rnPackage = options.version; // e.g. '0.38' or '/path/to/archive.tgz'
const forceNpmClient = options.npm;
const yarnVersion = !forceNpmClient && getYarnVersionIfAvailable();
let installCommand;
if (options.installCommand) {
// In CI environments it can be useful to provide a custom command,
// to set up and use an offline mirror for installing dependencies, for example.
installCommand = options.installCommand;
installCommand = cliOptions.installCommand; // eslint-disable-line prefer-destructuring
} else if (yarnVersion) {
console.log(`Using yarn v${yarnVersion}`);
console.log(`Installing ${getInstallPackage(rnPackage)}...`);
installCommand = `yarn add ${getInstallPackage(rnPackage)} --exact`;
if (options.verbose) {
installCommand += ' --verbose';
}
} else {
if (yarnVersion) {
console.log('Using yarn v' + yarnVersion);
console.log('Installing ' + getInstallPackage(rnPackage) + '...');
installCommand = 'yarn add ' + getInstallPackage(rnPackage) + ' --exact';
if (options.verbose) {
installCommand += ' --verbose';
}
} else {
console.log('Installing ' + getInstallPackage(rnPackage) + '...');
if (!forceNpmClient) {
console.log(
'Consider installing yarn to make this faster: https://yarnpkg.com',
);
}
installCommand =
'npm install --save --save-exact ' + getInstallPackage(rnPackage);
if (options.verbose) {
installCommand += ' --verbose';
}
console.log(`Installing ${getInstallPackage(rnPackage)}...`);
if (!forceNpmClient) {
console.log(
'Consider installing yarn to make this faster: https://yarnpkg.com'
);
}
installCommand = `npm install --save --save-exact ${getInstallPackage(
rnPackage
)}`;
if (options.verbose) {
installCommand += ' --verbose';
}
}
try {
execSync(installCommand, {stdio: 'inherit'});
execSync(installCommand, { stdio: 'inherit' });
} catch (err) {
console.error(err);
console.error('Command `' + installCommand + '` failed.');
console.error(`Command \`${installCommand}\` failed.`);
process.exit(1);
}
checkNodeVersion();
Expand All @@ -312,7 +307,7 @@ function run(root, projectName, options) {
}

function checkNodeVersion() {
var packageJson = require(REACT_NATIVE_PACKAGE_JSON_PATH());
const packageJson = require(REACT_NATIVE_PACKAGE_JSON_PATH());
if (!packageJson.engines || !packageJson.engines.node) {
return;
}
Expand All @@ -321,21 +316,21 @@ function checkNodeVersion() {
chalk.red(
'You are currently running Node %s but React Native requires %s. ' +
'Please use a supported version of Node.\n' +
'See https://facebook.github.io/react-native/docs/getting-started.html',
'See https://facebook.github.io/react-native/docs/getting-started.html'
),
process.version,
packageJson.engines.node,
packageJson.engines.node
);
}
}

function printVersionsAndExit(reactNativePackageJsonPath) {
console.log('react-native-cli: ' + require('./package.json').version);
console.log(`react-native-cli: ${require('./package.json').version}`);
try {
console.log('react-native: ' + require(reactNativePackageJsonPath).version);
console.log(`react-native: ${require(reactNativePackageJsonPath).version}`);
} catch (e) {
console.log(
'react-native: n/a - not inside a React Native project directory',
'react-native: n/a - not inside a React Native project directory'
);
}
process.exit();
Expand Down
4 changes: 1 addition & 3 deletions packages/local-cli/__mocks__/beeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
* @format
*/

'use strict';

// beeper@1.1.0 has a return statement outside of a function
// and therefore doesn't parse. Let's mock it so that we can
// run the tests.
module.exports = function() {};
module.exports = function beeper() {};
Loading