mirror of
https://github.com/status-im/react-native.git
synced 2025-01-24 00:09:08 +00:00
e8b508144f
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`](e57683e420/local-cli/default.config.js (L33)
), so they are available on the `new Config()` [instance](e57683e420/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
88 lines
2.8 KiB
JavaScript
88 lines
2.8 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
'use strict';
|
|
|
|
const mkdirp = require('mkdirp');
|
|
const path = require('path');
|
|
const Promise = require('promise');
|
|
|
|
const buildSourceMapWithMetaData = require('./build-unbundle-sourcemap-with-metadata');
|
|
const writeFile = require('../writeFile');
|
|
const writeSourceMap = require('./write-sourcemap');
|
|
const MAGIC_UNBUNDLE_NUMBER = require('./magic-number');
|
|
const {joinModules} = require('./util');
|
|
|
|
const MAGIC_UNBUNDLE_FILENAME = 'UNBUNDLE'; // must not start with a dot, as that won't go into the apk
|
|
const MODULES_DIR = 'js-modules';
|
|
|
|
/**
|
|
* Saves all JS modules of an app as single files
|
|
* The startup code (prelude, polyfills etc.) are written to the file
|
|
* designated by the `bundleOuput` option.
|
|
* All other modules go into a 'js-modules' folder that in the same parent
|
|
* directory as the startup file.
|
|
*/
|
|
function saveAsAssets(bundle, options, log) {
|
|
const {
|
|
bundleOutput,
|
|
bundleEncoding: encoding,
|
|
sourcemapOutput
|
|
} = options;
|
|
|
|
log('start');
|
|
const {startupModules, lazyModules} = bundle.getUnbundle();
|
|
log('finish');
|
|
const startupCode = joinModules(startupModules);
|
|
|
|
log('Writing bundle output to:', bundleOutput);
|
|
const modulesDir = path.join(path.dirname(bundleOutput), MODULES_DIR);
|
|
const writeUnbundle =
|
|
createDir(modulesDir).then( // create the modules directory first
|
|
() => Promise.all([
|
|
writeModules(lazyModules, modulesDir, encoding),
|
|
writeFile(bundleOutput, startupCode, encoding),
|
|
writeMagicFlagFile(modulesDir),
|
|
])
|
|
);
|
|
writeUnbundle.then(() => log('Done writing unbundle output'));
|
|
|
|
const sourceMap =
|
|
buildSourceMapWithMetaData({startupModules, lazyModules});
|
|
|
|
return Promise.all([
|
|
writeUnbundle,
|
|
writeSourceMap(sourcemapOutput, JSON.stringify(sourceMap), log)
|
|
]);
|
|
}
|
|
|
|
function createDir(dirName) {
|
|
return new Promise((resolve, reject) =>
|
|
mkdirp(dirName, error => error ? reject(error) : resolve()));
|
|
}
|
|
|
|
function writeModuleFile(module, modulesDir, encoding) {
|
|
const {code, id} = module;
|
|
return writeFile(path.join(modulesDir, id + '.js'), code, encoding);
|
|
}
|
|
|
|
function writeModules(modules, modulesDir, encoding) {
|
|
const writeFiles =
|
|
modules.map(module => writeModuleFile(module, modulesDir, encoding));
|
|
return Promise.all(writeFiles);
|
|
}
|
|
|
|
function writeMagicFlagFile(outputDir) {
|
|
/* global Buffer: true */
|
|
const buffer = Buffer(4);
|
|
buffer.writeUInt32LE(MAGIC_UNBUNDLE_NUMBER);
|
|
return writeFile(path.join(outputDir, MAGIC_UNBUNDLE_FILENAME), buffer);
|
|
}
|
|
|
|
module.exports = saveAsAssets;
|