Merge rnpm cli into react-native

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 https://github.com/facebook/react-native/pull/7899

Differential Revision: D3613193

Pulled By: bestander

fbshipit-source-id: 09a072f3b21e5239dfcd8da88a205bd28dc5d037
This commit is contained in:
Mike Grabowski
2016-08-01 07:13:49 +02:00
parent f5bdd52ddc
commit 86cbaedb7a
127 changed files with 1160 additions and 1390 deletions
+44 -57
View File
@@ -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 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;
+13 -9
View File
@@ -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,
};
+19 -28
View File
@@ -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,
},
];
+3 -3
View File
@@ -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');
+2 -2
View File
@@ -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({
@@ -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');
@@ -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');
+9 -3
View File
@@ -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,
};