2015-10-20 16:53:54 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-08-04 11:50:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-09-10 00:32:20 +00:00
|
|
|
var fs = require('fs');
|
2015-08-04 11:50:22 +00:00
|
|
|
var path = require('path');
|
|
|
|
var yeoman = require('yeoman-generator');
|
2015-09-23 18:30:13 +00:00
|
|
|
var utils = require('../generator-utils');
|
2015-08-04 11:50:22 +00:00
|
|
|
|
|
|
|
module.exports = yeoman.generators.NamedBase.extend({
|
|
|
|
constructor: function() {
|
|
|
|
yeoman.generators.NamedBase.apply(this, arguments);
|
2015-09-14 14:35:58 +00:00
|
|
|
this.option('skip-ios', {
|
|
|
|
desc: 'Skip generating iOS files',
|
|
|
|
type: Boolean,
|
|
|
|
defaults: false
|
2015-08-04 11:50:22 +00:00
|
|
|
});
|
2015-09-14 14:35:58 +00:00
|
|
|
this.option('skip-android', {
|
|
|
|
desc: 'Skip generating Android files',
|
|
|
|
type: Boolean,
|
|
|
|
defaults: false
|
|
|
|
});
|
2016-09-10 00:32:20 +00:00
|
|
|
this.option('skip-jest', {
|
|
|
|
desc: 'Skip installing Jest',
|
|
|
|
type: Boolean,
|
|
|
|
defaults: false
|
|
|
|
});
|
2015-10-23 10:35:59 +00:00
|
|
|
this.option('upgrade', {
|
|
|
|
desc: 'Specify an upgrade',
|
|
|
|
type: Boolean,
|
|
|
|
defaults: false
|
|
|
|
});
|
2015-09-14 14:35:58 +00:00
|
|
|
|
|
|
|
// this passes command line arguments down to the composed generators
|
2015-10-23 10:35:59 +00:00
|
|
|
var args = {args: arguments[0], options: this.options};
|
2015-09-14 14:35:58 +00:00
|
|
|
if (!this.options['skip-ios']) {
|
2015-10-23 10:35:59 +00:00
|
|
|
this.composeWith('react:ios', args, {
|
2015-09-14 14:35:58 +00:00
|
|
|
local: require.resolve(path.resolve(__dirname, '..', 'generator-ios'))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!this.options['skip-android']) {
|
2015-10-23 10:35:59 +00:00
|
|
|
this.composeWith('react:android', args, {
|
2015-09-14 14:35:58 +00:00
|
|
|
local: require.resolve(path.resolve(__dirname, '..', 'generator-android'))
|
|
|
|
});
|
|
|
|
}
|
2015-08-04 11:50:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
configuring: function() {
|
2015-09-23 18:30:13 +00:00
|
|
|
utils.copyAndReplace(
|
|
|
|
this.templatePath('../../../.flowconfig'),
|
|
|
|
this.destinationPath('.flowconfig'),
|
2016-03-21 13:26:02 +00:00
|
|
|
{
|
|
|
|
'Libraries\/react-native\/react-native-interface.js' : 'node_modules/react-native/Libraries/react-native/react-native-interface.js',
|
|
|
|
'^flow/$' : 'node_modules/react-native/flow\nflow/'
|
|
|
|
}
|
2015-08-04 11:50:22 +00:00
|
|
|
);
|
2015-09-23 18:30:13 +00:00
|
|
|
|
2015-08-04 11:50:22 +00:00
|
|
|
this.fs.copy(
|
|
|
|
this.templatePath('_gitignore'),
|
|
|
|
this.destinationPath('.gitignore')
|
|
|
|
);
|
|
|
|
this.fs.copy(
|
|
|
|
this.templatePath('_watchmanconfig'),
|
|
|
|
this.destinationPath('.watchmanconfig')
|
|
|
|
);
|
2016-04-01 15:53:28 +00:00
|
|
|
this.fs.copy(
|
|
|
|
this.templatePath('_buckconfig'),
|
|
|
|
this.destinationPath('.buckconfig')
|
|
|
|
);
|
2015-08-04 11:50:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
writing: function() {
|
2015-10-23 10:35:59 +00:00
|
|
|
if (this.options.upgrade) {
|
|
|
|
// never upgrade index.*.js files
|
|
|
|
return;
|
|
|
|
}
|
2015-09-14 14:35:58 +00:00
|
|
|
if (!this.options['skip-ios']) {
|
|
|
|
this.fs.copyTpl(
|
|
|
|
this.templatePath('index.ios.js'),
|
|
|
|
this.destinationPath('index.ios.js'),
|
|
|
|
{name: this.name}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!this.options['skip-android']) {
|
|
|
|
this.fs.copyTpl(
|
|
|
|
this.templatePath('index.android.js'),
|
|
|
|
this.destinationPath('index.android.js'),
|
|
|
|
{name: this.name}
|
|
|
|
);
|
|
|
|
}
|
2016-02-09 23:37:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
install: function() {
|
|
|
|
if (this.options.upgrade) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-07 07:06:19 +00:00
|
|
|
var reactNativePackageJson = require('../../package.json');
|
|
|
|
var { peerDependencies } = reactNativePackageJson;
|
|
|
|
if (!peerDependencies) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var reactVersion = peerDependencies.react;
|
|
|
|
if (!reactVersion) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-03 10:34:54 +00:00
|
|
|
this.npmInstall(`react@${reactVersion}`, { '--save': true, '--save-exact': true });
|
2016-09-10 00:32:20 +00:00
|
|
|
if (!this.options['skip-jest']) {
|
|
|
|
this.npmInstall(`jest babel-jest jest-react-native babel-preset-react-native react-test-renderer@${reactVersion}`.split(' '), {
|
|
|
|
saveDev: true,
|
|
|
|
'--save-exact': true
|
|
|
|
});
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(
|
|
|
|
this.destinationRoot(),
|
|
|
|
'.babelrc'
|
|
|
|
),
|
|
|
|
'{\n"presets": ["react-native"]\n}'
|
|
|
|
);
|
|
|
|
|
|
|
|
this.fs.copy(
|
|
|
|
this.templatePath('__tests__'),
|
|
|
|
this.destinationPath('__tests__'),
|
|
|
|
{
|
|
|
|
nodir: false
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
var packageJSONPath = path.join(
|
|
|
|
this.destinationRoot(),
|
|
|
|
'package.json'
|
|
|
|
);
|
|
|
|
var packageJSON = JSON.parse(
|
|
|
|
fs.readFileSync(
|
|
|
|
packageJSONPath
|
|
|
|
)
|
|
|
|
);
|
|
|
|
packageJSON.scripts.test = 'jest';
|
|
|
|
packageJSON.jest = {
|
|
|
|
preset: 'jest-react-native'
|
|
|
|
};
|
|
|
|
fs.writeFileSync(packageJSONPath, JSON.stringify(packageJSON, null, '\t'));
|
|
|
|
}
|
2015-08-04 11:50:22 +00:00
|
|
|
}
|
|
|
|
});
|