Skip to content

Commit

Permalink
Merge rnpm cli into react-native
Browse files Browse the repository at this point in the history
Summary:
This is an initial step of rewriting the CLI interface to use `rnpm` one (commander, plugins etc.).

It's scope is to move all existing commands to use rnpm CLI interface, so that we get plugins, flags and our existing ecosystem working out of the box.

<s>This is still WIP and some of the commands are left commented out.</s>

For the `config` of `rnpm` (functions get info about project and dependency), <s>I am thinking we can merge them with</s> we decided to merge it with [`default.config.js`](https://github.com/grabbou/react-native/blob/e57683e420210749a5a6b802b4e70adb69675786/local-cli/default.config.js#L33), so they are available on the `new Config()` [instance](https://github.com/grabbou/react-native/blob/e57683e420210749a5a6b802b4e70adb69675786/local-cli/cliEntry.js#L59) (which means we don't have to change anything and current plugins, like runIOS and runAndroid can just start using it [w/o depending on any extra argument](https://github.com/grabbou/react-native/blob/e57683e420210749a5a6b802b4e
Closes #7899

Differential Revision: D3613193

Pulled By: bestander

fbshipit-source-id: 09a072f3b21e5239dfcd8da88a205bd28dc5d037
  • Loading branch information
grabbou authored and Facebook Github Bot committed Jul 30, 2016
1 parent a37d5a8 commit e8b5081
Show file tree
Hide file tree
Showing 127 changed files with 1,151 additions and 1,382 deletions.
1 change: 1 addition & 0 deletions jestSupport/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ global.regeneratorRuntime = require.requireActual('regenerator-runtime/runtime')
jest
.mock('ensureComponentIsNative')
.mock('Image')
.mock('npmlog')
.mock('NativeModules')
.mock('Text')
.mock('View');
Expand Down
101 changes: 44 additions & 57 deletions local-cli/bundle/buildBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,69 +21,56 @@ function saveBundle(output, bundle, args) {
}

function buildBundle(args, config, output = outputBundle, packagerInstance) {
return new Promise((resolve, reject) => {
// This is used by a bazillion of npm modules we don't control so we don't
// have other choice than defining it as an env variable here.
process.env.NODE_ENV = args.dev ? 'development' : 'production';

This comment has been minimized.

Copy link
@xsdlr

xsdlr Aug 16, 2016


// This is used by a bazillion of npm modules we don't control so we don't
// have other choice than defining it as an env variable here.
if (!process.env.NODE_ENV) {
// If you're inlining environment variables, you can use babel to remove
// this line:
// https://www.npmjs.com/package/babel-remove-process-env-assignment
process.env.NODE_ENV = args.dev ? 'development' : 'production';
}
const options = {
projectRoots: config.getProjectRoots(),
assetRoots: config.getAssetRoots(),
blacklistRE: config.getBlacklistRE(args.platform),
getTransformOptionsModulePath: config.getTransformOptionsModulePath,
transformModulePath: args.transformer,
extraNodeModules: config.extraNodeModules,
nonPersistent: true,
resetCache: args.resetCache,
};

const transformModulePath =
args.transformer ? path.resolve(args.transformer) :
typeof config.getTransformModulePath === 'function' ? config.getTransformModulePath() :
undefined;
const requestOpts = {
entryFile: args.entryFile,
sourceMapUrl: args.sourcemapOutput,
dev: args.dev,
minify: !args.dev,
platform: args.platform,
};

const options = {
projectRoots: config.getProjectRoots(),
assetRoots: config.getAssetRoots(),
blacklistRE: config.getBlacklistRE(args.platform),
getTransformOptionsModulePath: config.getTransformOptionsModulePath,
transformModulePath: transformModulePath,
extraNodeModules: config.extraNodeModules,
nonPersistent: true,
resetCache: args['reset-cache'],
};
// If a packager instance was not provided, then just create one for this
// bundle command and close it down afterwards.
var shouldClosePackager = false;
if (!packagerInstance) {
packagerInstance = new Server(options);
shouldClosePackager = true;
}

const requestOpts = {
entryFile: args['entry-file'],
sourceMapUrl: args['sourcemap-output'],
dev: args.dev,
minify: !args.dev,
platform: args.platform,
};
const bundlePromise = output.build(packagerInstance, requestOpts)
.then(bundle => {
if (shouldClosePackager) {
packagerInstance.end();
}
return saveBundle(output, bundle, args);
});

// If a packager instance was not provided, then just create one for this
// bundle command and close it down afterwards.
var shouldClosePackager = false;
if (!packagerInstance) {
packagerInstance = new Server(options);
shouldClosePackager = true;
}
// Save the assets of the bundle
const assets = bundlePromise
.then(bundle => bundle.getAssets())
.then(outputAssets => saveAssets(
outputAssets,
args.platform,
args.assetsDest,
));

const bundlePromise = output.build(packagerInstance, requestOpts)
.then(bundle => {
if (shouldClosePackager) {
packagerInstance.end();
}
return saveBundle(output, bundle, args);
});

// Save the assets of the bundle
const assets = bundlePromise
.then(bundle => bundle.getAssets())
.then(outputAssets => saveAssets(
outputAssets,
args.platform,
args['assets-dest']
));

// When we're done saving bundle output and the assets, we're done.
resolve(assets);
});
// When we're done saving bundle output and the assets, we're done.
return assets;
}

module.exports = buildBundle;
22 changes: 13 additions & 9 deletions local-cli/bundle/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,30 @@
*/

const buildBundle = require('./buildBundle');
const bundleCommandLineArgs = require('./bundleCommandLineArgs');
const parseCommandLine = require('../util/parseCommandLine');
const outputBundle = require('./output/bundle');
const outputPrepack = require('./output/prepack');
const bundleCommandLineArgs = require('./bundleCommandLineArgs');

/**
* Builds the bundle starting to look for dependencies at the given entry path.
*/
function bundleWithOutput(argv, config, output, packagerInstance) {
const args = parseCommandLine(bundleCommandLineArgs, argv);
function bundleWithOutput(argv, config, args, output, packagerInstance) {
if (!output) {
output = args.prepack ? outputPrepack : outputBundle;
}
return buildBundle(args, config, output, packagerInstance);

}

function bundle(argv, config, packagerInstance) {
return bundleWithOutput(argv, config, undefined, packagerInstance);
function bundle(argv, config, args, packagerInstance) {
return bundleWithOutput(argv, config, args, undefined, packagerInstance);
}

module.exports = bundle;
module.exports.withOutput = bundleWithOutput;
module.exports = {
name: 'bundle',
description: 'builds the javascript bundle for offline use',
func: bundle,
options: bundleCommandLineArgs,

// not used by the CLI itself
withOutput: bundleWithOutput,
};
47 changes: 19 additions & 28 deletions local-cli/bundle/bundleCommandLineArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,47 @@

module.exports = [
{
command: 'entry-file',
command: '--entry-file <path>',
description: 'Path to the root JS file, either absolute or relative to JS root',
type: 'string',
required: true,
}, {
command: 'platform',
command: '--platform [string]',
description: 'Either "ios" or "android"',
type: 'string',
default: 'ios',
}, {
command: 'transformer',
description: 'Specify a custom transformer to be used',
type: 'string',
default: null,
command: '--transformer [string]',
description: 'Specify a custom transformer to be used (absolute path)',
default: require.resolve('../../packager/transformer'),
}, {
command: 'dev',
command: '--dev [boolean]',
description: 'If false, warnings are disabled and the bundle is minified',
parse: (val) => val === 'false' ? false : true,
default: true,
}, {
command: 'prepack',
description: 'If true, the output bundle will use the Prepack format.',
default: false
command: '--prepack',
description: 'When passed, the output bundle will use the Prepack format.',
}, {
command: 'bridge-config',
command: '--bridge-config [string]',
description: 'File name of a a JSON export of __fbBatchedBridgeConfig. Used by Prepack. Ex. ./bridgeconfig.json',
type: 'string'
}, {
command: 'bundle-output',
command: '--bundle-output <string>',
description: 'File name where to store the resulting bundle, ex. /tmp/groups.bundle',
type: 'string',
required: true,
}, {
command: 'bundle-encoding',
command: '--bundle-encoding [string]',
description: 'Encoding the bundle should be written in (https://nodejs.org/api/buffer.html#buffer_buffer).',
type: 'string',
default: 'utf8',
}, {
command: 'sourcemap-output',
command: '--sourcemap-output [string]',
description: 'File name where to store the sourcemap file for resulting bundle, ex. /tmp/groups.map',
type: 'string',
}, {
command: 'assets-dest',
command: '--assets-dest [string]',
description: 'Directory name where to store assets referenced in the bundle',
type: 'string',
}, {
command: 'verbose',
command: '--verbose',
description: 'Enables logging',
default: false,
}, {
command: 'reset-cache',
command: '--reset-cache',
description: 'Removes cached files',
default: false
}
default: false,
},
];
6 changes: 3 additions & 3 deletions local-cli/bundle/output/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ function createCodeWithMap(bundle, dev) {

function saveBundleAndMap(bundle, options, log) {
const {
'bundle-output': bundleOutput,
'bundle-encoding': encoding,
bundleOutput,
bundleEncoding: encoding,
dev,
'sourcemap-output': sourcemapOutput,
sourcemapOutput
} = options;

log('start');
Expand Down
4 changes: 2 additions & 2 deletions local-cli/bundle/output/prepack.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function buildPrepackBundle(packagerClient, requestOptions) {

function savePrepackBundle(bundle, options, log) {
const {
'bundle-output': bundleOutput,
'bridge-config': bridgeConfig,
bundleOutput,
bridgeConfig,
} = options;

const result = bundle.build({
Expand Down
6 changes: 3 additions & 3 deletions local-cli/bundle/output/unbundle/as-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ const MODULES_DIR = 'js-modules';
*/
function saveAsAssets(bundle, options, log) {
const {
'bundle-output': bundleOutput,
'bundle-encoding': encoding,
'sourcemap-output': sourcemapOutput,
bundleOutput,
bundleEncoding: encoding,
sourcemapOutput
} = options;

log('start');
Expand Down
6 changes: 3 additions & 3 deletions local-cli/bundle/output/unbundle/as-indexed-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const SIZEOF_UINT32 = 4;
*/
function saveAsIndexedFile(bundle, options, log) {
const {
'bundle-output': bundleOutput,
'bundle-encoding': encoding,
'sourcemap-output': sourcemapOutput,
bundleOutput,
bundleEncoding: encoding,
sourcemapOutput
} = options;

log('start');
Expand Down
12 changes: 9 additions & 3 deletions local-cli/bundle/unbundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
*/

const bundleWithOutput = require('./bundle').withOutput;
const bundleCommandLineArgs = require('./bundleCommandLineArgs');
const outputUnbundle = require('./output/unbundle');

/**
* Builds the bundle starting to look for dependencies at the given entry path.
*/
function unbundle(argv, config, packagerInstance) {
return bundleWithOutput(argv, config, outputUnbundle, packagerInstance);
function unbundle(argv, config, args, packagerInstance) {
return bundleWithOutput(argv, config, args, outputUnbundle, packagerInstance);
}

module.exports = unbundle;
module.exports = {
name: 'unbundle',
description: 'builds javascript as "unbundle" for offline use',
func: unbundle,
options: bundleCommandLineArgs,
};
Loading

5 comments on commit e8b5081

@danielbraun
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@grabbou Something is broken with --reset-cache. It seems the cli bundle command no longer recognises it - it just prompts the available CLI commands.
It breaks bundling.

Try running:
node ./node_modules/react-native/local-cli/cli.js bundle --platform android --dev false --entry-file index.android.js --bundle-output main.jsbundle --reset-cache true

@danielbraun
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related issue: #9134

@danielbraun
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the issue is that --reset-cache no longer accept a boolean parameter.

@danielbraun
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've submitted a PR: #9163

@xsdlr
Copy link

@xsdlr xsdlr commented on e8b5081 Aug 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at local-cli/bundle/buildBundle.js function buildBundle #8057

Please sign in to comment.